Skip to content

Commit

Permalink
Merge 78688d8 into 2ba0fc6
Browse files Browse the repository at this point in the history
  • Loading branch information
prasad83 committed Mar 4, 2020
2 parents 2ba0fc6 + 78688d8 commit 56fc880
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
29 changes: 29 additions & 0 deletions named.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,32 @@ func NamedExec(e Ext, query string, arg interface{}) (sql.Result, error) {
}
return e.Exec(q, args...)
}

// NamedSelect binds a named query and then runs Query on the result using the
// provided Ext (sqlx.Tx, sql.DB) and StructScans each row
// into dest, which must be a slice. If the slice elements are scannable, then
// the result set must have only one column. Otherwise, StructScan is used.
// The *sql.Rows are closed automatically.
func NamedSelect(e Ext, dest interface{}, query string, arg interface{}) error {
rows, err := NamedQuery(e, query, arg)
if err != nil {
return err
}
defer rows.Close()
return scanAll(rows, dest, false)
}

// NamedGet binds a named query and then runs Query on the result using the
// provided Ext (sqlx.Tx, sql.DB) and StructScan the row into dest.
// The *sql.Rows are closed automatically.
func NamedGet(e Ext, dest interface{}, query string, arg interface{}) error {
rows, err := NamedQuery(e, query, arg)
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
return rows.StructScan(dest)
}
return nil
}
19 changes: 19 additions & 0 deletions sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,26 @@ func (db *DB) Select(dest interface{}, query string, args ...interface{}) error
return Select(db, dest, query, args...)
}

// NamedSelect using this DB.
// Any named placeholder parameters are replaced with fields from arg.
func (db *DB) NamedSelect(dest interface{}, query string, arg interface{}) error {
return NamedSelect(db, dest, query, arg)
}

// Get using this DB.
// Any placeholder parameters are replaced with supplied args.
// An error is returned if the result set is empty.
func (db *DB) Get(dest interface{}, query string, args ...interface{}) error {
return Get(db, dest, query, args...)
}

// Get using this DB.
// Any named placeholder parameters are replaced with fields from arg.
// An error is returned if the result set is empty.
func (db *DB) NamedGet(dest interface{}, query string, arg map[string]interface{}) error {
return NamedGet(db, dest, query, arg)
}

// MustBegin starts a transaction, and panics on error. Returns an *sqlx.Tx instead
// of an *sql.Tx.
func (db *DB) MustBegin() *Tx {
Expand Down Expand Up @@ -427,6 +440,12 @@ func (tx *Tx) Select(dest interface{}, query string, args ...interface{}) error
return Select(tx, dest, query, args...)
}

// NamedSelect a named query within a transaction.
// Any named placeholder parameters are replaced with fields from arg.
func (tx *Tx) NamedSelect(dest interface{}, query string, arg interface{}) error {
return NamedSelect(tx, dest, query, arg)
}

// Queryx within a transaction.
// Any placeholder parameters are replaced with supplied args.
func (tx *Tx) Queryx(query string, args ...interface{}) (*Rows, error) {
Expand Down

0 comments on commit 56fc880

Please sign in to comment.