/
repository551.go
185 lines (136 loc) · 4.18 KB
/
repository551.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
package repository551
import (
"errors"
"github.com/go51/model551"
"github.com/go51/mysql551"
"github.com/go51/string551"
)
type Repository struct{}
var repositoryInstance *Repository
func Load() *Repository {
if repositoryInstance != nil {
return repositoryInstance
}
repositoryInstance = &Repository{}
return repositoryInstance
}
func (r *Repository) Find(db *mysql551.Mysql, mInfo *model551.ModelInformation, id int64) interface{} {
sql := mInfo.SqlInformation.Select + " AND `" + mInfo.TableInformation.PrimaryKey + "` = ?"
param := []interface{}{id}
rows := db.Query(sql, param...)
defer rows.Close()
if !rows.Next() {
return nil
}
model := mInfo.NewFuncPointer()
modelScan, ok := model.(model551.ScanInterface)
if !ok {
panic(errors.New("Not found: 'T.Scan(rows *sql.rows) error' method"))
}
err := modelScan.Scan(rows)
if err != nil {
panic(err)
}
return model
}
func (r *Repository) FindBy(db *mysql551.Mysql, mInfo *model551.ModelInformation, param map[string]interface{}) []interface{} {
sql := mInfo.SqlInformation.Select
var values []interface{}
for key, value := range param {
sql = string551.Join(sql, " AND `"+key+"` = ?")
values = append(values, value)
}
rows := db.Query(sql, values...)
defer rows.Close()
var models []interface{}
for rows.Next() {
model := mInfo.NewFuncPointer()
modelScan, ok := model.(model551.ScanInterface)
if !ok {
panic(errors.New("Not found: 'T.Scan(rows *sql.rows) error' method"))
}
err := modelScan.Scan(rows)
if err != nil {
panic(err)
}
models = append(models, model)
}
return models
}
func (r *Repository) FindOneBy(db *mysql551.Mysql, mInfo *model551.ModelInformation, param map[string]interface{}) interface{} {
sql := mInfo.SqlInformation.Select
var values []interface{}
for key, value := range param {
sql = string551.Join(sql, " AND `"+key+"` = ?")
values = append(values, value)
}
rows := db.Query(sql, values...)
defer rows.Close()
var model interface{} = nil
for rows.Next() {
if model != nil {
panic(errors.New("Extraction result there is more than one."))
}
model = mInfo.NewFuncPointer()
modelScan, ok := model.(model551.ScanInterface)
if !ok {
panic(errors.New("Not found: 'T.Scan(rows *sql.rows) error' method"))
}
err := modelScan.Scan(rows)
if err != nil {
panic(err)
}
}
return model
}
func (r *Repository) Create(db *mysql551.Mysql, mInfo *model551.ModelInformation, model interface{}) interface{} {
sql := mInfo.SqlInformation.Insert
if iInsert, ok := model.(model551.SqlInsertInterface); ok {
sql = iInsert.SqlInsert()
}
sqlValueModel, ok := model.(model551.ValuesInterface)
if !ok {
panic(errors.New("Not found: 'T.SqlValues(sqlType SqlType) []interface{}' method"))
}
param := sqlValueModel.SqlValues(model551.SQL_INSERT)
_, lastInsertId := db.Exec(sql, param...)
idModel, ok := model.(model551.PrimaryInterface)
if !ok {
panic(errors.New("Not found: 'T.SetId(int64)', 'T.Id() int64' method"))
}
idModel.SetId(lastInsertId)
return model
}
func (r *Repository) Update(db *mysql551.Mysql, mInfo *model551.ModelInformation, model interface{}) interface{} {
sql := mInfo.SqlInformation.Update
if iUpdate, ok := model.(model551.SqlUpdateInterface); ok {
sql = iUpdate.SqlUpdate()
}
sqlValueModel, ok := model.(model551.ValuesInterface)
if !ok {
panic(errors.New("Not found: 'T.SqlValues(sqlType SqlType) []interface{}' method"))
}
param := sqlValueModel.SqlValues(model551.SQL_UPDATE)
db.Exec(sql, param...)
return model
}
func (r *Repository) Delete(db *mysql551.Mysql, mInfo *model551.ModelInformation, model interface{}) {
// Delete
sql := mInfo.SqlInformation.Delete
idModel, ok := model.(model551.PrimaryInterface)
if !ok {
panic(errors.New("Not found: 'T.SetId(int64)', 'T.Id() int64' method"))
}
db.Exec(sql, idModel.GetId())
if !mInfo.TableInformation.DeleteTable {
return
}
// Logical Delete( Insert into delete table.)
sql = mInfo.SqlInformation.LogicalDelete
sqlValueModel, ok := model.(model551.ValuesInterface)
if !ok {
panic(errors.New("Not found: 'T.SqlValues(sqlType SqlType) []interface{}' method"))
}
param := sqlValueModel.SqlValues(model551.SQL_LOGICAL_DELETE)
db.Exec(sql, param...)
}