Browse Source

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
mlr3000 3 years ago
parent
commit
32c88b6352
3 changed files with 28 additions and 2 deletions
  1. 1 0
      core/stores/sqlx/bulkinserter.go
  2. 3 2
      core/stores/sqlx/utils.go
  3. 24 0
      core/stores/sqlx/utils_test.go

+ 1 - 0
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

+ 3 - 2
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)

+ 24 - 0
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 {