-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialect_pg.go
265 lines (232 loc) · 6 KB
/
dialect_pg.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package lorm
import (
"errors"
"github.com/lontten/lorm/insert-type"
"github.com/lontten/lorm/return-type"
"github.com/lontten/lorm/utils"
"strconv"
"strings"
)
type PgDialect struct {
ctx *ormContext
}
// ===----------------------------------------------------------------------===//
// 获取上下文
// ===----------------------------------------------------------------------===//
func (d *PgDialect) getCtx() *ormContext {
return d.ctx
}
func (d *PgDialect) initContext() Dialecter {
return &PgDialect{
ctx: &ormContext{
ormConf: d.ctx.ormConf,
query: &strings.Builder{},
wb: W(),
insertType: insert_type.Err,
},
}
}
func (d *PgDialect) hasErr() bool {
return d.ctx.err != nil
}
func (d *PgDialect) getErr() error {
return d.ctx.err
}
// ===----------------------------------------------------------------------===//
// sql 方言化
// ===----------------------------------------------------------------------===//
func (d *PgDialect) prepare(query string) string {
query = toPgSql(query)
return query
}
func (d *PgDialect) exec(query string, args ...any) (string, []any) {
query = toPgSql(query)
return query, args
}
func (d *PgDialect) query(query string, args ...any) (string, []any) {
query = toPgSql(query)
return query, args
}
func (d *PgDialect) queryBatch(query string) string {
query = toPgSql(query)
//return m.ldb.Prepare(query)
return query
}
func (d *PgDialect) getSql() string {
s := d.ctx.query.String()
return toPgSql(s)
}
// insert 生成
func (d *PgDialect) tableInsertGen() {
ctx := d.ctx
if ctx.hasErr() {
return
}
if ctx.insertType == insert_type.Replace {
ctx.err = errors.New("pg不支持的插入类型 insert-type.Replace")
return
}
extra := ctx.extra
set := extra.set
columns := ctx.columns
var query = d.ctx.query
query.WriteString("INSERT INTO ")
query.WriteString(ctx.tableName + " ")
query.WriteString("(")
query.WriteString(strings.Join(columns, ","))
query.WriteString(") ")
query.WriteString("VALUES")
query.WriteString("(")
ctx.genInsertValuesSqlBycolumnValues()
query.WriteString(") ")
if ctx.insertType == insert_type.Ignore || ctx.insertType == insert_type.Update {
query.WriteString("ON CONFLICT (")
query.WriteString(strings.Join(extra.duplicateKeyNames, ","))
query.WriteString(") DO ")
}
switch ctx.insertType {
case insert_type.Ignore:
query.WriteString("NOTHING ")
break
case insert_type.Update:
query.WriteString("UPDATE SET ")
// 当未设置更新字段时,默认为所有字段
if len(set.columns) == 0 && len(set.fieldNames) == 0 {
list := append(ctx.columns, extra.columns...)
for _, name := range list {
find := utils.Find(extra.duplicateKeyNames, name)
if find < 0 {
set.fieldNames = append(set.fieldNames, name)
}
}
}
for i, name := range set.fieldNames {
query.WriteString(name + "= EXCLUDED." + name)
if i < len(set.fieldNames)-1 {
query.WriteString(",")
}
}
for i, column := range set.columns {
if i > 0 {
query.WriteString(", ")
}
query.WriteString(column + "= ? ")
ctx.args = append(ctx.args, set.columnValues[i].Value)
}
break
default:
break
}
if ctx.scanIsPtr {
switch expr := ctx.returnType; expr {
case return_type.None:
ctx.sqlIsQuery = false
break
case return_type.PrimaryKey:
query.WriteString(" RETURNING " + strings.Join(ctx.primaryKeyNames, ","))
case return_type.ZeroField:
query.WriteString(" RETURNING " + strings.Join(ctx.modelZeroFieldNames, ","))
case return_type.AllField:
query.WriteString(" RETURNING " + strings.Join(ctx.modelAllFieldNames, ","))
}
} else {
ctx.sqlIsQuery = false
}
query.WriteString(";")
}
// del 生成
func (d *PgDialect) tableDelGen() {
}
// update 生成
func (d *PgDialect) tableUpdateGen() {
}
// select 生成
func (d *PgDialect) tableSelectGen() {
}
func (d *PgDialect) execBatch(query string, args [][]any) (string, [][]any) {
query = toPgSql(query)
//var num int64 = 0
//stmt, err := m.ldb.Prepare(query)
//defer stmt.Close()
//if err != nil {
// return 0, err
//}
//for _, arg := range args {
// exec, err := stmt.Exec(arg...)
//
// m.log.Println(query, arg)
// if err != nil {
// return num, err
// }
// rowsAffected, err := exec.RowsAffected()
// if err != nil {
// return num, err
// }
// num += rowsAffected
//}
return query, args
}
// ===----------------------------------------------------------------------===//
// 工具
// ===----------------------------------------------------------------------===//
func toPgSql(sql string) string {
var i = 1
for {
t := strings.Replace(sql, "?", "$"+strconv.Itoa(i), 1)
if t == sql {
break
}
i++
sql = t
}
return sql
}
// ===----------------------------------------------------------------------===//
// 中间服务
// ===----------------------------------------------------------------------===//
func (d *PgDialect) toSqlInsert() (string, []any) {
tableName := d.ctx.tableName
return tableName, nil
}
func (d *PgDialect) parse(c Clause) (string, error) {
sb := strings.Builder{}
switch c.Type {
case Eq:
sb.WriteString(c.query + " = ?")
case Neq:
sb.WriteString(c.query + " <> ?")
case Less:
sb.WriteString(c.query + " < ?")
case LessEq:
sb.WriteString(c.query + " <= ?")
case Greater:
sb.WriteString(c.query + " > ?")
case GreaterEq:
sb.WriteString(c.query + " >= ?")
case Like:
sb.WriteString(c.query + " LIKE ?")
case NotLike:
sb.WriteString(c.query + " NOT LIKE ?")
case In:
sb.WriteString(c.query + " IN (")
sb.WriteString(gen(c.argsNum))
sb.WriteString(")")
case NotIn:
sb.WriteString(c.query + " NOT IN (")
sb.WriteString(gen(c.argsNum))
sb.WriteString(")")
case Between:
sb.WriteString(c.query + " BETWEEN ? AND ?")
case NotBetween:
sb.WriteString(c.query + " NOT BETWEEN ? AND ?")
case IsNull:
sb.WriteString(c.query + " IS NULL")
case IsNotNull:
sb.WriteString(c.query + " IS NOT NULL")
case IsFalse:
sb.WriteString(c.query + " IS FALSE")
default:
return "", errors.New("unknown where token type")
}
return sb.String(), nil
}