forked from sqlc-dev/sqlc
/
mysql_type.go
106 lines (88 loc) · 2.02 KB
/
mysql_type.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
package golang
import (
"log"
"github.com/kyleconroy/sqlc/internal/compiler"
"github.com/kyleconroy/sqlc/internal/config"
"github.com/kyleconroy/sqlc/internal/debug"
"github.com/kyleconroy/sqlc/internal/sql/catalog"
)
func mysqlType(r *compiler.Result, col *compiler.Column, settings config.CombinedSettings) string {
columnType := col.DataType
notNull := col.NotNull || col.IsArray
switch columnType {
case "varchar", "text", "char", "tinytext", "mediumtext", "longtext":
if notNull {
return "string"
}
return "sql.NullString"
case "tinyint":
if col.Length != nil && *col.Length == 1 {
if notNull {
return "bool"
}
return "sql.NullBool"
} else {
if notNull {
return "int32"
}
return "sql.NullInt32"
}
case "int", "integer", "smallint", "mediumint", "year":
if notNull {
return "int32"
}
return "sql.NullInt32"
case "bigint":
if notNull {
return "int64"
}
return "sql.NullInt64"
case "blob", "binary", "varbinary", "tinyblob", "mediumblob", "longblob":
return "[]byte"
case "double", "double precision", "real":
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 r.Catalog.Schemas {
for _, typ := range schema.Types {
switch t := typ.(type) {
case *catalog.Enum:
if t.Name == columnType {
if schema.Name == r.Catalog.DefaultSchema {
return StructName(t.Name, settings)
}
return StructName(schema.Name+"_"+t.Name, settings)
}
}
}
}
if debug.Active {
log.Printf("Unknown MySQL type: %s\n", columnType)
}
return "interface{}"
}
}