-
Notifications
You must be signed in to change notification settings - Fork 26
/
type.go
162 lines (135 loc) · 3.07 KB
/
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
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
package qb
import (
"fmt"
"strings"
)
// Char creates char type
func Char() TypeElem {
return Type("CHAR")
}
// Varchar creates varchar type
func Varchar() TypeElem {
return Type("VARCHAR").Size(255)
}
// Text creates text type
func Text() TypeElem {
return Type("TEXT")
}
// Int creates int type
func Int() TypeElem {
return Type("INT")
}
// TinyInt creates tinyint type
func TinyInt() TypeElem {
return Type("TINYINT")
}
// SmallInt creates smallint type
func SmallInt() TypeElem {
return Type("SMALLINT")
}
// BigInt creates bigint type
func BigInt() TypeElem {
return Type("BIGINT")
}
// Numeric creates a numeric type
func Numeric() TypeElem {
return Type("NUMERIC")
}
// Decimal creates a decimal type
func Decimal() TypeElem {
return Type("DECIMAL")
}
// Float creates float type
func Float() TypeElem {
return Type("FLOAT")
}
// Boolean creates boolean type
func Boolean() TypeElem {
return Type("BOOLEAN")
}
// Timestamp creates timestamp type
func Timestamp() TypeElem {
return Type("TIMESTAMP")
}
// UUID creates a UUID type
func UUID() TypeElem {
return Type("UUID")
}
// Blob creates a BLOB type
func Blob() TypeElem {
return Type("BLOB")
}
const defaultTypeSize = -1
// Type returns a new TypeElem while defining columns in table
func Type(name string) TypeElem {
return TypeElem{
Name: name,
size: defaultTypeSize,
precision: []int{},
}
}
// TypeElem is the struct for defining column types
type TypeElem struct {
Name string
size int
precision []int
unsigned bool
}
// DefaultCompileType is a default implementation for Dialect.CompileType
func DefaultCompileType(t TypeElem, supportsUnsigned bool) string {
name := t.Name
if t.unsigned && !supportsUnsigned {
// use a bigger int type so the unsigned values can fit in
switch name {
case "TINYINT":
name = "SMALLINT"
case "SMALLINT":
name = "INT"
case "INT":
name = "BIGINT"
}
}
sizeSpecified := false
precisionSpecified := false
if t.size != defaultTypeSize {
sizeSpecified = true
} else if len(t.precision) > 0 {
precisionSpecified = true
}
if sizeSpecified {
name = fmt.Sprintf("%s(%d)", name, t.size)
} else if precisionSpecified {
precision := []string{}
for _, p := range t.precision {
precision = append(precision, fmt.Sprintf("%v", p))
}
name = fmt.Sprintf("%s(%s)", name, strings.Join(precision, ", "))
}
if t.unsigned && supportsUnsigned {
name = fmt.Sprintf("%s UNSIGNED", name)
}
return name
}
// Size adds size constraint to column type
func (t TypeElem) Size(size int) TypeElem {
t.size = size
return t
}
// Precision sets the precision of column type
// Note: Use it in Float, Decimal and Numeric types
func (t TypeElem) Precision(p int, s int) TypeElem {
t.precision = []int{p, s}
return t
}
// Unsigned change the column type to 'unsigned'
// Note: Use it in Float, Decimal and Numeric types
func (t TypeElem) Unsigned() TypeElem {
t.unsigned = true
return t
}
// Signed change the column type to 'signed'
// Note: Use it in Float, Decimal and Numeric types
func (t TypeElem) Signed() TypeElem {
t.unsigned = false
return t
}