-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsql_manager.go
executable file
·164 lines (143 loc) · 4.12 KB
/
sql_manager.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
package dsc
import (
"database/sql"
"fmt"
"github.com/pkg/errors"
"reflect"
"time"
)
func asSQLDb(wrapped interface{}) (*sql.DB, error) {
if result, ok := wrapped.(*sql.DB); ok {
return result, nil
}
wrappedType := reflect.ValueOf(wrapped)
return nil, fmt.Errorf(fmt.Sprintf("failed cast as sql.DB: was %v !", wrappedType.Type()))
}
func asSQLTx(wrapped interface{}) (*sql.Tx, error) {
if wrapped == nil {
return nil, nil
}
if result, ok := wrapped.(*sql.Tx); ok {
return result, nil
}
wrappedType := reflect.ValueOf(wrapped)
return nil, fmt.Errorf(fmt.Sprintf("failed cast as sql.Tx: was %v !", wrappedType.Type()))
}
func asScanner(wrapped interface{}) (Scanner, error) {
sqlRows, ok := wrapped.(*sql.Rows)
if !ok {
return nil, errors.Errorf("expected :%T, but had: %T", sqlRows, wrapped)
}
return &sqlScanner{sqlRows}, nil
}
type sqlScanner struct {
*sql.Rows
}
func (s *sqlScanner) ColumnTypes() ([]ColumnType, error) {
types, err := s.Rows.ColumnTypes()
if err != nil || len(types) == 0 {
return nil, err
}
var columnTypes = make([]ColumnType, len(types))
for i := range types {
columnTypes[i] = types[i]
}
return columnTypes, nil
}
type sqlExecutor interface {
Exec(sql string, parameters ...interface{}) (sql.Result, error)
}
type sqlManager struct {
*AbstractManager
}
func (m *sqlManager) initConnectionIfNeeded(connection Connection) error {
if sqlConnection, ok := connection.(*sqlConnection); ok {
if sqlConnection.init {
return nil
}
sqlConnection.init = true
dialect := GetDatastoreDialect(m.config.DriverName)
return dialect.Init(m, connection)
}
return nil
}
func (m *sqlManager) ExecuteOnConnection(connection Connection, sql string, args []interface{}) (sql.Result, error) {
m.Acquire()
db, err := asSQLDb(connection.Unwrap(sqlDbPointer))
if err == nil {
err = m.initConnectionIfNeeded(connection)
}
if err != nil {
return nil, err
}
var executable sqlExecutor = db
tx, err := asSQLTx(connection.Unwrap(sqlTxtPointer))
if err != nil {
return nil, err
}
if tx != nil {
executable = tx
}
if args == nil {
args = make([]interface{}, 0)
}
dialect := GetDatastoreDialect(m.config.DriverName)
sql = dialect.NormalizeSQL(sql)
result, err := executable.Exec(sql, args...)
if !dialect.CanHandleTransaction() {
result = NewSQLResult(1, 0)
}
Logf("[%v]:%v %v", m.config.username, sql, args)
if err != nil {
return nil, fmt.Errorf("failed to execute %w: %v %v on %v", err.Error(), sql, args, m.Manager.Config().Parameters)
}
return result, err
}
func (m *sqlManager) ReadAllOnWithHandlerOnConnection(connection Connection, query string, args []interface{}, readingHandler func(scanner Scanner) (toContinue bool, err error)) error {
m.Acquire()
startTime := time.Now()
db, err := asSQLDb(connection.Unwrap((*sql.DB)(nil)))
if err == nil {
err = m.initConnectionIfNeeded(connection)
}
if err != nil {
return err
}
dialect := GetDatastoreDialect(m.config.DriverName)
query = dialect.NormalizeSQL(query)
Logf("[%v]:%v", m.config.username, query)
sqlStatement, sqlError := db.Prepare(query)
if sqlError != nil {
return fmt.Errorf("failed to prepare sql: %v with %v due to:%v\n\t", query, args, sqlError.Error())
}
Logf("[%v]:prepare time: %v\n", m.config.username, time.Now().Sub(startTime))
defer sqlStatement.Close()
rows, queryError := m.executeQuery(sqlStatement, query, args)
if queryError != nil {
return fmt.Errorf(fmt.Sprintf("failed to execute sql: %v with %v due to:%v\n\t", query, args, queryError.Error()))
}
Logf("[%v]:execute time: %v\n", m.config.username, time.Now().Sub(startTime))
defer rows.Close()
for rows.Next() {
scanner, _ := asScanner(rows)
toContinue, err := readingHandler(NewScanner(scanner))
if err != nil {
return err
}
if !toContinue {
break
}
}
Logf("[%v]:fetched time: %v\n", m.config.username, time.Now().Sub(startTime))
return rows.Err()
}
func (m *sqlManager) executeQuery(sqlStatement *sql.Stmt, query string, args []interface{}) (rows *sql.Rows, err error) {
if args == nil {
args = make([]interface{}, 0)
}
rows, err = sqlStatement.Query(args...)
if err != nil {
return nil, err
}
return rows, nil
}