-
Notifications
You must be signed in to change notification settings - Fork 0
/
zf.go
326 lines (277 loc) · 10.5 KB
/
zf.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"context"
"database/sql/driver"
"reflect"
)
type rwFilter struct {
}
//DmDriver
func (rwf *rwFilter) DmDriverOpen(filterChain *filterChain, d *DmDriver, dsn string) (*DmConnection, error) {
return filterChain.DmDriverOpen(d, dsn)
}
func (rwf *rwFilter) DmDriverOpenConnector(filterChain *filterChain, d *DmDriver, dsn string) (*DmConnector, error) {
return filterChain.DmDriverOpenConnector(d, dsn)
}
//DmConnector
func (rwf *rwFilter) DmConnectorConnect(filterChain *filterChain, c *DmConnector, ctx context.Context) (*DmConnection, error) {
connection, err := filterChain.DmConnectorConnect(c, ctx)
if err != nil {
return nil, err
}
// printPrimary(connection);
connection.rwInfo.rwCounter = getRwCounterInstance(connection) // rwCounter放在连接上,不用每次都要从map中获取
RWUtil.connectStandby(connection)
// printStandby(connection);
return connection, nil
}
func (rwf *rwFilter) DmConnectorDriver(filterChain *filterChain, c *DmConnector) *DmDriver {
return filterChain.DmConnectorDriver(c)
}
//DmConnection
func (rwf *rwFilter) DmConnectionBegin(filterChain *filterChain, c *DmConnection) (*DmConnection, error) {
if RWUtil.isStandbyAlive(c) {
_, err := c.rwInfo.connStandby.begin()
if err != nil {
RWUtil.afterExceptionOnStandby(c, err)
}
}
return filterChain.DmConnectionBegin(c)
}
func (rwf *rwFilter) DmConnectionBeginTx(filterChain *filterChain, c *DmConnection, ctx context.Context, opts driver.TxOptions) (*DmConnection, error) {
if RWUtil.isStandbyAlive(c) {
_, err := c.rwInfo.connStandby.beginTx(ctx, opts)
if err != nil {
RWUtil.afterExceptionOnStandby(c, err)
}
}
return filterChain.DmConnectionBeginTx(c, ctx, opts)
}
func (rwf *rwFilter) DmConnectionCommit(filterChain *filterChain, c *DmConnection) error {
if RWUtil.isStandbyAlive(c) {
err := c.rwInfo.connStandby.commit()
if err != nil {
RWUtil.afterExceptionOnStandby(c, err)
}
}
return filterChain.DmConnectionCommit(c)
}
func (rwf *rwFilter) DmConnectionRollback(filterChain *filterChain, c *DmConnection) error {
if RWUtil.isStandbyAlive(c) {
err := c.rwInfo.connStandby.rollback()
if err != nil {
RWUtil.afterExceptionOnStandby(c, err)
}
}
return filterChain.DmConnectionRollback(c)
}
func (rwf *rwFilter) DmConnectionClose(filterChain *filterChain, c *DmConnection) error {
if RWUtil.isStandbyAlive(c) {
err := c.rwInfo.connStandby.close()
if err != nil {
RWUtil.afterExceptionOnStandby(c, err)
}
}
return filterChain.DmConnectionClose(c)
}
func (rwf *rwFilter) DmConnectionPing(filterChain *filterChain, c *DmConnection, ctx context.Context) error {
return filterChain.DmConnectionPing(c, ctx)
}
func (rwf *rwFilter) DmConnectionExec(filterChain *filterChain, c *DmConnection, query string, args []driver.Value) (*DmResult, error) {
ret, err := RWUtil.executeByConn(c, func() (interface{}, error) {
RWUtil.distributeSqlByConn(c, query)
return c.rwInfo.connCurrent.exec(query, args)
}, func(otherConn *DmConnection) (interface{}, error) {
return otherConn.exec(query, args)
})
if err != nil {
return nil, err
}
return ret.(*DmResult), nil
}
func (rwf *rwFilter) DmConnectionExecContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string, args []driver.NamedValue) (*DmResult, error) {
ret, err := RWUtil.executeByConn(c, func() (interface{}, error) {
RWUtil.distributeSqlByConn(c, query)
return c.rwInfo.connCurrent.execContext(ctx, query, args)
}, func(otherConn *DmConnection) (interface{}, error) {
return otherConn.execContext(ctx, query, args)
})
if err != nil {
return nil, err
}
return ret.(*DmResult), nil
}
func (rwf *rwFilter) DmConnectionQuery(filterChain *filterChain, c *DmConnection, query string, args []driver.Value) (*DmRows, error) {
ret, err := RWUtil.executeByConn(c, func() (interface{}, error) {
RWUtil.distributeSqlByConn(c, query)
return c.rwInfo.connCurrent.query(query, args)
}, func(otherConn *DmConnection) (interface{}, error) {
return otherConn.query(query, args)
})
if err != nil {
return nil, err
}
return ret.(*DmRows), nil
}
func (rwf *rwFilter) DmConnectionQueryContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string, args []driver.NamedValue) (*DmRows, error) {
ret, err := RWUtil.executeByConn(c, func() (interface{}, error) {
RWUtil.distributeSqlByConn(c, query)
return c.rwInfo.connCurrent.queryContext(ctx, query, args)
}, func(otherConn *DmConnection) (interface{}, error) {
return otherConn.queryContext(ctx, query, args)
})
if err != nil {
return nil, err
}
return ret.(*DmRows), nil
}
func (rwf *rwFilter) DmConnectionPrepare(filterChain *filterChain, c *DmConnection, query string) (*DmStatement, error) {
distribute := RWUtil.distributeSqlByConn(c, query)
stmt, err := filterChain.DmConnectionPrepare(c, query)
if err != nil {
return nil, err
}
stmt.rwInfo.stmtCurrent = stmt
if stmt != nil && distribute == STANDBY {
stmt.rwInfo.stmtStandby, err = c.rwInfo.connStandby.prepare(query)
if err != nil {
RWUtil.afterExceptionOnStandby(c, err)
}
stmt.rwInfo.stmtCurrent = stmt.rwInfo.stmtStandby
}
return stmt, nil
}
func (rwf *rwFilter) DmConnectionPrepareContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string) (*DmStatement, error) {
distribute := RWUtil.distributeSqlByConn(c, query)
stmt, err := filterChain.DmConnectionPrepareContext(c, ctx, query)
if err != nil {
return nil, err
}
stmt.rwInfo.stmtCurrent = stmt
if stmt != nil && distribute == STANDBY {
stmt.rwInfo.stmtStandby, err = c.rwInfo.connStandby.prepareContext(ctx, query)
if err != nil {
RWUtil.afterExceptionOnStandby(c, err)
}
stmt.rwInfo.stmtCurrent = stmt.rwInfo.stmtStandby
}
return stmt, nil
}
func (rwf *rwFilter) DmConnectionResetSession(filterChain *filterChain, c *DmConnection, ctx context.Context) error {
if RWUtil.isStandbyAlive(c) {
err := c.rwInfo.connStandby.resetSession(ctx)
if err != nil {
RWUtil.afterExceptionOnStandby(c, err)
}
}
return filterChain.DmConnectionResetSession(c, ctx)
}
func (rwf *rwFilter) DmConnectionCheckNamedValue(filterChain *filterChain, c *DmConnection, nv *driver.NamedValue) error {
return filterChain.DmConnectionCheckNamedValue(c, nv)
}
//DmStatement
func (rwf *rwFilter) DmStatementClose(filterChain *filterChain, s *DmStatement) error {
if RWUtil.isStandbyStatementValid(s) {
err := s.rwInfo.stmtStandby.close()
if err != nil {
RWUtil.afterExceptionOnStandby(s.dmConn, err)
}
}
return filterChain.DmStatementClose(s)
}
func (rwf *rwFilter) DmStatementNumInput(filterChain *filterChain, s *DmStatement) int {
return filterChain.DmStatementNumInput(s)
}
func (rwf *rwFilter) DmStatementExec(filterChain *filterChain, s *DmStatement, args []driver.Value) (*DmResult, error) {
ret, err := RWUtil.executeByStmt(s, func() (interface{}, error) {
RWUtil.distributeSqlByStmt(s, s.nativeSql)
return s.rwInfo.stmtCurrent.exec(args)
}, func(otherStmt *DmStatement) (interface{}, error) {
return otherStmt.exec(args)
})
if err != nil {
return nil, err
}
return ret.(*DmResult), nil
}
func (rwf *rwFilter) DmStatementExecContext(filterChain *filterChain, s *DmStatement, ctx context.Context, args []driver.NamedValue) (*DmResult, error) {
ret, err := RWUtil.executeByStmt(s, func() (interface{}, error) {
RWUtil.distributeSqlByStmt(s, s.nativeSql)
return s.rwInfo.stmtCurrent.execContext(ctx, args)
}, func(otherStmt *DmStatement) (interface{}, error) {
return otherStmt.execContext(ctx, args)
})
if err != nil {
return nil, err
}
return ret.(*DmResult), nil
}
func (rwf *rwFilter) DmStatementQuery(filterChain *filterChain, s *DmStatement, args []driver.Value) (*DmRows, error) {
ret, err := RWUtil.executeByStmt(s, func() (interface{}, error) {
RWUtil.distributeSqlByStmt(s, s.nativeSql)
return s.rwInfo.stmtCurrent.query(args)
}, func(otherStmt *DmStatement) (interface{}, error) {
return otherStmt.query(args)
})
if err != nil {
return nil, err
}
return ret.(*DmRows), nil
}
func (rwf *rwFilter) DmStatementQueryContext(filterChain *filterChain, s *DmStatement, ctx context.Context, args []driver.NamedValue) (*DmRows, error) {
ret, err := RWUtil.executeByStmt(s, func() (interface{}, error) {
RWUtil.distributeSqlByStmt(s, s.nativeSql)
return s.rwInfo.stmtCurrent.queryContext(ctx, args)
}, func(otherStmt *DmStatement) (interface{}, error) {
return otherStmt.queryContext(ctx, args)
})
if err != nil {
return nil, err
}
return ret.(*DmRows), nil
}
func (rwf *rwFilter) DmStatementCheckNamedValue(filterChain *filterChain, s *DmStatement, nv *driver.NamedValue) error {
return filterChain.DmStatementCheckNamedValue(s, nv)
}
//DmResult
func (rwf *rwFilter) DmResultLastInsertId(filterChain *filterChain, r *DmResult) (int64, error) {
return filterChain.DmResultLastInsertId(r)
}
func (rwf *rwFilter) DmResultRowsAffected(filterChain *filterChain, r *DmResult) (int64, error) {
return filterChain.DmResultRowsAffected(r)
}
//DmRows
func (rwf *rwFilter) DmRowsColumns(filterChain *filterChain, r *DmRows) []string {
return filterChain.DmRowsColumns(r)
}
func (rwf *rwFilter) DmRowsClose(filterChain *filterChain, r *DmRows) error {
return filterChain.DmRowsClose(r)
}
func (rwf *rwFilter) DmRowsNext(filterChain *filterChain, r *DmRows, dest []driver.Value) error {
return filterChain.DmRowsNext(r, dest)
}
func (rwf *rwFilter) DmRowsHasNextResultSet(filterChain *filterChain, r *DmRows) bool {
return filterChain.DmRowsHasNextResultSet(r)
}
func (rwf *rwFilter) DmRowsNextResultSet(filterChain *filterChain, r *DmRows) error {
return filterChain.DmRowsNextResultSet(r)
}
func (rwf *rwFilter) DmRowsColumnTypeScanType(filterChain *filterChain, r *DmRows, index int) reflect.Type {
return filterChain.DmRowsColumnTypeScanType(r, index)
}
func (rwf *rwFilter) DmRowsColumnTypeDatabaseTypeName(filterChain *filterChain, r *DmRows, index int) string {
return filterChain.DmRowsColumnTypeDatabaseTypeName(r, index)
}
func (rwf *rwFilter) DmRowsColumnTypeLength(filterChain *filterChain, r *DmRows, index int) (length int64, ok bool) {
return filterChain.DmRowsColumnTypeLength(r, index)
}
func (rwf *rwFilter) DmRowsColumnTypeNullable(filterChain *filterChain, r *DmRows, index int) (nullable, ok bool) {
return filterChain.DmRowsColumnTypeNullable(r, index)
}
func (rwf *rwFilter) DmRowsColumnTypePrecisionScale(filterChain *filterChain, r *DmRows, index int) (precision, scale int64, ok bool) {
return filterChain.DmRowsColumnTypePrecisionScale(r, index)
}