SQLX: Expose sqlxdb query functions (#57227)

This commit is contained in:
Ryan McKinley 2022-10-19 10:33:40 -04:00 committed by GitHub
parent de3737b5de
commit 15517f8329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -30,6 +30,10 @@ func (gs *SessionDB) Select(ctx context.Context, dest interface{}, query string,
return gs.sqlxdb.SelectContext(ctx, dest, gs.sqlxdb.Rebind(query), args...)
}
func (gs *SessionDB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return gs.sqlxdb.QueryContext(ctx, gs.sqlxdb.Rebind(query), args...)
}
func (gs *SessionDB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return gs.sqlxdb.ExecContext(ctx, gs.sqlxdb.Rebind(query), args...)
}
@ -80,6 +84,10 @@ func (gtx *SessionTx) Exec(ctx context.Context, query string, args ...interface{
return gtx.sqlxtx.ExecContext(ctx, gtx.sqlxtx.Rebind(query), args...)
}
func (gtx *SessionTx) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return gtx.sqlxtx.QueryContext(ctx, gtx.sqlxtx.Rebind(query), args...)
}
func (gtx *SessionTx) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return gtx.sqlxtx.GetContext(ctx, dest, gtx.sqlxtx.Rebind(query), args...)
}

View File

@ -59,4 +59,20 @@ func TestRetryingOnFailures(t *testing.T) {
require.Equal(t, i, store.dbCfg.QueryRetries+1)
})
}
// Check SQL query
sess := store.GetSqlxSession()
rows, err := sess.Query(context.Background(), `SELECT "hello",2.3,4`)
require.NoError(t, err)
require.True(t, rows.Next()) // first row
str1 := ""
val2 := float64(100.1)
val3 := int64(200)
err = rows.Scan(&str1, &val2, &val3)
require.NoError(t, err)
require.Equal(t, "hello", str1)
require.Equal(t, 2.3, val2)
require.Equal(t, int64(4), val3)
require.False(t, rows.Next()) // no more rows
}