-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
195 lines (189 loc) · 4.14 KB
/
models.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
package gomygen
//查询数据库
func (m *ModelS) Find(sql string, args ...interface{}) ([]map[string]interface{}, error) {
stmt, err := m.DB.Prepare(sql)
defer stmt.Close()
if err != nil {
return nil, err
}
rows, err := stmt.Query(args...)
defer rows.Close()
if err != nil {
return nil, err
}
//获取列名称
var columns []string
if columns, err = rows.Columns(); err != nil {
return nil, err
}
//新建存储结果变量
result := make([]map[string]interface{}, 0)
count := len(columns)
//存储数据变量
values := make([]interface{}, count)
//扫描变量
scanValues := make([]interface{}, count)
for rows.Next() {
//将带&赋值给scanValues切片,然后全部给Scan方法
for i := 0; i < count; i++ {
scanValues[i] = &values[i]
}
err = rows.Scan(scanValues...) //赋值给scan
if err != nil {
continue
}
//新建一个实体,用于存储数据
entity := make(map[string]interface{})
for i, field := range columns {
var v interface{} //定义一个通用变量
val := values[i] //获取一个结果数据
if b, ok := val.([]byte); ok { //判定是否是切片字节
v = string(b)
} else {
v = val
}
entity[field] = v //以字段名为键,存储数据
}
//追加到结果集里
result = append(result, entity)
}
return result, nil
}
//查询一行数据,即1维数据
func (m *ModelS) First(sql string, args ...interface{}) (map[string]interface{}, error) {
stmt, err := m.DB.Prepare(sql)
defer stmt.Close()
rows, err := stmt.Query(args...)
defer rows.Close()
if err != nil {
return nil, err
}
var columns []string
columns, err = rows.Columns()
if err != nil {
return nil, err
}
values := make([]interface{}, len(columns))
scanValues := make([]interface{}, len(columns))
result := make(map[string]interface{})
for rows.Next() {
for i := range columns {
scanValues[i] = &values[i]
}
err = rows.Scan(scanValues...)
if err != nil {
continue
}
entity := make(map[string]interface{})
for i, field := range columns {
var v interface{}
val := values[i]
if b, ok := val.([]byte); ok {
v = string(b)
} else {
v = val
}
entity[field] = v
}
result = entity
break
}
return result, nil
}
//查询一列数据,即1维数据 map
func (m *ModelS) Pluck(sql string, name string, args ...interface{}) ([]interface{}, error) {
stmt, err := m.DB.Prepare(sql)
defer stmt.Close()
if err != nil {
return nil, err
}
rows, err := stmt.Query(args...)
defer rows.Close()
if err != nil {
return nil, err
}
var columns []string
columns, err = rows.Columns()
if err != nil {
return nil, err
}
values := make([]interface{}, len(columns))
scanValues := make([]interface{}, len(columns))
result := make([]interface{}, 0)
for rows.Next() {
for i := range columns {
scanValues[i] = &values[i]
}
err = rows.Scan(scanValues...)
if err != nil {
continue
}
var one interface{}
for i, field := range columns {
var v interface{}
val := values[i]
if b, ok := val.([]byte); ok {
v = string(b)
} else {
v = val
}
if field == name {
one = v
break
}
}
result = append(result, one)
}
return result, nil
}
//更新
func (m *ModelS) Update(sql string, args ...interface{}) (int64, error) {
stmt, err := m.DB.Prepare(sql)
defer stmt.Close()
if err != nil {
return 0, err
}
res, err := stmt.Exec(args...)
if err != nil {
return 0, err
}
count, err := res.RowsAffected()
if err != nil {
return 0, err
}
return count, nil
}
//删除
func (m *ModelS) Delete(sql string, args ...interface{}) (int64, error) {
stmt, err := m.DB.Prepare(sql)
defer stmt.Close()
if err != nil {
return 0, err
}
res, err := stmt.Exec(args...)
if err != nil {
return 0, err
}
count, err := res.RowsAffected()
if err != nil {
return 0, err
}
return count, nil
}
//增加
func (m *ModelS) Insert(sql string, args ...interface{}) (int64, error) {
stmt, err := m.DB.Prepare(sql)
defer stmt.Close()
if err != nil {
return 0, err
}
res, err := stmt.Exec(args...)
if err != nil {
return 0, err
}
lastId, err := res.LastInsertId()
if err != nil {
return 0, err
}
return lastId, nil
}