-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil.go
144 lines (123 loc) · 2.33 KB
/
util.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
package sql
import (
"strings"
"time"
"github.com/go-rel/rel"
)
// DefaultTimeLayout default time layout.
const DefaultTimeLayout = "2006-01-02 15:04:05"
func DropKeyMapper(keyType rel.KeyType) string {
return "CONSTRAINT"
}
// ColumnMapper function.
func ColumnMapper(column *rel.Column) (string, int, int) {
var (
typ string
m, n int
timeLayout = DefaultTimeLayout
)
switch column.Type {
case rel.ID:
typ = "INT UNSIGNED AUTO_INCREMENT"
case rel.BigID:
typ = "BIGINT UNSIGNED AUTO_INCREMENT"
case rel.Bool:
typ = "BOOL"
case rel.Int:
typ = "INT"
m = column.Limit
case rel.BigInt:
typ = "BIGINT"
m = column.Limit
case rel.Float:
typ = "FLOAT"
m = column.Precision
case rel.Decimal:
typ = "DECIMAL"
m = column.Precision
n = column.Scale
case rel.String:
typ = "VARCHAR"
m = column.Limit
if m == 0 {
m = 255
}
case rel.Text:
typ = "TEXT"
m = column.Limit
case rel.JSON:
typ = "TEXT"
case rel.Date:
typ = "DATE"
timeLayout = "2006-01-02"
case rel.DateTime:
typ = "DATETIME"
case rel.Time:
typ = "TIME"
timeLayout = "15:04:05"
default:
typ = string(column.Type)
}
if t, ok := column.Default.(time.Time); ok {
column.Default = t.Format(timeLayout)
}
return typ, m, n
}
// ColumnOptionsMapper function.
func ColumnOptionsMapper(column *rel.Column) string {
var buffer strings.Builder
if column.Unsigned {
buffer.WriteString(" UNSIGNED")
}
if column.Unique {
buffer.WriteString(" UNIQUE")
}
if column.Required {
buffer.WriteString(" NOT NULL")
}
if column.Primary {
buffer.WriteString(" PRIMARY KEY")
}
buf := buffer.String()
if buf != "" {
buf = buf[1:]
}
return buf
}
// ExtractString between two string.
func ExtractString(s, left, right string) string {
var (
start = strings.Index(s, left)
end = strings.LastIndex(s, right)
)
if start < 0 || end < 0 || start+len(left) >= end {
return s
}
return s[start+len(left) : end]
}
func toInt64(i any) int64 {
var result int64
switch s := i.(type) {
case int:
result = int64(s)
case int64:
result = s
case int32:
result = int64(s)
case int16:
result = int64(s)
case int8:
result = int64(s)
case uint:
result = int64(s)
case uint64:
result = int64(s)
case uint32:
result = int64(s)
case uint16:
result = int64(s)
case uint8:
result = int64(s)
}
return result
}