-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlp.go
More file actions
68 lines (52 loc) · 1.62 KB
/
sqlp.go
File metadata and controls
68 lines (52 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package sqlp
import (
"context"
"database/sql"
"github.com/kaboc/sqlp/placeholder"
)
type sqler interface {
sqlExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
sqlQueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
sqlPrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}
func execContext(ctx context.Context, sq sqler, query string, args ...interface{}) (Result, error) {
var result Result
query, bind, err := placeholder.Convert(query, args...)
if err != nil {
return result, err
}
res, err := sq.sqlExecContext(ctx, query, bind...)
if err != nil {
return result, err
}
affectedRows, _ := res.RowsAffected()
insertId, _ := res.LastInsertId()
result = Result{
affectedRows: affectedRows,
insertId: insertId,
}
return result, err
}
func queryContext(ctx context.Context, sq sqler, query string, args ...interface{}) (*Rows, error) {
var sqlRows *sql.Rows
query, bind, err := placeholder.Convert(query, args...)
if err == nil {
sqlRows, err = sq.sqlQueryContext(ctx, query, bind...)
}
return &Rows{Rows: sqlRows}, err
}
func queryRowContext(ctx context.Context, sq sqler, query string, args ...interface{}) *Row {
rows, err := queryContext(ctx, sq, query, args...)
return &Row{rows: rows, err: err}
}
func prepareContext(ctx context.Context, sq sqler, query string) (*Stmt, error) {
queryUnnamed, err := placeholder.ConvertSQL(query)
if err != nil {
return nil, err
}
stmt, err := sq.sqlPrepareContext(ctx, queryUnnamed)
return &Stmt{
SqlStmt: stmt,
query: query, // Stores original query.
}, err
}