-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
139 lines (113 loc) · 2.91 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
package activerecord
import (
"fmt"
"strings"
)
type Predicate struct {
Cond string
Args []interface{}
}
type join struct {
Relation *Relation
Association Association
}
type QueryMethods interface {
Where(cond string, arg interface{}) *Relation
Select(attrs ...string) *Relation
Group(attrs ...string) *Relation
Joins(assocs ...string) *Relation
Limit(num int) *Relation
}
type QueryBuilder struct {
from string
limit *int
selectValues []string
whereValues []Predicate
groupValues []string
joinValues []join
}
func (q *QueryBuilder) copy() *QueryBuilder {
newq := QueryBuilder{
from: q.from,
limit: q.limit,
selectValues: make([]string, len(q.selectValues)),
whereValues: make([]Predicate, len(q.whereValues)),
groupValues: make([]string, len(q.groupValues)),
joinValues: make([]join, len(q.joinValues)),
}
copy(newq.selectValues, q.selectValues)
copy(newq.whereValues, q.whereValues)
copy(newq.groupValues, q.groupValues)
copy(newq.joinValues, q.joinValues)
return &newq
}
func (q *QueryBuilder) From(from string) {
q.from = from
}
func (q *QueryBuilder) Select(columns ...string) {
q.selectValues = append(q.selectValues, columns...)
}
func (q *QueryBuilder) Where(cond string, args ...interface{}) {
q.whereValues = append(q.whereValues, Predicate{cond, args})
}
func (q *QueryBuilder) Group(values ...string) {
q.groupValues = append(q.groupValues, values...)
}
func (q *QueryBuilder) Join(rel *Relation, assoc Association) {
q.joinValues = append(q.joinValues, join{rel, assoc})
}
func (q *QueryBuilder) Limit(num int) {
q.limit = &num
}
func (q *QueryBuilder) String() string {
if q.from == "" {
panic("from is not set")
}
var (
buf strings.Builder
selectValues = q.selectValues
)
if len(selectValues) == 0 {
selectValues = []string{"*"}
}
fmt.Fprintf(&buf, `SELECT %s FROM "%s"`, strings.Join(selectValues, ", "), q.from)
for _, join := range q.joinValues {
var (
on = join.Relation.TableName()
pk = join.Relation.PrimaryKey()
fk = join.Association.AssociationForeignKey()
)
fmt.Fprintf(&buf, ` INNER JOIN "%s" ON `, on)
fmt.Fprintf(&buf, `%s.%s = %s.%s `, q.from, fk, on, pk)
}
if len(q.whereValues) > 0 {
fmt.Fprintf(&buf, ` WHERE`)
}
for i, where := range q.whereValues {
if i < len(q.whereValues)-1 {
fmt.Fprintf(&buf, ` AND`)
}
fmt.Fprintf(&buf, ` (%s)`, where.Cond)
}
if len(q.groupValues) > 0 {
fmt.Fprintf(&buf, ` GROUP BY %s`, strings.Join(q.groupValues, ", "))
}
if q.limit != nil {
fmt.Fprintf(&buf, ` LIMIT %d`, *q.limit)
}
return buf.String()
}
func (q *QueryBuilder) Args() []interface{} {
args := make([]interface{}, 0, len(q.whereValues))
for i := range q.whereValues {
args = append(args, q.whereValues[i].Args...)
}
return args
}
func (q *QueryBuilder) Operation() *QueryOperation {
return &QueryOperation{
Text: q.String(),
Args: q.Args(),
Columns: q.selectValues,
}
}