-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
183 lines (142 loc) · 5.08 KB
/
root.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
176
177
178
179
180
181
182
183
package qrb
import (
"github.com/networkteam/qrb/builder"
)
// This file exports the root level functions for building queries.
// All functions are just wrappers around the builder package, so all the builder types and interfaces don't clutter the root package exports.
// Build starts a new query builder based on the given SQLWriter.
// For executing the query, use qrbpgx.Build or qrbsql.Build which can set an executor specific to a driver.
func Build(w builder.SQLWriter) *builder.QueryBuilder {
return builder.Build(w)
}
// ---
// With starts a new builder with the given WITH query.
// Call WithBuilder.As to define the query.
func With(queryName string) builder.WithWithBuilder {
return builder.With(queryName)
}
// WithRecursive starts a new builder with the given WITH RECURSIVE query.
func WithRecursive(queryName string) builder.WithWithBuilder {
return builder.WithRecursive(queryName)
}
// Select the given output expressions for the select list and start a new SelectBuilder.
func Select(exps ...builder.Exp) builder.SelectSelectBuilder {
var selectBuilder builder.SelectBuilder
return selectBuilder.Select(exps...)
}
// SelectJson sets the JSON selection for this select builder.
//
// It will always be the first element in the select list.
// It can be modified later by SelectBuilder.ApplySelectJson.
func SelectJson(obj builder.JsonBuildObjectBuilder) builder.SelectJsonSelectBuilder {
var selectBuilder builder.SelectBuilder
return selectBuilder.ApplySelectJson(func(builder builder.JsonBuildObjectBuilder) builder.JsonBuildObjectBuilder { return obj })
}
// Agg builds an aggregate function expression.
func Agg(name string, exps []builder.Exp) builder.AggExpBuilder {
return builder.Agg(name, exps)
}
// Func is a function call expression.
func Func(name string, args ...builder.Exp) builder.FuncBuilder {
return builder.Func(name, args...)
}
func RowsFrom(fn builder.FuncBuilder, fns ...builder.FuncBuilder) builder.RowsFromBuilder {
return builder.NewRowsFromBuilder(
append([]builder.FuncBuilder{fn}, fns...)...,
)
}
func And(exps ...builder.Exp) builder.Exp {
return builder.And(exps...)
}
func Or(exps ...builder.Exp) builder.Exp {
return builder.Or(exps...)
}
func Not(exp builder.Exp) builder.Exp {
return builder.Not(exp)
}
func Case(exp ...builder.Exp) builder.CaseBuilder {
return builder.Case(exp...)
}
func Coalesce(exp builder.Exp, rest ...builder.Exp) builder.ExpBase {
return builder.Coalesce(exp, rest...)
}
func NullIf(value1, value2 builder.Exp) builder.ExpBase {
return builder.NullIf(value1, value2)
}
func Greatest(exp builder.Exp, rest ...builder.Exp) builder.ExpBase {
return builder.Greatest(exp, rest...)
}
func Least(exp builder.Exp, rest ...builder.Exp) builder.ExpBase {
return builder.Least(exp, rest...)
}
// Arg creates an expression that represents an argument that will be bound to a placeholder with the given value.
// Each call to Arg will create a new placeholder and emit the argument when writing the query.
func Arg(argument any) builder.ExpBase {
return builder.Arg(argument)
}
// Args creates argument expressions for the given arguments (of the same type).
func Args[T any](arguments ...T) builder.Expressions {
return builder.Args(arguments...)
}
// Bind creates an expression that represents an argument that will be bound to a placeholder with the given value.
// If Bind is called again with the same name, the same placeholder will be used.
func Bind(argName string) builder.Exp {
return builder.Bind(argName)
}
// N writes the given name / identifier.
//
// It will validate the identifier when writing the query,
// but it will not detect all invalid identifiers that are invalid in PostgreSQL (especially considering reserved keywords).
func N(s string) builder.IdentExp {
return builder.N(s)
}
func String(s string) builder.Exp {
return builder.String(s)
}
func Float(f float64) builder.Exp {
return builder.Float(f)
}
func Int(s int) builder.Exp {
return builder.Int(s)
}
func Bool(b bool) builder.Exp {
return builder.Bool(b)
}
func Array(exps ...builder.Exp) builder.ExpBase {
return builder.Array(exps...)
}
func Null() builder.Exp {
return builder.Null()
}
func Default() builder.Exp {
return builder.Default()
}
func Interval(s string) builder.Exp {
return builder.Interval(s)
}
// Exps returns a slice of expressions, just for syntactic sugar.
// TODO We could use this as a way to express a scalar list of expressions e.g. for IN by using a custom slice type
func Exps(exps ...builder.Exp) builder.Expressions {
return exps
}
// --- Subquery Expressions
func Exists(subquery builder.SelectExp) builder.Exp {
return builder.Exists(subquery)
}
// --- Row and Array Comparisons
func Any(exp builder.Exp) builder.Exp {
return builder.Any(exp)
}
func All(exp builder.Exp) builder.Exp {
return builder.All(exp)
}
// --- Commands
func InsertInto(tableName builder.Identer) builder.InsertBuilder {
return builder.InsertInto(tableName)
}
func Update(tableName builder.Identer) builder.UpdateBuilder {
return builder.Update(tableName)
}
func DeleteFrom(tableName builder.Identer) builder.DeleteBuilder {
return builder.DeleteFrom(tableName)
}