meddler: github.com/russross/meddler Index | Files

package meddler

import "github.com/russross/meddler"

Index

Package Files

doc.go loadsave.go mapper.go meddler.go scan.go

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

func Columns Uses

func Columns(src interface{}, includePk bool) ([]string, error)

Columns using the Default Database type

func ColumnsQuoted Uses

func ColumnsQuoted(src interface{}, includePk bool) (string, error)

ColumnsQuoted using the Default Database type

func DriverErr Uses

func DriverErr(err error) (error, bool)

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 Insert Uses

func Insert(db DB, table string, src interface{}) error

Insert using the Default Database type

func Load Uses

func Load(db DB, table string, dst interface{}, pk int64) error

Load using the Default Database type

func LowerCase Uses

func LowerCase(in string) string

LowerCase returns a lowercased version of the input string

func Placeholders Uses

func Placeholders(src interface{}, includePk bool) ([]string, error)

Placeholders using the Default Database type

func PlaceholdersString Uses

func PlaceholdersString(src interface{}, includePk bool) (string, error)

PlaceholdersString using the Default Database type

func PrimaryKey Uses

func PrimaryKey(src interface{}) (name string, pk int64, err error)

PrimaryKey using the Default Database type

func QueryAll Uses

func QueryAll(db DB, dst interface{}, query string, args ...interface{}) error

QueryAll using the Default Database type

func QueryRow Uses

func QueryRow(db DB, dst interface{}, query string, args ...interface{}) error

QueryRow using the Default Database type

func Register Uses

func Register(name string, m Meddler)

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 Save Uses

func Save(db DB, table string, src interface{}) error

Save using the Default Database type

func Scan Uses

func Scan(rows *sql.Rows, dst interface{}) error

Scan using the Default Database type

func ScanAll Uses

func ScanAll(rows *sql.Rows, dst interface{}) error

ScanAll using the Default Database type

func ScanRow Uses

func ScanRow(rows *sql.Rows, dst interface{}) error

ScanRow using the Default Database type

func SetPrimaryKey Uses

func SetPrimaryKey(src interface{}, pk int64) error

SetPrimaryKey using the Default Database type

func SnakeCase Uses

func SnakeCase(in string) string

SnakeCase returns a snake_cased version of the input string

func SomeValues Uses

func SomeValues(src interface{}, columns []string) ([]interface{}, error)

SomeValues using the Default Database type

func Targets Uses

func Targets(dst interface{}, columns []string) ([]interface{}, error)

Targets using the Default Database type

func Update Uses

func Update(db DB, table string, src interface{}) error

Update using the Default Database type

func Values Uses

func Values(src interface{}, includePk bool) ([]interface{}, error)

Values using the Default Database type

func WriteTargets Uses

func WriteTargets(dst interface{}, columns []string, targets []interface{}) error

WriteTargets using the Default Database type

type DB Uses

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 Uses

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) Columns Uses

func (d *Database) Columns(src interface{}, includePk bool) ([]string, error)

Columns returns a list of column names for its input struct.

func (*Database) ColumnsQuoted Uses

func (d *Database) ColumnsQuoted(src interface{}, includePk bool) (string, error)

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 Uses

func (d *Database) Insert(db DB, table string, src interface{}) error

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 Uses

func (d *Database) Load(db DB, table string, dst interface{}, pk int64) error

Load loads a record using a query for the primary key field. Returns sql.ErrNoRows if not found.

func (*Database) Placeholders Uses

func (d *Database) Placeholders(src interface{}, includePk bool) ([]string, error)

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 Uses

func (d *Database) PlaceholdersString(src interface{}, includePk bool) (string, error)

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 Uses

func (d *Database) PrimaryKey(src interface{}) (name string, pk int64, err error)

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 Uses

func (d *Database) QueryAll(db DB, dst interface{}, query string, args ...interface{}) error

QueryAll performs the given query with the given arguments, scanning all results rows into dst.

func (*Database) QueryRow Uses

func (d *Database) QueryRow(db DB, dst interface{}, query string, args ...interface{}) error

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 Uses

func (d *Database) Save(db DB, table string, src interface{}) error

Save performs an INSERT or an UPDATE, depending on whether or not a primary keys exists and is non-zero.

func (*Database) Scan Uses

func (d *Database) Scan(rows *sql.Rows, dst interface{}) error

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 Uses

func (d *Database) ScanAll(rows *sql.Rows, dst interface{}) error

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 Uses

func (d *Database) ScanRow(rows *sql.Rows, dst interface{}) error

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 Uses

func (d *Database) SetPrimaryKey(src interface{}, pk int64) error

SetPrimaryKey sets the primary key field to the given int value.

func (*Database) SomeValues Uses

func (d *Database) SomeValues(src interface{}, columns []string) ([]interface{}, error)

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 Uses

func (d *Database) Targets(dst interface{}, columns []string) ([]interface{}, error)

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 Uses

func (d *Database) Update(db DB, table string, src interface{}) error

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 Uses

func (d *Database) Values(src interface{}, includePk bool) ([]interface{}, error)

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 Uses

func (d *Database) WriteTargets(dst interface{}, columns []string, targets []interface{}) error

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 Uses

type GobMeddler bool

GobMeddler encodes or decodes the field value to or from gob

func (GobMeddler) PostRead Uses

func (zip GobMeddler) PostRead(fieldAddr, scanTarget interface{}) error

PostRead is called after a Scan operation for fields that have the GobMeddler

func (GobMeddler) PreRead Uses

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 Uses

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 Uses

type IdentityMeddler bool

IdentityMeddler is the default meddler, and it passes the original value through with no changes.

func (IdentityMeddler) PostRead Uses

func (elt IdentityMeddler) PostRead(fieldAddr, scanTarget interface{}) error

PostRead is called after a Scan operation for fields that have the IdentityMeddler

func (IdentityMeddler) PreRead Uses

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 Uses

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 Uses

type JSONMeddler bool

JSONMeddler encodes or decodes the field value to or from JSON

func (JSONMeddler) PostRead Uses

func (zip JSONMeddler) PostRead(fieldAddr, scanTarget interface{}) error

PostRead is called after a Scan operation for fields that have the JSONMeddler

func (JSONMeddler) PreRead Uses

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 Uses

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 Uses

type MapperFunc func(in string) string

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 Uses

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 Uses

type TimeMeddler struct {
    ZeroIsNull bool
    Local      bool
}

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 Uses

func (elt TimeMeddler) PostRead(fieldAddr, scanTarget interface{}) error

PostRead is called after a Scan operation for fields that have a TimeMeddler

func (TimeMeddler) PreRead Uses

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 Uses

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 Uses

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 Uses

func (elt ZeroIsNullMeddler) PostRead(fieldAddr, scanTarget interface{}) error

PostRead is called after a Scan operation for fields that have the ZeroIsNullMeddler

func (ZeroIsNullMeddler) PreRead Uses

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 Uses

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

Package meddler imports 13 packages (graph) and is imported by 214 packages. Updated 2019-02-26. Refresh now. Tools for package owners.