-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
51 lines (45 loc) · 1.36 KB
/
mysql.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
package dac
import "gorm.io/gorm"
// MySQLDatabase 结构体实现 MySQL 数据库访问方法
type MySQLDatabase struct {
DataAccess
}
// Limit 实现Limit方法
func (m *MySQLDatabase) Limit(db *gorm.DB, page, pageSize int64) *gorm.DB {
db = db.Limit(int(pageSize)).Offset(int(page * pageSize))
return db
}
type MysqlOperator struct {
OperatorI
}
func init() {
RegisterDatabase(Mysql, &MySQLDatabase{})
RegisterOperator(Mysql, &MysqlOperator{})
}
func (m MysqlOperator) BuildQuery(condition Condition, qf *QueryFilter) {
switch condition.Operator {
case Equal:
m.Equal(condition, qf)
case NotEqual:
m.NotEqual(condition, qf)
case GreaterThan:
m.GreaterThan(condition, qf)
case GreaterThanOrEqual:
m.GreaterThanOrEqual(condition, qf)
}
}
func (m MysqlOperator) GreaterThanOrEqual(condition Condition, qf *QueryFilter) {
qf.And(condition.Key+" >= ?", condition.Value)
}
func (m MysqlOperator) GreaterThan(condition Condition, qf *QueryFilter) {
qf.And(condition.Key+" > ?", condition.Value)
}
func (m MysqlOperator) LessThanOrEqual(condition Condition, qf *QueryFilter) {
qf.And(condition.Key+" <= ?", condition.Value)
}
func (m MysqlOperator) Equal(condition Condition, qf *QueryFilter) {
qf.And(condition.Key+" = ?", condition.Value)
}
func (m MysqlOperator) NotEqual(condition Condition, qf *QueryFilter) {
qf.And(condition.Key+" != ?", condition.Value)
}