Skip to content

Commit

Permalink
Fill out API with Selectf/v, add MustExec alias for Execp as MustFunc…
Browse files Browse the repository at this point in the history
… is more go-idiomatic
  • Loading branch information
jmoiron committed May 12, 2013
1 parent a92a00a commit 859ec15
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
31 changes: 31 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// General purpose extensions to database/sql
//
// sqlx is intended to seamlessly wrap database/sql and provide some convenience
// methods which range from basic common error handling techniques to complex
// reflect-base Scan extensions. Replacing `sql.Open` with `sqlx.Open` should
// provide access to most of the features within sqlx while not changing the
// interface used by any existing code.
//
// sqlx introduces the following concepts which are accessible wherever they
// make sense:
//
// The addition of a mnemonic set of "Exec" functions:
//
// Execv - log.Println errors, return (rows, err)
// Execl - log.Println errors, return only rows
// Execp - panic(err) on error
// Execf - log.Fatal(err) on error
// MustExec - same as Execp
//
// The addition of a "StructScan" function, which takes an the result from a
// query and a struct slice and automatically scans each row as a struct.
//
// The addition of a set of "Select" functions, which combine Query and
// StructScan and have "f" and "v" error handling variantes like Exec.
//
// A "LoadFile" convenience function which executes the queries in a file.
//
// Panicing variants of Connect and Begin: MustConnect, MustBegin.
//

package sqlx
60 changes: 50 additions & 10 deletions sqlx.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
// General purpose extensions to database/sql
//
// sqlx is intended to seamlessly wrap database/sql and provide some convenience
// methods which range from basic common error handling techniques to complex
// reflect-base Scan extensions. Replacing `sql.Open` with `sqlx.Open` should
// provide access to most of the features within sqlx while not changing the
// interface used by any existing code.
//
package sqlx

import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"log"
"path/filepath"
Expand Down Expand Up @@ -102,6 +95,12 @@ func (db *DB) Execp(query string, args ...interface{}) sql.Result {
return Execp(db, query, args...)
}

// MustExec ("panic") runs MustExec using this database.
func (db *DB) MustExec(query string, args ...interface{}) sql.Result {
return MustExec(db, query, args...)
}

// Preparex returns an sqlx.Stmt instead of a sql.Stmt
func (db *DB) Preparex(query string) (*Stmt, error) {
return Preparex(db, query)
}
Expand All @@ -119,6 +118,16 @@ func (tx *Tx) Select(dest interface{}, query string, args ...interface{}) error
return Select(tx, dest, query, args...)
}

// Call Select using this transaction to issue the Query.
func (tx *Tx) Selectv(dest interface{}, query string, args ...interface{}) error {
return Selectv(tx, dest, query, args...)
}

// Call Selectf using this transaction to issue the Query.
func (tx *Tx) Selectf(dest interface{}, query string, args ...interface{}) {
Selectf(tx, dest, query, args...)
}

// Execv ("verbose") runs Execv using this transaction.
func (tx *Tx) Execv(query string, args ...interface{}) (sql.Result, error) {
return Execv(tx, query, args...)
Expand All @@ -139,6 +148,11 @@ func (tx *Tx) Execp(query string, args ...interface{}) sql.Result {
return Execp(tx, query, args...)
}

// MustExec ("panic") runs MustExec using this transaction.
func (tx *Tx) MustExec(query string, args ...interface{}) sql.Result {
return MustExec(tx, query, args...)
}

func (tx *Tx) Preparex(query string) (*Stmt, error) {
st, err := tx.Tx.Prepare(query)
return &Stmt{*st}, err
Expand Down Expand Up @@ -180,6 +194,16 @@ func (s *Stmt) Select(dest interface{}, args ...interface{}) error {
return Select(&qStmt{*s}, dest, "", args...)
}

// Call Selectv using this statement to issue the Query.
func (s *Stmt) Selectv(dest interface{}, args ...interface{}) error {
return Selectv(&qStmt{*s}, dest, "", args...)
}

// Call Selectf using this statement to issue the Query.
func (s *Stmt) Selectf(dest interface{}, args ...interface{}) {
Selectf(&qStmt{*s}, dest, "", args...)
}

// Execv ("verbose") runs Execv using this statement. Note that the query is
// not recoverable once a statement has been prepared, so the query portion
// will be blank.
Expand All @@ -206,6 +230,11 @@ func (s *Stmt) Execp(args ...interface{}) sql.Result {
return Execp(&qStmt{*s}, "", args...)
}

// MustExec ("panic") runs MustExec using this statement.
func (s *Stmt) MustExec(args ...interface{}) sql.Result {
return MustExec(&qStmt{*s}, "", args...)
}

// Like sql.Rows.Scan, but scans a single Row into a single Struct. Use this
// and iterate over Rows manually when the memory load of Select() might be
// prohibitive. *Rows.StructScan caches the reflect work of matching up
Expand Down Expand Up @@ -309,7 +338,7 @@ func Selectf(q Querier, dest interface{}, query string, args ...interface{}) {
// errors can be encountered locating or reading the file, before a Result is
// created. LoadFile reads the entire file into memory, so it is not suitable
// for loading large data dumps, but can be useful for initializing database
// schemas or loading indexes.
// schemas or loading indexes.
func LoadFile(e Execer, path string) (*sql.Result, error) {
realpath, err := filepath.Abs(path)
if err != nil {
Expand Down Expand Up @@ -369,6 +398,16 @@ func Execp(e Execer, query string, args ...interface{}) sql.Result {
return res
}

// MustExec ("panic") runs Exec on the query and args and panics on error. Since
// the panic interrupts the control flow, errors are not returned to the caller.
func MustExec(e Execer, query string, args ...interface{}) sql.Result {
res, err := e.Exec(query, args...)
if err != nil {
panic(err)
}
return res
}

// A map of names to field positions for destination structs
type fieldmap map[string]int

Expand Down Expand Up @@ -471,7 +510,8 @@ func StructScan(rows *sql.Rows, dest interface{}) error {
// find that name in the struct
num, ok = fm[name]
if !ok {
return errors.New("Could not find name " + name + " in interface.")
fmt.Println(fm)
return errors.New("Could not find name " + name + " in interface")
}
fields[i] = num
}
Expand Down

0 comments on commit 859ec15

Please sign in to comment.