-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdialect.go
340 lines (300 loc) · 9.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
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
package genmai
import (
"database/sql"
"errors"
"fmt"
"strings"
"time"
)
// Dialect is an interface that the dialect of the database.
type Dialect interface {
// Name returns a name of the dialect.
// Return value must be same as the driver name.
Name() string
// Quote returns a quoted s.
// It is for a column name, not a value.
Quote(s string) string
// PlaceHolder returns the placeholder character of the database.
// A current number of placeholder will passed to i.
PlaceHolder(i int) string
// SQLType returns the SQL type of the v.
// autoIncrement is whether the field is auto increment.
// If "size" tag specified to struct field, it will passed to size
// argument. If it doesn't specify, size is 0.
SQLType(v interface{}, autoIncrement bool, size uint64) (name string, allowNull bool)
// AutoIncrement returns the keyword of auto increment.
AutoIncrement() string
// FormatBool returns boolean value as string according to the value of b.
FormatBool(b bool) string
// LastInsertId returns an SQL to get the last inserted id.
LastInsertId() string
}
var (
ErrUsingFloatType = errors.New("float types have a rounding error problem.\n" +
"Please use `genmai.Rat` if you want an exact value.\n" +
"However, if you still want a float types, please use `genmai.Float32` and `Float64`.")
)
const (
// Precision of the fixed-point number.
// Digits of precision before the decimal point.
decimalPrecision = 65
// Scale of the fixed-point number.
// Digits of precision after the decimal point.
decimalScale = 30
)
// SQLite3Dialect represents a dialect of the SQLite3.
// It implements the Dialect interface.
type SQLite3Dialect struct{}
// Name returns name of the dialect.
func (d *SQLite3Dialect) Name() string {
return "sqlite3"
}
// Quote returns a quoted s for a column name.
func (d *SQLite3Dialect) Quote(s string) string {
return fmt.Sprintf(`"%s"`, strings.Replace(s, `"`, `""`, -1))
}
// PlaceHolder returns the placeholder character of the SQLite3.
func (d *SQLite3Dialect) PlaceHolder(i int) string {
return "?"
}
// SQLType returns the SQL type of the v for SQLite3.
func (d *SQLite3Dialect) SQLType(v interface{}, autoIncrement bool, size uint64) (name string, allowNull bool) {
switch v.(type) {
case bool:
return "boolean", false
case *bool, sql.NullBool:
return "boolean", true
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return "integer", false
case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, sql.NullInt64:
return "integer", true
case string:
return "text", false
case *string, sql.NullString:
return "text", true
case []byte:
return "blob", true
case time.Time:
return "datetime", false
case *time.Time:
return "datetime", true
case Float32, Float64:
return "real", false
case *Float32, *Float64:
return "real", true
case Rat:
return "numeric", false
case *Rat:
return "numeric", true
case float32, *float32, float64, *float64, sql.NullFloat64:
panic(ErrUsingFloatType)
}
panic(fmt.Errorf("SQLite3Dialect: unsupported SQL type: %T", v))
}
func (d *SQLite3Dialect) AutoIncrement() string {
return "AUTOINCREMENT"
}
// FormatBool returns "1" or "0" according to the value of b as boolean for SQLite3.
func (d *SQLite3Dialect) FormatBool(b bool) string {
if b {
return "1"
} else {
return "0"
}
}
func (d *SQLite3Dialect) LastInsertId() string {
return `SELECT last_insert_rowid()`
}
// MySQLDialect represents a dialect of the MySQL.
// It implements the Dialect interface.
type MySQLDialect struct{}
// Name returns name of the MySQLDialect.
func (d *MySQLDialect) Name() string {
return "mysql"
}
// Quote returns a quoted s for a column name.
func (d *MySQLDialect) Quote(s string) string {
return fmt.Sprintf("`%s`", strings.Replace(s, "`", "``", -1))
}
// PlaceHolder returns the placeholder character of the MySQL.
func (d *MySQLDialect) PlaceHolder(i int) string {
return "?"
}
// SQLType returns the SQL type of the v for MySQL.
func (d *MySQLDialect) SQLType(v interface{}, autoIncrement bool, size uint64) (name string, allowNull bool) {
switch v.(type) {
case bool:
return "BOOLEAN", false
case *bool, sql.NullBool:
return "BOOLEAN", true
case int8, int16, uint8, uint16:
return "SMALLINT", false
case *int8, *int16, *uint8, *uint16:
return "SMALLINT", true
case int, int32, uint, uint32:
return "INT", false
case *int, *int32, *uint, *uint32:
return "INT", true
case int64, uint64:
return "BIGINT", false
case *int64, *uint64, sql.NullInt64:
return "BIGINT", true
case string:
return d.varchar(size), false
case *string, sql.NullString:
return d.varchar(size), true
case []byte:
switch {
case size == 0:
return "VARBINARY(255)", true // default.
case size < (1<<16)-1-2: // approximate 64KB.
// 65533 ((2^16) - 1) - (length of prefix)
// See http://dev.mysql.com/doc/refman/5.5/en/string-type-overview.html#idm47703458759504
return fmt.Sprintf("VARBINARY(%d)", size), true
case size < 1<<24: // 16MB.
return "MEDIUMBLOB", true
}
return "LONGBLOB", true
case time.Time:
return "DATETIME", false
case *time.Time:
return "DATETIME", true
case Rat:
return fmt.Sprintf("DECIMAL(%d, %d)", decimalPrecision, decimalScale), false
case *Rat:
return fmt.Sprintf("DECIMAL(%d, %d)", decimalPrecision, decimalScale), true
case Float32, Float64:
return "DOUBLE", false
case *Float32, *Float64:
return "DOUBLE", true
case float32, *float32, float64, *float64, sql.NullFloat64:
panic(ErrUsingFloatType)
}
panic(fmt.Errorf("MySQLDialect: unsupported SQL type: %T", v))
}
func (d *MySQLDialect) AutoIncrement() string {
return "AUTO_INCREMENT"
}
// FormatBool returns "TRUE" or "FALSE" according to the value of b as boolean for MySQL.
func (d *MySQLDialect) FormatBool(b bool) string {
if b {
return "TRUE"
} else {
return "FALSE"
}
}
func (d *MySQLDialect) LastInsertId() string {
return `SELECT LAST_INSERT_ID()`
}
func (d *MySQLDialect) varchar(size uint64) string {
switch {
case size == 0:
return "VARCHAR(255)" // default.
case size < (1<<16)-1-2: // approximate 64KB.
// 65533 ((2^16) - 1) - (length of prefix)
// See http://dev.mysql.com/doc/refman/5.5/en/string-type-overview.html#idm47703458792704
return fmt.Sprintf("VARCHAR(%d)", size)
case size < 1<<24: // 16MB.
return "MEDIUMTEXT"
}
return "LONGTEXT"
}
// PostgresDialect represents a dialect of the PostgreSQL.
// It implements the Dialect interface.
type PostgresDialect struct{}
// Name returns name of the PostgresDialect.
func (d *PostgresDialect) Name() string {
return "postgres"
}
// Quote returns a quoted s for a column name.
func (d *PostgresDialect) Quote(s string) string {
return fmt.Sprintf(`"%s"`, strings.Replace(s, `"`, `""`, -1))
}
// PlaceHolder returns the placeholder character of the PostgreSQL.
func (d *PostgresDialect) PlaceHolder(i int) string {
return fmt.Sprintf("$%d", i+1)
}
// SQLType returns the SQL type of the v for PostgreSQL.
func (d *PostgresDialect) SQLType(v interface{}, autoIncrement bool, size uint64) (name string, allowNull bool) {
switch v.(type) {
case bool:
return "boolean", false
case *bool, sql.NullBool:
return "boolean", true
case int8, int16, uint8, uint16:
return d.smallint(autoIncrement), false
case *int8, *int16, *uint8, *uint16:
return d.smallint(autoIncrement), true
case int, int32, uint, uint32:
return d.integer(autoIncrement), false
case *int, *int32, *uint, *uint32:
return d.integer(autoIncrement), true
case int64, uint64:
return d.bigint(autoIncrement), false
case *int64, *uint64, sql.NullInt64:
return d.bigint(autoIncrement), true
case string:
return d.varchar(size), false
case *string, sql.NullString:
return d.varchar(size), true
case []byte:
return "bytea", true
case time.Time:
return "timestamp with time zone", false
case *time.Time:
return "timestamp with time zone", true
case Rat:
return fmt.Sprintf("numeric(%d, %d)", decimalPrecision, decimalScale), false
case *Rat:
return fmt.Sprintf("numeric(%d, %d)", decimalPrecision, decimalScale), true
case Float32, Float64:
return "double precision", false
case *Float32, *Float64:
return "double precision", true
case float32, *float32, float64, *float64, sql.NullFloat64:
panic(ErrUsingFloatType)
}
panic(fmt.Errorf("PostgresDialect: unsupported SQL type: %T", v))
}
func (d *PostgresDialect) AutoIncrement() string {
return ""
}
// FormatBool returns "TRUE" or "FALSE" according to the value of b as boolean for PostgreSQL.
func (d *PostgresDialect) FormatBool(b bool) string {
if b {
return "TRUE"
} else {
return "FALSE"
}
}
func (d *PostgresDialect) LastInsertId() string {
return `SELECT lastval()`
}
func (d *PostgresDialect) smallint(autoIncrement bool) string {
if autoIncrement {
return "smallserial"
}
return "smallint"
}
func (d *PostgresDialect) integer(autoIncrement bool) string {
if autoIncrement {
return "serial"
}
return "integer"
}
func (d *PostgresDialect) bigint(autoIncrement bool) string {
if autoIncrement {
return "bigserial"
}
return "bigint"
}
func (d *PostgresDialect) varchar(size uint64) string {
switch {
case size == 0:
return "varchar(255)" // default.
case size < (1<<16)-1-2: // approximate 64KB.
// This isn't required in PostgreSQL, but defined in order to match to the MySQLDialect.
return fmt.Sprintf("varchar(%d)", size)
}
return "text"
}