-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
192 lines (170 loc) · 4.35 KB
/
core.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package sqldata
import (
"context"
"database/sql"
_ "github.com/go-sql-driver/mysql" //must import
"log"
"reflect"
)
//connDb connect db information
type connDb struct {
dns string
retry int32
driverName string
coreDb *sql.DB
}
//newConnDb star create a db driver
func newConnDb(conf *configDb) (re *connDb, err error) {
re = &connDb{
dns: conf.Addr,
retry: conf.Retry,
driverName: conf.DriverName,
}
re.coreDb, err = re.connect()
return
}
//connect open the driver
func (cn *connDb) connect() (db *sql.DB, err error) {
if cn.coreDb != nil {
return cn.coreDb, nil
}
db, err = sql.Open(cn.driverName, cn.dns)
if err != nil {
log.Fatalf("connect fail;%v", err)
} else {
cn.coreDb = db
}
return
}
//GetConnDb get the DB infors
func (cn *connDb) getConnDb() (db *sql.DB) {
db = cn.coreDb
return
}
//begin db trans start
func (cn *connDb) begin() (btx *sql.Tx, err error) {
btx, err = cn.coreDb.Begin()
return
}
//txPrepare btx from begin
func (cn *connDb) txPrepare(btx *sql.Tx, query string) (stmt *sql.Stmt, err error) {
stmt, err = btx.Prepare(query)
return
}
//commit db trans commit
func (cn *connDb) commit(btx *sql.Tx) error {
return btx.Commit()
}
//rollback db trans reset
func (cn *connDb) rollback(btx *sql.Tx) error {
return btx.Rollback()
}
//prepare
func (cn *connDb) prepare(query string) (stmt *sql.Stmt, err error) {
stmt, err = cn.coreDb.Prepare(query)
return
}
//execFrStmt operate by exec from stmt
func (cn *connDb) execFromStmt(stmt *sql.Stmt, args ...interface{}) (rs sql.Result, sterr error) {
rs, sterr = stmt.Exec(args...)
return
}
//execTx operate by exec from tx
func (cn *connDb) execFromTx(btx *sql.Tx, query string, args ...interface{}) (rs sql.Result, exerr error) {
rs, exerr = btx.Exec(query, args...)
return
}
//exec operate by exec from db,a normal op
func (cn *connDb) execFromDb(query string, args ...interface{}) (result sql.Result, err error) {
result, err = cn.coreDb.Exec(query, args...)
return
}
//getLastId get the result last id
func (cn *connDb) getLastID(result sql.Result) (num int64, err error) {
num, err = result.LastInsertId()
return
}
//rowsAffected result is ok or fail
func (cn *connDb) rowsAffected(result sql.Result) (num int64, err error) {
num, err = result.RowsAffected()
return
}
//close close db
func (cn *connDb) close() error {
return cn.coreDb.Close()
}
//results from sql and args,get the datas
func (cn *connDb) query(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
rows, err = cn.coreDb.QueryContext(ctx, query, args...)
if err != nil {
log.Printf("sql:%v,result:%v", query, err)
}
return
}
//fetchMap reset new datas,become to map type
//return data the same array
func (cn *connDb) rowsMap(rows *sql.Rows) (results resultData) {
columns, _ := rows.Columns()
values := make([][]byte, len(columns)) //make a byte slice
fields := make([]interface{}, len(columns))
for i := range values {
fields[i] = &values[i]
}
results = make(map[int]map[string]string)
var ii int
for rows.Next() {
if err := rows.Scan(fields...); err != nil {
log.Fatal("sqldata_fetchMap||err=%v", err)
continue
//return
}
row := make(map[string]string) //every on line
for k, v := range values {
key := columns[k]
row[key] = string(v) //string info
}
results[ii] = row
ii++
}
return
}
//rowsObject
func (cn *connDb) rowsObject(rows *sql.Rows, dataStruct interface{}) (results []interface{}) {
columns, _ := rows.Columns()
svalues := make([]interface{}, len(columns))
reStruct := reflect.ValueOf(&dataStruct).Elem()
toType := reflect.TypeOf(dataStruct)
var toTypeNum int
toTypeNum = toType.NumField()
for si, sv := range columns {
var tagName string
for kk := 0; kk < toTypeNum; kk++ {
ptag := toType.Field(kk).Tag.Get("sql")
if sv == ptag {
tagName = toType.Field(kk).Name
break
}
}
if tagName == "" {
continue
}
svalues[si] = reStruct.FieldByName(tagName).Addr().Interface()
}
for rows.Next() {
rows.Scan(svalues...)
results = append(results, reflect.ValueOf(dataStruct))
}
return
}
//findTagName
func (cn *connDb) findTagName(dStruct interface{}, tag string) (tagName string) {
toType := reflect.TypeOf(dStruct)
for kk := 0; kk < toType.NumField(); kk++ {
ptag := toType.Field(kk).Tag.Get("sql")
if tag == ptag {
tagName = toType.Field(kk).Name
break
}
}
return
}