-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdbexec.go
149 lines (141 loc) · 3.11 KB
/
dbexec.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
// Copyright (c) 2024, donnie <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// github.com/donnie4w/gdao
package gdao
import (
"database/sql"
. "github.com/donnie4w/gdao/base"
"github.com/donnie4w/gdao/util"
"github.com/donnie4w/gofer/pool/buffer"
)
var bufpool = buffer.NewPool[[]any](func() *[]any {
return nil
}, func(a *[]any) {
*a = (*a)[:0]
})
func newAnys(length int) (r *[]any) {
if r = bufpool.Get(); r == nil {
var v []any
return &v
} else {
return r
}
}
func executeQueryBeans(tx *sql.Tx, db *sql.DB, sqlstr string, args ...any) (databases []*DataBean, err error) {
if tx == nil && db == nil {
return nil, errInit
}
//defer util.Recover(&err)
var rows *sql.Rows
if tx != nil {
rows, err = tx.Query(sqlstr, args...)
} else {
rows, err = db.Query(sqlstr, args...)
}
if err != nil {
return nil, err
}
defer rows.Close()
databases = make([]*DataBean, 0)
if names, er := rows.Columns(); er == nil {
for rows.Next() {
databean := NewDataBean(len(names))
buff := newAnys(len(names))
for _, name := range names {
fb := NewFieldBeen()
*buff = append(*buff, &fb.FieldValue)
databean.Put(name, fb)
}
if err = rows.Scan(*buff...); err != nil {
databean.SetError(err)
return
}
bufpool.Put(&buff)
databases = append(databases, databean)
}
} else {
err = er
}
return
}
func executeQueryBean(tx *sql.Tx, db *sql.DB, sqlstr string, args ...any) (dataBean *DataBean, err error) {
if tx == nil && db == nil {
return nil, errInit
}
//defer util.Recover(&err)
var rows *sql.Rows
if tx != nil {
rows, err = tx.Query(sqlstr, args...)
} else {
rows, err = db.Query(sqlstr, args...)
}
if err != nil {
return nil, err
}
defer rows.Close()
if names, er := rows.Columns(); er == nil {
dataBean = NewDataBean(len(names))
if rows.Next() {
buff := newAnys(len(names))
for _, name := range names {
fb := NewFieldBeen()
*buff = append(*buff, &fb.FieldValue)
dataBean.Put(name, fb)
}
if err = rows.Scan(*buff...); err != nil {
dataBean.SetError(err)
return
}
bufpool.Put(&buff)
}
} else {
err = er
}
return
}
func executeUpdate(tx *sql.Tx, db *sql.DB, sqlstr string, args ...any) (rs sql.Result, err error) {
if tx == nil && db == nil {
return nil, errInit
}
defer util.Recover(&err)
var stmtIns *sql.Stmt
if tx != nil {
stmtIns, err = tx.Prepare(sqlstr)
} else {
stmtIns, err = db.Prepare(sqlstr)
}
if err != nil {
return
}
defer stmtIns.Close()
return stmtIns.Exec(args...)
}
func executeBatch(tx *sql.Tx, db *sql.DB, sqlstr string, args [][]any) (r []sql.Result, err error) {
if tx == nil && db == nil {
return nil, errInit
}
defer util.Recover(&err)
var stmtIns *sql.Stmt
if tx != nil {
stmtIns, err = tx.Prepare(sqlstr)
} else {
stmtIns, err = db.Prepare(sqlstr)
}
if err != nil {
return nil, err
}
defer stmtIns.Close()
r = make([]sql.Result, 0)
for _, record := range args {
if rs, er := stmtIns.Exec(record...); er == nil {
r = append(r, rs)
} else {
err = er
break
}
}
return
}