-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_generator.go
More file actions
169 lines (140 loc) · 4.47 KB
/
query_generator.go
File metadata and controls
169 lines (140 loc) · 4.47 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
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
package morph
import (
"bytes"
"errors"
"fmt"
"strings"
"text/template"
)
type queryGenerator struct {
table *Table
keyColumns []Column
nonKeyColumns []Column
}
// newQueryGenerator creates a new query generator for the given table.
func newQueryGenerator(table *Table, keyColumns, nonKeyColumns []Column) queryGenerator {
return queryGenerator{
table: table,
keyColumns: keyColumns,
nonKeyColumns: nonKeyColumns,
}
}
// query generates a query for the table using the provided template and options.
func (generator *queryGenerator) query(tmpl *template.Template, options ...QueryOption) (string, error) {
if err := generator.table.validate(); err != nil {
return "", err
}
qo := &QueryOptions{}
opts := append(DefaultQueryOptions, options...)
for _, opt := range opts {
opt(qo)
}
data := struct {
Table *Table
Key []Column
NonKeys []Column
Options *QueryOptions
Data EvaluationResult
}{
Table: generator.table,
Options: qo,
Key: generator.keyColumns,
NonKeys: generator.nonKeyColumns,
}
if qo.OmitEmpty && qo.obj != nil {
var err error
if data.Data, err = generator.table.Evaluate(qo.obj); err != nil {
return "", err
}
}
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, data)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (generator *queryGenerator) queryWithArgs(namedQuery string, obj any, options ...QueryOption) (string, []any, error) {
qo := &QueryOptions{}
opts := append(DefaultQueryOptions, options...)
for _, opt := range opts {
opt(qo)
}
result, err := generator.table.Evaluate(obj)
if err != nil {
return "", nil, err
}
args := []any{}
missing := []string{}
count := 0
query := namedParamRegExp.ReplaceAllStringFunc(namedQuery, func(match string) string {
name := match[1:]
if arg, ok := result[name]; ok {
args = append(args, arg)
if qo.Ordered {
count += 1
return qo.Placeholder + fmt.Sprintf("%d", count)
}
return qo.Placeholder
}
missing = append(missing, name)
return match
})
if len(missing) > 0 {
return "", nil, errors.New("morph: missing values for named parameters: " + strings.Join(missing, ", "))
}
return query, args, nil
}
// InsertQuery generates an INSERT query for the table.
func (generator queryGenerator) InsertQuery(options ...QueryOption) (string, error) {
return generator.query(insertTmpl, options...)
}
// InsertQueryWithArgs generates an INSERT query for the table along with arguments
// derived from the provided object.
func (generator queryGenerator) InsertQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
opts := append(options, WithNamedParameters())
query, err := generator.InsertQuery(opts...)
if err != nil {
return "", nil, err
}
return generator.queryWithArgs(query, obj, opts...)
}
// SelectQuery generates a SELECT query for the table.
func (generator queryGenerator) SelectQuery(options ...QueryOption) (string, error) {
return generator.query(selectTmpl, options...)
}
// SelectQueryWithArgs generates a SELECT query for the table along with arguments
// derived from the provided object.
func (generator *queryGenerator) SelectQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
query, err := generator.SelectQuery(options...)
if err != nil {
return "", nil, err
}
return generator.queryWithArgs(query, obj, options...)
}
// UpdateQuery generates an UPDATE query for the table.
func (generator queryGenerator) UpdateQuery(options ...QueryOption) (string, error) {
return generator.query(updateTmpl, options...)
}
// UpdateQueryWithArgs generates an UPDATE query for the table along with arguments
// derived from the provided object.
func (generator *queryGenerator) UpdateQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
query, err := generator.UpdateQuery(options...)
if err != nil {
return "", nil, err
}
return generator.queryWithArgs(query, obj, options...)
}
// DeleteQueryWithArgs generates a DELETE query for the table along with arguments
// derived from the provided object.
func (generator *queryGenerator) DeleteQueryWithArgs(obj any, options ...QueryOption) (string, []any, error) {
query, err := generator.DeleteQuery(options...)
if err != nil {
return "", nil, err
}
return generator.queryWithArgs(query, obj, options...)
}
// DeleteQuery generates a DELETE query for the table.
func (generator queryGenerator) DeleteQuery(options ...QueryOption) (string, error) {
return generator.query(deleteTmpl, options...)
}