-
Notifications
You must be signed in to change notification settings - Fork 9
/
orm_execute.go
147 lines (133 loc) · 3.73 KB
/
orm_execute.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
package gorose
import (
"errors"
"github.com/gohouse/t"
"reflect"
)
// Insert : insert data and get affected rows
func (dba *Orm) Insert(data ...interface{}) (int64, error) {
return dba.exec("insert", data...)
}
// insertGetId : insert data and get id
func (dba *Orm) InsertGetId(data ...interface{}) (int64, error) {
_, err := dba.Insert(data...)
if err != nil {
return 0, err
}
return dba.GetISession().LastInsertId(), nil
}
// Update : update data
func (dba *Orm) Update(data ...interface{}) (int64, error) {
return dba.exec("update", data...)
}
// Replace : replace data and get affected rows
func (dba *Orm) Replace(data ...interface{}) (int64, error) {
return dba.exec("replace", data...)
}
// Force 强制执行没有where的删除和修改
func (dba *Orm) Force() IOrm {
dba.force = true
return dba
}
// Delete : delete data
func (dba *Orm) Delete() (int64, error) {
return dba.exec("delete")
}
// exec : Execute a sql
func (dba *Orm) exec(operType string, data ...interface{}) (int64, error) {
if operType == "insert" || operType == "update" {
if dba.GetData() == nil {
if len(data) > 0 {
dba.Data(data[0])
} else {
return 0, GetErr(ERR_PARAMS_MISSING, "Data()")
}
}
//if dba.GetISession().GetIBinder() == nil {
// 如果这里是默认值, 则需要对其进行table处理
//if dba.GetISession().GetIBinder().GetBindType() == OBJECT_NIL {
// if dba.GetData() != nil {
// dba.Table(dba.GetData())
// } else {
// return 0, GetErr(ERR_PARAMS_MISSING, "Data() or Table()")
// }
//}
rl := reflect.ValueOf(dba.GetData())
rl2 := reflect.Indirect(rl)
switch rl2.Kind() {
case reflect.Struct, reflect.Ptr:
//return 0, errors.New("传入的结构体必须是对象的地址")
if tn := rl2.MethodByName("TableName"); tn.IsValid() {
dba.Table(dba.GetData())
}
case reflect.Map:
if tn := rl2.MethodByName("TableName"); tn.IsValid() {
dba.Table(dba.GetData())
}
if tn := rl.MethodByName("TableName"); tn.IsValid() {
dba.Table(dba.GetData())
}
case reflect.Slice:
r2 := rl2.Type().Elem()
r2val := reflect.New(r2)
switch r2val.Kind() {
case reflect.Struct, reflect.Ptr:
if tn := r2val.MethodByName("TableName"); tn.IsValid() {
dba.Table(dba.GetData())
}
case reflect.Map:
if tn := r2val.MethodByName("TableName"); tn.IsValid() {
dba.Table(dba.GetData())
}
default:
return 0, errors.New("表名有误")
}
}
}
// 构建sql
sqlStr, args, err := dba.BuildSql(operType)
if err != nil {
return 0, err
}
return dba.GetISession().Execute(sqlStr, args...)
}
// Increment : auto Increment +1 default
// we can define step (such as 2, 3, 6 ...) if give the second params
// we can use this method as decrement with the third param as "-"
// orm.Increment("top") , orm.Increment("top", 2, "-")=orm.Decrement("top",2)
func (dba *Orm) Increment(args ...interface{}) (int64, error) {
argLen := len(args)
var field string
var mode = "+"
var value = "1"
switch argLen {
case 1:
field = t.New(args[0]).String()
case 2:
field = t.New(args[0]).String()
value = t.New(args[1]).String()
case 3:
field = t.New(args[0]).String()
value = t.New(args[1]).String()
mode = t.New(args[2]).String()
default:
return 0, errors.New("参数数量只允许1个,2个或3个")
}
dba.Data(field + "=" + field + mode + value)
return dba.Update()
}
// Decrement : auto Decrement -1 default
// we can define step (such as 2, 3, 6 ...) if give the second params
func (dba *Orm) Decrement(args ...interface{}) (int64, error) {
arglen := len(args)
switch arglen {
case 1:
args = append(args, 1)
args = append(args, "-")
case 2:
args = append(args, "-")
default:
return 0, errors.New("Decrement参数个数有误")
}
return dba.Increment(args...)
}