gitee.com/curryzheng/dm@v0.0.1/zh.go (about)

     1  /*
     2   * Copyright (c) 2000-2018, 达梦数据库有限公司.
     3   * All rights reserved.
     4   */
     5  
     6  package dm
     7  
     8  import (
     9  	"context"
    10  	"database/sql/driver"
    11  	"reflect"
    12  )
    13  
    14  type rwFilter struct {
    15  }
    16  
    17  //DmDriver
    18  func (rwf *rwFilter) DmDriverOpen(filterChain *filterChain, d *DmDriver, dsn string) (*DmConnection, error) {
    19  	return filterChain.DmDriverOpen(d, dsn)
    20  }
    21  
    22  func (rwf *rwFilter) DmDriverOpenConnector(filterChain *filterChain, d *DmDriver, dsn string) (*DmConnector, error) {
    23  	return filterChain.DmDriverOpenConnector(d, dsn)
    24  }
    25  
    26  //DmConnector
    27  func (rwf *rwFilter) DmConnectorConnect(filterChain *filterChain, c *DmConnector, ctx context.Context) (*DmConnection, error) {
    28  	return RWUtil.connect(c, ctx)
    29  }
    30  
    31  func (rwf *rwFilter) DmConnectorDriver(filterChain *filterChain, c *DmConnector) *DmDriver {
    32  	return filterChain.DmConnectorDriver(c)
    33  }
    34  
    35  //DmConnection
    36  func (rwf *rwFilter) DmConnectionBegin(filterChain *filterChain, c *DmConnection) (*DmConnection, error) {
    37  	if RWUtil.isStandbyAlive(c) {
    38  		_, err := c.rwInfo.connStandby.begin()
    39  		if err != nil {
    40  			RWUtil.afterExceptionOnStandby(c, err)
    41  		}
    42  	}
    43  
    44  	return filterChain.DmConnectionBegin(c)
    45  }
    46  
    47  func (rwf *rwFilter) DmConnectionBeginTx(filterChain *filterChain, c *DmConnection, ctx context.Context, opts driver.TxOptions) (*DmConnection, error) {
    48  	if RWUtil.isStandbyAlive(c) {
    49  		_, err := c.rwInfo.connStandby.beginTx(ctx, opts)
    50  		if err != nil {
    51  			RWUtil.afterExceptionOnStandby(c, err)
    52  		}
    53  	}
    54  
    55  	return filterChain.DmConnectionBeginTx(c, ctx, opts)
    56  }
    57  
    58  func (rwf *rwFilter) DmConnectionCommit(filterChain *filterChain, c *DmConnection) error {
    59  	if RWUtil.isStandbyAlive(c) {
    60  		err := c.rwInfo.connStandby.commit()
    61  		if err != nil {
    62  			RWUtil.afterExceptionOnStandby(c, err)
    63  		}
    64  	}
    65  
    66  	return filterChain.DmConnectionCommit(c)
    67  }
    68  
    69  func (rwf *rwFilter) DmConnectionRollback(filterChain *filterChain, c *DmConnection) error {
    70  	if RWUtil.isStandbyAlive(c) {
    71  		err := c.rwInfo.connStandby.rollback()
    72  		if err != nil {
    73  			RWUtil.afterExceptionOnStandby(c, err)
    74  		}
    75  	}
    76  
    77  	return filterChain.DmConnectionRollback(c)
    78  }
    79  
    80  func (rwf *rwFilter) DmConnectionClose(filterChain *filterChain, c *DmConnection) error {
    81  	if RWUtil.isStandbyAlive(c) {
    82  		err := c.rwInfo.connStandby.close()
    83  		if err != nil {
    84  			RWUtil.afterExceptionOnStandby(c, err)
    85  		}
    86  	}
    87  
    88  	return filterChain.DmConnectionClose(c)
    89  }
    90  
    91  func (rwf *rwFilter) DmConnectionPing(filterChain *filterChain, c *DmConnection, ctx context.Context) error {
    92  	return filterChain.DmConnectionPing(c, ctx)
    93  }
    94  
    95  func (rwf *rwFilter) DmConnectionExec(filterChain *filterChain, c *DmConnection, query string, args []driver.Value) (*DmResult, error) {
    96  	ret, err := RWUtil.executeByConn(c, query, func() (interface{}, error) {
    97  		return c.rwInfo.connCurrent.exec(query, args)
    98  	}, func(otherConn *DmConnection) (interface{}, error) {
    99  		return otherConn.exec(query, args)
   100  	})
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  	return ret.(*DmResult), nil
   105  }
   106  
   107  func (rwf *rwFilter) DmConnectionExecContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string, args []driver.NamedValue) (*DmResult, error) {
   108  	ret, err := RWUtil.executeByConn(c, query, func() (interface{}, error) {
   109  		return c.rwInfo.connCurrent.execContext(ctx, query, args)
   110  	}, func(otherConn *DmConnection) (interface{}, error) {
   111  		return otherConn.execContext(ctx, query, args)
   112  	})
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	return ret.(*DmResult), nil
   117  }
   118  
   119  func (rwf *rwFilter) DmConnectionQuery(filterChain *filterChain, c *DmConnection, query string, args []driver.Value) (*DmRows, error) {
   120  	ret, err := RWUtil.executeByConn(c, query, func() (interface{}, error) {
   121  		return c.rwInfo.connCurrent.query(query, args)
   122  	}, func(otherConn *DmConnection) (interface{}, error) {
   123  		return otherConn.query(query, args)
   124  	})
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  	return ret.(*DmRows), nil
   129  }
   130  
   131  func (rwf *rwFilter) DmConnectionQueryContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string, args []driver.NamedValue) (*DmRows, error) {
   132  	ret, err := RWUtil.executeByConn(c, query, func() (interface{}, error) {
   133  		return c.rwInfo.connCurrent.queryContext(ctx, query, args)
   134  	}, func(otherConn *DmConnection) (interface{}, error) {
   135  		return otherConn.queryContext(ctx, query, args)
   136  	})
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  	return ret.(*DmRows), nil
   141  }
   142  
   143  func (rwf *rwFilter) DmConnectionPrepare(filterChain *filterChain, c *DmConnection, query string) (*DmStatement, error) {
   144  	stmt, err := c.prepare(query)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	stmt.rwInfo.stmtCurrent = stmt
   149  	stmt.rwInfo.readOnly = RWUtil.checkReadonlyByStmt(stmt)
   150  	if RWUtil.isCreateStandbyStmt(stmt) {
   151  		stmt.rwInfo.stmtStandby, err = c.rwInfo.connStandby.prepare(query)
   152  		if err == nil {
   153  			stmt.rwInfo.stmtCurrent = stmt.rwInfo.stmtStandby
   154  		} else {
   155  			RWUtil.afterExceptionOnStandby(c, err)
   156  		}
   157  	}
   158  	return stmt, nil
   159  }
   160  
   161  func (rwf *rwFilter) DmConnectionPrepareContext(filterChain *filterChain, c *DmConnection, ctx context.Context, query string) (*DmStatement, error) {
   162  	stmt, err := c.prepareContext(ctx, query)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  	stmt.rwInfo.stmtCurrent = stmt
   167  	stmt.rwInfo.readOnly = RWUtil.checkReadonlyByStmt(stmt)
   168  	if RWUtil.isCreateStandbyStmt(stmt) {
   169  		stmt.rwInfo.stmtStandby, err = c.rwInfo.connStandby.prepareContext(ctx, query)
   170  		if err == nil {
   171  			stmt.rwInfo.stmtCurrent = stmt.rwInfo.stmtStandby
   172  		} else {
   173  			RWUtil.afterExceptionOnStandby(c, err)
   174  		}
   175  	}
   176  	return stmt, nil
   177  }
   178  
   179  func (rwf *rwFilter) DmConnectionResetSession(filterChain *filterChain, c *DmConnection, ctx context.Context) error {
   180  	if RWUtil.isStandbyAlive(c) {
   181  		err := c.rwInfo.connStandby.resetSession(ctx)
   182  		if err != nil {
   183  			RWUtil.afterExceptionOnStandby(c, err)
   184  		}
   185  	}
   186  
   187  	return filterChain.DmConnectionResetSession(c, ctx)
   188  }
   189  
   190  func (rwf *rwFilter) DmConnectionCheckNamedValue(filterChain *filterChain, c *DmConnection, nv *driver.NamedValue) error {
   191  	return filterChain.DmConnectionCheckNamedValue(c, nv)
   192  }
   193  
   194  //DmStatement
   195  func (rwf *rwFilter) DmStatementClose(filterChain *filterChain, s *DmStatement) error {
   196  	if RWUtil.isStandbyStatementValid(s) {
   197  		err := s.rwInfo.stmtStandby.close()
   198  		if err != nil {
   199  			RWUtil.afterExceptionOnStandby(s.dmConn, err)
   200  		}
   201  	}
   202  
   203  	return filterChain.DmStatementClose(s)
   204  }
   205  
   206  func (rwf *rwFilter) DmStatementNumInput(filterChain *filterChain, s *DmStatement) int {
   207  	return filterChain.DmStatementNumInput(s)
   208  }
   209  
   210  func (rwf *rwFilter) DmStatementExec(filterChain *filterChain, s *DmStatement, args []driver.Value) (*DmResult, error) {
   211  	ret, err := RWUtil.executeByStmt(s, func() (interface{}, error) {
   212  		return s.rwInfo.stmtCurrent.exec(args)
   213  	}, func(otherStmt *DmStatement) (interface{}, error) {
   214  		return otherStmt.exec(args)
   215  	})
   216  	if err != nil {
   217  		return nil, err
   218  	}
   219  	return ret.(*DmResult), nil
   220  }
   221  
   222  func (rwf *rwFilter) DmStatementExecContext(filterChain *filterChain, s *DmStatement, ctx context.Context, args []driver.NamedValue) (*DmResult, error) {
   223  	ret, err := RWUtil.executeByStmt(s, func() (interface{}, error) {
   224  		return s.rwInfo.stmtCurrent.execContext(ctx, args)
   225  	}, func(otherStmt *DmStatement) (interface{}, error) {
   226  		return otherStmt.execContext(ctx, args)
   227  	})
   228  	if err != nil {
   229  		return nil, err
   230  	}
   231  	return ret.(*DmResult), nil
   232  }
   233  
   234  func (rwf *rwFilter) DmStatementQuery(filterChain *filterChain, s *DmStatement, args []driver.Value) (*DmRows, error) {
   235  	ret, err := RWUtil.executeByStmt(s, func() (interface{}, error) {
   236  		return s.rwInfo.stmtCurrent.query(args)
   237  	}, func(otherStmt *DmStatement) (interface{}, error) {
   238  		return otherStmt.query(args)
   239  	})
   240  	if err != nil {
   241  		return nil, err
   242  	}
   243  	return ret.(*DmRows), nil
   244  }
   245  
   246  func (rwf *rwFilter) DmStatementQueryContext(filterChain *filterChain, s *DmStatement, ctx context.Context, args []driver.NamedValue) (*DmRows, error) {
   247  	ret, err := RWUtil.executeByStmt(s, func() (interface{}, error) {
   248  		return s.rwInfo.stmtCurrent.queryContext(ctx, args)
   249  	}, func(otherStmt *DmStatement) (interface{}, error) {
   250  		return otherStmt.queryContext(ctx, args)
   251  	})
   252  	if err != nil {
   253  		return nil, err
   254  	}
   255  	return ret.(*DmRows), nil
   256  }
   257  
   258  func (rwf *rwFilter) DmStatementCheckNamedValue(filterChain *filterChain, s *DmStatement, nv *driver.NamedValue) error {
   259  	return filterChain.DmStatementCheckNamedValue(s, nv)
   260  }
   261  
   262  //DmResult
   263  func (rwf *rwFilter) DmResultLastInsertId(filterChain *filterChain, r *DmResult) (int64, error) {
   264  	return filterChain.DmResultLastInsertId(r)
   265  }
   266  
   267  func (rwf *rwFilter) DmResultRowsAffected(filterChain *filterChain, r *DmResult) (int64, error) {
   268  	return filterChain.DmResultRowsAffected(r)
   269  }
   270  
   271  //DmRows
   272  func (rwf *rwFilter) DmRowsColumns(filterChain *filterChain, r *DmRows) []string {
   273  	return filterChain.DmRowsColumns(r)
   274  }
   275  
   276  func (rwf *rwFilter) DmRowsClose(filterChain *filterChain, r *DmRows) error {
   277  	return filterChain.DmRowsClose(r)
   278  }
   279  
   280  func (rwf *rwFilter) DmRowsNext(filterChain *filterChain, r *DmRows, dest []driver.Value) error {
   281  	return filterChain.DmRowsNext(r, dest)
   282  }
   283  
   284  func (rwf *rwFilter) DmRowsHasNextResultSet(filterChain *filterChain, r *DmRows) bool {
   285  	return filterChain.DmRowsHasNextResultSet(r)
   286  }
   287  
   288  func (rwf *rwFilter) DmRowsNextResultSet(filterChain *filterChain, r *DmRows) error {
   289  	return filterChain.DmRowsNextResultSet(r)
   290  }
   291  
   292  func (rwf *rwFilter) DmRowsColumnTypeScanType(filterChain *filterChain, r *DmRows, index int) reflect.Type {
   293  	return filterChain.DmRowsColumnTypeScanType(r, index)
   294  }
   295  
   296  func (rwf *rwFilter) DmRowsColumnTypeDatabaseTypeName(filterChain *filterChain, r *DmRows, index int) string {
   297  	return filterChain.DmRowsColumnTypeDatabaseTypeName(r, index)
   298  }
   299  
   300  func (rwf *rwFilter) DmRowsColumnTypeLength(filterChain *filterChain, r *DmRows, index int) (length int64, ok bool) {
   301  	return filterChain.DmRowsColumnTypeLength(r, index)
   302  }
   303  
   304  func (rwf *rwFilter) DmRowsColumnTypeNullable(filterChain *filterChain, r *DmRows, index int) (nullable, ok bool) {
   305  	return filterChain.DmRowsColumnTypeNullable(r, index)
   306  }
   307  
   308  func (rwf *rwFilter) DmRowsColumnTypePrecisionScale(filterChain *filterChain, r *DmRows, index int) (precision, scale int64, ok bool) {
   309  	return filterChain.DmRowsColumnTypePrecisionScale(r, index)
   310  }