|
| 1 | +// +build go1.8 |
| 2 | + |
| 3 | +package sqlx |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "database/sql" |
| 8 | +) |
| 9 | + |
| 10 | +// A union interface of contextPreparer and binder, required to be able to |
| 11 | +// prepare named statements with context (as the bindtype must be determined). |
| 12 | +type namedPreparerContext interface { |
| 13 | + PreparerContext |
| 14 | + binder |
| 15 | +} |
| 16 | + |
| 17 | +func prepareNamedContext(ctx context.Context, p namedPreparerContext, query string) (*NamedStmt, error) { |
| 18 | + bindType := BindType(p.DriverName()) |
| 19 | + q, args, err := compileNamedQuery([]byte(query), bindType) |
| 20 | + if err != nil { |
| 21 | + return nil, err |
| 22 | + } |
| 23 | + stmt, err := PreparexContext(ctx, p, q) |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + return &NamedStmt{ |
| 28 | + QueryString: q, |
| 29 | + Params: args, |
| 30 | + Stmt: stmt, |
| 31 | + }, nil |
| 32 | +} |
| 33 | + |
| 34 | +// ExecContext executes a named statement using the struct passed. |
| 35 | +// Any named placeholder parameters are replaced with fields from arg. |
| 36 | +func (n *NamedStmt) ExecContext(ctx context.Context, arg interface{}) (sql.Result, error) { |
| 37 | + args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper) |
| 38 | + if err != nil { |
| 39 | + return *new(sql.Result), err |
| 40 | + } |
| 41 | + return n.Stmt.ExecContext(ctx, args...) |
| 42 | +} |
| 43 | + |
| 44 | +// QueryContext executes a named statement using the struct argument, returning rows. |
| 45 | +// Any named placeholder parameters are replaced with fields from arg. |
| 46 | +func (n *NamedStmt) QueryContext(ctx context.Context, arg interface{}) (*sql.Rows, error) { |
| 47 | + args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + return n.Stmt.QueryContext(ctx, args...) |
| 52 | +} |
| 53 | + |
| 54 | +// QueryRowContext executes a named statement against the database. Because sqlx cannot |
| 55 | +// create a *sql.Row with an error condition pre-set for binding errors, sqlx |
| 56 | +// returns a *sqlx.Row instead. |
| 57 | +// Any named placeholder parameters are replaced with fields from arg. |
| 58 | +func (n *NamedStmt) QueryRowContext(ctx context.Context, arg interface{}) *Row { |
| 59 | + args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper) |
| 60 | + if err != nil { |
| 61 | + return &Row{err: err} |
| 62 | + } |
| 63 | + return n.Stmt.QueryRowxContext(ctx, args...) |
| 64 | +} |
| 65 | + |
| 66 | +// MustExecContext execs a NamedStmt, panicing on error |
| 67 | +// Any named placeholder parameters are replaced with fields from arg. |
| 68 | +func (n *NamedStmt) MustExecContext(ctx context.Context, arg interface{}) sql.Result { |
| 69 | + res, err := n.ExecContext(ctx, arg) |
| 70 | + if err != nil { |
| 71 | + panic(err) |
| 72 | + } |
| 73 | + return res |
| 74 | +} |
| 75 | + |
| 76 | +// QueryxContext using this NamedStmt |
| 77 | +// Any named placeholder parameters are replaced with fields from arg. |
| 78 | +func (n *NamedStmt) QueryxContext(ctx context.Context, arg interface{}) (*Rows, error) { |
| 79 | + r, err := n.QueryContext(ctx, arg) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + return &Rows{Rows: r, Mapper: n.Stmt.Mapper, unsafe: isUnsafe(n)}, err |
| 84 | +} |
| 85 | + |
| 86 | +// QueryRowxContext this NamedStmt. Because of limitations with QueryRow, this is |
| 87 | +// an alias for QueryRow. |
| 88 | +// Any named placeholder parameters are replaced with fields from arg. |
| 89 | +func (n *NamedStmt) QueryRowxContext(ctx context.Context, arg interface{}) *Row { |
| 90 | + return n.QueryRowContext(ctx, arg) |
| 91 | +} |
| 92 | + |
| 93 | +// SelectContext using this NamedStmt |
| 94 | +// Any named placeholder parameters are replaced with fields from arg. |
| 95 | +func (n *NamedStmt) SelectContext(ctx context.Context, dest interface{}, arg interface{}) error { |
| 96 | + rows, err := n.QueryxContext(ctx, arg) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + // if something happens here, we want to make sure the rows are Closed |
| 101 | + defer rows.Close() |
| 102 | + return scanAll(rows, dest, false) |
| 103 | +} |
| 104 | + |
| 105 | +// GetContext using this NamedStmt |
| 106 | +// Any named placeholder parameters are replaced with fields from arg. |
| 107 | +func (n *NamedStmt) GetContext(ctx context.Context, dest interface{}, arg interface{}) error { |
| 108 | + r := n.QueryRowxContext(ctx, arg) |
| 109 | + return r.scanAny(dest, false) |
| 110 | +} |
| 111 | + |
| 112 | +// NamedQueryContext binds a named query and then runs Query on the result using the |
| 113 | +// provided Ext (sqlx.Tx, sqlx.Db). It works with both structs and with |
| 114 | +// map[string]interface{} types. |
| 115 | +func NamedQueryContext(ctx context.Context, e ExtContext, query string, arg interface{}) (*Rows, error) { |
| 116 | + q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e)) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + return e.QueryxContext(ctx, q, args...) |
| 121 | +} |
| 122 | + |
| 123 | +// NamedExecContext uses BindStruct to get a query executable by the driver and |
| 124 | +// then runs Exec on the result. Returns an error from the binding |
| 125 | +// or the query excution itself. |
| 126 | +func NamedExecContext(ctx context.Context, e ExtContext, query string, arg interface{}) (sql.Result, error) { |
| 127 | + q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e)) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + return e.ExecContext(ctx, q, args...) |
| 132 | +} |
0 commit comments