diff --git a/core/stores/sqlx/bulkinserter.go b/core/stores/sqlx/bulkinserter.go index 507170f4..a4a2d99a 100644 --- a/core/stores/sqlx/bulkinserter.go +++ b/core/stores/sqlx/bulkinserter.go @@ -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 diff --git a/core/stores/sqlx/utils.go b/core/stores/sqlx/utils.go index 9888e400..6aa966e7 100644 --- a/core/stores/sqlx/utils.go +++ b/core/stores/sqlx/utils.go @@ -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) diff --git a/core/stores/sqlx/utils_test.go b/core/stores/sqlx/utils_test.go index af6c8067..6631e134 100644 --- a/core/stores/sqlx/utils_test.go +++ b/core/stores/sqlx/utils_test.go @@ -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 {