-
Notifications
You must be signed in to change notification settings - Fork 2
/
executable.go
92 lines (77 loc) · 2.3 KB
/
executable.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package sol
import (
"database/sql"
"reflect"
"github.com/aodin/sol/dialect"
)
// Executable is - for now - an alias of Compiles
type Executable interface {
Compiles
}
// executer is a common interface that database/sql *DB and *Tx can share
type executer interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
}
var _ executer = &sql.DB{}
var _ executer = &sql.Tx{}
func compile(d dialect.Dialect, stmt Executable) (string, *Parameters, error) {
// Initialize a list of empty parameters
params := Params()
// Compile with the database connection's current dialect
compiled, err := stmt.Compile(d, params)
return compiled, params, err
}
func execute(exec executer, d dialect.Dialect, stmt Executable) (sql.Result, error) {
compiled, params, err := compile(d, stmt)
if err != nil {
return nil, err
}
return exec.Exec(compiled, *params...)
}
func perform(exec executer, d dialect.Dialect, stmt Executable, dest ...interface{}) error {
if len(dest) == 0 {
_, err := execute(exec, d, stmt)
return err
}
if len(dest) > 1 {
return queryAll(exec, d, stmt, dest)
}
t := reflect.Indirect(reflect.ValueOf(dest[0]))
if t.Kind() == reflect.Slice {
return queryAll(exec, d, stmt, dest[0])
}
return queryOne(exec, d, stmt, dest[0])
}
func query(exec executer, d dialect.Dialect, stmt Executable) (*Result, error) {
compiled, params, err := compile(d, stmt)
if err != nil {
return nil, err
}
rows, err := exec.Query(compiled, *params...)
if err != nil {
return nil, err
}
// Wrap the sql rows in a result
return &Result{Scanner: rows, stmt: compiled}, nil
}
// QueryAll will query the statement and populate the given destination
// interface with all results.
func queryAll(exec executer, d dialect.Dialect, stmt Executable, dest interface{}) error {
result, err := query(exec, d, stmt)
if err != nil {
return err
}
return result.All(dest)
}
// QueryOne will query the statement and populate the given destination
// interface with a single result.
func queryOne(exec executer, d dialect.Dialect, stmt Executable, dest interface{}) error {
result, err := query(exec, d, stmt)
if err != nil {
return err
}
// Close the result rows or sqlite3 will open another connection
defer result.Close()
return result.One(dest)
}