go.undefinedlabs.com/scopeagent@v0.4.2/instrumentation/sql/stmt.go (about) 1 package sql 2 3 import ( 4 "context" 5 "database/sql/driver" 6 ) 7 8 type instrumentedStmt struct { 9 stmt driver.Stmt 10 configuration *driverConfiguration 11 } 12 13 // Close closes the statement. 14 // 15 // As of Go 1.1, a Stmt will not be closed if it's in use 16 // by any queries. 17 func (s *instrumentedStmt) Close() error { 18 return s.stmt.Close() 19 } 20 21 // NumInput returns the number of placeholder parameters. 22 // 23 // If NumInput returns >= 0, the sql package will sanity check 24 // argument counts from callers and return errors to the caller 25 // before the statement's Exec or Query methods are called. 26 // 27 // NumInput may also return -1, if the driver doesn't know 28 // its number of placeholders. In that case, the sql package 29 // will not sanity check Exec or Query argument counts. 30 func (s *instrumentedStmt) NumInput() int { 31 return s.stmt.NumInput() 32 } 33 34 // Exec executes a query that doesn't return rows, such 35 // as an INSERT or UPDATE. 36 // 37 // Deprecated: Drivers should implement StmtExecContext instead (or additionally). 38 func (s *instrumentedStmt) Exec(args []driver.Value) (driver.Result, error) { 39 return s.stmt.Exec(args) 40 } 41 42 // Query executes a query that may return rows, such as a 43 // SELECT. 44 // 45 // Deprecated: Drivers should implement StmtQueryContext instead (or additionally). 46 func (s *instrumentedStmt) Query(args []driver.Value) (driver.Rows, error) { 47 return s.stmt.Query(args) 48 } 49 50 // ExecContext executes a query that doesn't return rows, such 51 // as an INSERT or UPDATE. 52 // 53 // ExecContext must honor the context timeout and return when it is canceled. 54 func (s *instrumentedStmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { 55 span := s.configuration.newSpan("ExecContext", query, args, s.configuration, ctx) 56 defer span.Finish() 57 if execerContext, ok := s.stmt.(driver.ExecerContext); ok { 58 return execerContext.ExecContext(ctx, query, args) 59 } 60 values, err := namedValueToValue(args) 61 if err != nil { 62 return nil, err 63 } 64 return s.Exec(values) 65 } 66 67 // QueryContext executes a query that may return rows, such as a 68 // SELECT. 69 // 70 // QueryContext must honor the context timeout and return when it is canceled. 71 func (s *instrumentedStmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (rows driver.Rows, err error) { 72 span := s.configuration.newSpan("QueryContext", query, args, s.configuration, ctx) 73 defer span.Finish() 74 if queryerContext, ok := s.stmt.(driver.QueryerContext); ok { 75 rows, err := queryerContext.QueryContext(ctx, query, args) 76 return rows, err 77 } 78 values, err := namedValueToValue(args) 79 if err != nil { 80 return nil, err 81 } 82 return s.Query(values) 83 }