-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqldata.go
170 lines (152 loc) · 4.35 KB
/
sqldata.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
package sqldata
import (
"context"
"database/sql"
"log"
)
//ResultData is map setting
type resultData map[int]map[string]string
//SQLData interface set
//Define the development method, which will provide better service.
type SQLData interface {
// Gets native objects, supports access to existing SQL methods
GetDb() (db *sql.DB)
// fetch data information
FetchMapFromSql(sql string, args ...interface{}) (data resultData, err error)
// fetch data from rows
FetchMapFromRows(result *sql.Rows) (data resultData)
// insert data information
PrepareInsert(sql string, args ...interface{}) (lastID int64, err error)
// op database information
PrepareOpAffected(sql string, args ...interface{}) (affectedID int64, err error)
// Insert
Insert(sql string, args ...interface{}) (lastID int64, err error)
// OpAffected
OpAffected(sql string, args ...interface{}) (affectedID int64, err error)
// Begin
// For database transaction, the following interfaces can be used
Begin() (err error)
// TxPrepare
TxPrepare(query string) (stmt *sql.Stmt, err error)
// TxExec
TxExec(stmt *sql.Stmt, args ...interface{}) (rs sql.Result, sterr error)
// Commit
Commit() (err error)
// Rollback
Rollback() (err error)
}
//implSqlData impl information
type implSQLData struct {
ctx context.Context //ctx from config
conndb *connDb //db object
btx *sql.Tx
}
//GetDb get CoreDb
func (sd *implSQLData) GetDb() (db *sql.DB) {
if sd.conndb.coreDb == nil {
log.Fatal("sqldata_GetDb||db is null")
return nil
}
db = sd.conndb.coreDb
return
}
//MysqlFetchMap get the datas,He's a map format
//The only way to get data is to return the information you want.
func (sd *implSQLData) FetchMapFromSql(sql string, args ...interface{}) (data resultData, err error) {
conDatabase := sd.conndb
resultRows, err := conDatabase.query(sd.ctx, sql, args...)
if err != nil {
log.Fatal("sqldata_MysqlFetchMap||sql=%s||err=%v", sql, err)
}
data = conDatabase.rowsMap(resultRows)
return
}
//FetchMapFromRows
func (sd *implSQLData) FetchMapFromRows(result *sql.Rows) (data resultData) {
conDatabase := sd.conndb
data = conDatabase.rowsMap(result)
return
}
//PrepareInsert Batch insert data
func (sd *implSQLData) PrepareInsert(sql string, args ...interface{}) (lastID int64, err error) {
conDatabase := sd.conndb
stmt, err := conDatabase.prepare(sql)
defer stmt.Close()
result, err := conDatabase.execFromStmt(stmt, args...)
if err != nil {
log.Fatal("sqldata_PrepareInsert||sql=%s||err=%v", sql, err)
}
lastID, _ = result.LastInsertId()
return
}
//PrepareOpAffected Batch op data
func (sd *implSQLData) PrepareOpAffected(sql string, args ...interface{}) (affectedID int64, err error) {
conDatabase := sd.conndb
stmt, err := conDatabase.prepare(sql)
defer stmt.Close()
result, err := conDatabase.execFromStmt(stmt, args...)
if err != nil {
log.Fatal("sqldata_PrepareOpAffected||sql=%s||err=%v", sql, err)
}
affectedID, _ = result.RowsAffected()
return
}
//Insert Single insert data
func (sd *implSQLData) Insert(sql string, args ...interface{}) (lastID int64, err error) {
conDatabase := sd.conndb
result, err := conDatabase.execFromDb(sql, args...)
if err != nil {
log.Fatal("sqldata_Insert||sql=%s||err=%v", sql, err)
}
lastID, _ = result.LastInsertId()
return
}
//OpAffected Single op data
func (sd *implSQLData) OpAffected(sql string, args ...interface{}) (affectedID int64, err error) {
conDatabase := sd.conndb
result, err := conDatabase.execFromDb(sql, args...)
if err != nil {
log.Fatal("sqldata_OpAffected||sql=%s||err=%v", sql, err)
}
affectedID, _ = result.RowsAffected()
return
}
//Begin
func (sd *implSQLData) Begin() (err error) {
sd.btx, err = sd.conndb.begin()
if err != nil {
log.Fatal("sqldata_Begin||err=%v", err)
}
return
}
//TxPrepare
func (sd *implSQLData) TxPrepare(query string) (stmt *sql.Stmt, err error) {
btx := sd.btx
stmt, err = sd.conndb.txPrepare(btx, query)
return
}
//TxExec
func (sd *implSQLData) TxExec(stmt *sql.Stmt, args ...interface{}) (rs sql.Result, sterr error) {
rs, sterr = stmt.Exec(args...)
return
}
//Commit
func (sd *implSQLData) Commit() (err error) {
err = sd.conndb.commit(sd.btx)
if err == nil {
sd.closeTx()
}
return
}
//Rollback
func (sd *implSQLData) Rollback() (err error) {
err = sd.conndb.rollback(sd.btx)
if err == nil {
sd.closeTx()
}
return
}
//closeTx
func (sd *implSQLData) closeTx() {
//sd.btx.Commit()
}