Statements execution with context.
This commit is contained in:
parent
cdfd8f1dff
commit
1ac324e198
11 changed files with 146 additions and 28 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package sqlbuilder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/sqlbuilder/execution"
|
||||
"strconv"
|
||||
|
|
@ -14,7 +15,10 @@ type Statement interface {
|
|||
DebugSql() (query string, err error)
|
||||
|
||||
Query(db execution.Db, destination interface{}) error
|
||||
QueryContext(db execution.Db, context context.Context, destination interface{}) error
|
||||
|
||||
Exec(db execution.Db) (sql.Result, error)
|
||||
ExecContext(db execution.Db, context context.Context) (sql.Result, error)
|
||||
}
|
||||
|
||||
func DebugSql(statement Statement) (string, error) {
|
||||
|
|
@ -33,3 +37,43 @@ func DebugSql(statement Statement) (string, error) {
|
|||
|
||||
return debugSql, nil
|
||||
}
|
||||
|
||||
func Query(statement Statement, db execution.Db, destination interface{}) error {
|
||||
query, args, err := statement.Sql()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return execution.Query(db, context.Background(), query, args, destination)
|
||||
}
|
||||
|
||||
func QueryContext(statement Statement, db execution.Db, context context.Context, destination interface{}) error {
|
||||
query, args, err := statement.Sql()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return execution.Query(db, context, query, args, destination)
|
||||
}
|
||||
|
||||
func Exec(statement Statement, db execution.Db) (res sql.Result, err error) {
|
||||
query, args, err := statement.Sql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return db.Exec(query, args...)
|
||||
}
|
||||
|
||||
func ExecContext(statement Statement, db execution.Db, context context.Context) (res sql.Result, err error) {
|
||||
query, args, err := statement.Sql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return db.ExecContext(context, query, args...)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue