-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
175 lines (140 loc) · 3.94 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package qbdb // import "git.ultraware.nl/NiseVoid/qb/qbdb"
import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"strconv"
"strings"
"git.ultraware.nl/NiseVoid/qb"
)
func (db QueryTarget) printType(v interface{}, c *int) (string, bool) {
if _, ok := v.(driver.Valuer); ok {
(*c)++
return db.Driver.ValueString(*c), true
}
if v == nil {
return `NULL`, false
}
switch t := reflect.ValueOf(v); t.Type().Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(t.Int(), 10), false
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(t.Uint(), 10), false
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(t.Float(), 'g', -1, 64), false
case reflect.Bool:
return db.Driver.BoolString(t.Bool()), false
default:
(*c)++
return db.Driver.ValueString(*c), true
}
}
// Render returns the generated SQL and values without executing the query
func (db QueryTarget) Render(q qb.Query) (string, []interface{}) {
return db.prepare(q)
}
func (db QueryTarget) ctes(ctes []*qb.CTE, done map[*qb.CTE]bool, b qb.SQLBuilder) []string {
var list []string
for _, v := range ctes {
if _, ok := done[v]; ok {
continue
}
done[v] = true
tmp := b.Context.Values
newValues, newCTEs := []interface{}{}, []*qb.CTE{}
b.Context.Values, b.Context.CTEs = &newValues, &newCTEs
new := v.With(b)
b.Context.Values = tmp
list = append(append(list, db.ctes(*b.Context.CTEs, done, b)...), new)
b.Context.Add(newValues...)
}
return list
}
func shouldContext(q qb.Query) bool {
switch t := q.(type) {
case *qb.UpdateBuilder, qb.DeleteBuilder:
return true
case qb.ReturningBuilder:
return shouldContext(t.Query)
}
return false
}
func (db QueryTarget) prepare(q qb.Query) (string, []interface{}) {
b := qb.NewSQLBuilder(db.Driver)
if shouldContext(q) {
b.Context = qb.NewContext(b.Context.Driver, qb.AliasGenerator())
}
s, v := q.SQL(b)
if _, ok := q.(qb.SelectQuery); ok {
ctes := b.Context.CTEs
newList := []interface{}{}
b.Context.Values = &newList
if len(*ctes) > 0 {
done := make(map[*qb.CTE]bool)
s = `WITH ` + strings.Join(db.ctes(*ctes, done, b), `, `) + "\n\n" + s
}
v = append(*b.Context.Values, v...)
}
return db.prepareSQL(s, v)
}
func (db QueryTarget) prepareSQL(s string, v []interface{}) (string, []interface{}) {
vc := 0
c := 0
b := &strings.Builder{}
new := []interface{}{}
for _, chr := range s {
if chr != '?' {
if _, err := b.WriteRune(chr); err != nil {
panic(err)
}
continue
}
str, param := db.printType(v[vc], &c)
if param {
new = append(new, v[vc])
}
vc++
if _, err := b.WriteString(str); err != nil {
panic(err)
}
}
db.log(b.String(), new)
return b.String(), new
}
func (db QueryTarget) log(s string, v []interface{}) {
if db.Debug {
fmt.Printf("-- Running query:\n%s-- With values: %v\n\n", s, v)
}
}
// Query executes the given SelectQuery on the database
func (db QueryTarget) Query(q qb.SelectQuery) (*sql.Rows, error) {
s, v := db.prepare(q)
return db.src.Query(s, v...)
}
// QueryRow executes the given SelectQuery on the database, only returns one row
func (db QueryTarget) QueryRow(q qb.SelectQuery) *sql.Row {
if sq, ok := q.(*qb.SelectBuilder); ok {
sq.Limit(1)
}
s, v := db.prepare(q)
return db.src.QueryRow(s, v...)
}
// Exec executes the given query, returns only an error
func (db QueryTarget) Exec(q qb.Query) (sql.Result, error) {
s, v := db.prepare(q)
return db.RawExec(s, v...)
}
// RawExec executes the given SQL with the given params directly on the database
func (db QueryTarget) RawExec(s string, v ...interface{}) (sql.Result, error) {
return db.src.Exec(s, v...)
}
// Prepare prepares a query for efficient repeated executions
func (db QueryTarget) Prepare(q qb.Query) (*Stmt, error) {
s, v := db.prepare(q)
stmt, err := db.src.Prepare(s)
if err != nil {
return nil, err
}
return &Stmt{stmt, v}, nil
}