This repository was archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
214 lines (184 loc) · 6.97 KB
/
api.go
File metadata and controls
214 lines (184 loc) · 6.97 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Provides database connections in factory mode to optimize database connections
*/
package database
import (
"context"
"database/sql"
"io"
"runtime/debug"
"github.com/gwaylib/errors"
"github.com/gwaylib/log"
)
const (
DRV_NAME_MYSQL = "mysql"
DRV_NAME_ORACLE = "oracle" // or "oci8"
DRV_NAME_POSTGRES = "postgres"
DRV_NAME_SQLITE3 = "sqlite3"
DRV_NAME_SQLSERVER = "sqlserver" // or "mssql"
)
var (
// Whe reflect the QueryStruct, InsertStruct, it need set the Driver first.
// For example:
// func init(){
// database.REFLECT_DRV_NAME = database.DEV_NAME_SQLITE3
// }
// Default is using the mysql driver.
REFLECT_DRV_NAME = DRV_NAME_MYSQL
)
type Execer interface {
Exec(query string, args ...interface{}) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}
type Queryer interface {
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
type Rows interface {
Close() error
Columns() ([]string, error)
Err() error
Next() bool
Scan(...interface{}) error
}
func NewDB(drvName string, db *sql.DB) *DB {
return newDB(drvName, db)
}
// Implement the sql.Open
func Open(drvName, dsn string) (*DB, error) {
db, err := sql.Open(drvName, dsn)
if err != nil {
return nil, errors.As(err, drvName, dsn)
}
return newDB(drvName, db), nil
}
// Register a db to the connection pool by manully.
func RegCache(iniFileName, sectionName string, db *DB) {
regCache(iniFileName, sectionName, db)
}
// Get the db instance from the cache.
// If the db not in the cache, it will create a new instance from the ini file.
func GetCache(iniFileName, sectionName string) *DB {
db, err := getCache(iniFileName, sectionName)
if err != nil {
panic(err)
}
return db
}
// Checking the cache does it have a db instance.
func HasCache(etcFileName, sectionName string) (*DB, error) {
return getCache(etcFileName, sectionName)
}
// Close all instance in the cache.
func CloseCache() {
closeCache()
}
// A lazy function to closed the io.Closer
func Close(closer io.Closer) {
if closer == nil {
return
}
if err := closer.Close(); err != nil {
println(errors.As(err).Error())
debug.PrintStack()
}
}
// A lazy function to rollback the *sql.Tx
func Rollback(tx *sql.Tx) {
err := tx.Rollback()
// roll back error is a serious error
if err != nil {
log.Error(errors.As(err))
}
}
// A way implement the sql.Exec
func Exec(db Execer, querySql string, args ...interface{}) (sql.Result, error) {
return db.Exec(querySql, args...)
}
func ExecContext(db Execer, ctx context.Context, querySql string, args ...interface{}) (sql.Result, error) {
return db.ExecContext(ctx, querySql, args...)
}
// A way to ran multiply tx
func ExecMultiTx(tx *sql.Tx, mTx []*MultiTx) error {
return execMultiTx(tx, context.TODO(), mTx)
}
func ExecMultiTxContext(tx *sql.Tx, ctx context.Context, mTx []*MultiTx) error {
return execMultiTx(tx, ctx, mTx)
}
// Reflect one db data to the struct. the struct tag format like `db:"field_title"`, reference to: http://github.com/jmoiron/sqlx
// When you no set the REFLECT_DRV_NAME, you can point out with the drvName
func InsertStruct(exec Execer, obj interface{}, tbName string, drvNames ...string) (sql.Result, error) {
return insertStruct(exec, context.TODO(), obj, tbName, drvNames...)
}
func InsertStructContext(exec Execer, ctx context.Context, obj interface{}, tbName string, drvNames ...string) (sql.Result, error) {
return insertStruct(exec, ctx, obj, tbName, drvNames...)
}
// A sql.Query implements
func Query(db Queryer, querySql string, args ...interface{}) (*sql.Rows, error) {
return db.Query(querySql, args...)
}
func QueryContext(db Queryer, ctx context.Context, querySql string, args ...interface{}) (*sql.Rows, error) {
return db.QueryContext(ctx, querySql, args...)
}
// A sql.QueryRow implements
func QueryRow(db Queryer, querySql string, args ...interface{}) *sql.Row {
return db.QueryRow(querySql, args...)
}
func QueryRowContext(db Queryer, ctx context.Context, querySql string, args ...interface{}) *sql.Row {
return db.QueryRowContext(ctx, querySql, args...)
}
// Relect the sql.Rows to a struct.
func ScanStruct(rows Rows, obj interface{}) error {
return scanStruct(rows, obj)
}
// Reflect the sql.Rows to a struct array.
// Return empty array if data not found.
// Refere to: github.com/jmoiron/sqlx
func ScanStructs(rows Rows, obj interface{}) error {
return scanStructs(rows, obj)
}
// Reflect the sql.Query result to a struct.
func QueryStruct(db Queryer, obj interface{}, querySql string, args ...interface{}) error {
return queryStruct(db, context.TODO(), obj, querySql, args...)
}
func QueryStructContext(db Queryer, ctx context.Context, obj interface{}, querySql string, args ...interface{}) error {
return queryStruct(db, ctx, obj, querySql, args...)
}
// Reflect the sql.Query result to a struct array.
// Return empty array if data not found.
func QueryStructs(db Queryer, obj interface{}, querySql string, args ...interface{}) error {
return queryStructs(db, context.TODO(), obj, querySql, args...)
}
func QueryStructsContext(db Queryer, ctx context.Context, obj interface{}, querySql string, args ...interface{}) error {
return queryStructs(db, ctx, obj, querySql, args...)
}
// Query one field to a sql.Scanner.
func QueryElem(db Queryer, result interface{}, querySql string, args ...interface{}) error {
return queryElem(db, context.TODO(), result, querySql, args...)
}
func QueryElemContext(db Queryer, ctx context.Context, result interface{}, querySql string, args ...interface{}) error {
return queryElem(db, ctx, result, querySql, args...)
}
// Query one field to a sql.Scanner array.
func QueryElems(db Queryer, result interface{}, querySql string, args ...interface{}) error {
return queryElems(db, context.TODO(), result, querySql, args...)
}
func QueryElemsContext(db Queryer, ctx context.Context, result interface{}, querySql string, args ...interface{}) error {
return queryElems(db, ctx, result, querySql, args...)
}
// Reflect the query result to a string array.
func QueryPageArr(db Queryer, querySql string, args ...interface{}) (titles []string, result [][]interface{}, err error) {
return queryPageArr(db, context.TODO(), querySql, args...)
}
func QueryPageArrContext(db Queryer, ctx context.Context, querySql string, args ...interface{}) (titles []string, result [][]interface{}, err error) {
return queryPageArr(db, ctx, querySql, args...)
}
// Reflect the query result to a string map.
func QueryPageMap(db Queryer, querySql string, args ...interface{}) (titles []string, result []map[string]interface{}, err error) {
return queryPageMap(db, context.TODO(), querySql, args...)
}
func QueryPageMapContext(db Queryer, ctx context.Context, querySql string, args ...interface{}) (titles []string, result []map[string]interface{}, err error) {
return queryPageMap(db, ctx, querySql, args...)
}