-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
postgres_dialect.go
349 lines (297 loc) · 9.11 KB
/
postgres_dialect.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package migrator
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/lib/pq"
"xorm.io/xorm"
)
type PostgresDialect struct {
BaseDialect
}
func NewPostgresDialect() Dialect {
d := PostgresDialect{}
d.BaseDialect.dialect = &d
d.BaseDialect.driverName = Postgres
return &d
}
func (db *PostgresDialect) SupportEngine() bool {
return false
}
func (db *PostgresDialect) Quote(name string) string {
return "\"" + name + "\""
}
func (db *PostgresDialect) LikeStr() string {
return "ILIKE"
}
func (db *PostgresDialect) AutoIncrStr() string {
return ""
}
func (db *PostgresDialect) BooleanStr(value bool) string {
return strconv.FormatBool(value)
}
func (db *PostgresDialect) BatchSize() int {
return 1000
}
func (db *PostgresDialect) SQLType(c *Column) string {
var res string
switch t := c.Type; t {
case DB_TinyInt:
res = DB_SmallInt
return res
case DB_MediumInt, DB_Int, DB_Integer:
if c.IsAutoIncrement {
return DB_Serial
}
return DB_Integer
case DB_Serial, DB_BigSerial:
c.IsAutoIncrement = true
c.Nullable = false
res = t
case DB_Binary, DB_VarBinary:
return DB_Bytea
case DB_DateTime:
res = DB_TimeStamp
case DB_TimeStampz:
return "timestamp with time zone"
case DB_Float:
res = DB_Real
case DB_TinyText, DB_MediumText, DB_LongText:
res = DB_Text
case DB_NVarchar:
res = DB_Varchar
case DB_Uuid:
res = DB_Uuid
case DB_Blob, DB_TinyBlob, DB_MediumBlob, DB_LongBlob:
return DB_Bytea
case DB_Double:
return "DOUBLE PRECISION"
default:
if c.IsAutoIncrement {
return DB_Serial
}
res = t
}
var hasLen1 = (c.Length > 0)
var hasLen2 = (c.Length2 > 0)
if hasLen2 {
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
} else if hasLen1 {
res += "(" + strconv.Itoa(c.Length) + ")"
}
return res
}
func (db *PostgresDialect) IndexCheckSQL(tableName, indexName string) (string, []any) {
args := []any{tableName, indexName}
sql := "SELECT 1 FROM " + db.Quote("pg_indexes") + " WHERE" + db.Quote("tablename") + "=? AND " + db.Quote("indexname") + "=?"
return sql, args
}
func (db *PostgresDialect) DropIndexSQL(tableName string, index *Index) string {
quote := db.Quote
idxName := index.XName(tableName)
return fmt.Sprintf("DROP INDEX %v CASCADE", quote(idxName))
}
func (db *PostgresDialect) UpdateTableSQL(tableName string, columns []*Column) string {
var statements = []string{}
for _, col := range columns {
statements = append(statements, "ALTER "+db.Quote(col.Name)+" TYPE "+db.SQLType(col))
}
return "ALTER TABLE " + db.Quote(tableName) + " " + strings.Join(statements, ", ") + ";"
}
func (db *PostgresDialect) CleanDB(engine *xorm.Engine) error {
sess := engine.NewSession()
defer sess.Close()
if _, err := sess.Exec("DROP SCHEMA public CASCADE;"); err != nil {
return fmt.Errorf("%v: %w", "failed to drop schema public", err)
}
if _, err := sess.Exec("CREATE SCHEMA public;"); err != nil {
return fmt.Errorf("%v: %w", "failed to create schema public", err)
}
return nil
}
// TruncateDBTables truncates all the tables.
// A special case is the dashboard_acl table where we keep the default permissions.
func (db *PostgresDialect) TruncateDBTables(engine *xorm.Engine) error {
tables, err := engine.DBMetas()
if err != nil {
return err
}
sess := engine.NewSession()
defer sess.Close()
for _, table := range tables {
switch table.Name {
case "":
continue
case "migration_log":
continue
case "dashboard_acl":
// keep default dashboard permissions
if _, err := sess.Exec(fmt.Sprintf("DELETE FROM %v WHERE dashboard_id != -1 AND org_id != -1;", db.Quote(table.Name))); err != nil {
return fmt.Errorf("failed to truncate table %q: %w", table.Name, err)
}
if _, err := sess.Exec(fmt.Sprintf("ALTER SEQUENCE %v RESTART WITH 3;", db.Quote(fmt.Sprintf("%v_id_seq", table.Name)))); err != nil {
return fmt.Errorf("failed to reset table %q: %w", table.Name, err)
}
default:
if _, err := sess.Exec(fmt.Sprintf("TRUNCATE TABLE %v RESTART IDENTITY CASCADE;", db.Quote(table.Name))); err != nil {
if db.isUndefinedTable(err) {
continue
}
return fmt.Errorf("failed to truncate table %q: %w", table.Name, err)
}
}
}
return nil
}
func (db *PostgresDialect) isThisError(err error, errcode string) bool {
var driverErr *pq.Error
if errors.As(err, &driverErr) {
if string(driverErr.Code) == errcode {
return true
}
}
return false
}
func (db *PostgresDialect) ErrorMessage(err error) string {
var driverErr *pq.Error
if errors.As(err, &driverErr) {
return driverErr.Message
}
return ""
}
func (db *PostgresDialect) isUndefinedTable(err error) bool {
return db.isThisError(err, "42P01")
}
func (db *PostgresDialect) IsUniqueConstraintViolation(err error) bool {
return db.isThisError(err, "23505")
}
func (db *PostgresDialect) IsDeadlock(err error) bool {
return db.isThisError(err, "40P01")
}
func (db *PostgresDialect) PostInsertId(table string, sess *xorm.Session) error {
if table != "org" {
return nil
}
// sync primary key sequence of org table
if _, err := sess.Exec("SELECT setval('org_id_seq', (SELECT max(id) FROM org));"); err != nil {
return fmt.Errorf("failed to sync primary key for org table: %w", err)
}
return nil
}
// UpsertSQL returns the upsert sql statement for PostgreSQL dialect
func (db *PostgresDialect) UpsertSQL(tableName string, keyCols, updateCols []string) string {
str, _ := db.UpsertMultipleSQL(tableName, keyCols, updateCols, 1)
return str
}
// UpsertMultipleSQL returns the upsert sql statement for PostgreSQL dialect
func (db *PostgresDialect) UpsertMultipleSQL(tableName string, keyCols, updateCols []string, count int) (string, error) {
if count < 1 {
return "", fmt.Errorf("upsert statement must have count >= 1. Got %v", count)
}
columnsStr := strings.Builder{}
onConflictStr := strings.Builder{}
setStr := strings.Builder{}
const separator = ", "
separatorVar := separator
for i, c := range updateCols {
if i == len(updateCols)-1 {
separatorVar = ""
}
columnsStr.WriteString(fmt.Sprintf("%s%s", db.Quote(c), separatorVar))
setStr.WriteString(fmt.Sprintf("%s=EXCLUDED.%s%s", db.Quote(c), db.Quote(c), separatorVar))
}
separatorVar = separator
for i, c := range keyCols {
if i == len(keyCols)-1 {
separatorVar = ""
}
onConflictStr.WriteString(fmt.Sprintf("%s%s", db.Quote(c), separatorVar))
}
valuesStr := strings.Builder{}
separatorVar = separator
nextPlaceHolder := 1
for i := 0; i < count; i++ {
if i == count-1 {
separatorVar = ""
}
colPlaceHoldersStr := strings.Builder{}
placeHolderSep := separator
for j := 1; j <= len(updateCols); j++ {
if j == len(updateCols) {
placeHolderSep = ""
}
placeHolder := fmt.Sprintf("$%v%s", nextPlaceHolder, placeHolderSep)
nextPlaceHolder++
colPlaceHoldersStr.WriteString(placeHolder)
}
colPlaceHolders := colPlaceHoldersStr.String()
valuesStr.WriteString(fmt.Sprintf("(%s)%s", colPlaceHolders, separatorVar))
}
s := fmt.Sprintf(`INSERT INTO %s (%s) VALUES %s ON CONFLICT (%s) DO UPDATE SET %s;`,
tableName,
columnsStr.String(),
valuesStr.String(),
onConflictStr.String(),
setStr.String(),
)
return s, nil
}
func (db *PostgresDialect) Lock(cfg LockCfg) error {
// trying to obtain the lock for a resource identified by a 64-bit or 32-bit key value
// the lock is exclusive: multiple lock requests stack, so that if the same resource is locked three times
// it must then be unlocked three times to be released for other sessions' use.
// it will either obtain the lock immediately and return true,
// or return false if the lock cannot be acquired immediately.
query := "SELECT pg_try_advisory_lock(?)"
var success bool
_, err := cfg.Session.SQL(query, cfg.Key).Get(&success)
if err != nil {
return err
}
if !success {
return ErrLockDB
}
return nil
}
// OrderBy normalizes ordering so that nulls end up last in sorting, which they do by default in both sqlite and mysql but not postgres
// order should be a string like `dashboard.id ASC`
func (db *PostgresDialect) OrderBy(order string) string {
nullSort := "FIRST"
normalizedOrder := strings.ToUpper(strings.TrimSpace(order))
if strings.HasSuffix(normalizedOrder, " DESC") {
nullSort = "LAST"
}
return fmt.Sprintf("%s NULLS %s", order, nullSort)
}
func (db *PostgresDialect) Unlock(cfg LockCfg) error {
// trying to release a previously-acquired exclusive session level advisory lock.
// it will either return true if the lock is successfully released or
// false if the lock was not held (in addition an SQL warning will be reported by the server)
query := "SELECT pg_advisory_unlock(?)"
var success bool
_, err := cfg.Session.SQL(query, cfg.Key).Get(&success)
if err != nil {
return err
}
if !success {
return ErrReleaseLockDB
}
return nil
}
func (db *PostgresDialect) GetDBName(dsn string) (string, error) {
if strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://") {
parsedDSN, err := pq.ParseURL(dsn)
if err != nil {
return "", err
}
dsn = parsedDSN
}
re := regexp.MustCompile(`dbname=(\w+)`)
submatch := re.FindSubmatch([]byte(dsn))
if len(submatch) < 2 {
return "", fmt.Errorf("failed to get database name")
}
return string(submatch[1]), nil
}