This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
270 lines (255 loc) · 6.63 KB
/
fields.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
package simpledb
import (
"database/sql"
"errors"
"reflect"
"strings"
"github.com/Nigel2392/typeutils"
)
// Get the column types for a specified golang type
func GetColType(typ string, dialect string) string {
switch strings.ToLower(dialect) {
case "sqlite", "sqlite3":
switch typ {
case "string":
return "TEXT"
case "int":
return "INTEGER"
case "bool":
return "BOOLEAN"
case "int8":
return "INTEGER"
case "int16":
return "INTEGER"
case "int32":
return "INTEGER"
case "int64":
return "INTEGER"
case "float32":
return "FLOAT"
case "float64":
return "FLOAT"
case "Time":
return "DATETIME"
case "[]byte":
return "BLOB"
default:
return "TEXT"
}
case "mysql", "mariadb", "mssql", "sqlserver":
switch typ {
case "string":
return "VARCHAR"
case "int":
return "INT"
case "bool":
return "BOOLEAN"
case "int8":
return "TINYINT"
case "int16":
return "SMALLINT"
case "int32":
return "INT"
case "int64":
return "BIGINT"
case "float32":
return "FLOAT"
case "float64":
return "DOUBLE"
case "Time":
return "DATETIME"
case "[]byte":
return "BLOB"
default:
return "VARCHAR"
}
}
return ""
}
// Get the column types for the model
func GetColTypes(types []string, dialect string) []string {
for i, typ := range types {
types[i] = GetColType(typ, dialect)
}
return types
}
// Get the model columns, excluding related fields.
// Optionally, you can specify a list of columns to exclude.
func Columns(model any, exclude ...string) []string {
// Validate kind
kind := modelKind(model)
// Loop through all fields in the struct
columns := []string{}
inlineLoopFields(kind, func(f reflect.StructField, i int) {
if isRelated(f) {
return
}
if typeutils.Contains(exclude, strings.ToLower(f.Name)) {
return
}
// Get the name of the struct field
columns = append(columns, strings.ToLower(f.Name))
})
return columns
}
// Get all of the model columns, including related fields.
// Optionally, you can specify a list of columns to exclude.
func AllColumns(model any, exclude ...string) []string {
// Validate kind
kind := modelKind(model)
// Loop through all fields in the struct
columns := []string{}
inlineLoopFields(kind, func(f reflect.StructField, i int) {
if typeutils.Contains(exclude, strings.ToLower(f.Name)) {
return
}
// Get the name of the struct field
columns = append(columns, strings.ToLower(f.Name))
})
return columns
}
// Get the columns needed for a migration
func MigrationColumns(model Model) []Column {
kind := modelKind(model)
columns := []Column{}
inlineLoopFields(kind, func(f reflect.StructField, i int) {
if isRelated(f) {
return //Skip related fields
}
var tv ModelTags = TagMap(f)
typ := GetColType(f.Type.Name(), "mysql")
col := tv.ToColumn(model.TableName(), strings.ToLower(f.Name), typ)
columns = append(columns, col)
})
return columns
}
// Get the related fields for migrating a model.
func MigrationRelations(model Model) []Relation {
kind := modelKind(model)
relations := []Relation{}
for i := 0; i < kind.NumField(); i++ {
f := kind.Field(i)
if f.Tag.Get(TAG) == "" {
continue
} else if f.Tag.Get(TAG) == "-" {
continue
}
if isRelated(f) {
tm := TagMap(f)
other := strings.TrimPrefix(f.Name, "Rel_")
relations = append(relations, Relation{
From: model.TableName(),
To: other, //Provide the name of the other table
Type: DBType(tm.RelType()),
})
}
}
return relations
}
// Get the columns with golang types
func ColumnsWithTypes(model any) ([]string, []string) {
// Validate kind
kind := modelKind(model)
// Columns to return
types := make([]string, 0)
columns := []string{}
inlineLoopFields(kind, func(f reflect.StructField, i int) {
if isRelated(f) {
return //Skip related fields
}
// Get the name of the struct field
columns = append(columns, strings.ToLower(f.Name))
// Get the type of the struct field
types = append(types, f.Type.Name())
})
return columns, types
}
// Get a value from a model struct
func GetValue(model Model, column string) any {
// Validate kind
kind := modelKind(model)
// Loop through all fields in the struct
for i := 0; i < kind.NumField(); i++ {
f_kind := kind.Field(i)
if !TagValid(f_kind) {
continue
}
// Get the name of the struct field
if strings.EqualFold(f_kind.Name, column) {
var val any
// var err error
//if isRelated(f_kind) {
// // TODO: Handle related fields
// // Query the related table
// // Get the value of the related fields
// continue
//}
if f_kind.Type.Kind() == reflect.Ptr {
val = reflect.ValueOf(model).Elem().Field(i).Elem().Interface()
} else {
val = reflect.ValueOf(model).Elem().Field(i).Interface()
}
return val
}
}
return nil
}
// Set a value on a model struct
func SetValue(model Model, column string, value any) {
// Validate kind
kind := modelKind(model)
// Loop through all fields in the struct
for i := 0; i < kind.NumField(); i++ {
f_kind := kind.Field(i)
if !TagValid(f_kind) {
continue
}
if isRelated(f_kind) {
continue
}
// Get the name of the struct field
if strings.EqualFold(f_kind.Name, column) {
// Set the value of the struct field
// Check if types match
reflect.ValueOf(model).Elem().Field(i).Set(reflect.ValueOf(value))
return
}
}
}
// Validate if field is a related field
func isRelated(f reflect.StructField) bool {
return strings.HasPrefix(strings.ToLower(f.Name), "rel_")
}
// Scan a row into a model
func Scan(model Model, row *sql.Rows, exclude []string) error {
// Validate kind
fields, err := modelFields(model, exclude)
if err != nil {
return err
}
return row.Scan(fields...)
}
// Get the model fields to scan into
func modelFields(model Model, include []string) ([]any, error) {
if len(include) == 0 {
include = Columns(model)
}
// Use reflection to get the columns from the model
typeof := reflect.TypeOf(model)
struct_fields := make([]any, 0)
if typeof.Kind() != reflect.Ptr {
return nil, errors.New("model is not a pointer to struct")
}
typeof = typeof.Elem()
for i := 0; i < typeof.NumField(); i++ {
if !TagValid(typeof.Field(i)) {
continue
} else if isRelated(typeof.Field(i)) {
continue
}
if typeutils.Contains(include, strings.ToLower(typeof.Field(i).Name)) {
struct_fields = append(struct_fields, reflect.ValueOf(model).Elem().Field(i).Addr().Interface())
}
}
return struct_fields, nil
}