2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
import (
|
2019-06-20 12:22:19 +02:00
|
|
|
"context"
|
2019-03-05 18:55:47 +01:00
|
|
|
"database/sql"
|
2019-06-21 13:56:57 +02:00
|
|
|
"github.com/go-jet/jet/execution"
|
2019-05-12 18:15:23 +02:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2019-03-02 12:34:08 +01:00
|
|
|
)
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
type Statement interface {
|
2019-06-29 16:58:41 +02:00
|
|
|
// Sql returns parametrized sql query with list of arguments.
|
|
|
|
|
// err is returned if statement is not composed correctly
|
2019-04-29 14:39:48 +02:00
|
|
|
Sql() (query string, args []interface{}, err error)
|
2019-06-29 16:58:41 +02:00
|
|
|
// DebugSql returns debug query where every parametrized placeholder is replaced with its argument.
|
|
|
|
|
// Do not use it in production. Use it only for debug purposes.
|
|
|
|
|
// err is returned if statement is not composed correctly
|
2019-05-12 18:15:23 +02:00
|
|
|
DebugSql() (query string, err error)
|
|
|
|
|
|
2019-06-29 16:58:41 +02:00
|
|
|
// Query executes statement over database connection db and stores row result in destination.
|
|
|
|
|
// Destination can be arbitrary structure
|
2019-06-23 18:55:57 +02:00
|
|
|
Query(db execution.DB, destination interface{}) error
|
2019-06-29 16:58:41 +02:00
|
|
|
// QueryContext executes statement with a context over database connection db and stores row result in destination.
|
|
|
|
|
// Destination can be of arbitrary structure
|
2019-06-23 18:55:57 +02:00
|
|
|
QueryContext(db execution.DB, context context.Context, destination interface{}) error
|
2019-06-20 12:22:19 +02:00
|
|
|
|
2019-06-29 16:58:41 +02:00
|
|
|
//Exec executes statement over db connection without returning any rows.
|
2019-06-23 18:55:57 +02:00
|
|
|
Exec(db execution.DB) (sql.Result, error)
|
2019-06-29 16:58:41 +02:00
|
|
|
//Exec executes statement with context over db connection without returning any rows.
|
2019-06-23 18:55:57 +02:00
|
|
|
ExecContext(db execution.DB, context context.Context) (sql.Result, error)
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
2019-05-12 18:15:23 +02:00
|
|
|
|
2019-06-29 16:58:41 +02:00
|
|
|
func debugSql(statement Statement) (string, error) {
|
2019-06-21 13:56:57 +02:00
|
|
|
sqlQuery, args, err := statement.Sql()
|
2019-05-12 18:15:23 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
debugSqlQuery := sqlQuery
|
2019-05-12 18:15:23 +02:00
|
|
|
|
|
|
|
|
for i, arg := range args {
|
|
|
|
|
argPlaceholder := "$" + strconv.Itoa(i+1)
|
2019-06-21 13:56:57 +02:00
|
|
|
debugSqlQuery = strings.Replace(debugSqlQuery, argPlaceholder, ArgToString(arg), 1)
|
2019-05-12 18:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
return debugSqlQuery, nil
|
2019-05-12 18:15:23 +02:00
|
|
|
}
|
2019-06-20 12:22:19 +02:00
|
|
|
|
2019-06-29 16:58:41 +02:00
|
|
|
func query(statement Statement, db execution.DB, destination interface{}) error {
|
2019-06-20 12:22:19 +02:00
|
|
|
query, args, err := statement.Sql()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return execution.Query(db, context.Background(), query, args, destination)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 16:58:41 +02:00
|
|
|
func queryContext(statement Statement, db execution.DB, context context.Context, destination interface{}) error {
|
2019-06-20 12:22:19 +02:00
|
|
|
query, args, err := statement.Sql()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return execution.Query(db, context, query, args, destination)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 16:58:41 +02:00
|
|
|
func exec(statement Statement, db execution.DB) (res sql.Result, err error) {
|
2019-06-20 12:22:19 +02:00
|
|
|
query, args, err := statement.Sql()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return db.Exec(query, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 16:58:41 +02:00
|
|
|
func execContext(statement Statement, db execution.DB, context context.Context) (res sql.Result, err error) {
|
2019-06-20 12:22:19 +02:00
|
|
|
query, args, err := statement.Sql()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return db.ExecContext(context, query, args...)
|
|
|
|
|
}
|