-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
157 lines (136 loc) · 4.66 KB
/
query.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
package morph
import (
"strconv"
"text/template"
)
// DefaultPlaceholder represents the default placeholder value used for query generation.
const DefaultPlaceholder = "?"
// QueryOptions represents the options available for generating a query.
type QueryOptions struct {
Placeholder string
Ordered bool
Named bool
OmitEmpty bool
obj any
}
// QueryOption represents a function that modifies the query options.
type QueryOption func(*QueryOptions)
// DefaultQueryOptions represents the default query options used for query generation.
var DefaultQueryOptions = []QueryOption{WithDefaultPlaceholder()}
// WithPlaceholder sets the placeholder value and whether the parameter should have
// a sequence number appended to it.
func WithPlaceholder(p string, o bool) QueryOption {
return func(q *QueryOptions) {
q.Placeholder = p
q.Ordered = o
}
}
func WithDefaultPlaceholder() QueryOption {
return func(q *QueryOptions) {
q.Placeholder = DefaultPlaceholder
q.Ordered = false
}
}
// WithNamedParameters sets the query to use named parameters.
func WithNamedParameters() QueryOption {
return func(q *QueryOptions) {
q.Named = true
}
}
// WithoutEmptyValues indicates that columns with no value should be omitted from the query.
func WithoutEmptyValues(obj any) QueryOption {
return func(q *QueryOptions) {
q.OmitEmpty = true
q.obj = obj
}
}
// insertSQL is the raw template contents used to generate an insert query.
const insertSQL = `
{{- $table := .Table -}}
{{- $options := .Options -}}
{{- $seq := 0 -}}
INSERT INTO {{$table.Name}} (
{{- range $idx, $col := $table.Columns -}}
{{$col.Name}}{{if ne $idx (sub (len $table.Columns) 1)}}, {{end}}
{{- end -}}
) VALUES (
{{- range $idx, $col := $table.Columns -}}
{{- $seq = add $seq 1 -}}
{{param $col.Name $options $seq}}{{if ne $idx (sub (len $table.Columns) 1)}}, {{end}}
{{- end -}}
);`
// updateSQL is the raw template contents used to generate an update query.
const updateSQL = `
{{- $table := .Table -}}
{{- $options := .Options -}}
{{- $seq := 0 -}}
{{- $data := .Data -}}
{{- $nonPrimaryKeys := .NonPrimaryKeys -}}
UPDATE {{$table.Name}} AS {{$table.Alias}} SET {{- if true}} {{end}}
{{- range $idx, $col := $nonPrimaryKeys -}}
{{- if omit $data $col.Name -}} {{continue}} {{- end -}}
{{- if ne $idx 0 -}} , {{end}}
{{- $seq = add $seq 1 -}}
{{$table.Alias}}.{{.Name}} = {{param $col.Name $options $seq}}
{{- end }} WHERE 1=1
{{- range $idx, $col := .PrimaryKeys -}}
{{- $seq = add $seq 1 }} AND {{$table.Alias}}.{{.Name}} = {{param $col.Name $options $seq}}
{{- end -}};`
// deleteSQL is the raw template contents used to generate a delete query.
const deleteSQL = `
{{- $table := .Table -}}
{{- $options := .Options -}}
{{- $seq := 0 -}}
DELETE FROM {{$table.Name}} WHERE 1=1
{{- range $idx, $col := .PrimaryKeys -}}
{{- $seq = add $seq 1 }} AND {{.Name}} = {{param $col.Name $options $seq}}
{{- end -}};`
const selectSQL = `
{{- $table := .Table -}}
{{- $options := .Options -}}
{{- $seq := 0 -}}
SELECT {{- if true}} {{end}}
{{- range $idx, $col := $table.Columns -}}
{{$col.Name}}{{if ne $idx (sub (len $table.Columns) 1)}}, {{end}}
{{- end -}}
{{- if true}} {{end -}} FROM {{$table.Name}} AS {{$table.Alias}} WHERE 1=1
{{- range $idx, $col := .PrimaryKeys -}}
{{- $seq = add $seq 1 }} AND {{$table.Alias}}.{{.Name}} = {{param $col.Name $options $seq}}
{{- end -}};`
var (
// funcs defines the custom functions leveraged within the query templates.
funcs = template.FuncMap{
"param": func(columnName string, options *QueryOptions, seq int) string {
if options.Named {
return ":" + columnName
}
p := options.Placeholder
if options.Ordered {
p += strconv.Itoa(seq)
}
return p
},
"omit": func(data EvaluationResult, columnName string) bool {
for _, col := range data.Empties() {
if col == columnName {
return true
}
}
return false
},
"sub": func(a, b int) int {
return a - b
},
"add": func(a, b int) int {
return a + b
},
}
// insertTmpl is the parsed template used to generate an INSERT query.
insertTmpl = template.Must(template.New("insertQuery").Funcs(funcs).Parse(insertSQL))
// updateTmpl is the parsed template used to generate an UPDATE query.
updateTmpl = template.Must(template.New("updateQuery").Funcs(funcs).Parse(updateSQL))
// deleteTmpl is the parsed template used to generate a DELETE query.
deleteTmpl = template.Must(template.New("deleteQuery").Funcs(funcs).Parse(deleteSQL))
// selectTmpl is the parsed template used to generate a SELECT query.
selectTmpl = template.Must(template.New("selectQuery").Funcs(funcs).Parse(selectSQL))
)