-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bulk insert with one field #710
Closed
Closed
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5342546
add broken tests
podhmo 2418963
fix regexp, the current implementation does not match the value such …
podhmo cc0c1e8
add tests with whitespace
podhmo 5ad23e9
adjust style
podhmo 723c89b
test: add helper for debugging
podhmo 1c63f0a
supports sql functions such as now()
f3a199d
supports whitespace in calling function (e.g. now( ))
podhmo bc61cf9
supports bulk insert with on duplicate key ...
podhmo 41ca8bf
adjust style (revert)
podhmo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,7 @@ type Test struct { | |
} | ||
|
||
func (t Test) Error(err error, msg ...interface{}) { | ||
t.t.Helper() | ||
if err != nil { | ||
if len(msg) == 0 { | ||
t.t.Error(err) | ||
|
@@ -114,6 +115,7 @@ func (t Test) Error(err error, msg ...interface{}) { | |
} | ||
|
||
func (t Test) Errorf(err error, format string, args ...interface{}) { | ||
t.t.Helper() | ||
if err != nil { | ||
t.t.Errorf(format, args...) | ||
} | ||
|
@@ -296,3 +298,81 @@ func TestNamedQueries(t *testing.T) { | |
|
||
}) | ||
} | ||
|
||
func TestNamedBulkInsert(t *testing.T) { | ||
type Val struct { | ||
K string `db:"k"` | ||
V int `db:"v"` | ||
} | ||
|
||
vs := []Val{{K: "x"}, {K: "y"}, {K: "z"}} | ||
table := []struct { | ||
values []interface{} | ||
q, expected string | ||
}{ | ||
{ | ||
values: []interface{}{vs[0]}, | ||
q: "INSERT INTO val (k) VALUES (:k)", | ||
expected: "INSERT INTO val (k) VALUES (?)", | ||
}, | ||
{ | ||
values: []interface{}{vs[0], vs[1]}, | ||
q: "INSERT INTO val (k) VALUES (:k)", | ||
expected: "INSERT INTO val (k) VALUES (?),(?)", | ||
}, | ||
{ | ||
values: []interface{}{vs[0]}, | ||
q: "INSERT INTO val (k,v) VALUES (:k,:v)", | ||
expected: "INSERT INTO val (k,v) VALUES (?,?)", | ||
}, | ||
{ | ||
values: []interface{}{vs[0], vs[1]}, | ||
q: "INSERT INTO val (k,v) VALUES (:k,:v)", | ||
expected: "INSERT INTO val (k,v) VALUES (?,?),(?,?)", | ||
}, | ||
{ | ||
values: []interface{}{vs[0]}, | ||
q: "INSERT INTO val (k,v) VALUES ( :k, :v )", | ||
expected: "INSERT INTO val (k,v) VALUES ( ?, ? )", | ||
}, | ||
{ | ||
values: []interface{}{vs[0], vs[1]}, | ||
q: "INSERT INTO val (k,v) VALUES ( :k, :v )", | ||
expected: "INSERT INTO val (k,v) VALUES ( ?, ? ),( ?, ? )", | ||
}, | ||
{ | ||
values: []interface{}{vs[0], vs[1]}, | ||
q: func() string { | ||
_, _, now := defaultSchema.Postgres() | ||
return fmt.Sprintf("INSERT INTO val (k, v, added_at) VALUES (:k, :v, %v)", now) | ||
}(), | ||
expected: "INSERT INTO val (k, v, added_at) VALUES (?, ?, now()),(?, ?, now())", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Supports only no-arguments function (e.g. |
||
}, | ||
{ | ||
values: []interface{}{vs[0], vs[1]}, | ||
q: func() string { | ||
_, _, now := defaultSchema.MySQL() | ||
return fmt.Sprintf("INSERT INTO val (k, v, added_at) VALUES (:k, :v, %v)", now) | ||
}(), | ||
expected: "INSERT INTO val (k, v, added_at) VALUES (?, ?, now()),(?, ?, now())", | ||
}, | ||
{ | ||
values: []interface{}{vs[0], vs[1]}, | ||
q: func() string { | ||
_, _, now := defaultSchema.Sqlite3() | ||
return fmt.Sprintf("INSERT INTO val (k, v, added_at) VALUES (:k, :v, %v)", now) | ||
}(), | ||
expected: "INSERT INTO val (k, v, added_at) VALUES (?, ?, CURRENT_TIMESTAMP),(?, ?, CURRENT_TIMESTAMP)", | ||
}, | ||
} | ||
|
||
for _, test := range table { | ||
actual, _, err := Named(test.q, test.values) | ||
if err != nil { | ||
t.Fatalf("unexpected error %+v, when len(values) == %d", err, len(test.values)) | ||
} | ||
if test.expected != actual { | ||
t.Errorf("expected query is (len(values) == %d)\n\t%q\nbut actual result is\n\t%q", len(test.values), test.expected, actual) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the test for #709