Skip to content

Commit

Permalink
Return SnowflakeResult instead of driver.ResultNoRows
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pbulawa committed Nov 29, 2023
1 parent 15fac29 commit 61e84d1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
6 changes: 5 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,11 @@ func (sc *snowflakeConn) ExecContext(
return sc.handleMultiExec(ctx, data.Data)
}
logger.Debug("DDL")
return driver.ResultNoRows, nil
return &snowflakeResult{
affectedRows: 0,
insertID: -1,
queryID: data.Data.QueryID,
}, nil
}

func (sc *snowflakeConn) QueryContext(
Expand Down
5 changes: 4 additions & 1 deletion connection_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,13 @@ func getResultType(ctx context.Context) resultType {

// isDml returns true if the statement type code is in the range of DML.
func isDml(v int64) bool {
return statementTypeIDDml <= v && v <= statementTypeIDMultiTableInsert
return (statementTypeIDDml <= v && v <= statementTypeIDMultiTableInsert) || v == statementTypeIDSelect
}

func updateRows(data execResponseData) (int64, error) {
if data.RowSet == nil {
return 0, nil
}
var count int64
for i, n := 0, len(data.RowType); i < n; i++ {
v, err := strconv.ParseInt(*data.RowSet[0][i], 10, 64)
Expand Down
62 changes: 62 additions & 0 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,68 @@ func openConn(t *testing.T) *sql.Conn {
return conn
}

func TestDMLExec(t *testing.T) {
query := "SELECT 1"
runDBTest(t, func(dbt *DBTest) {
testcases := []struct {
name string
f func(dbt *DBTest) (any, error)
}{
{
name: "Exec",
f: func(dbt *DBTest) (any, error) {
stmt, _ := dbt.prepare(query)
return stmt.Exec()
},
},
{
name: "ExecContext",
f: func(dbt *DBTest) (any, error) {
stmt, _ := dbt.prepare(query)
return stmt.ExecContext(context.Background())
},
},
}
for _, tc := range testcases {
_, err := tc.f(dbt)
if err != nil {
t.Error(err)
}
}
})
}

func TestDDLExec(t *testing.T) {
query := "CREATE OR REPLACE TABLE TestDDLExec (num NUMBER)"
runDBTest(t, func(dbt *DBTest) {
testcases := []struct {
name string
f func(dbt *DBTest) (any, error)
}{
{
name: "Exec",
f: func(dbt *DBTest) (any, error) {
stmt, _ := dbt.prepare(query)
return stmt.Exec()
},
},
{
name: "ExecContext",
f: func(dbt *DBTest) (any, error) {
stmt, _ := dbt.prepare(query)
return stmt.ExecContext(context.Background())
},
},
}
for _, tc := range testcases {
_, err := tc.f(dbt)
if err != nil {
t.Error(err)
}
}
})
}

func TestFailedQueryIdInSnowflakeError(t *testing.T) {
failingQuery := "SELECTT 1"
failingExec := "INSERT 1 INTO NON_EXISTENT_TABLE"
Expand Down

0 comments on commit 61e84d1

Please sign in to comment.