-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect.go
73 lines (56 loc) · 1.46 KB
/
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
package geq
import "fmt"
type StrConcatType uint
const (
StrConcatStandard StrConcatType = iota
StrConcatFunc
)
type Dialect interface {
Placeholder(typeName string, prevArgs []any) string
Ident(v string) string
StrConcatType() StrConcatType
}
func DialectByName(driverName string) (d Dialect, err error) {
switch driverName {
case "postgres":
return &DialectPostgres{}, nil
case "mysql":
return &DialectMySQL{}, nil
default:
return nil, fmt.Errorf("unsupported DB dialect: %s", driverName)
}
}
type DialectGeneric struct{}
func (d *DialectGeneric) Placeholder(typeName string, prevArgs []any) string {
return "?"
}
func (d *DialectGeneric) Ident(v string) string {
return v
}
func (d *DialectGeneric) StrConcatType() StrConcatType {
return StrConcatStandard
}
type DialectPostgres struct{}
func (d *DialectPostgres) Placeholder(typeName string, prevArgs []any) string {
phNum := len(prevArgs) + 1
if typeName == "" {
return fmt.Sprintf("$%d", phNum)
}
return fmt.Sprintf("$%d::%s", phNum, typeName)
}
func (d *DialectPostgres) Ident(v string) string {
return fmt.Sprintf(`"%s"`, v)
}
func (d *DialectPostgres) StrConcatType() StrConcatType {
return StrConcatStandard
}
type DialectMySQL struct{}
func (d *DialectMySQL) Placeholder(typeName string, prevArgs []any) string {
return "?"
}
func (d *DialectMySQL) Ident(v string) string {
return fmt.Sprintf("`%s`", v)
}
func (d *DialectMySQL) StrConcatType() StrConcatType {
return StrConcatFunc
}