/
model551.go
317 lines (260 loc) · 6.47 KB
/
model551.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package model551
import (
"database/sql"
"errors"
"github.com/go51/string551"
"reflect"
"strconv"
)
type Model struct {
models map[string]*ModelInformation
}
type ModelInformation struct {
NewFunc NewModelFunc
NewFuncPointer NewModelFunc
ModelType reflect.Type
ModelName string
TableInformation *TableInformation
SqlInformation *SqlCache
}
type TableInformation struct {
TableName string
PrimaryKey string
Fields []string
DeleteTable bool
DeletedAtField string
}
type SqlCache struct {
Insert string
Select string
Update string
Delete string
LogicalDelete string
}
var modelInstance *Model
type NewModelFunc func() interface{}
type SqlType int
const (
SQL_INSERT SqlType = iota
SQL_UPDATE
SQL_LOGICAL_DELETE
)
type PrimaryInterface interface {
SetId(int64)
GetId() int64
}
type ScanInterface interface {
Scan(rows *sql.Rows) error
}
type ValuesInterface interface {
SqlValues(sqlType SqlType) []interface{}
}
type SqlInsertInterface interface {
SqlInsert() string
}
type SqlUpdateInterface interface {
SqlUpdate() string
}
func Load() *Model {
if modelInstance != nil {
return modelInstance
}
modelInstance = &Model{
models: map[string]*ModelInformation{},
}
return modelInstance
}
func (m *Model) Add(newFunc NewModelFunc, newPointerFunc NewModelFunc) {
model := newFunc()
mType := reflect.TypeOf(model)
mName := mType.Name()
if m.models[mName] != nil {
panic(errors.New("追加されたモデルは既に登録されています。"))
}
mInfo := &ModelInformation{
NewFunc: newFunc,
NewFuncPointer: newPointerFunc,
ModelType: mType,
ModelName: mName,
}
mInfo.TableInformation = loadTableSetting(mType)
mInfo.SqlInformation = cacheSql(mInfo.TableInformation)
m.models[mName] = mInfo
}
func (m *Model) Get(modelName string) *ModelInformation {
return m.models[modelName]
}
func loadTableSetting(mType reflect.Type) *TableInformation {
tInfo := &TableInformation{
TableName: string551.SnakeCase(mType.Name()),
PrimaryKey: "id",
Fields: []string{},
DeleteTable: false,
DeletedAtField: "",
}
if name := loadTableName(mType); name != "" {
tInfo.TableName = name
}
if primaryKey := loadPrimaryKey(mType); primaryKey != "" {
tInfo.PrimaryKey = primaryKey
}
if del, name := loadDeleteAt(mType); del {
tInfo.DeleteTable = true
tInfo.DeletedAtField = name
}
tInfo.Fields = loadFields(mType)
return tInfo
}
func loadTableName(mType reflect.Type) string {
for i := 0; i < mType.NumField(); i++ {
sField := mType.Field(i)
if name := sField.Tag.Get("db_table"); name != "" {
return name
}
}
return ""
}
func loadPrimaryKey(mType reflect.Type) string {
for i := 0; i < mType.NumField(); i++ {
sField := mType.Field(i)
pk, err := strconv.ParseBool(sField.Tag.Get("db_pk"))
if err == nil && pk {
db := sField.Tag.Get("db")
if db == "" {
return string551.SnakeCase(sField.Name)
} else {
return string551.SnakeCase(db)
}
}
}
return ""
}
func loadDeleteAt(mType reflect.Type) (bool, string) {
for i := 0; i < mType.NumField(); i++ {
sField := mType.Field(i)
del, err := strconv.ParseBool(sField.Tag.Get("db_delete"))
if err == nil && del {
return true, string551.SnakeCase(sField.Name)
}
}
return false, ""
}
func loadFields(mType reflect.Type) []string {
fields := make([]string, 0)
for i := 0; i < mType.NumField(); i++ {
sField := mType.Field(i)
db := sField.Tag.Get("db")
del, err := strconv.ParseBool(sField.Tag.Get("db_delete"))
if err != nil || !del {
if db == "" {
fields = append(fields, string551.SnakeCase(sField.Name))
} else if db != "-" {
fields = append(fields, string551.SnakeCase(db))
}
}
}
return fields
}
func cacheSql(tInfo *TableInformation) *SqlCache {
sqlCache := &SqlCache{
Insert: cacheSqlInsert(tInfo),
Select: cacheSqlSelect(tInfo),
Update: cacheSqlUpdate(tInfo),
Delete: cacheSqlDelete(tInfo),
LogicalDelete: cacheSqlLogicalDelete(tInfo),
}
return sqlCache
}
func cacheSqlInsert(tInfo *TableInformation) string {
sql := ""
var append int = 0
sql = string551.Join(sql, "INSERT INTO `"+tInfo.TableName+"` ")
sql = string551.Join(sql, "(")
for i := 0; i < len(tInfo.Fields); i++ {
if tInfo.Fields[i] == tInfo.PrimaryKey {
continue
}
if append == 0 {
sql = string551.Join(sql, "`"+tInfo.Fields[i]+"`")
} else {
sql = string551.Join(sql, ", `"+tInfo.Fields[i]+"`")
}
append++
}
append = 0
sql = string551.Join(sql, ") VALUES (")
for i := 0; i < len(tInfo.Fields); i++ {
if tInfo.Fields[i] == tInfo.PrimaryKey {
continue
}
if append == 0 {
sql = string551.Join(sql, "?")
} else {
sql = string551.Join(sql, ", ?")
}
append++
}
sql = string551.Join(sql, ")")
return sql
}
func cacheSqlSelect(tInfo *TableInformation) string {
sql := ""
sql = string551.Join(sql, "SELECT ")
for i := 0; i < len(tInfo.Fields); i++ {
if i == 0 {
sql = string551.Join(sql, "`"+tInfo.Fields[i]+"`")
} else {
sql = string551.Join(sql, ", `"+tInfo.Fields[i]+"`")
}
}
sql = string551.Join(sql, " FROM `"+tInfo.TableName+"` WHERE 1 = 1")
return sql
}
func cacheSqlUpdate(tInfo *TableInformation) string {
sql := ""
var append int = 0
sql = string551.Join(sql, "UPDATE `"+tInfo.TableName+"` SET ")
for i := 0; i < len(tInfo.Fields); i++ {
if tInfo.Fields[i] == tInfo.PrimaryKey {
continue
}
if append == 0 {
sql = string551.Join(sql, "`"+tInfo.Fields[i]+"` = ?")
} else {
sql = string551.Join(sql, ", `"+tInfo.Fields[i]+"` = ?")
}
append++
}
sql = string551.Join(sql, " WHERE `"+tInfo.PrimaryKey+"` = ?")
return sql
}
func cacheSqlDelete(tInfo *TableInformation) string {
sql := ""
sql = string551.Join(sql, "DELETE FROM `"+tInfo.TableName+"` WHERE `"+tInfo.PrimaryKey+"` = ?")
return sql
}
func cacheSqlLogicalDelete(tInfo *TableInformation) string {
sql := ""
if tInfo.DeleteTable == false {
return sql
}
sql = string551.Join(sql, "INSERT INTO `"+tInfo.TableName+"_delete` ")
sql = string551.Join(sql, "(")
for i := 0; i < len(tInfo.Fields); i++ {
if i == 0 {
sql = string551.Join(sql, "`"+tInfo.Fields[i]+"`")
} else {
sql = string551.Join(sql, ", `"+tInfo.Fields[i]+"`")
}
}
sql = string551.Join(sql, ", `"+tInfo.DeletedAtField+"`) VALUES (")
for i := 0; i < len(tInfo.Fields); i++ {
if i == 0 {
sql = string551.Join(sql, "?")
} else {
sql = string551.Join(sql, ", ?")
}
}
sql = string551.Join(sql, ", ?)")
return sql
}