forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_builder.go
156 lines (135 loc) · 3.38 KB
/
sql_builder.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
package pop
import (
"fmt"
"regexp"
"strings"
"github.com/jmoiron/sqlx"
. "github.com/markbates/pop/columns"
)
type sqlBuilder struct {
Query Query
Model *Model
AddColumns []string
sql string
args []interface{}
}
func newSQLBuilder(q Query, m *Model, addColumns ...string) *sqlBuilder {
return &sqlBuilder{
Query: q,
Model: m,
AddColumns: addColumns,
args: []interface{}{},
}
}
func (sq *sqlBuilder) String() string {
if sq.sql == "" {
sq.compile()
}
return sq.sql
}
func (sq *sqlBuilder) Args() []interface{} {
if len(sq.args) == 0 {
if len(sq.Query.RawSQL.Arguments) > 0 {
sq.args = sq.Query.RawSQL.Arguments
} else {
sq.compile()
}
}
return sq.args
}
func (sq *sqlBuilder) compile() {
if sq.sql == "" {
if sq.Query.RawSQL.Fragment != "" {
sq.sql = sq.Query.RawSQL.Fragment
} else {
sq.sql = sq.buildSelectSQL()
}
re := regexp.MustCompile(`(?i)in\s*\(\s*\?\s*\)`)
if re.MatchString(sq.sql) {
s, _, err := sqlx.In(sq.sql, sq.Args())
if err == nil {
sq.sql = s
}
}
sq.sql = sq.Query.Connection.Dialect.TranslateSQL(sq.sql)
}
}
func (sq *sqlBuilder) buildSelectSQL() string {
cols := sq.buildColumns()
fc := sq.buildfromClauses()
sql := fmt.Sprintf("SELECT %s FROM %s", cols.Readable().SelectString(), fc)
sql = sq.buildWhereClauses(sql)
sql = sq.buildOrderClauses(sql)
sql = sq.buildPaginationClauses(sql)
return sql
}
func (sq *sqlBuilder) buildfromClauses() fromClauses {
models := []*Model{
sq.Model,
}
for _, mc := range sq.Query.belongsToThroughClauses {
models = append(models, mc.Through)
}
fc := sq.Query.fromClauses
for _, m := range models {
tableName := m.TableName()
fc = append(fc, fromClause{
From: tableName,
As: strings.Replace(tableName, ".", "_", -1),
})
}
return fc
}
func (sq *sqlBuilder) buildWhereClauses(sql string) string {
mcs := sq.Query.belongsToThroughClauses
for _, mc := range mcs {
sq.Query.Where(fmt.Sprintf("%s.%s = ?", mc.Through.TableName(), mc.BelongsTo.associationName()), mc.BelongsTo.ID())
sq.Query.Where(fmt.Sprintf("%s.id = %s.%s", sq.Model.TableName(), mc.Through.TableName(), sq.Model.associationName()))
}
wc := sq.Query.whereClauses
if len(wc) > 0 {
sql = fmt.Sprintf("%s WHERE %s", sql, wc.Join(" AND "))
for _, arg := range wc.Args() {
sq.args = append(sq.args, arg)
}
}
return sql
}
func (sq *sqlBuilder) buildOrderClauses(sql string) string {
oc := sq.Query.orderClauses
if len(oc) > 0 {
sql = fmt.Sprintf("%s ORDER BY %s", sql, oc.Join(", "))
for _, arg := range oc.Args() {
sq.args = append(sq.args, arg)
}
}
return sql
}
func (sq *sqlBuilder) buildPaginationClauses(sql string) string {
if sq.Query.limitResults > 0 && sq.Query.Paginator == nil {
sql = fmt.Sprintf("%s LIMIT %d", sql, sq.Query.limitResults)
}
if sq.Query.Paginator != nil {
sql = fmt.Sprintf("%s LIMIT %d", sql, sq.Query.Paginator.PerPage)
sql = fmt.Sprintf("%s OFFSET %d", sql, sq.Query.Paginator.Offset)
}
return sql
}
var columnCache = map[string]Columns{}
func (sq *sqlBuilder) buildColumns() Columns {
tableName := sq.Model.TableName()
acl := len(sq.AddColumns)
if acl <= 0 {
cols, ok := columnCache[tableName]
if ok {
return cols
}
cols = ColumnsForStruct(sq.Model.Value, tableName)
columnCache[tableName] = cols
return cols
} else {
cols := NewColumns("")
cols.Add(sq.AddColumns...)
return cols
}
}