-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
82 lines (68 loc) · 1.68 KB
/
bind.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
package querybuilder
import (
"strconv"
"strings"
)
// The Rebind function is inspired by sqlx: https://pkg.go.dev/github.com/jmoiron/sqlx#Rebind
// Bindvar types supported by Rebind
const (
UNKNOWN = iota
QUESTION
DOLLAR
NAMED
AT
)
const (
DriverPostgres = "postgres"
DriverPGX = "pgx"
DriverPqTimeout = "pq-timeouts"
DriverCloudSqlPostgres = "cloudsqlpostgres"
DriverMySQL = "mysql"
DriverSqlite3 = "sqlite3"
DriverOCI8 = "oci8"
DriverORA = "ora"
DriverGORACLE = "goracle"
DriverSqlServer = "sqlserver"
)
type DriverName string
// BindType returns the bindtype for a given a drivername/database.
func BindType(driverName DriverName) int {
switch driverName {
case "postgres", "pgx", "pq-timeouts", "cloudsqlpostgres":
return DOLLAR
case "mysql":
return QUESTION
case "sqlite3":
return QUESTION
case "oci8", "ora", "goracle":
return NAMED
case "sqlserver":
return AT
}
return UNKNOWN
}
// rebind a query table the default bindtype (QUESTION) to the target bindtype.
func rebind(bindType int, query string) string {
switch bindType {
case QUESTION, UNKNOWN:
return query
}
// Add space enough for 10 params before we have to allocate
rqb := make([]byte, 0, len(query)+10)
var i, j int
for i = strings.Index(query, "?"); i != -1; i = strings.Index(query, "?") {
rqb = append(rqb, query[:i]...)
switch bindType {
case DOLLAR:
rqb = append(rqb, '$')
case NAMED:
rqb = append(rqb, ':', 'a', 'r', 'g')
case AT:
rqb = append(rqb, '@', 'p')
}
j++
rqb = strconv.AppendInt(rqb, int64(j), 10)
query = query[i+1:]
}
return string(append(rqb, query...))
}