Skip to content

Commit

Permalink
SNOW-645253 Handle binding named parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pfus committed Jul 17, 2023
1 parent 3623a16 commit 78ddbd2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
9 changes: 8 additions & 1 deletion bind_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func getBindValues(bindings []driver.NamedValue) (map[string]execBindParameter,
if t == nullType || t == unSupportedType {
t = textType // if null or not supported, pass to GS as text
}
bindValues[strconv.Itoa(idx)] = execBindParameter{
bindValues[bindingName(binding, idx)] = execBindParameter{
Type: t.String(),
Value: val,
}
Expand All @@ -250,6 +250,13 @@ func getBindValues(bindings []driver.NamedValue) (map[string]execBindParameter,
return bindValues, nil
}

func bindingName(nv driver.NamedValue, idx int) string {
if nv.Name != "" {
return nv.Name
}
return strconv.Itoa(idx)
}

func arrayBindValueCount(bindValues []driver.NamedValue) int {
if !isArrayBind(bindValues) {
return 0
Expand Down
79 changes: 78 additions & 1 deletion bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestBindingInterface(t *testing.T) {
if !rows.Next() {
dbt.Error("failed to query")
}
var v1, v2, v3, v4, v5, v6 interface{}
var v1, v2, v3, v4, v5, v6 any
if err := rows.Scan(&v1, &v2, &v3, &v4, &v5, &v6); err != nil {
dbt.Errorf("failed to scan: %#v", err)
}
Expand Down Expand Up @@ -930,3 +930,80 @@ func TestFunctionParameters(t *testing.T) {
}
})
}

func TestVariousBindingModes(t *testing.T) {
testcases := []struct {
testDesc string
paramType string
input interface{}
isNil bool
}{
{"textAndString", "text", "string", false},
{"numberAndInteger", "number", 123, false},
{"floatAndFloat", "float", 123.01, false},
{"booleanAndBoolean", "boolean", true, false},
{"dateAndTime", "date", time.Now().Truncate(24 * time.Hour), false},
{"datetimeAndTime", "datetime", time.Now(), false},
{"timeAndTime", "time", "12:34:56", false},
{"timestampAndTime", "timestamp", time.Now(), false},
{"timestamp_ntzAndTime", "timestamp_ntz", time.Now(), false},
{"timestamp_ltzAndTime", "timestamp_ltz", time.Now(), false},
{"timestamp_tzAndTime", "timestamp_tz", time.Now(), false},
{"textAndNullString", "text", sql.NullString{}, true},
{"numberAndNullInt64", "number", sql.NullInt64{}, true},
{"floatAndNullFloat64", "float", sql.NullFloat64{}, true},
{"booleanAndAndNullBool", "boolean", sql.NullBool{}, true},
{"dateAndTypedNullTime", "date", TypedNullTime{sql.NullTime{}, DateType}, true},
{"datetimeAndTypedNullTime", "datetime", TypedNullTime{sql.NullTime{}, TimestampNTZType}, true},
{"timeAndTypedNullTime", "time", TypedNullTime{sql.NullTime{}, TimeType}, true},
{"timestampAndTypedNullTime", "timestamp", TypedNullTime{sql.NullTime{}, TimestampNTZType}, true},
{"timestamp_ntzAndTypedNullTime", "timestamp_ntz", TypedNullTime{sql.NullTime{}, TimestampNTZType}, true},
{"timestamp_ltzAndTypedNullTime", "timestamp_ltz", TypedNullTime{sql.NullTime{}, TimestampLTZType}, true},
{"timestamp_tzAndTypedNullTime", "timestamp_tz", TypedNullTime{sql.NullTime{}, TimestampTZType}, true},
}

bindingModes := []struct {
param string
query string
transform func(any) any
}{
{
param: "?",
transform: func(v any) any { return v },
},
{
param: ":1",
transform: func(v any) any { return v },
},
{
param: ":param",
transform: func(v any) any { return sql.Named("param", v) },
},
}

runTests(t, dsn, func(dbt *DBTest) {
for _, tc := range testcases {
for _, bindingMode := range bindingModes {
t.Run(tc.testDesc+" "+bindingMode.param, func(t *testing.T) {
query := fmt.Sprintf(`CREATE OR REPLACE TABLE BINDING_MODES(param1 %v)`, tc.paramType)
dbt.mustExec(query)
if _, err := dbt.db.Exec(fmt.Sprintf("INSERT INTO BINDING_MODES VALUES (%v)", bindingMode.param), bindingMode.transform(tc.input)); err != nil {
t.Fatal(err)
}
if tc.isNil {
query = "SELECT * FROM BINDING_MODES WHERE param1 IS NULL"
} else {
query = fmt.Sprintf("SELECT * FROM BINDING_MODES WHERE param1 = %v", bindingMode.param)
}
rows, err := dbt.db.Query(query, bindingMode.transform(tc.input))
if err != nil {
t.Fatal(err)
}
if !rows.Next() {
t.Fatal("Expected to return a row")
}
})
}
}
})
}

0 comments on commit 78ddbd2

Please sign in to comment.