Skip to content

Commit

Permalink
Support nil query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nineinchnick authored and losipiuk committed Oct 20, 2023
1 parent 66a50f1 commit b8081d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion trino/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func Timestamp(year int,
func Serial(v interface{}) (string, error) {
switch x := v.(type) {
case nil:
return "", UnsupportedArgError{"<nil>"}
return "NULL", nil

// numbers convertible to int
case int8:
Expand Down
6 changes: 3 additions & 3 deletions trino/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ func TestSerial(t *testing.T) {
expectedSerial: "TIMESTAMP '2017-07-10 11:34:25.000123456 Z'",
},
{
name: "nil",
value: nil,
expectedError: true,
name: "nil",
value: nil,
expectedSerial: "NULL",
},
{
name: "slice typed nil",
Expand Down
2 changes: 2 additions & 0 deletions trino/trino.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,8 @@ func (st *driverStmt) ExecContext(ctx context.Context, args []driver.NamedValue)

func (st *driverStmt) CheckNamedValue(arg *driver.NamedValue) error {
switch arg.Value.(type) {
case nil:
return nil
case Numeric, trinoDate, trinoTime, trinoTimeTz, trinoTimestamp:
return nil
default:
Expand Down
21 changes: 18 additions & 3 deletions trino/trino_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1739,10 +1739,13 @@ func TestExec(t *testing.T) {
assert.NoError(t, db.Close())
})

result, err := db.Exec("CREATE TABLE memory.default.test (id INTEGER, name VARCHAR)")
result, err := db.Exec("CREATE TABLE memory.default.test (id INTEGER, name VARCHAR, optional VARCHAR)")
require.NoError(t, err, "Failed executing CREATE TABLE query")

result, err = db.Exec("INSERT INTO memory.default.test (id, name) VALUES (?, ?), (?, ?), (?, ?)", 123, "abc", 456, "def", 789, "ghi")
result, err = db.Exec("INSERT INTO memory.default.test (id, name, optional) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?)",
123, "abc", nil,
456, "def", "present",
789, "ghi", nil)
require.NoError(t, err, "Failed executing INSERT query")
_, err = result.LastInsertId()
assert.Error(t, err, "trino: operation not supported")
Expand All @@ -1755,16 +1758,28 @@ func TestExec(t *testing.T) {

expectedIds := []int{123, 456, 789}
expectedNames := []string{"abc", "def", "ghi"}
expectedOptionals := []sql.NullString{
sql.NullString{Valid: false},
sql.NullString{String: "present", Valid: true},
sql.NullString{Valid: false},
}
actualIds := []int{}
actualNames := []string{}
actualOptionals := []sql.NullString{}
for rows.Next() {
var id int
var name string
require.NoError(t, rows.Scan(&id, &name), "Failed scanning query result")
var optional sql.NullString
require.NoError(t, rows.Scan(&id, &name, &optional), "Failed scanning query result")
actualIds = append(actualIds, id)
actualNames = append(actualNames, name)
actualOptionals = append(actualOptionals, optional)

}
assert.Equal(t, expectedIds, actualIds)
assert.Equal(t, expectedNames, actualNames)
assert.Equal(t, expectedOptionals, actualOptionals)

_, err = db.Exec("DROP TABLE memory.default.test")
require.NoError(t, err, "Failed executing DROP TABLE query")
}

0 comments on commit b8081d7

Please sign in to comment.