| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- /*
- * 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) {
- return RWUtil.connect(c, ctx)
- }
- 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, query, func() (interface{}, error) {
- 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, query, func() (interface{}, error) {
- 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, query, func() (interface{}, error) {
- 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, query, func() (interface{}, error) {
- 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) {
- stmt, err := c.prepare(query)
- if err != nil {
- return nil, err
- }
- stmt.rwInfo.stmtCurrent = stmt
- stmt.rwInfo.readOnly = RWUtil.checkReadonlyByStmt(stmt)
- if RWUtil.isCreateStandbyStmt(stmt) {
- stmt.rwInfo.stmtStandby, err = c.rwInfo.connStandby.prepare(query)
- if err == nil {
- stmt.rwInfo.stmtCurrent = stmt.rwInfo.stmtStandby
- } else {
- RWUtil.afterExceptionOnStandby(c, err)
- }
- }
- return stmt, nil
- }
- func (rwf *rwFilter) DmConnectionPrepareContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string) (*DmStatement, error) {
- stmt, err := c.prepareContext(ctx, query)
- if err != nil {
- return nil, err
- }
- stmt.rwInfo.stmtCurrent = stmt
- stmt.rwInfo.readOnly = RWUtil.checkReadonlyByStmt(stmt)
- if RWUtil.isCreateStandbyStmt(stmt) {
- stmt.rwInfo.stmtStandby, err = c.rwInfo.connStandby.prepareContext(ctx, query)
- if err == nil {
- stmt.rwInfo.stmtCurrent = stmt.rwInfo.stmtStandby
- } else {
- RWUtil.afterExceptionOnStandby(c, err)
- }
- }
- 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) {
- 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) {
- 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) {
- 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) {
- 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)
- }
|