/
pgoper.go
235 lines (206 loc) · 6.67 KB
/
pgoper.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package processors
import (
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
)
type ZeroPostgresQueryOperation struct {
ZeroCoreProcessor
query *ZeroQuery
tableName string
distinctID string
filterTableName string
columns string
conditions string
orderby string
limit string
Start int
Length int
}
func NewZeroPostgresQueryOperation(xQuery *ZeroQuery, xTableName string) *ZeroPostgresQueryOperation {
return &ZeroPostgresQueryOperation{
query: xQuery,
tableName: xTableName,
}
}
func (opera *ZeroPostgresQueryOperation) Build(transaction *sql.Tx) {
opera.ZeroCoreProcessor.Build(transaction)
opera.makeColumns()
opera.makeConditions()
opera.makeOrderby()
opera.makeLimit()
}
func (opera *ZeroPostgresQueryOperation) AddQuery(xQuery *ZeroQuery) { opera.query = xQuery }
func (opera *ZeroPostgresQueryOperation) AddTableName(tableName string) { opera.tableName = tableName }
func (opera *ZeroPostgresQueryOperation) AddDistinctID(distinctID string) {
opera.distinctID = distinctID
}
func (opera *ZeroPostgresQueryOperation) AddFilterTableName(filterTableName string) {
opera.filterTableName = filterTableName
}
func (opera *ZeroPostgresQueryOperation) AppendCondition(condition string) {
if len(opera.conditions) <= 0 {
opera.conditions = fmt.Sprintf(" WHERE (%s)", condition)
} else {
opera.conditions = fmt.Sprintf(" %s AND (%s)", opera.conditions, condition)
}
}
func (opera *ZeroPostgresQueryOperation) parserConditions(condition *ZeroCondition) (string, error) {
if condition.Relation == nil || len(condition.Relation) <= 0 {
symbol, ok := symbols()[condition.Symbol]
if !ok {
return "", errors.New(fmt.Sprintf("symbol `%s` not found", condition.Symbol))
}
return fmt.Sprintf("(\"%s\" %s '%s')", exHumpToLine(condition.Column), symbol, condition.Value), nil
} else {
relatSymbol, ok := relations()[condition.Symbol]
if !ok {
return "", errors.New(fmt.Sprintf("relation `%s` not found", condition.Symbol))
}
relats := make([]string, len(condition.Relation))
for i, relation := range condition.Relation {
relat, err := opera.parserConditions(relation)
if err != nil {
return "", nil
}
relats[i] = relat
}
return fmt.Sprintf("(%s)", strings.Join(relats, relatSymbol)), nil
}
}
func (opera *ZeroPostgresQueryOperation) makeColumns() {
columnsPrefix := ""
if len(opera.distinctID) > 0 && len(opera.filterTableName) > 0 {
columnsPrefix = "a."
}
if opera.query.Columns == nil || len(opera.query.Columns) <= 0 {
opera.columns = fmt.Sprintf(" %s* ", columnsPrefix)
} else {
columns := make([]string, len(opera.query.Columns))
for i, column := range opera.query.Columns {
columns[i] = fmt.Sprintf("\"%s\"", exHumpToLine(column))
}
opera.columns = fmt.Sprintf(" %s%s ", columnsPrefix, strings.Join(columns, fmt.Sprintf(", %s", columnsPrefix)))
}
}
func (opera *ZeroPostgresQueryOperation) makeConditions() error {
if opera.query.Condition == nil || len(opera.query.Condition.Symbol) <= 0 {
opera.conditions = ""
} else {
condi, err := opera.parserConditions(opera.query.Condition)
if err != nil {
return err
}
opera.conditions = fmt.Sprintf(" WHERE %s ", condi)
}
return nil
}
func (opera *ZeroPostgresQueryOperation) makeOrderby() {
if opera.query.Orderby == nil || len(opera.query.Orderby) <= 0 {
opera.orderby = ""
} else {
orders := make([]string, len(opera.query.Orderby))
for i, o := range opera.query.Orderby {
orders[i] = fmt.Sprintf(" \"%s\" %s", exHumpToLine(o.Column), o.Seq)
}
opera.orderby = fmt.Sprintf(" ORDER BY %s ", strings.Join(orders, ","))
}
}
func (opera *ZeroPostgresQueryOperation) makeLimit() {
if opera.query.Limit.Length > 0 {
if opera.query.Limit.Length > 100 {
opera.Start = opera.query.Limit.Start
opera.Length = 100
} else {
opera.Start = opera.query.Limit.Start
opera.Length = opera.query.Limit.Length
}
} else {
opera.Start = 0
opera.Length = 1
}
opera.limit = fmt.Sprintf(" OFFSET %d LIMIT %d ", opera.Start, opera.Length)
}
const DISTINCT_POSTGRES_QUERY_SQL_TEMPLATE = `
SELECT
{{columns}}
FROM
(SELECT
distinct {{distinctID}}
FROM
{{filterTableName}}
{{conditions}}) t,
{{tableName}} a
WHERE
t.{{distinctID}} = a.ID
{{orderby}} {{limit}}
`
const DISTINCT_POSTGRES_QUERY_COUNT_SQL_TEMPLATE = `
SELECT
count(distinct {{distinctID}}) AS QUERY_COUNT
FROM
{{filterTableName}}
{{conditions}}
`
func (opera *ZeroPostgresQueryOperation) makeQuerySQL() string {
querySQL := ""
if len(opera.distinctID) > 0 && len(opera.filterTableName) > 0 {
querySQL = strings.ReplaceAll(DISTINCT_POSTGRES_QUERY_SQL_TEMPLATE, "{{columns}}", opera.columns)
querySQL = strings.ReplaceAll(querySQL, "{{tableName}}", opera.tableName)
querySQL = strings.ReplaceAll(querySQL, "{{conditions}}", opera.conditions)
querySQL = strings.ReplaceAll(querySQL, "{{orderby}}", opera.orderby)
querySQL = strings.ReplaceAll(querySQL, "{{limit}}", opera.limit)
querySQL = strings.ReplaceAll(querySQL, "{{distinctID}}", opera.distinctID)
querySQL = strings.ReplaceAll(querySQL, "{{filterTableName}}", opera.filterTableName)
} else {
querySQL = fmt.Sprintf("SELECT%sFROM %s %s %s %s",
opera.columns,
opera.tableName,
opera.conditions,
opera.orderby,
opera.limit)
}
return querySQL
}
func (opera *ZeroPostgresQueryOperation) makeQueryCountSQL() string {
queryCountSQL := ""
if len(opera.distinctID) > 0 && len(opera.filterTableName) > 0 {
queryCountSQL = strings.ReplaceAll(DISTINCT_POSTGRES_QUERY_COUNT_SQL_TEMPLATE, "{{conditions}}", opera.conditions)
queryCountSQL = strings.ReplaceAll(DISTINCT_POSTGRES_QUERY_COUNT_SQL_TEMPLATE, "{{distinctID}}", opera.distinctID)
queryCountSQL = strings.ReplaceAll(DISTINCT_POSTGRES_QUERY_COUNT_SQL_TEMPLATE, "{{filterTableName}}", opera.filterTableName)
} else {
queryCountSQL = fmt.Sprintf("SELECT count(1) AS QUERY_COUNT FROM %s%s", opera.tableName, opera.conditions)
}
return queryCountSQL
}
func (opera *ZeroPostgresQueryOperation) Exec() ([]map[string]interface{}, map[string]interface{}) {
queryCountSQL := opera.makeQueryCountSQL()
rows, err := opera.PreparedStmt(queryCountSQL).Query()
defer func() {
if rows != nil {
rows.Close()
}
}()
if err != nil {
panic(err)
}
if !rows.Next() {
panic(fmt.Sprintf("query -> %s result error", queryCountSQL))
}
var total int64
err = rows.Scan(&total)
if err != nil {
panic(err)
}
rows.Close()
querySQL := opera.makeQuerySQL()
rows, err = opera.PreparedStmt(querySQL).Query()
rowsmap := opera.Parser(rows)
expands := make(map[string]interface{})
expands["start"] = strconv.Itoa(opera.Start)
expands["length"] = strconv.Itoa(opera.Length)
expands["total"] = strconv.FormatInt(total, 10)
return rowsmap, expands
}