Skip to content

Commit

Permalink
alter sqlx.BindNamed to implicitly use question (breaking change)
Browse files Browse the repository at this point in the history
To get the old behavior, code must now rebind to the desired bindvar.
For example, if you want sqlx.DOLLAR bindvars, before your code was:

    sqlx.BindNamed(sqlx.DOLLAR, "query..", myarg)

Your new code will be:

    query, args, err := sqlx.BindNamed("query...", myarg)
    sqlx.Rebind(sqlx.DOLLAR, query)

This causes a secondary binding for non-QUESTION using databases, but
this is considered to be less important because:

 * consistently using QUESTION internally makes sqlx.In and
   sqlx.BindNamed composable
 * this function is not widely used and db.BindNamed/tx.BindNamed can
   use the internal APIs to skip the double binding
 * bindvar types are sort of 95% an internal concept
  • Loading branch information
jmoiron committed Apr 7, 2015
1 parent f488609 commit b0e8a20
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions named.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ func compileNamedQuery(qs []byte, bindType int) (query string, names []string, e
}

// Bind binds a struct or a map to a query with named parameters.
func BindNamed(bindType int, query string, arg interface{}) (string, []interface{}, error) {
return bindNamedMapper(bindType, query, arg, mapper())
func BindNamed(query string, arg interface{}) (string, []interface{}, error) {
return bindNamedMapper(QUESTION, query, arg, mapper())
}

func bindNamedMapper(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
Expand Down
6 changes: 4 additions & 2 deletions sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ func (db *DB) Unsafe() *DB {

// BindNamed binds a query using the DB driver's bindvar type.
func (db *DB) BindNamed(query string, arg interface{}) (string, []interface{}, error) {
return BindNamed(BindType(db.driverName), query, arg)
q, a, err := BindNamed(query, arg)
return db.Rebind(q), a, err
}

// NamedQuery using this DB.
Expand Down Expand Up @@ -377,7 +378,8 @@ func (tx *Tx) Unsafe() *Tx {

// BindNamed binds a query within a transaction's bindvar type.
func (tx *Tx) BindNamed(query string, arg interface{}) (string, []interface{}, error) {
return BindNamed(BindType(tx.driverName), query, arg)
q, a, err := BindNamed(query, arg)
return tx.Rebind(q), a, err
}

// NamedQuery within a transaction.
Expand Down

0 comments on commit b0e8a20

Please sign in to comment.