-
Notifications
You must be signed in to change notification settings - Fork 39
/
starters.go
99 lines (81 loc) · 2.17 KB
/
starters.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
package psql
import (
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/psql/dialect"
"github.com/stephenafamo/bob/expr"
)
type Expression = dialect.Expression
//nolint:gochecknoglobals
var bmod = expr.Builder[Expression, Expression]{}
// F creates a function expression with the given name and args
//
// SQL: generate_series(1, 3)
// Go: psql.F("generate_series", 1, 3)
func F(name string, args ...any) *dialect.Function {
f := dialect.NewFunction(name, args...)
// We have embedded the same function as the chain base
// this is so that chained methods can also be used by functions
f.Chain.Base = &f
return &f
}
// S creates a string literal
// SQL: 'a string'
// Go: psql.S("a string")
func S(s string) Expression {
return bmod.S(s)
}
// SQL: NOT true
// Go: psql.Not("true")
func Not(exp bob.Expression) Expression {
return bmod.Not(exp)
}
// SQL: a OR b OR c
// Go: psql.Or("a", "b", "c")
func Or(args ...bob.Expression) Expression {
return bmod.Or(args...)
}
// SQL: a AND b AND c
// Go: psql.And("a", "b", "c")
func And(args ...bob.Expression) Expression {
return bmod.And(args...)
}
// SQL: a || b || c
// Go: psql.Concat("a", "b", "c")
func Concat(args ...bob.Expression) Expression {
return expr.X[Expression, Expression](expr.Join{Exprs: args, Sep: " || "})
}
// SQL: $1, $2, $3
// Go: psql.Args("a", "b", "c")
func Arg(args ...any) Expression {
return bmod.Arg(args...)
}
// SQL: ($1, $2, $3)
// Go: psql.ArgGroup("a", "b", "c")
func ArgGroup(args ...any) Expression {
return bmod.ArgGroup(args...)
}
// SQL: $1, $2, $3
// Go: psql.Placeholder(3)
func Placeholder(n uint) Expression {
return bmod.Placeholder(n)
}
// SQL: (a, b)
// Go: psql.Group("a", "b")
func Group(exps ...bob.Expression) Expression {
return bmod.Group(exps...)
}
// SQL: "table"."column"
// Go: psql.Quote("table", "column")
func Quote(ss ...string) Expression {
return bmod.Quote(ss...)
}
// SQL: where a = $1
// Go: psql.Raw("where a = ?", "something")
func Raw(query string, args ...any) Expression {
return bmod.Raw(query, args...)
}
// SQL: a as "alias"
// Go: psql.As("a", "alias")
func As(e Expression, alias string) bob.Expression {
return expr.OP("AS", e, expr.Quote(alias))
}