forked from jmoiron/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jmoiron#718 from abraithwaite/alan/fix-named-batch
NamedExec Bulk Insert Fix
- Loading branch information
Showing
2 changed files
with
87 additions
and
9 deletions.
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ package sqlx | |
import ( | ||
"database/sql" | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
|
@@ -202,7 +203,10 @@ func TestNamedQueries(t *testing.T) { | |
{FirstName: "Ngani", LastName: "Laumape", Email: "[email protected]"}, | ||
} | ||
|
||
insert := fmt.Sprintf("INSERT INTO person (first_name, last_name, email, added_at) VALUES (:first_name, :last_name, :email, %v)\n", now) | ||
insert := fmt.Sprintf( | ||
"INSERT INTO person (first_name, last_name, email, added_at) VALUES (:first_name, :last_name, :email, %v)\n", | ||
now, | ||
) | ||
_, err = db.NamedExec(insert, sls) | ||
test.Error(err) | ||
|
||
|
@@ -214,7 +218,7 @@ func TestNamedQueries(t *testing.T) { | |
} | ||
|
||
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email) | ||
VALUES (:first_name, :last_name, :email) `, slsMap) | ||
VALUES (:first_name, :last_name, :email) ;--`, slsMap) | ||
test.Error(err) | ||
|
||
type A map[string]interface{} | ||
|
@@ -226,7 +230,7 @@ func TestNamedQueries(t *testing.T) { | |
} | ||
|
||
_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email) | ||
VALUES (:first_name, :last_name, :email) `, typedMap) | ||
VALUES (:first_name, :last_name, :email) ;--`, typedMap) | ||
test.Error(err) | ||
|
||
for _, p := range sls { | ||
|
@@ -296,3 +300,70 @@ func TestNamedQueries(t *testing.T) { | |
|
||
}) | ||
} | ||
|
||
func TestFixBounds(t *testing.T) { | ||
table := []struct { | ||
name, query, expect string | ||
loop int | ||
}{ | ||
{ | ||
name: `named syntax`, | ||
query: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last)`, | ||
expect: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last),(:name, :age, :first, :last)`, | ||
loop: 2, | ||
}, | ||
{ | ||
name: `mysql syntax`, | ||
query: `INSERT INTO foo (a,b,c,d) VALUES (?, ?, ?, ?)`, | ||
expect: `INSERT INTO foo (a,b,c,d) VALUES (?, ?, ?, ?),(?, ?, ?, ?)`, | ||
loop: 2, | ||
}, | ||
{ | ||
name: `named syntax w/ trailer`, | ||
query: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last) ;--`, | ||
expect: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last),(:name, :age, :first, :last) ;--`, | ||
loop: 2, | ||
}, | ||
{ | ||
name: `mysql syntax w/ trailer`, | ||
query: `INSERT INTO foo (a,b,c,d) VALUES (?, ?, ?, ?) ;--`, | ||
expect: `INSERT INTO foo (a,b,c,d) VALUES (?, ?, ?, ?),(?, ?, ?, ?) ;--`, | ||
loop: 2, | ||
}, | ||
{ | ||
name: `not found test`, | ||
query: `INSERT INTO foo (a,b,c,d) (:name, :age, :first, :last)`, | ||
expect: `INSERT INTO foo (a,b,c,d) (:name, :age, :first, :last)`, | ||
loop: 2, | ||
}, | ||
{ | ||
name: `found twice test`, | ||
query: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last) VALUES (:name, :age, :first, :last)`, | ||
expect: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last) VALUES (:name, :age, :first, :last)`, | ||
loop: 2, | ||
}, | ||
} | ||
|
||
for _, tc := range table { | ||
t.Run(tc.name, func(t *testing.T) { | ||
res := fixBound(tc.query, tc.loop) | ||
if res != tc.expect { | ||
t.Errorf("mismatched results") | ||
} | ||
}) | ||
} | ||
|
||
t.Run("regex changed", func(t *testing.T) { | ||
var valueBracketRegChanged = regexp.MustCompile(`(VALUES)\s+(\([^(]*.[^(]\))`) | ||
saveRegexp := valueBracketReg | ||
defer func() { | ||
valueBracketReg = saveRegexp | ||
}() | ||
valueBracketReg = valueBracketRegChanged | ||
|
||
res := fixBound("VALUES (:a, :b)", 2) | ||
if res != "VALUES (:a, :b)" { | ||
t.Errorf("changed regex should return string") | ||
} | ||
}) | ||
} |