feat: support oracle :N dynamic parameters (#1552)
* chore:use struct pointer * feat: support oracle :N dynamic parameters * Update utils.go * Update utils.go * Update utils.go pg argIndex will not always go up * Update utils_test.go * Keep the original * Update utils_test.go
This commit is contained in:
@@ -25,6 +25,7 @@ type (
|
||||
|
||||
// A BulkInserter is used to batch insert records.
|
||||
// Postgresql is not supported yet, because of the sql is formated with symbol `$`.
|
||||
// Oracle is not supported yet, because of the sql is formated with symbol `:`.
|
||||
BulkInserter struct {
|
||||
executor *executors.PeriodicalExecutor
|
||||
inserter *dbInserter
|
||||
|
||||
@@ -67,7 +67,7 @@ func format(query string, args ...interface{}) (string, error) {
|
||||
|
||||
writeValue(&b, args[argIndex])
|
||||
argIndex++
|
||||
case '$':
|
||||
case ':', '$':
|
||||
var j int
|
||||
for j = i + 1; j < bytes; j++ {
|
||||
char := query[j]
|
||||
@@ -81,10 +81,11 @@ func format(query string, args ...interface{}) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// index starts from 1 for pg
|
||||
// index starts from 1 for pg or oracle
|
||||
if index > argIndex {
|
||||
argIndex = index
|
||||
}
|
||||
|
||||
index--
|
||||
if index < 0 || numArgs <= index {
|
||||
return "", fmt.Errorf("error: wrong index %d in sql", index)
|
||||
|
||||
@@ -73,6 +73,30 @@ func TestFormat(t *testing.T) {
|
||||
args: []interface{}{"133", false},
|
||||
hasErr: true,
|
||||
},
|
||||
{
|
||||
name: "oracle normal",
|
||||
query: "select name, age from users where bool=:1 and phone=:2",
|
||||
args: []interface{}{true, "133"},
|
||||
expect: "select name, age from users where bool=1 and phone='133'",
|
||||
},
|
||||
{
|
||||
name: "oracle normal reverse",
|
||||
query: "select name, age from users where bool=:2 and phone=:1",
|
||||
args: []interface{}{"133", false},
|
||||
expect: "select name, age from users where bool=0 and phone='133'",
|
||||
},
|
||||
{
|
||||
name: "oracle error not number",
|
||||
query: "select name, age from users where bool=:a and phone=:1",
|
||||
args: []interface{}{"133", false},
|
||||
hasErr: true,
|
||||
},
|
||||
{
|
||||
name: "oracle error more args",
|
||||
query: "select name, age from users where bool=:2 and phone=:1 and nickname=:3",
|
||||
args: []interface{}{"133", false},
|
||||
hasErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
Reference in New Issue
Block a user