-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
149 lines (120 loc) · 2.95 KB
/
table.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
package mysql
import (
"errors"
"fmt"
"github.com/go-xorm/xorm"
"strings"
)
type Table interface {
SelfObj() *xorm.TableName
}
type TableFactory struct {
tableNode string `json:"-" xorm:"-" xml:"-"`
myself func() interface{} `json:"-" xorm:"-"`
}
func (tf TableFactory) checkNode() error {
if tf.tableNode == "" {
return errors.New("table_node not set")
}
return nil
}
// 校验
func (tf TableFactory) check() error {
if err := tf.checkNode(); err != nil {
return err
}
return nil
}
func (tf *TableFactory) SetMyself(self func() interface{}) {
tf.myself = self
}
func (tf *TableFactory) SetTableNode(node string) {
tf.tableNode = node
}
func (tf TableFactory) TableNode() string {
if tf.tableNode == "" {
return "default"
}
return tf.tableNode
}
func (tf TableFactory) Myself() interface{} {
return tf.myself()
}
func (tf *TableFactory) Insert() (affectRows int64, err error) {
if err := tf.check(); err != nil {
return affectRows, err
}
affectRows, err = Select(tf.TableNode()).Master().Insert(tf.Myself())
return
}
// 删除表结构
func (tf *TableFactory) Delete() (int64, error) {
return Select(tf.TableNode()).Master().Delete(tf.Myself())
}
// 查询一条数据
func (tf *TableFactory) Get(fromMaster ...bool) (bool, error) {
if len(fromMaster) > 0 && fromMaster[0] {
return Select(tf.TableNode()).Master().Get(tf.Myself())
}
return Select(tf.TableNode()).Slave().Get(tf.Myself())
}
// 查询列表
func (tf *TableFactory) Find(whereBuilder *WhereBuilder, fields string, orderBy string, listResult interface{}, fromMaster ...bool) (err error) {
var (
session *xorm.Session
master = false
whereData = ""
whereArgs []interface{}
)
whereData, whereArgs = whereBuilder.Encode()
if len(whereData) == 0 {
return errors.New("where condition required")
}
fmt.Printf("where: %s\n", whereData)
fmt.Printf("args: %v\n", whereArgs)
if len(fromMaster) > 0 && fromMaster[0] {
master = true
}
if master {
session = Select(tf.TableNode()).Master().NewSession()
} else {
session = Select(tf.TableNode()).Slave().NewSession()
}
if len(fields) == 0 {
session.AllCols()
} else {
session.Cols(strings.Split(fields, ",")...)
}
session.Where(whereData, whereArgs...)
fmt.Printf("%v\n", whereData)
fmt.Printf("%v\n", whereArgs)
if orderBy != "" {
session.OrderBy(orderBy)
}
if err := session.Table(tf.Myself()).Find(listResult); err != nil {
return err
}
return err
}
// 更新表
func (tf *TableFactory) Update(whereBuilder *WhereBuilder, updateFields ...string) (int64, error) {
var (
whereStr string
whereData []interface{}
session *xorm.Session
)
if err := tf.check(); err != nil {
return 0, err
}
session = Select(tf.TableNode()).Master().NewSession()
whereStr, whereData = whereBuilder.Encode()
if whereStr != "" {
session.Where(whereStr, whereData...)
}
if len(updateFields) == 0 {
session.AllCols()
} else {
session.Cols(updateFields...)
}
return session.Update(tf.Myself())
}