-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect.go
197 lines (178 loc) · 4.27 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
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
package orm
import (
"fmt"
"orm/internal/errs"
"reflect"
"time"
)
var (
MySQL Dialect = &mysqlDialect{}
SQLite3 Dialect = &sqlite3Dialect{}
)
type Dialect interface {
// quoter 返回一个引号,引用列名,表名的引号
quoter() byte
ColTypeOf(typ reflect.Value) string
// buildOnConflict 构造插入冲突部分
buildOnConflict(b *Builder, odk *OnConflict) error
}
func dialectOf(driver string) (Dialect, error) {
switch driver {
case "sqlite3":
return SQLite3, nil
case "mysql":
return MySQL, nil
default:
return nil, errs.NewUnsupportedDriverError(driver)
}
}
type standardSQL struct{}
func (d *standardSQL) quoter() byte {
// TODO implement me
panic("implement me")
}
func (d *standardSQL) buildOnConflict(b *Builder, odk *OnConflict) error {
// TODO implement me
panic("implement me")
}
func (d *standardSQL) ColTypeOf(typ reflect.Value) string {
// TODO implement me
panic("implement me")
}
type mysqlDialect struct {
standardSQL
}
func (d *mysqlDialect) quoter() byte {
return '`'
}
func (d *mysqlDialect) buildOnConflict(b *Builder, odk *OnConflict) error {
b.writeString(" ON DUPLICATE KEY UPDATE ")
for pos, assign := range odk.assigns {
if pos > 0 {
b.writeComma()
}
switch a := assign.(type) {
case Assignment:
if err := b.buildAssignment(a); err != nil {
return err
}
case Column:
if err := d.buildConflictColumn(b, a); err != nil {
return err
}
default:
return errs.NewErrUnsupportedAssignableType(assign)
}
}
return nil
}
func (d *mysqlDialect) buildConflictColumn(b *Builder, c Column) error {
fd, ok := b.model.FieldMap[c.name]
if !ok {
return errs.NewErrUnknownField(c.name)
}
b.quote(fd.ColName)
b.writeString(" = ")
b.writeString("VALUES")
b.writeLeftParenthesis()
b.quote(fd.ColName)
b.writeRightParenthesis()
return nil
}
func (d *mysqlDialect) ColTypeOf(typ reflect.Value) string {
switch typ.Kind() {
case reflect.Bool:
return "bool"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
return "int(11)"
case reflect.Int64, reflect.Uint64:
return "bigint(11)"
case reflect.Float32, reflect.Float64:
return "float(11)"
case reflect.String:
return "longtext"
case reflect.Array, reflect.Slice:
return "blob"
case reflect.Struct:
if _, ok := typ.Interface().(time.Time); ok {
return "datetime"
}
}
panic(fmt.Sprintf("invalid sql type %s (%s)", typ.Type().Name(), typ.Kind()))
}
type sqlite3Dialect struct {
standardSQL
}
func (d *sqlite3Dialect) quoter() byte {
return '`'
}
func (d *sqlite3Dialect) ColTypeOf(typ reflect.Value) string {
switch typ.Kind() {
case reflect.Bool:
return "bool"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
return "integer"
case reflect.Int64, reflect.Uint64:
return "bigint"
case reflect.Float32, reflect.Float64:
return "real"
case reflect.String:
return "text"
case reflect.Array, reflect.Slice:
return "blob"
case reflect.Struct:
if _, ok := typ.Interface().(time.Time); ok {
return "datetime"
}
}
panic(fmt.Sprintf("invalid sql type %s (%s)", typ.Type().Name(), typ.Kind()))
}
func (d *sqlite3Dialect) buildOnConflict(b *Builder, odk *OnConflict) error {
b.writeString(" ON CONFLICT")
if len(odk.conflictColumns) > 0 {
b.writeLeftParenthesis()
for i, col := range odk.conflictColumns {
if i > 0 {
b.writeComma()
}
fd, ok := b.model.FieldMap[col]
if !ok {
return errs.NewErrUnknownField(col)
}
b.quote(fd.ColName)
}
b.writeRightParenthesis()
}
b.writeString(" DO UPDATE SET ")
for pos, assign := range odk.assigns {
if pos > 0 {
b.writeComma()
}
switch a := assign.(type) {
case Assignment:
if err := b.buildAssignment(a); err != nil {
return err
}
case Column:
if err := d.buildConflictColumn(b, a); err != nil {
return err
}
default:
return errs.NewErrUnsupportedAssignableType(assign)
}
}
return nil
}
func (d *sqlite3Dialect) buildConflictColumn(b *Builder, c Column) error {
fd, ok := b.model.FieldMap[c.name]
if !ok {
return errs.NewErrUnknownField(c.name)
}
b.quote(fd.ColName)
b.writeString(" = ")
b.writeString("excluded.")
b.quote(fd.ColName)
return nil
}