forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.go
103 lines (88 loc) · 2.85 KB
/
sql.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
package globals
import (
"database/sql"
"regexp"
"strings"
)
var SqliteEngine = false
type batch struct {
Old string
New string
Regex bool
}
func batchReplace(sql string, batch []batch) string {
for _, item := range batch {
if item.Regex {
sql = regexp.MustCompile(item.Old).ReplaceAllString(sql, item.New)
continue
}
sql = strings.ReplaceAll(sql, item.Old, item.New)
}
return sql
}
func PreflightSql(sql string) string {
// this is a simple way to adapt the sql to the sqlite engine
// it's not a common way to use sqlite in production, just as polyfill
if SqliteEngine {
if strings.Contains(sql, "DUPLICATE KEY") {
sql = batchReplace(sql, []batch{
{
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE quota = ?",
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON CONFLICT(user_id) DO UPDATE SET quota = ?",
false,
},
{
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE used = ?",
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON CONFLICT(user_id) DO UPDATE SET used = ?",
false,
},
{
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE quota = quota + ?",
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON CONFLICT(user_id) DO UPDATE SET quota = quota + ?",
false,
},
{
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE used = used + ?",
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON CONFLICT(user_id) DO UPDATE SET used = used + ?",
false,
},
{
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE quota = quota - ?",
"INSERT INTO quota (user_id, quota, used) VALUES (?, ?, ?) ON CONFLICT(user_id) DO UPDATE SET quota = quota - ?",
false,
},
})
}
sql = batchReplace(sql, []batch{
// KEYWORD REPLACEMENT
{`INT `, `INTEGER `, false},
{` AUTO_INCREMENT`, ` AUTOINCREMENT`, false},
{`DATETIME`, `TEXT`, false},
{`DECIMAL`, `REAL`, false},
{`MEDIUMTEXT`, `TEXT`, false},
{`VARCHAR`, `TEXT`, false},
// TEXT(65535) -> TEXT, REAL(10,2) -> REAL
{`TEXT\(\d+\)`, `TEXT`, true},
{`REAL\(\d+,\d+\)`, `REAL`, true},
// UNIQUE KEY -> UNIQUE
{`UNIQUE KEY`, `UNIQUE`, false},
})
}
return sql
}
func ExecDb(db *sql.DB, sql string, args ...interface{}) (sql.Result, error) {
sql = PreflightSql(sql)
return db.Exec(sql, args...)
}
func PrepareDb(db *sql.DB, sql string) (*sql.Stmt, error) {
sql = PreflightSql(sql)
return db.Prepare(sql)
}
func QueryDb(db *sql.DB, sql string, args ...interface{}) (*sql.Rows, error) {
sql = PreflightSql(sql)
return db.Query(sql, args...)
}
func QueryRowDb(db *sql.DB, sql string, args ...interface{}) *sql.Row {
sql = PreflightSql(sql)
return db.QueryRow(sql, args...)
}