forked from slicebit/qb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclauses.go
107 lines (91 loc) · 2.35 KB
/
clauses.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
package qb
// SQLText returns a raw SQL clause
func SQLText(text string) TextClause {
return TextClause{Text: text}
}
// TextClause is a raw SQL clause
type TextClause struct {
Text string
}
// Accept calls the compiler VisitText method
func (c TextClause) Accept(context Context) string {
return context.Compiler().VisitText(context, c)
}
// List returns a list-of-clauses clause
func List(clauses ...Clause) ListClause {
return ListClause{
Clauses: clauses,
}
}
// ListClause is a list of clause elements (for IN operator for example)
type ListClause struct {
Clauses []Clause
}
// Accept calls the compiler VisitList method
func (c ListClause) Accept(context Context) string {
return context.Compiler().VisitList(context, c)
}
// Bind a value
func Bind(value interface{}) BindClause {
return BindClause{
Value: value,
}
}
// BindClause binds a value to a placeholder
type BindClause struct {
Value interface{}
}
// Accept calls the compiler VisitBind method
func (c BindClause) Accept(context Context) string {
return context.Compiler().VisitBind(context, c)
}
// GetClauseFrom returns the value if already a Clause, or make one
// if it is a scalar value
func GetClauseFrom(value interface{}) Clause {
if clause, ok := value.(Clause); ok {
return clause
}
// For now we assume any non-clause is a Value:
return Bind(value)
}
// GetListFrom returns a list clause from any list
//
// If only one value is passed and is a ListClause, it is returned
// as-is.
// In any other case, a ListClause is built with each value wrapped
// by a Bind() if not already a Clause
func GetListFrom(values ...interface{}) Clause {
if len(values) == 1 {
if clause, ok := values[0].(ListClause); ok {
return clause
}
}
var clauses []Clause
for _, value := range values {
clauses = append(clauses, GetClauseFrom(value))
}
return List(clauses...)
}
// Exists returns a EXISTS clause
func Exists(sel SelectStmt) ExistsClause {
return ExistsClause{
Select: sel,
Not: false,
}
}
// NotExists returns a NOT EXISTS clause
func NotExists(sel SelectStmt) ExistsClause {
return ExistsClause{
Select: sel,
Not: true,
}
}
// ExistsClause is a EXISTS clause
type ExistsClause struct {
Select SelectStmt
Not bool
}
// Accept calls compiler VisitExists methos
func (c ExistsClause) Accept(context Context) string {
return context.Compiler().VisitExists(context, c)
}