-
Notifications
You must be signed in to change notification settings - Fork 8
/
sqlf.go
95 lines (87 loc) · 2.93 KB
/
sqlf.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
// Package sqlf generates parameterized SQL statements in Go, sprintf style.
//
// A simple example:
//
// q := sqlf.Sprintf("SELECT * FROM users WHERE country = %s AND age > %d", "US", 27);
// rows, err := db.Query(q.Query(sqlf.SimpleBindVar), q.Args()...) // db is a database/sql.DB
//
// sqlf.Sprintf does not return a string. It returns *sqlf.Query which has
// methods for a parameterized SQL query and arguments. You then pass that to
// db.Query, db.Exec, etc. This is not like using fmt.Sprintf, which could
// expose you to malformed SQL or SQL injection attacks.
//
// sqlf.Query can be passed as an argument to sqlf.Sprintf. It will "flatten"
// the query string, while preserving the correct variable binding. This
// allows you to easily compose and build SQL queries. See the below examples
// to find out more.
package sqlf
import (
"fmt"
"io"
"strings"
)
// Query stores a SQL expression and arguments for passing on to
// database/sql/db.Query or gorp.SqlExecutor.
type Query struct {
fmt string
args []interface{}
}
// Sprintf formats according to a format specifier and returns the resulting
// Query.
func Sprintf(format string, args ...interface{}) *Query {
f := make([]interface{}, len(args))
a := make([]interface{}, 0, len(args))
for i, arg := range args {
if q, ok := arg.(*Query); ok {
f[i] = ignoreFormat{q.fmt}
a = append(a, q.args...)
} else {
f[i] = ignoreFormat{"%s"}
a = append(a, arg)
}
}
// The format string below goes through fmt.Sprintf, which would reduce `%%` (a literal `%`
// according to fmt format specifier semantics), but it would also go through fmt.Sprintf
// again at Query.Query(binder) time - so we need to make sure `%%` remains as `%%` in our
// format string. See the literal_percent_operator test.
format = strings.ReplaceAll(format, "%%", "%%%%")
return &Query{
fmt: fmt.Sprintf(format, f...),
args: a,
}
}
// Query returns a string for use in database/sql/db.Query. binder is used to
// update the format specifiers with the relevant BindVar format
func (q *Query) Query(binder BindVar) string {
a := make([]interface{}, len(q.args))
for i := range a {
a[i] = ignoreFormat{binder.BindVar(i)}
}
return fmt.Sprintf(q.fmt, a...)
}
// Args returns the args for use in database/sql/db.Query along with
// q.Query()
func (q *Query) Args() []interface{} {
return q.args
}
// Join concatenates the elements of queries to create a single Query. The
// separator string sep is placed between elements in the resulting Query.
//
// This is commonly used to join clauses in a WHERE query. As such sep is
// usually "AND" or "OR".
func Join(queries []*Query, sep string) *Query {
f := make([]string, 0, len(queries))
var a []interface{}
for _, q := range queries {
f = append(f, q.fmt)
a = append(a, q.args...)
}
return &Query{
fmt: strings.Join(f, " "+sep+" "),
args: a,
}
}
type ignoreFormat struct{ s string }
func (e ignoreFormat) Format(f fmt.State, c rune) {
io.WriteString(f, e.s)
}