-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmysql_type.go
More file actions
144 lines (124 loc) · 2.98 KB
/
mysql_type.go
File metadata and controls
144 lines (124 loc) · 2.98 KB
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
package golang
import (
"log"
"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
"github.com/sqlc-dev/sqlc/internal/codegen/sdk"
"github.com/sqlc-dev/sqlc/internal/debug"
"github.com/sqlc-dev/sqlc/internal/plugin"
)
func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
unsigned := col.Unsigned
switch columnType {
case "varchar", "text", "char", "tinytext", "mediumtext", "longtext":
if notNull {
return "string"
}
return "sql.NullString"
case "tinyint":
if col.Length == 1 {
if notNull {
return "bool"
}
return "sql.NullBool"
} else {
if notNull {
if unsigned {
return "uint8"
}
return "int8"
}
// The database/sql package does not have a sql.NullInt8 type, so we
// use the smallest type they have which is NullInt16
return "sql.NullInt16"
}
case "year":
if notNull {
return "int16"
}
return "sql.NullInt16"
case "smallint":
if notNull {
if unsigned {
return "uint16"
}
return "int16"
}
return "sql.NullInt16"
case "int", "integer", "mediumint":
if notNull {
if unsigned {
return "uint32"
}
return "int32"
}
return "sql.NullInt32"
case "bigint", "bigint unsigned", "bigint signed":
// "bigint unsigned" and "bigint signed" are MySQL CAST types
// Note: We use int64 for CAST AS UNSIGNED to match original behavior,
// even though uint64 would be more semantically correct.
// The Unsigned flag on columns (from table schema) still uses uint64.
if notNull {
if unsigned {
return "uint64"
}
return "int64"
}
return "sql.NullInt64"
case "blob", "binary", "varbinary", "tinyblob", "mediumblob", "longblob":
if notNull {
return "[]byte"
}
return "sql.NullString"
case "double", "double precision", "real", "float":
if notNull {
return "float64"
}
return "sql.NullFloat64"
case "decimal", "dec", "fixed":
if notNull {
return "string"
}
return "sql.NullString"
case "enum":
// TODO: Proper Enum support
return "string"
case "date", "timestamp", "datetime", "time":
if notNull {
return "time.Time"
}
return "sql.NullTime"
case "boolean", "bool":
if notNull {
return "bool"
}
return "sql.NullBool"
case "json":
return "json.RawMessage"
case "any":
return "interface{}"
default:
for _, schema := range req.Catalog.Schemas {
for _, enum := range schema.Enums {
if enum.Name == columnType {
if notNull {
if schema.Name == req.Catalog.DefaultSchema {
return StructName(enum.Name, options)
}
return StructName(schema.Name+"_"+enum.Name, options)
} else {
if schema.Name == req.Catalog.DefaultSchema {
return "Null" + StructName(enum.Name, options)
}
return "Null" + StructName(schema.Name+"_"+enum.Name, options)
}
}
}
}
if debug.Active {
log.Printf("Unknown MySQL type: %s\n", columnType)
}
return "interface{}"
}
}