github.com/RevenueMonster/sqlike@v1.0.6/sql/instrumented/interceptor.go (about) 1 package instrumented 2 3 import ( 4 "context" 5 "database/sql/driver" 6 ) 7 8 // Interceptor : 9 type Interceptor interface { 10 // Connection interceptors 11 ConnPing(context.Context, driver.Pinger) error 12 ConnBeginTx(context.Context, driver.ConnBeginTx, driver.TxOptions) (driver.Tx, error) 13 ConnPrepareContext(context.Context, driver.ConnPrepareContext, string) (driver.Stmt, error) 14 ConnExecContext(context.Context, driver.ExecerContext, string, []driver.NamedValue) (driver.Result, error) 15 ConnQueryContext(context.Context, driver.QueryerContext, string, []driver.NamedValue) (driver.Rows, error) 16 17 // Connector interceptors 18 // ConnectorConnect(context.Context, driver.Connector) (driver.Conn, error) 19 20 // Results interceptors 21 ResultLastInsertId(context.Context, driver.Result) (int64, error) 22 ResultRowsAffected(context.Context, driver.Result) (int64, error) 23 24 // Rows interceptors 25 RowsNext(context.Context, driver.Rows, []driver.Value) error 26 27 // Stmt interceptors 28 StmtExecContext(context.Context, driver.StmtExecContext, string, []driver.NamedValue) (driver.Result, error) 29 StmtQueryContext(context.Context, driver.StmtQueryContext, string, []driver.NamedValue) (driver.Rows, error) 30 StmtClose(context.Context, driver.Stmt) error 31 32 // Tx interceptors 33 TxCommit(context.Context, driver.Tx) error 34 TxRollback(context.Context, driver.Tx) error 35 } 36 37 var _ Interceptor = NullInterceptor{} 38 39 // NullInterceptor is a complete passthrough interceptor that implements every method of the Interceptor 40 // interface and performs no additional logic. Users should Embed it in their own interceptor so that they 41 // only need to define the specific functions they are interested in intercepting. 42 type NullInterceptor struct{} 43 44 // ConnBeginTx : 45 func (NullInterceptor) ConnBeginTx(ctx context.Context, conn driver.ConnBeginTx, txOpts driver.TxOptions) (driver.Tx, error) { 46 return conn.BeginTx(ctx, txOpts) 47 } 48 49 // ConnPrepareContext : 50 func (NullInterceptor) ConnPrepareContext(ctx context.Context, conn driver.ConnPrepareContext, query string) (driver.Stmt, error) { 51 return conn.PrepareContext(ctx, query) 52 } 53 54 // ConnPing : 55 func (NullInterceptor) ConnPing(ctx context.Context, conn driver.Pinger) error { 56 return conn.Ping(ctx) 57 } 58 59 // ConnExecContext : 60 func (NullInterceptor) ConnExecContext(ctx context.Context, conn driver.ExecerContext, query string, args []driver.NamedValue) (driver.Result, error) { 61 return conn.ExecContext(ctx, query, args) 62 } 63 64 // ConnQueryContext : 65 func (NullInterceptor) ConnQueryContext(ctx context.Context, conn driver.QueryerContext, query string, args []driver.NamedValue) (driver.Rows, error) { 66 return conn.QueryContext(ctx, query, args) 67 } 68 69 // func (NullInterceptor) ConnectorConnect(ctx context.Context, connect driver.Connector) (driver.Conn, error) { 70 // return connect.Connect(ctx) 71 // } 72 73 // ResultLastInsertId : 74 func (NullInterceptor) ResultLastInsertId(ctx context.Context, res driver.Result) (int64, error) { 75 return res.LastInsertId() 76 } 77 78 // ResultRowsAffected : 79 func (NullInterceptor) ResultRowsAffected(ctx context.Context, res driver.Result) (int64, error) { 80 return res.RowsAffected() 81 } 82 83 // RowsNext : 84 func (NullInterceptor) RowsNext(ctx context.Context, rows driver.Rows, dest []driver.Value) error { 85 return rows.Next(dest) 86 } 87 88 // StmtExecContext : 89 func (NullInterceptor) StmtExecContext(ctx context.Context, stmt driver.StmtExecContext, _ string, args []driver.NamedValue) (driver.Result, error) { 90 return stmt.ExecContext(ctx, args) 91 } 92 93 // StmtQueryContext : 94 func (NullInterceptor) StmtQueryContext(ctx context.Context, stmt driver.StmtQueryContext, _ string, args []driver.NamedValue) (driver.Rows, error) { 95 return stmt.QueryContext(ctx, args) 96 } 97 98 // StmtClose : 99 func (NullInterceptor) StmtClose(ctx context.Context, stmt driver.Stmt) error { 100 return stmt.Close() 101 } 102 103 // TxCommit : 104 func (NullInterceptor) TxCommit(ctx context.Context, tx driver.Tx) error { 105 return tx.Commit() 106 } 107 108 // TxRollback : 109 func (NullInterceptor) TxRollback(ctx context.Context, tx driver.Tx) error { 110 return tx.Rollback() 111 }