forked from deoxxa/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlfunc.go
116 lines (92 loc) · 2.53 KB
/
sqlfunc.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
package sqlbuilder
// SqlFunc represents function on SQL(ex:count(*)). This can be use in the same way as Column.
type SqlFunc interface {
Column
columns() []Column
}
type sqlFuncColumnList []Column
func (l sqlFuncColumnList) serialize(bldr *builder) {
first := true
for _, part := range l {
if first {
first = false
} else {
bldr.Append(" , ")
}
part.serialize(bldr)
}
}
type sqlFuncImpl struct {
name string
args sqlFuncColumnList
}
// Func returns new SQL function. The name is function name, and the args is arguments of function
func Func(name string, args ...Column) SqlFunc {
return &sqlFuncImpl{
name: name,
args: args,
}
}
func (m *sqlFuncImpl) As(alias string) Column {
return &aliasColumn{
column: m,
alias: alias,
}
}
func (m *sqlFuncImpl) column_name() string {
return m.name
}
func (m *sqlFuncImpl) not_null() bool {
return true
}
func (m *sqlFuncImpl) config() ColumnConfig {
return nil
}
func (m *sqlFuncImpl) acceptType(interface{}) bool {
return false
}
func (m *sqlFuncImpl) serialize(bldr *builder) {
bldr.Append(m.name)
bldr.Append("(")
bldr.AppendItem(m.args)
bldr.Append(")")
}
func (left *sqlFuncImpl) Eq(right interface{}) Condition {
return newBinaryOperationCondition(left, right, "=")
}
func (left *sqlFuncImpl) NotEq(right interface{}) Condition {
return newBinaryOperationCondition(left, right, "<>")
}
func (left *sqlFuncImpl) Gt(right interface{}) Condition {
return newBinaryOperationCondition(left, right, ">")
}
func (left *sqlFuncImpl) GtEq(right interface{}) Condition {
return newBinaryOperationCondition(left, right, ">=")
}
func (left *sqlFuncImpl) Lt(right interface{}) Condition {
return newBinaryOperationCondition(left, right, "<")
}
func (left *sqlFuncImpl) LtEq(right interface{}) Condition {
return newBinaryOperationCondition(left, right, "<=")
}
func (left *sqlFuncImpl) Like(right string) Condition {
return newBinaryOperationCondition(left, right, " LIKE ")
}
func (left *sqlFuncImpl) Between(lower, higher interface{}) Condition {
return newBetweenCondition(left, lower, higher)
}
func (left *sqlFuncImpl) In(vals ...interface{}) Condition {
return newInCondition(left, vals...)
}
func (left *sqlFuncImpl) IntersectJSON(data string) Condition {
return newIntersectJSONCondition(left, data)
}
func (left *sqlFuncImpl) NotIn(val ...interface{}) Condition {
return newNotInCondition(left, val...)
}
func (m *sqlFuncImpl) columns() []Column {
return m.args
}
func (left *sqlFuncImpl) IsNull() Condition {
return newUnaryOperationCondition(left, "IS NULL")
}