-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exec.go
174 lines (164 loc) · 3.95 KB
/
Exec.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
package gpa
import (
"github.com/cihub/seelog"
"reflect"
"strings"
)
/**
返回影响记录的行数
*/
func (dao *Gpa) Exec(runSql string, p ...interface{}) (int64, error) {
row, er := dao.Conn.Exec(runSql, p...)
if er == nil {
ra, _ := row.RowsAffected()
return ra, nil
} else {
seelog.Error("SQL执行失败:\n\tsql=", runSql, "\n\tparam=", p, "\n\terror=", er)
return -1, er
}
}
/**
返回 自增ID
*/
func (dao *Gpa) Insert(s string, param ...interface{}) (int64, error) {
row, err := dao.Conn.Exec(s, param...)
if err == nil {
return row.LastInsertId()
} else {
seelog.Error("insert对象失败,", s, param, err)
return -1, err
}
}
func (dao *Gpa) Save(model interface{}) (int64, error) {
toe := reflect.TypeOf(model).Elem()
voe := reflect.ValueOf(model).Elem()
e, err := dao.exist(toe, voe)
if err != nil {
return -1, err
} else {
if e == 1 {
return dao.update(toe, voe)
} else {
return dao.insert(toe, voe)
}
}
}
func (dao *Gpa) InsertObject(model interface{}) (int64, error) {
toe := reflect.TypeOf(model).Elem()
voe := reflect.ValueOf(model).Elem()
return dao.insert(toe, voe)
}
func (dao *Gpa) InsertMap(table string, val map[string]string) (int64, error) {
sql, fix := "insert into "+table+"(", ""
var va []interface{}
for k, v := range val {
sql += k + ","
fix += "?,"
va = append(va, v)
}
sql = sql[0:len(sql)-1] + ")values(" + fix[0:len(fix)-1] + ")"
return dao.Insert(sql, va...)
}
func (dao *Gpa) exist(toe reflect.Type, voe reflect.Value) (int64, error) {
n := toe.NumField()
s := "select 1 from " + toe.Name() + " where "
var param []interface{}
for i := 0; i < n; i++ {
fx := toe.Field(i)
tag := string(fx.Tag)
col := strings.ToLower(fx.Name[0:1]) + fx.Name[1:]
vF := voe.FieldByName(fx.Name).String()
if len(vF) >= 1 {
if strings.Index(tag, PrimaryId) >= 0 {
s += col + "=? and "
param = append(param, vF)
}
}
}
s = s[0 : len(s)-4]
//fmt.Println(s)
rows, err := dao.Conn.Query(s, param...)
defer rows.Close()
if err != nil {
seelog.Error("SQL出错", s, ":", err)
return -1, err
}
if rows.Next() {
var res int64
rows.Scan(&res)
return res, nil
} else {
return 0, nil
}
}
func (dao *Gpa) insert(toe reflect.Type, voe reflect.Value) (int64, error) {
n := toe.NumField()
s := "insert into " + toe.Name() + "("
cols := ""
var param []interface{}
auto := ""
for i := 0; i < n; i++ {
fx := toe.Field(i)
tag := string(fx.Tag)
vF := voe.FieldByName(fx.Name).String()
if len(vF) == 0 {
continue
}
c := strings.ToLower(fx.Name[0:1]) + fx.Name[1:]
if strings.Index(tag, AutoIncrement) < 0 {
s += c + ","
cols += "?,"
param = append(param, voe.FieldByName(fx.Name).Interface())
} else {
auto = fx.Name
}
}
s = s[0:len(s)-1] + ")values(" + cols[0:len(cols)-1] + ")"
row, err := dao.Conn.Exec(s, param...)
if err == nil {
rii, _ := row.LastInsertId()
if rii > 0 && len(auto) > 0 {
voe.FieldByName(auto).SetInt(rii)
} else {
rii, _ = row.RowsAffected()
}
return rii, err
} else {
seelog.Error("insert对象失败,", s, param, err)
return -1, err
}
}
func (dao *Gpa) update(toe reflect.Type, voe reflect.Value) (int64, error) {
n := toe.NumField()
s := "update " + toe.Name() + " set "
pri := ""
var param, priParam []interface{}
for i := 0; i < n; i++ {
fx := toe.Field(i)
tag := string(fx.Tag)
col := strings.ToLower(fx.Name[0:1]) + fx.Name[1:]
vF := voe.FieldByName(fx.Name).String()
if len(vF) >= 1 {
if strings.Index(tag, PrimaryId) >= 0 || strings.Index(tag, AutoIncrement) >= 0 {
pri += col + "=? and "
priParam = append(priParam, vF)
} else {
s += col + "=?,"
param = append(param, vF)
}
}
}
s = s[0:len(s)-1] + " where " + pri[0:len(pri)-4]
//fmt.Println(s)
for _, v := range priParam {
param = append(param, v)
}
row, er := dao.Conn.Exec(s, param...)
if er == nil {
raf, _ := row.RowsAffected()
return raf, er
} else {
seelog.Error("update对象失败,", er)
return -1, er
}
}