forked from xiaoenai/tp-micro
/
mysql_log.gen.go
executable file
·279 lines (256 loc) · 7.75 KB
/
mysql_log.gen.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
// Code generated by 'micro gen' command.
// DO NOT EDIT!
package model
import (
"database/sql"
"unsafe"
"github.com/henrylee2cn/goutil/coarsetime"
tp "github.com/henrylee2cn/teleport"
"github.com/xiaoenai/tp-micro/model/mysql"
"github.com/xiaoenai/tp-micro/model/sqlx"
"github.com/xiaoenai/tp-micro/examples/project/args"
)
// Log comment...
type Log args.Log
// ToLog converts to *Log type.
func ToLog(_l *args.Log) *Log {
return (*Log)(unsafe.Pointer(_l))
}
// ToArgsLog converts to *args.Log type.
func ToArgsLog(_l *Log) *args.Log {
return (*args.Log)(unsafe.Pointer(_l))
}
// ToLogSlice converts to []*Log type.
func ToLogSlice(a []*args.Log) []*Log {
return *(*[]*Log)(unsafe.Pointer(&a))
}
// ToArgsLogSlice converts to []*args.Log type.
func ToArgsLogSlice(a []*Log) []*args.Log {
return *(*[]*args.Log)(unsafe.Pointer(&a))
}
// TableName implements 'github.com/xiaoenai/tp-micro/model'.Cacheable
func (*Log) TableName() string {
return "log"
}
func (_l *Log) isZeroPrimaryKey() bool {
var _id int64
if _l.Id != _id {
return false
}
return true
}
var logDB, _ = mysqlHandler.RegCacheableDB(new(Log), cacheExpire, args.LogSql)
// GetLogDB returns the Log DB handler.
func GetLogDB() *mysql.CacheableDB {
return logDB
}
// InsertLog insert a Log data into database.
// NOTE:
// Primary key: 'id';
// Without cache layer.
func InsertLog(_l *Log, tx ...*sqlx.Tx) (int64, error) {
_l.UpdatedAt = coarsetime.FloorTimeNow().Unix()
if _l.CreatedAt == 0 {
_l.CreatedAt = _l.UpdatedAt
}
return _l.Id, logDB.Callback(func(tx sqlx.DbOrTx) error {
var (
query string
isZeroPrimaryKey = _l.isZeroPrimaryKey()
)
if isZeroPrimaryKey {
query = "INSERT INTO `log` (`text`,`updated_at`,`created_at`)VALUES(:text,:updated_at,:created_at);"
} else {
query = "INSERT INTO `log` (`id`,`text`,`updated_at`,`created_at`)VALUES(:id,:text,:updated_at,:created_at);"
}
r, err := tx.NamedExec(query, _l)
if isZeroPrimaryKey && err == nil {
_l.Id, err = r.LastInsertId()
}
return err
}, tx...)
}
// UpsertLog insert or update the Log data by primary key.
// NOTE:
// Primary key: 'id';
// With cache layer;
// Insert data if the primary key is specified;
// Update data based on _updateFields if no primary key is specified;
// _updateFields' members must be db field style (snake format);
// Automatic update 'updated_at' field;
// Don't update the primary keys, 'created_at' key and 'deleted_ts' key;
// Update all fields except the primary keys, 'created_at' key and 'deleted_ts' key, if _updateFields is empty.
func UpsertLog(_l *Log, _updateFields []string, tx ...*sqlx.Tx) (int64, error) {
if _l.UpdatedAt == 0 {
_l.UpdatedAt = coarsetime.FloorTimeNow().Unix()
}
if _l.CreatedAt == 0 {
_l.CreatedAt = _l.UpdatedAt
}
err := logDB.Callback(func(tx sqlx.DbOrTx) error {
var (
query string
isZeroPrimaryKey = _l.isZeroPrimaryKey()
)
if isZeroPrimaryKey {
query = "INSERT INTO `log` (`text`,`updated_at`,`created_at`)VALUES(:text,:updated_at,:created_at)"
} else {
query = "INSERT INTO `log` (`id`,`text`,`updated_at`,`created_at`)VALUES(:id,:text,:updated_at,:created_at)"
}
query += " ON DUPLICATE KEY UPDATE "
if len(_updateFields) == 0 {
query += "`text`=VALUES(`text`),`updated_at`=VALUES(`updated_at`);"
} else {
for _, s := range _updateFields {
if s == "updated_at" || s == "created_at" || s == "deleted_ts" || s == "id" {
continue
}
query += "`" + s + "`=VALUES(`" + s + "`),"
}
if query[len(query)-1] != ',' {
return nil
}
query += "`updated_at`=VALUES(`updated_at`),`deleted_ts`=0;"
}
r, err := tx.NamedExec(query, _l)
if isZeroPrimaryKey && err == nil {
var rowsAffected int64
rowsAffected, err = r.RowsAffected()
if rowsAffected == 1 {
_l.Id, err = r.LastInsertId()
}
}
return err
}, tx...)
if err != nil {
return _l.Id, err
}
err = logDB.DeleteCache(_l)
if err != nil {
tp.Errorf("%s", err.Error())
}
return _l.Id, nil
}
// UpdateLogByPrimary update the Log data in database by primary key.
// NOTE:
// Primary key: 'id';
// With cache layer;
// _updateFields' members must be db field style (snake format);
// Automatic update 'updated_at' field;
// Don't update the primary keys, 'created_at' key and 'deleted_ts' key;
// Update all fields except the primary keys, 'created_at' key and 'deleted_ts' key, if _updateFields is empty.
func UpdateLogByPrimary(_l *Log, _updateFields []string, tx ...*sqlx.Tx) error {
_l.UpdatedAt = coarsetime.FloorTimeNow().Unix()
err := logDB.Callback(func(tx sqlx.DbOrTx) error {
query := "UPDATE `log` SET "
if len(_updateFields) == 0 {
query += "`text`=:text,`updated_at`=:updated_at WHERE `id`=:id AND `deleted_ts`=0 LIMIT 1;"
} else {
for _, s := range _updateFields {
if s == "updated_at" || s == "created_at" || s == "deleted_ts" || s == "id" {
continue
}
query += "`" + s + "`=:" + s + ","
}
if query[len(query)-1] != ',' {
return nil
}
query += "`updated_at`=:updated_at WHERE `id`=:id AND `deleted_ts`=0 LIMIT 1;"
}
_, err := tx.NamedExec(query, _l)
return err
}, tx...)
if err != nil {
return err
}
err = logDB.DeleteCache(_l)
if err != nil {
tp.Errorf("%s", err.Error())
}
return nil
}
// DeleteLogByPrimary delete a Log data in database by primary key.
// NOTE:
// Primary key: 'id';
// With cache layer.
func DeleteLogByPrimary(_id int64, deleteHard bool, tx ...*sqlx.Tx) error {
var err error
if deleteHard {
// Immediately delete from the hard disk.
err = logDB.Callback(func(tx sqlx.DbOrTx) error {
_, err := tx.Exec("DELETE FROM `log` WHERE `id`=? AND `deleted_ts`=0;", _id)
return err
}, tx...)
} else {
// Delay delete from the hard disk.
ts := coarsetime.FloorTimeNow().Unix()
err = logDB.Callback(func(tx sqlx.DbOrTx) error {
_, err := tx.Exec("UPDATE `log` SET `updated_at`=?, `deleted_ts`=? WHERE `id`=? AND `deleted_ts`=0;", ts, ts, _id)
return err
}, tx...)
}
if err != nil {
return err
}
err = logDB.DeleteCache(&Log{
Id: _id,
})
if err != nil {
tp.Errorf("%s", err.Error())
}
return nil
}
// GetLogByPrimary query a Log data from database by primary key.
// NOTE:
// Primary key: 'id';
// With cache layer;
// If @return bool=false error=nil, means the data is not exist.
func GetLogByPrimary(_id int64) (*Log, bool, error) {
var _l = &Log{
Id: _id,
}
err := logDB.CacheGet(_l)
switch err {
case nil:
if _l.CreatedAt == 0 || _l.DeletedTs != 0 {
return nil, false, nil
}
return _l, true, nil
case sql.ErrNoRows:
return nil, false, nil
default:
return nil, false, err
}
}
// GetLogByWhere query a Log data from database by WHERE condition.
// NOTE:
// Without cache layer;
// If @return bool=false error=nil, means the data is not exist.
func GetLogByWhere(whereCond string, arg ...interface{}) (*Log, bool, error) {
var _l = new(Log)
err := logDB.Get(_l, "SELECT `id`,`text`,`updated_at`,`created_at` FROM `log` WHERE "+insertZeroDeletedTsField(whereCond)+" LIMIT 1;", arg...)
switch err {
case nil:
return _l, true, nil
case sql.ErrNoRows:
return nil, false, nil
default:
return nil, false, err
}
}
// SelectLogByWhere query some Log data from database by WHERE condition.
// NOTE:
// Without cache layer.
func SelectLogByWhere(whereCond string, arg ...interface{}) ([]*Log, error) {
var objs = new([]*Log)
err := logDB.Select(objs, "SELECT `id`,`text`,`updated_at`,`created_at` FROM `log` WHERE "+insertZeroDeletedTsField(whereCond), arg...)
return *objs, err
}
// CountLogByWhere count Log data number from database by WHERE condition.
// NOTE:
// Without cache layer.
func CountLogByWhere(whereCond string, arg ...interface{}) (int64, error) {
var count int64
err := logDB.Get(&count, "SELECT count(*) FROM `log` WHERE "+insertZeroDeletedTsField(whereCond), arg...)
return count, err
}