Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Mar 6, 2015
1 parent f725594 commit 1dc08f3
Show file tree
Hide file tree
Showing 10 changed files with 813 additions and 48 deletions.
488 changes: 487 additions & 1 deletion README.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ var ds_adapters = make(map[string]func(dataset *Dataset) Adapter)

//Registers an adapter.
//
//dialect: The dialect this adapter is for
//factory: a function that can be called to create a new Adapter for the dialect.
// dialect: The dialect this adapter is for
// factory: a function that can be called to create a new Adapter for the dialect.
func RegisterAdapter(dialect string, factory func(ds *Dataset) Adapter) {
dialect = strings.ToLower(dialect)
ds_adapters[dialect] = factory
}

//Returns true if the dialect has an adapter registered
//
//dialect: The dialect to test
// dialect: The dialect to test
func HasAdapter(dialect string) bool {
dialect = strings.ToLower(dialect)
_, ok := ds_adapters[dialect]
Expand All @@ -219,8 +219,8 @@ func removeAdapter(dialect string) {

//Creates the appropriate adapter for the given dialect.
//
//dialect: the dialect to create an adapter for
//dataset: The dataset to be used by the adapter
// dialect: the dialect to create an adapter for
// dataset: The dataset to be used by the adapter
func NewAdapter(dialect string, dataset *Dataset) Adapter {
dialect = strings.ToLower(dialect)
if adapterGen, ok := ds_adapters[dialect]; ok {
Expand Down
21 changes: 11 additions & 10 deletions exec.go → crud_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type (
GoType reflect.Type
}
columnMap map[string]columnData
Exec struct {
CrudExec struct {
database database
Sql string
Args []interface{}
Expand All @@ -26,11 +26,11 @@ type (

var struct_map_cache = make(map[interface{}]columnMap)

func newExec(database database, err error, sql string, args ...interface{}) *Exec {
return &Exec{database: database, err: err, Sql: sql, Args: args}
func newCrudExec(database database, err error, sql string, args ...interface{}) *CrudExec {
return &CrudExec{database: database, err: err, Sql: sql, Args: args}
}

func (me Exec) Exec() (sql.Result, error) {
func (me CrudExec) Exec() (sql.Result, error) {
if me.err != nil {
return nil, me.err
}
Expand All @@ -44,8 +44,9 @@ func (me Exec) Exec() (sql.Result, error) {
// }
// //use your structs
//
//
//i: A pointer to a slice of structs.
func (me Exec) ScanStructs(i interface{}) error {
func (me CrudExec) ScanStructs(i interface{}) error {
if me.err != nil {
return me.err
}
Expand All @@ -71,7 +72,7 @@ func (me Exec) ScanStructs(i interface{}) error {
// }
//
//i: A pointer to a struct
func (me Exec) ScanStruct(i interface{}) (bool, error) {
func (me CrudExec) ScanStruct(i interface{}) (bool, error) {
if me.err != nil {
return false, me.err
}
Expand All @@ -92,7 +93,7 @@ func (me Exec) ScanStruct(i interface{}) (bool, error) {
// }
//
//i: Takes a pointer to a slice of primitive values.
func (me Exec) ScanVals(i interface{}) error {
func (me CrudExec) ScanVals(i interface{}) error {
if me.err != nil {
return me.err
}
Expand Down Expand Up @@ -138,8 +139,8 @@ func (me Exec) ScanVals(i interface{}) error {
// fmt.Println("NOT FOUND")
// }
//
//i: Takes a pointer to a primitive value.
func (me Exec) ScanVal(i interface{}) (bool, error) {
// i: Takes a pointer to a primitive value.
func (me CrudExec) ScanVal(i interface{}) (bool, error) {
if me.err != nil {
return false, me.err
}
Expand All @@ -165,7 +166,7 @@ func (me Exec) ScanVal(i interface{}) (bool, error) {
return count != 0, nil
}

func (me Exec) scan(i interface{}, query string, args ...interface{}) (bool, error) {
func (me CrudExec) scan(i interface{}, query string, args ...interface{}) (bool, error) {
var (
found bool
results []Record
Expand Down
40 changes: 26 additions & 14 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (
//This is the common entry point into goqu.
//
//dialect: This is the adapter dialect, you should see your database adapter for the string to use. Built in adpaters can be found at https://github.com/doug-martin/goqu/tree/master/adapters
//
//db: A sql.Db to use for querying the database
// import (
// "database/sql"
Expand Down Expand Up @@ -102,6 +103,7 @@ func (me *Database) Trace(op, sql string, args ...interface{}) {
//Uses the db to Execute the query with arguments and return the sql.Result
//
//query: The SQL to execute
//
//args...: for any placeholder parameters in the query
func (me *Database) Exec(query string, args ...interface{}) (sql.Result, error) {
me.Trace("EXEC", query, args...)
Expand Down Expand Up @@ -138,7 +140,7 @@ func (me *Database) Prepare(query string) (*sql.Stmt, error) {
return me.Db.Prepare(query)
}

//Can be used to prepare a query.
//Used to query for multiple rows.
//
//You can use this in tandem with a dataset by doing the following.
// sql, err := db.From("items").Where(goqu.I("id").Gt(10)).Sql()
Expand All @@ -158,13 +160,14 @@ func (me *Database) Prepare(query string) (*sql.Stmt, error) {
// }
//
//query: The SQL to execute
//
//args...: for any placeholder parameters in the query
func (me *Database) Query(query string, args ...interface{}) (*sql.Rows, error) {
me.Trace("QUERY", query, args...)
return me.Db.Query(query, args...)
}

//Can be used to prepare a query.
//Used to query for a single row.
//
//You can use this in tandem with a dataset by doing the following.
// sql, err := db.From("items").Where(goqu.I("id").Gt(10)).Limit(1).Sql()
Expand All @@ -178,49 +181,58 @@ func (me *Database) Query(query string, args ...interface{}) (*sql.Rows, error)
// //scan your row
//
//query: The SQL to execute
//
//args...: for any placeholder parameters in the query
func (me *Database) QueryRow(query string, args ...interface{}) *sql.Row {
me.Trace("QUERY ROW", query, args...)
return me.Db.QueryRow(query, args...)
}

//Queries the database using the supplied query, and args and uses Exec#ScanStructs to scan the results into a slice of structs
//Queries the database using the supplied query, and args and uses CrudExec.ScanStructs to scan the results into a slice of structs
//
//i: A pointer to a slice of structs
//
//query: The SQL to execute
//
//args...: for any placeholder parameters in the query
func (me *Database) ScanStructs(i interface{}, query string, args ...interface{}) error {
exec := newExec(me, nil, query, args...)
exec := newCrudExec(me, nil, query, args...)
return exec.ScanStructs(i)
}

//Queries the database using the supplied query, and args and uses Exec#ScanStruct to scan the results into a struct
//Queries the database using the supplied query, and args and uses CrudExec.ScanStruct to scan the results into a struct
//
//i: A pointer to a struct
//
//query: The SQL to execute
//
//args...: for any placeholder parameters in the query
func (me *Database) ScanStruct(i interface{}, query string, args ...interface{}) (bool, error) {
exec := newExec(me, nil, query, args...)
exec := newCrudExec(me, nil, query, args...)
return exec.ScanStruct(i)
}

//Queries the database using the supplied query, and args and uses Exec#ScanVals to scan the results into a slice of primitive values
//Queries the database using the supplied query, and args and uses CrudExec.ScanVals to scan the results into a slice of primitive values
//
//i: A pointer to a slice of primitive values
//
//query: The SQL to execute
//
//args...: for any placeholder parameters in the query
func (me *Database) ScanVals(i interface{}, query string, args ...interface{}) error {
exec := newExec(me, nil, query, args...)
exec := newCrudExec(me, nil, query, args...)
return exec.ScanVals(i)
}

//Queries the database using the supplied query, and args and uses Exec#ScanVal to scan the results into a primitive value
//Queries the database using the supplied query, and args and uses CrudExec.ScanVal to scan the results into a primitive value
//
//i: A pointer to a primitive value
//
//query: The SQL to execute
//
//args...: for any placeholder parameters in the query
func (me *Database) ScanVal(i interface{}, query string, args ...interface{}) (bool, error) {
exec := newExec(me, nil, query, args...)
exec := newCrudExec(me, nil, query, args...)
return exec.ScanVal(i)
}

Expand Down Expand Up @@ -287,25 +299,25 @@ func (me *TxDatabase) QueryRow(query string, args ...interface{}) *sql.Row {

//See Database#ScanStructs
func (me *TxDatabase) ScanStructs(i interface{}, query string, args ...interface{}) error {
exec := newExec(me, nil, query, args...)
exec := newCrudExec(me, nil, query, args...)
return exec.ScanStructs(i)
}

//See Database#ScanStruct
func (me *TxDatabase) ScanStruct(i interface{}, query string, args ...interface{}) (bool, error) {
exec := newExec(me, nil, query, args...)
exec := newCrudExec(me, nil, query, args...)
return exec.ScanStruct(i)
}

//See Database#ScanVals
func (me *TxDatabase) ScanVals(i interface{}, query string, args ...interface{}) error {
exec := newExec(me, nil, query, args...)
exec := newCrudExec(me, nil, query, args...)
return exec.ScanVals(i)
}

//See Database#ScanVal
func (me *TxDatabase) ScanVal(i interface{}, query string, args ...interface{}) (bool, error) {
exec := newExec(me, nil, query, args...)
exec := newCrudExec(me, nil, query, args...)
return exec.ScanVal(i)
}

Expand Down
3 changes: 2 additions & 1 deletion dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type (
// * Count() - Returns a count of rows
// * Pluck(i interface{}, col string) - Retrives a columns from rows and scans the resules into a slice of primitive values.
//
//Update, Delete, and Insert return an Exec struct which can be used to scan values or just execute the statment. You might
//Update, Delete, and Insert return an CrudExec struct which can be used to scan values or just execute the statment. You might
//use the scan methods if the database supports return values. For example
// UPDATE "items" SET updated = NOW RETURNING "items".*
//Could be executed with ScanStructs.
Expand Down Expand Up @@ -161,6 +161,7 @@ func (me *Dataset) hasSources() bool {
// * Expressions
//
//buf: The SqlBuilder to write the generated SQL to
//
//val: The value to serialize
//
//Errors:
Expand Down
23 changes: 12 additions & 11 deletions dataset_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ package goqu
//i: A pointer to a slice of structs
func (me *Dataset) ScanStructs(i interface{}) error {
sql, err := me.Sql()
return newExec(me.database, err, sql).ScanStructs(i)
return newCrudExec(me.database, err, sql).ScanStructs(i)
}

//Generates the SELECT sql for this dataset and uses Exec#ScanStruct to scan the result into a slice of structs
//
//i: A pointer to a structs
func (me *Dataset) ScanStruct(i interface{}) (bool, error) {
sql, err := me.Limit(1).Sql()
return newExec(me.database, err, sql).ScanStruct(i)
return newCrudExec(me.database, err, sql).ScanStruct(i)
}

//Generates the SELECT sql for this dataset and uses Exec#ScanVals to scan the results into a slice of primitive values
//
//i: A pointer to a slice of primitive values
func (me *Dataset) ScanVals(i interface{}) error {
sql, err := me.Sql()
return newExec(me.database, err, sql).ScanVals(i)
return newCrudExec(me.database, err, sql).ScanVals(i)
}

//Generates the SELECT sql for this dataset and uses Exec#ScanVal to scan the result into a primitive value
//
//i: A pointer to a primitive value
func (me *Dataset) ScanVal(i interface{}) (bool, error) {
sql, err := me.Sql()
return newExec(me.database, err, sql).ScanVal(i)
sql, err := me.Limit(1).Sql()
return newCrudExec(me.database, err, sql).ScanVal(i)
}

//Generates the SELECT COUNT(*) sql for this dataset and uses Exec#ScanVal to scan the result into an int64.
Expand All @@ -42,6 +42,7 @@ func (me *Dataset) Count() (int64, error) {
//Generates the SELECT sql only selecting the passed in column and uses Exec#ScanVals to scan the result into a slice of primitive values.
//
//i: A slice of primitive values
//
//col: The column to select when generative the SQL
func (me *Dataset) Pluck(i interface{}, col string) error {
return me.Select(col).ScanVals(i)
Expand All @@ -51,23 +52,23 @@ func (me *Dataset) Pluck(i interface{}, col string) error {
// db.From("test").Update(Record{"name":"Bob", update: time.Now()}).Exec()
//
//See Dataset#UpdateSql for arguments
func (me *Dataset) Update(i interface{}) *Exec {
func (me *Dataset) Update(i interface{}) *CrudExec {
sql, err := me.UpdateSql(i)
return newExec(me.database, err, sql)
return newCrudExec(me.database, err, sql)
}

//Generates the UPDATE sql, and returns an Exec struct with the sql set to the INSERT statement
// db.From("test").Insert(Record{"name":"Bob").Exec()
//
//See Dataset#InsertSql for arguments
func (me *Dataset) Insert(i ...interface{}) *Exec {
func (me *Dataset) Insert(i ...interface{}) *CrudExec {
sql, err := me.InsertSql(i...)
return newExec(me.database, err, sql)
return newCrudExec(me.database, err, sql)
}

//Generates the DELETE sql, and returns an Exec struct with the sql set to the DELETE statement
// db.From("test").Where(I("id").Gt(10)).Exec()
func (me *Dataset) Delete() *Exec {
func (me *Dataset) Delete() *CrudExec {
sql, err := me.DeleteSql()
return newExec(me.database, err, sql)
return newCrudExec(me.database, err, sql)
}
1 change: 1 addition & 0 deletions dataset_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (me *Dataset) TruncateWithOptsSql(opts TruncateOptions) (string, error) {
//Generates a TRUNCATE statement.
//
//isPrepared: Set to true to true to ensure values are NOT interpolated. See examples.
//
//opts: Options to use when generating the TRUNCATE statement
//
//Errors:
Expand Down
1 change: 1 addition & 0 deletions dataset_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (me *Dataset) InsertSql(rows ...interface{}) (string, error) {
// }
//
//isPrepared: Set to true to true to ensure values are NOT interpolated
//
//rows: variable number arguments of either map[string]interface, Record, struct, or a single slice argument of the accepted types.
//
//Errors:
Expand Down
1 change: 1 addition & 0 deletions dataset_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (me *Dataset) canUpdateField(field reflect.StructField) bool {
// }
//
//isPrepared: set to true to generate an sql statement with placeholders for primitive values
//
//update: can either be a a map[string]interface{}, Record or a struct
//
//Errors:
Expand Down
Loading

0 comments on commit 1dc08f3

Please sign in to comment.