-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsqlrunner.go
More file actions
177 lines (156 loc) · 5.15 KB
/
sqlrunner.go
File metadata and controls
177 lines (156 loc) · 5.15 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
package godb
import (
"database/sql"
"fmt"
"time"
)
// pointersGetter is a func type, returning a list of pointers (and error) for
// a given instance pointer and a columns names list.
type pointersGetter func(record interface{}, columns []string) ([]interface{}, error)
// do executes the given query (with its arguments) after replacing the
// placeholders if neeeded, and returns sql.Result.
func (db *DB) do(query string, arguments []interface{}) (sql.Result, error) {
query = db.replacePlaceholders(query)
// Execute the statement
startTime := time.Now()
queryable, err := db.getQueryable(query)
if err != nil {
db.logExecutionErr(err, query, arguments)
return nil, err
}
result, err := queryable.Exec(arguments...)
consumedTime := timeElapsedSince(startTime)
db.addConsumedTime(consumedTime)
db.logExecution(consumedTime, query, arguments)
if err != nil {
db.logExecutionErr(err, query, arguments)
if db.useErrorParser {
return nil, db.adapter.ParseError(err)
}
return nil, err
}
return result, err
}
// doSelectOrWithReturning executes the statement and fills the auto fields.
// It returns the count of rows returned.
// It is called when the adapter implements ReturningSuffixer.
func (db *DB) doSelectOrWithReturning(query string, arguments []interface{}, recordDescription *recordDescription, pointersGetter pointersGetter) (int64, error) {
rows, columns, err := db.executeQuery(query, arguments, false, false)
if err != nil {
return 0, err
}
defer rows.Close()
// If the given slice is empty, the slice grows as the rows are read.
// If the given slice isn't empty it's filled with rows, and both rows and
// slice length have to be equals.
// If it's a single instance, it's juste filled, and the result must have
// only one row.
var rowsCount int
if recordDescription.len() > 0 {
rowsCount, err = db.fillWithValues(recordDescription, pointersGetter, columns, rows)
} else {
rowsCount, err = db.growAndFillWithValues(recordDescription, pointersGetter, columns, rows)
}
if err != nil {
db.logExecutionErr(err, query, arguments)
return 0, err
}
err = rows.Err()
if err != nil {
db.logExecutionErr(err, query, arguments)
}
return int64(rowsCount), err
}
// executeQuery executes the given query with its arguments and returns the
// resulting *sql.Rows, the list of columns names, and an error.
func (db *DB) executeQuery(query string, arguments []interface{}, noTx, noStmtCache bool) (*sql.Rows, []string, error) {
query = db.replacePlaceholders(query)
startTime := time.Now()
queryable, err := db.getQueryableWithOptions(query, noTx, noStmtCache)
if err != nil {
db.logExecutionErr(err, query, arguments)
return nil, nil, err
}
rows, err := queryable.Query(arguments...)
consumedTime := timeElapsedSince(startTime)
db.addConsumedTime(consumedTime)
db.logExecution(consumedTime, query, arguments)
if err != nil {
db.logExecutionErr(err, query, arguments)
return nil, nil, err
}
columns, err := rows.Columns()
if err != nil {
db.logExecutionErr(err, query, arguments)
rows.Close()
return nil, nil, err
}
return rows, columns, nil
}
// fillWithReturningValues fill the record with rows, the record size must have
// at least the same size has the rows count.
// There could be less rows than awaited, it must be checked by the caller. It's
// not managed here because is could be specific case like optimistic locking failure.
func (db *DB) fillWithValues(recordDescription *recordDescription, pointersGetter pointersGetter, columns []string, rows *sql.Rows) (int, error) {
rowsCount := 0
recordLength := recordDescription.len()
for rows.Next() {
rowsCount++
if rowsCount > recordLength {
return 0, fmt.Errorf("there are more rows returned than the target size : %v", recordLength)
}
instancePtr := recordDescription.index(rowsCount - 1)
pointers, err := pointersGetter(instancePtr, columns)
if err != nil {
return 0, err
}
err = rows.Scan(pointers...)
if err != nil {
return 0, err
}
}
return rowsCount, nil
}
// growAndFillWithReturningValues fill the record with rows, and make it growing.
func (db *DB) growAndFillWithValues(recordDescription *recordDescription, pointersGetter pointersGetter, columns []string, rows *sql.Rows) (int, error) {
rowsCount := 0
for rows.Next() {
rowsCount++
if rowsCount > 1 && !recordDescription.isSlice {
return 0, fmt.Errorf("there are multiple rows for a single instance")
}
err := recordDescription.fillRecord(
// Fill one instance with one row
func(record interface{}) error {
pointers, err := pointersGetter(record, columns)
if err != nil {
return err
}
err = rows.Scan(pointers...)
if err != nil {
return err
}
return nil
})
if err != nil {
return 0, err
}
}
return rowsCount, nil
}
// doWithIterator executes the given query (with its arguments) and returns
// an Iterator.
func (db *DB) doWithIterator(query string, arguments []interface{}) (Iterator, error) {
rows, columns, err := db.executeQuery(query, arguments, true, true)
if err != nil {
if rows != nil {
rows.Close()
}
return nil, err
}
iterator := iteratorInternals{
rows: rows,
columns: columns,
}
return &iterator, nil
}