Skip to content

Commit

Permalink
remove tons of cruft from the API, making it a lot more focused
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoiron committed Apr 27, 2014
1 parent c6571d8 commit 213d8de
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 265 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ Major additional concepts are:
Read the usage below to see how sqlx might help you, or check out the [API
documentation on godoc](http://godoc.org/github.com/jmoiron/sqlx).

## Important API Stability Note

The sqlx API has been stable for a long time as I have attempted to learn
from the way that it interacts with real code and taken in bug reports.
There have been very minor breaking changes in the past, many of which would
not have affected most code.

However, the API is very large, and there's little evidence that some of the
repeated "convenience" verbs deserve to be so widely distributed amongst the
nouns of DB, Tx, and Stmt. Removing these methods but preserving the base
convenience functions (which take any of these via an interface) would reduce
the surface of the API a lot and make it easier to understand.

In addition to this, there are some functions which were exported with the
intent to facilitate building richer ORM libraries on top of sqlx, but after
having [built one myself](http://github.com/jmoiron/modl), I'm not convinced
that this is possible given the limited exposure of the name mapping that is
revealed in the public API. If it's a choice between exposing more of it or
exposing none of it, I'd rather expose none of it in the `sqlx` namespace.


## install

go get github.com/jmoiron/sqlx
Expand Down
70 changes: 0 additions & 70 deletions named.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"database/sql"
"errors"
"fmt"
"log"
"reflect"
"strconv"
"unicode"
Expand Down Expand Up @@ -65,38 +64,6 @@ func (n *NamedStmt) QueryRow(arg interface{}) *Row {
return n.Stmt.QueryRowx(args...)
}

// Execv execs a NamedStmt with the given arg, printing errors and returning them
func (n *NamedStmt) Execv(arg interface{}) (sql.Result, error) {
res, err := n.Exec(arg)
if err != nil {
log.Println(n.QueryString, res, err)
}
return res, err
}

// Execl execs a NamedStmt with the given arg, logging errors
func (n *NamedStmt) Execl(arg interface{}) sql.Result {
res, err := n.Exec(arg)
if err != nil {
log.Println(n.QueryString, res, err)
}
return res
}

// Execf execs a NamedStmt, using log.fatal to print out errors
func (n *NamedStmt) Execf(arg interface{}) sql.Result {
res, err := n.Exec(arg)
if err != nil {
log.Fatal(n.QueryString, res, err)
}
return res
}

// Execp execs a NamedStmt, panicing on error
func (n *NamedStmt) Execp(arg interface{}) sql.Result {
return n.MustExec(arg)
}

// MustExec execs a NamedStmt, panicing on error
func (n *NamedStmt) MustExec(arg interface{}) sql.Result {
res, err := n.Exec(arg)
Expand Down Expand Up @@ -132,23 +99,6 @@ func (n *NamedStmt) Select(dest interface{}, arg interface{}) error {
return StructScan(rows, dest)
}

// Selectv using this NamedStmt
func (n *NamedStmt) Selectv(dest interface{}, arg interface{}) error {
err := n.Select(dest, arg)
if err != nil {
log.Println(n.QueryString, err)
}
return err
}

// Selectf using this NamedStmt
func (n *NamedStmt) Selectf(dest interface{}, arg interface{}) {
err := n.Select(dest, arg)
if err != nil {
log.Fatal(n.QueryString, err)
}
}

// Get using this NamedStmt
func (n *NamedStmt) Get(dest interface{}, arg interface{}) error {
r := n.QueryRowx(arg)
Expand Down Expand Up @@ -375,23 +325,3 @@ func NamedExec(e Ext, query string, arg interface{}) (sql.Result, error) {
}
return e.Exec(q, args...)
}

// NamedQueryMap runs a named query using a map instead of a struct.
// DEPRECATED: Use NamedQuery instead, which also supports maps.
func NamedQueryMap(e Ext, query string, argmap map[string]interface{}) (*Rows, error) {
q, args, err := e.BindMap(query, argmap)
if err != nil {
return nil, err
}
return e.Queryx(q, args...)
}

// NamedExecMap executes a named query using a map instead of a struct.
// DEPRECATED: Use NamedExec instead, which also supports maps.
func NamedExecMap(e Ext, query string, argmap map[string]interface{}) (sql.Result, error) {
q, args, err := e.BindMap(query, argmap)
if err != nil {
return nil, err
}
return e.Exec(q, args...)
}
189 changes: 1 addition & 188 deletions sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"

"io/ioutil"
"log"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -203,18 +202,6 @@ func (db *DB) BindStruct(query string, arg interface{}) (string, []interface{},
return BindStruct(BindType(db.driverName), query, arg)
}

// NamedQueryMap using this DB.
// DEPRECATED: use NamedQuery instead.
func (db *DB) NamedQueryMap(query string, argmap map[string]interface{}) (*Rows, error) {
return NamedQueryMap(db, query, argmap)
}

// NamedExecMap using this DB.
// DEPRECATED: use NamedExec instead
func (db *DB) NamedExecMap(query string, argmap map[string]interface{}) (sql.Result, error) {
return NamedExecMap(db, query, argmap)
}

// NamedQuery using this DB.
func (db *DB) NamedQuery(query string, arg interface{}) (*Rows, error) {
return NamedQuery(db, query, arg)
Expand All @@ -230,26 +217,11 @@ func (db *DB) Select(dest interface{}, query string, args ...interface{}) error
return Select(db, dest, query, args...)
}

// Selectf using this DB.
func (db *DB) Selectf(dest interface{}, query string, args ...interface{}) {
Selectf(db, dest, query, args...)
}

// Selectv using this DB.
func (db *DB) Selectv(dest interface{}, query string, args ...interface{}) error {
return Selectv(db, dest, query, args...)
}

// Get using this DB.
func (db *DB) Get(dest interface{}, query string, args ...interface{}) error {
return Get(db, dest, query, args...)
}

// LoadFile using this DB.
func (db *DB) LoadFile(path string) (*sql.Result, error) {
return LoadFile(db, path)
}

// 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 @@ -284,26 +256,6 @@ func (db *DB) QueryRowx(query string, args ...interface{}) *Row {
return &Row{rows: rows, err: err, unsafe: db.unsafe}
}

// Execv (verbose) runs Execv using this database.
func (db *DB) Execv(query string, args ...interface{}) (sql.Result, error) {
return Execv(db, query, args...)
}

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

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

// Execp (panic) runs Execp using this database.
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...)
Expand Down Expand Up @@ -362,23 +314,6 @@ func (tx *Tx) NamedExec(query string, arg interface{}) (sql.Result, error) {
return NamedExec(tx, query, arg)
}

// NamedQueryMap within a transaction.
// DEPRECATED: Use NamedQuery instead
func (tx *Tx) NamedQueryMap(query string, arg map[string]interface{}) (*Rows, error) {
return NamedQueryMap(tx, query, arg)
}

// NamedExecMap a named query within a transaction.
// DEPRECATED: Use NamedExec instead
func (tx *Tx) NamedExecMap(query string, arg map[string]interface{}) (sql.Result, error) {
return NamedExecMap(tx, query, arg)
}

// LoadFile within a transaction.
func (tx *Tx) LoadFile(path string) (*sql.Result, error) {
return LoadFile(tx, path)
}

// Select within a transaction.
func (tx *Tx) Select(dest interface{}, query string, args ...interface{}) error {
return Select(tx, dest, query, args...)
Expand All @@ -404,36 +339,6 @@ func (tx *Tx) Get(dest interface{}, query string, args ...interface{}) error {
return Get(tx, dest, query, args...)
}

// Selectv (verbose) within a transaction.
func (tx *Tx) Selectv(dest interface{}, query string, args ...interface{}) error {
return Selectv(tx, dest, query, args...)
}

// Selectf (fatal) within a transaction.
func (tx *Tx) Selectf(dest interface{}, query string, args ...interface{}) {
Selectf(tx, dest, query, args...)
}

// Execv (verbose) runs Execv within a transaction.
func (tx *Tx) Execv(query string, args ...interface{}) (sql.Result, error) {
return Execv(tx, query, args...)
}

// Execl (log) runs Execl within a transaction.
func (tx *Tx) Execl(query string, args ...interface{}) sql.Result {
return Execl(tx, query, args...)
}

// Execf (fatal) runs Execf within a transaction.
func (tx *Tx) Execf(query string, args ...interface{}) sql.Result {
return Execf(tx, query, args...)
}

// Execp (panic) runs Execp within a transaction.
func (tx *Tx) Execp(query string, args ...interface{}) sql.Result {
return Execp(tx, query, args...)
}

// MustExec runs MustExec within a transaction.
func (tx *Tx) MustExec(query string, args ...interface{}) sql.Result {
return MustExec(tx, query, args...)
Expand Down Expand Up @@ -494,45 +399,11 @@ func (s *Stmt) Select(dest interface{}, args ...interface{}) error {
return Select(&qStmt{*s}, dest, "", args...)
}

// Selectv (verbose) using the prepared statement.
func (s *Stmt) Selectv(dest interface{}, args ...interface{}) error {
return Selectv(&qStmt{*s}, dest, "", args...)
}

// Selectf (fatal) using the prepared statement.
func (s *Stmt) Selectf(dest interface{}, args ...interface{}) {
Selectf(&qStmt{*s}, dest, "", args...)
}

// Get using the prepared statement.
func (s *Stmt) Get(dest interface{}, args ...interface{}) error {
return Get(&qStmt{*s}, dest, "", args...)
}

// Execv (verbose) runs Execv using this statement. Note that the query
// portion of the error output will be blank, as Stmt does not expose its query.
func (s *Stmt) Execv(args ...interface{}) (sql.Result, error) {
return Execv(&qStmt{*s}, "", args...)
}

// Execl (log) using this statement. Note that the query portion of the error
// output will be blank, as Stmt does not expose its query.
func (s *Stmt) Execl(args ...interface{}) sql.Result {
return Execl(&qStmt{*s}, "", args...)
}

// Execf (fatal) using this statement. Note that the query portion of the error
// output will be blank, as Stmt does not expose its query.
func (s *Stmt) Execf(args ...interface{}) sql.Result {
return Execf(&qStmt{*s}, "", args...)
}

// Execp (panic) using this statement. Note that the query portion of the error
// output will be blank, as Stmt does not expose its query.
func (s *Stmt) Execp(args ...interface{}) sql.Result {
return Execp(&qStmt{*s}, "", args...)
}

// MustExec (panic) using this statement. Note that the query portion of the error
// output will be blank, as Stmt does not expose its query.
func (s *Stmt) MustExec(args ...interface{}) sql.Result {
Expand Down Expand Up @@ -693,25 +564,6 @@ func Select(q Queryer, dest interface{}, query string, args ...interface{}) erro
return StructScan(rows, dest)
}

// Selectv (verbose) will Select using a Queryer and use log.Println to print
//the query and the error in the event of an error.
func Selectv(q Queryer, dest interface{}, query string, args ...interface{}) error {
err := Select(q, dest, query, args...)
if err != nil {
log.Println(query, err)
}
return err
}

// Selectf (fatal) will Select using a Queryer and use log.Fatal to print
// the query and the error in the event of an error.
func Selectf(q Queryer, dest interface{}, query string, args ...interface{}) {
err := Select(q, dest, query, args...)
if err != nil {
log.Fatal(query, err)
}
}

// Get does a QueryRow using the provided Queryer, and StructScan the resulting
// row into dest, which must be a pointer to a struct. If there was no row,
// Get will return sql.ErrNoRows like row.Scan would.
Expand Down Expand Up @@ -743,46 +595,7 @@ func LoadFile(e Execer, path string) (*sql.Result, error) {
return &res, err
}

// Execv (verbose) Exec's the query using the Execer and uses log.Println to
// print the query, result, and error in the event of an error.
func Execv(e Execer, query string, args ...interface{}) (sql.Result, error) {
res, err := e.Exec(query, args...)
if err != nil {
log.Println(query, res, err)
}
return res, err
}

// Execl (log) runs Exec on the query and args and ses log.Println to
// print the query, result, and error in the event of an error. Unlike Execv,
// Execl does not return the error, and can be used in single-value contexts.
//
// Do not abuse Execl; it is convenient for experimentation but generally not
// for production use.
func Execl(e Execer, query string, args ...interface{}) sql.Result {
res, err := e.Exec(query, args...)
if err != nil {
log.Println(query, res, err)
}
return res
}

// Execf (fatal) runs Exec on the query and args and uses log.Fatal to
// print the query, result, and error in the event of an error.
func Execf(e Execer, query string, args ...interface{}) sql.Result {
res, err := e.Exec(query, args...)
if err != nil {
log.Fatal(query, res, err)
}
return res
}

// Execp (panic) runs Exec on the query and args and panics on error.
func Execp(e Execer, query string, args ...interface{}) sql.Result {
return MustExec(e, query, args...)
}

// MustExec (panic) is an alias for Execp.
// MustExec execs the query using e and panics if there was an error.
func MustExec(e Execer, query string, args ...interface{}) sql.Result {
res, err := e.Exec(query, args...)
if err != nil {
Expand Down
Loading

0 comments on commit 213d8de

Please sign in to comment.