forked from xiaoenai/tp-micro
/
gray_match.go
executable file
·136 lines (123 loc) · 4.35 KB
/
gray_match.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
package model
import (
"database/sql"
"time"
"github.com/henrylee2cn/goutil/coarsetime"
tp "github.com/henrylee2cn/teleport"
"github.com/xiaoenai/tp-micro/model/mysql"
"github.com/xiaoenai/tp-micro/model/sqlx"
)
// GrayMatch
type GrayMatch struct {
Uri string `protobuf:"bytes,1,opt,name=Uri,proto3" json:"uri"`
Regexp string `protobuf:"bytes,2,opt,name=Regexp,proto3" json:"regexp"`
CreatedAt int64 `protobuf:"varint,3,opt,name=CreatedAt,proto3" json:"created_at"`
UpdatedAt int64 `protobuf:"varint,4,opt,name=UpdatedAt,proto3" json:"updated_at"`
}
// TableName implements 'github.com/xiaoenai/tp-micro/model'.Cacheable
func (*GrayMatch) TableName() string {
return "gray_match"
}
var grayMatchDB, _ = dbHandler.RegCacheableDB(
new(GrayMatch),
time.Hour*24,
`CREATE TABLE IF NOT EXISTS gray_match (
uri VARCHAR(190) COMMENT 'URI',
`+"`regexp`"+` LONGTEXT COMMENT 'regular expression to match UID',
updated_at INT(11) NOT NULL COMMENT 'updated time',
created_at INT(11) NOT NULL COMMENT 'created time',
PRIMARY KEY(uri)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='gray rule';
`)
// GetGrayMatchDB returns the GrayMatch DB handler.
func GetGrayMatchDB() *mysql.CacheableDB {
return grayMatchDB
}
// UpsertGrayMatch insert a GrayMatch data into database.
func UpsertGrayMatch(_g *GrayMatch, tx ...*sqlx.Tx) error {
_g.UpdatedAt = coarsetime.FloorTimeNow().Unix()
if _g.CreatedAt == 0 {
_g.CreatedAt = _g.UpdatedAt
}
return grayMatchDB.TransactCallback(func(tx *sqlx.Tx) error {
query := "INSERT INTO `gray_match` (`uri`,`regexp`,`updated_at`,`created_at`)VALUES(:uri,:regexp,:updated_at,:created_at)\n" +
"ON DUPLICATE KEY UPDATE `regexp`=VALUES(`regexp`),`updated_at`=VALUES(`updated_at`);"
_, err := tx.NamedExec(query, _g)
if err != nil {
return err
}
return grayMatchDB.PutCache(_g)
}, tx...)
}
// UpdateGrayMatchByUri update the GrayMatch data in database by id.
func UpdateGrayMatchByUri(_g *GrayMatch, tx ...*sqlx.Tx) error {
return grayMatchDB.TransactCallback(func(tx *sqlx.Tx) error {
_g.UpdatedAt = coarsetime.FloorTimeNow().Unix()
_, err := tx.NamedExec("UPDATE `gray_match` SET `uri`=:uri,`regexp`=:regexp,`created_at`=:created_at,`updated_at`=:updated_at WHERE uri=:uri LIMIT 1;", _g)
if err != nil {
return err
}
return grayMatchDB.PutCache(_g)
}, tx...)
}
// DeleteGrayMatchByUri delete a GrayMatch data in database by id.
func DeleteGrayMatchByUri(uri string, tx ...*sqlx.Tx) error {
return grayMatchDB.TransactCallback(func(tx *sqlx.Tx) error {
_, err := tx.Exec("DELETE FROM `gray_match` WHERE uri=?;", uri)
if err != nil {
return err
}
return grayMatchDB.PutCache(&GrayMatch{
Uri: uri,
})
}, tx...)
}
// GetGrayMatchByUri query a GrayMatch data from database by id.
// If @reply bool=false error=nil, means the data is not exist.
func GetGrayMatchByUri(uri string) (*GrayMatch, bool, error) {
var _g = &GrayMatch{
Uri: uri,
}
err := grayMatchDB.CacheGet(_g)
switch err {
case nil:
if _g.CreatedAt == 0 {
return nil, false, nil
}
return _g, true, nil
case sql.ErrNoRows:
err2 := grayMatchDB.PutCache(_g)
if err2 != nil {
tp.Errorf("%s", err2.Error())
}
return nil, false, nil
default:
return nil, false, err
}
}
// GetGrayMatchByWhere query a GrayMatch data from database by WHERE condition.
// If @reply bool=false error=nil, means the data is not exist.
func GetGrayMatchByWhere(whereCond string, args ...interface{}) (*GrayMatch, bool, error) {
var _g = new(GrayMatch)
err := grayMatchDB.Get(_g, "SELECT `uri`,`regexp`,`created_at`,`updated_at` FROM `gray_match` WHERE "+whereCond+" LIMIT 1;", args...)
switch err {
case nil:
return _g, true, nil
case sql.ErrNoRows:
return nil, false, nil
default:
return nil, false, err
}
}
// SelectGrayMatchByWhere query some GrayMatch data from database by WHERE condition.
func SelectGrayMatchByWhere(whereCond string, args ...interface{}) ([]*GrayMatch, error) {
var objs = new([]*GrayMatch)
err := grayMatchDB.Select(objs, "SELECT `uri`,`regexp`,`created_at`,`updated_at` FROM `gray_match` WHERE "+whereCond, args...)
return *objs, err
}
// CountGrayMatchByWhere count GrayMatch data number from database by WHERE condition.
func CountGrayMatchByWhere(whereCond string, args ...interface{}) (int64, error) {
var count int64
err := grayMatchDB.Get(&count, "SELECT count(1) FROM `gray_match` WHERE "+whereCond, args...)
return count, err
}