Documentation ¶
Index ¶
- Variables
- func Columns(src interface{}, includePk bool) ([]string, error)
- func ColumnsQuoted(src interface{}, includePk bool) (string, error)
- func DriverErr(err error) (error, bool)
- func Insert(db DB, table string, src interface{}) error
- func Load(db DB, table string, dst interface{}, pk int64) error
- func LowerCase(in string) string
- func Placeholders(src interface{}, includePk bool) ([]string, error)
- func PlaceholdersString(src interface{}, includePk bool) (string, error)
- func PrimaryKey(src interface{}) (name string, pk int64, err error)
- func QueryAll(db DB, dst interface{}, query string, args ...interface{}) error
- func QueryRow(db DB, dst interface{}, query string, args ...interface{}) error
- func Register(name string, m Meddler)
- func Save(db DB, table string, src interface{}) error
- func Scan(rows *sql.Rows, dst interface{}) error
- func ScanAll(rows *sql.Rows, dst interface{}) error
- func ScanRow(rows *sql.Rows, dst interface{}) error
- func SetPrimaryKey(src interface{}, pk int64) error
- func SnakeCase(in string) string
- func SomeValues(src interface{}, columns []string) ([]interface{}, error)
- func Targets(dst interface{}, columns []string) ([]interface{}, error)
- func Update(db DB, table string, src interface{}) error
- func Values(src interface{}, includePk bool) ([]interface{}, error)
- func WriteTargets(dst interface{}, columns []string, targets []interface{}) error
- type DB
- type Database
- func (d *Database) Columns(src interface{}, includePk bool) ([]string, error)
- func (d *Database) ColumnsQuoted(src interface{}, includePk bool) (string, error)
- func (d *Database) Insert(db DB, table string, src interface{}) error
- func (d *Database) Load(db DB, table string, dst interface{}, pk int64) error
- func (d *Database) Placeholders(src interface{}, includePk bool) ([]string, error)
- func (d *Database) PlaceholdersString(src interface{}, includePk bool) (string, error)
- func (d *Database) PrimaryKey(src interface{}) (name string, pk int64, err error)
- func (d *Database) QueryAll(db DB, dst interface{}, query string, args ...interface{}) error
- func (d *Database) QueryRow(db DB, dst interface{}, query string, args ...interface{}) error
- func (d *Database) Save(db DB, table string, src interface{}) error
- func (d *Database) Scan(rows *sql.Rows, dst interface{}) error
- func (d *Database) ScanAll(rows *sql.Rows, dst interface{}) error
- func (d *Database) ScanRow(rows *sql.Rows, dst interface{}) error
- func (d *Database) SetPrimaryKey(src interface{}, pk int64) error
- func (d *Database) SomeValues(src interface{}, columns []string) ([]interface{}, error)
- func (d *Database) Targets(dst interface{}, columns []string) ([]interface{}, error)
- func (d *Database) Update(db DB, table string, src interface{}) error
- func (d *Database) Values(src interface{}, includePk bool) ([]interface{}, error)
- func (d *Database) WriteTargets(dst interface{}, columns []string, targets []interface{}) error
- type GobMeddler
- type IdentityMeddler
- type JSONMeddler
- type MapperFunc
- type Meddler
- type TimeMeddler
- type ZeroIsNullMeddler
Constants ¶
This section is empty.
Variables ¶
var Debug = true
Debug enables debug mode, where unused columns and struct fields will be logged
var Default = MySQL
Default contains the default database options (which defaults to MySQL)
var MySQL = &Database{ Quote: "`", Placeholder: "?", UseReturningToGetID: false, }
MySQL contains database specific options for executing queries in a MySQL database
var PostgreSQL = &Database{ Quote: `"`, Placeholder: "$1", UseReturningToGetID: true, }
PostgreSQL contains database specific options for executing queries in a PostgreSQL database
var SQLite = &Database{ Quote: `"`, Placeholder: "?", UseReturningToGetID: false, }
SQLite contains database specific options for executing queries in a SQLite database
Functions ¶
func ColumnsQuoted ¶
ColumnsQuoted using the Default Database type
func DriverErr ¶
DriverErr returns the original error as returned by the database driver if the error comes from the driver, with the second value set to true. Otherwise, it returns err itself with false as second value.
func Placeholders ¶
Placeholders using the Default Database type
func PlaceholdersString ¶
PlaceholdersString using the Default Database type
func PrimaryKey ¶
PrimaryKey using the Default Database type
func Register ¶
Register sets up a meddler type. Meddlers get a chance to meddle with the data being loaded or saved when a field is annotated with the name of the meddler. The registry is global.
func SetPrimaryKey ¶
SetPrimaryKey using the Default Database type
func SomeValues ¶
SomeValues using the Default Database type
func WriteTargets ¶
WriteTargets using the Default Database type
Types ¶
type DB ¶
type DB interface { Exec(query string, args ...interface{}) (sql.Result, error) Query(query string, args ...interface{}) (*sql.Rows, error) QueryRow(query string, args ...interface{}) *sql.Row }
DB is a generic database interface, matching both *sql.Db and *sql.Tx
type Database ¶
type Database struct { Quote string // the quote character for table and column names Placeholder string // the placeholder style to use in generated queries UseReturningToGetID bool // use PostgreSQL-style RETURNING "ID" instead of calling sql.Result.LastInsertID }
Database contains database-specific options. MySQL, PostgreSQL, and SQLite are provided for convenience. Setting Default to any of these lets you use the package-level convenience functions.
func (*Database) ColumnsQuoted ¶
ColumnsQuoted is similar to Columns, but it return the list of columns in the form:
`column1`,`column2`,...
using Quote as the quote character.
func (*Database) Insert ¶
Insert performs an INSERT query for the given record. If the record has a primary key flagged, it must be zero, and it will be set to the newly-allocated primary key value from the database as returned by LastInsertId.
func (*Database) Load ¶
Load loads a record using a query for the primary key field. Returns sql.ErrNoRows if not found.
func (*Database) Placeholders ¶
Placeholders returns a list of placeholders suitable for an INSERT or UPDATE query. If includePk is false, the primary key field is omitted.
func (*Database) PlaceholdersString ¶
PlaceholdersString returns a list of placeholders suitable for an INSERT or UPDATE query in string form, e.g.:
?,?,?,?
if includePk is false, the primary key field is omitted.
func (*Database) PrimaryKey ¶
PrimaryKey returns the name and value of the primary key field. The name is the empty string if there is not primary key field marked.
func (*Database) QueryAll ¶
QueryAll performs the given query with the given arguments, scanning all results rows into dst.
func (*Database) QueryRow ¶
QueryRow performs the given query with the given arguments, scanning a single row of results into dst. Returns sql.ErrNoRows if there was no result row.
func (*Database) Save ¶
Save performs an INSERT or an UPDATE, depending on whether or not a primary keys exists and is non-zero.
func (*Database) Scan ¶
Scan scans a single sql result row into a struct. It leaves rows ready to be scanned again for the next row. Returns sql.ErrNoRows if there is no data to read.
func (*Database) ScanAll ¶
ScanAll scans all sql result rows into a slice of structs. It reads all rows and closes rows when finished. dst should be a pointer to a slice of the appropriate type. The new results will be appended to any existing data in dst.
func (*Database) ScanRow ¶
ScanRow scans a single sql result row into a struct. It reads exactly one result row and closes rows when finished. Returns sql.ErrNoRows if there is no result row.
func (*Database) SetPrimaryKey ¶
SetPrimaryKey sets the primary key field to the given int value.
func (*Database) SomeValues ¶
SomeValues returns a list of PreWrite processed values suitable for use in an INSERT or UPDATE query. The columns used are the same ones (in the same order) as specified in the columns argument.
func (*Database) Targets ¶
Targets returns a list of values suitable for handing to a Scan function in the sql package, complete with meddling. After the Scan is performed, the same values should be handed to WriteTargets to finalize the values and record them in the struct.
func (*Database) Update ¶
Update performs and UPDATE query for the given record. The record must have an integer primary key field that is non-zero, and it will be used to select the database row that gets updated.
func (*Database) Values ¶
Values returns a list of PreWrite processed values suitable for use in an INSERT or UPDATE query. If includePk is false, the primary key field is omitted. The columns used are the same ones (in the same order) as returned by Columns.
func (*Database) WriteTargets ¶
WriteTargets post-processes values with meddlers after a Scan from the sql package has been performed. The list of targets is normally produced by Targets.
type GobMeddler ¶
type GobMeddler bool
GobMeddler encodes or decodes the field value to or from gob
func (GobMeddler) PostRead ¶
func (zip GobMeddler) PostRead(fieldAddr, scanTarget interface{}) error
PostRead is called after a Scan operation for fields that have the GobMeddler
func (GobMeddler) PreRead ¶
func (zip GobMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, err error)
PreRead is called before a Scan operation for fields that have the GobMeddler
func (GobMeddler) PreWrite ¶
func (zip GobMeddler) PreWrite(field interface{}) (saveValue interface{}, err error)
PreWrite is called before an Insert or Update operation for fields that have the GobMeddler
type IdentityMeddler ¶
type IdentityMeddler bool
IdentityMeddler is the default meddler, and it passes the original value through with no changes.
func (IdentityMeddler) PostRead ¶
func (elt IdentityMeddler) PostRead(fieldAddr, scanTarget interface{}) error
PostRead is called after a Scan operation for fields that have the IdentityMeddler
func (IdentityMeddler) PreRead ¶
func (elt IdentityMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, err error)
PreRead is called before a Scan operation for fields that have the IdentityMeddler
func (IdentityMeddler) PreWrite ¶
func (elt IdentityMeddler) PreWrite(field interface{}) (saveValue interface{}, err error)
PreWrite is called before an Insert or Update operation for fields that have the IdentityMeddler
type JSONMeddler ¶
type JSONMeddler bool
JSONMeddler encodes or decodes the field value to or from JSON
func (JSONMeddler) PostRead ¶
func (zip JSONMeddler) PostRead(fieldAddr, scanTarget interface{}) error
PostRead is called after a Scan operation for fields that have the JSONMeddler
func (JSONMeddler) PreRead ¶
func (zip JSONMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, err error)
PreRead is called before a Scan operation for fields that have the JSONMeddler
func (JSONMeddler) PreWrite ¶
func (zip JSONMeddler) PreWrite(field interface{}) (saveValue interface{}, err error)
PreWrite is called before an Insert or Update operation for fields that have the JSONMeddler
type MapperFunc ¶
MapperFunc signature. Argument is field name, return value is database column.
var Mapper MapperFunc = strings.TrimSpace
Mapper defines the function to transform struct field names into database columns. Default is strings.TrimSpace, basically a no-op
type Meddler ¶
type Meddler interface { // PreRead is called before a Scan operation. It is given a pointer to // the raw struct field, and returns the value that will be given to // the database driver. PreRead(fieldAddr interface{}) (scanTarget interface{}, err error) // PostRead is called after a Scan operation. It is given the value returned // by PreRead and a pointer to the raw struct field. It is expected to fill // in the struct field if the two are different. PostRead(fieldAddr interface{}, scanTarget interface{}) error // PreWrite is called before an Insert or Update operation. It is given // a pointer to the raw struct field, and returns the value that will be // given to the database driver. PreWrite(field interface{}) (saveValue interface{}, err error) }
Meddler is the interface for a field meddler. Implementations can be registered to convert struct fields being loaded and saved in the database.
type TimeMeddler ¶
TimeMeddler provides useful operations on time.Time fields. It can convert the zero time to and from a null column, and it can convert the time zone to UTC on save and to Local on load.
func (TimeMeddler) PostRead ¶
func (elt TimeMeddler) PostRead(fieldAddr, scanTarget interface{}) error
PostRead is called after a Scan operation for fields that have a TimeMeddler
func (TimeMeddler) PreRead ¶
func (elt TimeMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, err error)
PreRead is called before a Scan operation for fields that have a TimeMeddler
func (TimeMeddler) PreWrite ¶
func (elt TimeMeddler) PreWrite(field interface{}) (saveValue interface{}, err error)
PreWrite is called before an Insert or Update operation for fields that have a TimeMeddler
type ZeroIsNullMeddler ¶
type ZeroIsNullMeddler bool
ZeroIsNullMeddler converts zero value fields (integers both signed and unsigned, floats, complex numbers, and strings) to and from null database columns.
func (ZeroIsNullMeddler) PostRead ¶
func (elt ZeroIsNullMeddler) PostRead(fieldAddr, scanTarget interface{}) error
PostRead is called after a Scan operation for fields that have the ZeroIsNullMeddler
func (ZeroIsNullMeddler) PreRead ¶
func (elt ZeroIsNullMeddler) PreRead(fieldAddr interface{}) (scanTarget interface{}, err error)
PreRead is called before a Scan operation for fields that have the ZeroIsNullMeddler
func (ZeroIsNullMeddler) PreWrite ¶
func (elt ZeroIsNullMeddler) PreWrite(field interface{}) (saveValue interface{}, err error)
PreWrite is called before an Insert or Update operation for fields that have the ZeroIsNullMeddler