Documentation ¶
Overview ¶
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Package gorp provides a simple way to marshal Go structs to and from SQL databases. It uses the database/sql package, and should work with any compliant database/sql driver.
Source code and project home: https://github.com/go-gorp/gorp
Index ¶
- func NonFatalError(err error) bool
- func SelectFloat(e SqlExecutor, query string, args ...interface{}) (float64, error)
- func SelectInt(e SqlExecutor, query string, args ...interface{}) (int64, error)
- func SelectNullFloat(e SqlExecutor, query string, args ...interface{}) (sql.NullFloat64, error)
- func SelectNullInt(e SqlExecutor, query string, args ...interface{}) (sql.NullInt64, error)
- func SelectNullStr(e SqlExecutor, query string, args ...interface{}) (sql.NullString, error)
- func SelectOne(m *DbMap, e SqlExecutor, holder interface{}, query string, args ...interface{}) error
- func SelectStr(e SqlExecutor, query string, args ...interface{}) (string, error)
- type ColumnMap
- type CustomScanner
- type DbMap
- func (m *DbMap) AddTable(i interface{}) *TableMap
- func (m *DbMap) AddTableWithName(i interface{}, name string) *TableMap
- func (m *DbMap) AddTableWithNameAndSchema(i interface{}, schema string, name string) *TableMap
- func (m *DbMap) Begin() (*Transaction, error)
- func (m *DbMap) CreateIndex() error
- func (m *DbMap) CreateTables() error
- func (m *DbMap) CreateTablesIfNotExists() error
- func (m *DbMap) Delete(list ...interface{}) (int64, error)
- func (m *DbMap) DropTable(table interface{}) error
- func (m *DbMap) DropTableIfExists(table interface{}) error
- func (m *DbMap) DropTables() error
- func (m *DbMap) DropTablesIfExists() error
- func (m *DbMap) Exec(query string, args ...interface{}) (sql.Result, error)
- func (m *DbMap) Get(i interface{}, keys ...interface{}) (interface{}, error)
- func (m *DbMap) Insert(list ...interface{}) error
- func (m *DbMap) Prepare(query string) (*sql.Stmt, error)
- func (m *DbMap) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error)
- func (m *DbMap) SelectFloat(query string, args ...interface{}) (float64, error)
- func (m *DbMap) SelectInt(query string, args ...interface{}) (int64, error)
- func (m *DbMap) SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error)
- func (m *DbMap) SelectNullInt(query string, args ...interface{}) (sql.NullInt64, error)
- func (m *DbMap) SelectNullStr(query string, args ...interface{}) (sql.NullString, error)
- func (m *DbMap) SelectOne(holder interface{}, query string, args ...interface{}) error
- func (m *DbMap) SelectStr(query string, args ...interface{}) (string, error)
- func (m *DbMap) TableFor(t reflect.Type, checkPK bool) (*TableMap, error)
- func (m *DbMap) TraceOff()
- func (m *DbMap) TraceOn(prefix string, logger GorpLogger)
- func (m *DbMap) TruncateTables() error
- func (m *DbMap) Update(list ...interface{}) (int64, error)
- type Dialect
- type GorpLogger
- type HasPostDelete
- type HasPostGet
- type HasPostInsert
- type HasPostUpdate
- type HasPreDelete
- type HasPreInsert
- type HasPreUpdate
- type IndexMap
- type IntegerAutoIncrInserter
- type MySQLDialect
- func (d MySQLDialect) AutoIncrBindValue() string
- func (d MySQLDialect) AutoIncrInsertSuffix(col *ColumnMap) string
- func (d MySQLDialect) AutoIncrStr() string
- func (d MySQLDialect) BindVar(i int) string
- func (m MySQLDialect) CreateIndexSuffix() string
- func (d MySQLDialect) CreateTableSuffix() string
- func (m MySQLDialect) DropIndexSuffix() string
- func (d MySQLDialect) IfSchemaNotExists(command, schema string) string
- func (d MySQLDialect) IfTableExists(command, schema, table string) string
- func (d MySQLDialect) IfTableNotExists(command, schema, table string) string
- func (d MySQLDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error)
- func (d MySQLDialect) QuerySuffix() string
- func (d MySQLDialect) QuoteField(f string) string
- func (d MySQLDialect) QuotedTableForQuery(schema string, table string) string
- func (d MySQLDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string
- func (m MySQLDialect) TruncateClause() string
- type NoFieldInTypeError
- type NullTime
- type OptimisticLockError
- type OracleDialect
- func (d OracleDialect) AutoIncrBindValue() string
- func (d OracleDialect) AutoIncrInsertSuffix(col *ColumnMap) string
- func (d OracleDialect) AutoIncrStr() string
- func (d OracleDialect) BindVar(i int) string
- func (d OracleDialect) CreateIndexSuffix() string
- func (d OracleDialect) CreateTableSuffix() string
- func (d OracleDialect) DropIndexSuffix() string
- func (d OracleDialect) IfSchemaNotExists(command, schema string) string
- func (d OracleDialect) IfTableExists(command, schema, table string) string
- func (d OracleDialect) IfTableNotExists(command, schema, table string) string
- func (d OracleDialect) InsertQueryToTarget(exec SqlExecutor, insertSql, idSql string, target interface{}, ...) error
- func (d OracleDialect) QuerySuffix() string
- func (d OracleDialect) QuoteField(f string) string
- func (d OracleDialect) QuotedTableForQuery(schema string, table string) string
- func (d OracleDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string
- func (d OracleDialect) TruncateClause() string
- type OracleString
- type PostgresDialect
- func (d PostgresDialect) AutoIncrBindValue() string
- func (d PostgresDialect) AutoIncrInsertSuffix(col *ColumnMap) string
- func (d PostgresDialect) AutoIncrStr() string
- func (d PostgresDialect) BindVar(i int) string
- func (d PostgresDialect) CreateIndexSuffix() string
- func (d PostgresDialect) CreateTableSuffix() string
- func (d PostgresDialect) DropIndexSuffix() string
- func (d PostgresDialect) IfSchemaNotExists(command, schema string) string
- func (d PostgresDialect) IfTableExists(command, schema, table string) string
- func (d PostgresDialect) IfTableNotExists(command, schema, table string) string
- func (d PostgresDialect) InsertAutoIncrToTarget(exec SqlExecutor, insertSql string, target interface{}, params ...interface{}) error
- func (d PostgresDialect) QuerySuffix() string
- func (d PostgresDialect) QuoteField(f string) string
- func (d PostgresDialect) QuotedTableForQuery(schema string, table string) string
- func (d PostgresDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string
- func (d PostgresDialect) TruncateClause() string
- type SqlExecutor
- type SqlServerDialect
- func (d SqlServerDialect) AutoIncrBindValue() string
- func (d SqlServerDialect) AutoIncrInsertSuffix(col *ColumnMap) string
- func (d SqlServerDialect) AutoIncrStr() string
- func (d SqlServerDialect) BindVar(i int) string
- func (d SqlServerDialect) CreateIndexSuffix() string
- func (d SqlServerDialect) CreateTableSuffix() string
- func (d SqlServerDialect) DropIndexSuffix() string
- func (d SqlServerDialect) IfSchemaNotExists(command, schema string) string
- func (d SqlServerDialect) IfTableExists(command, schema, table string) string
- func (d SqlServerDialect) IfTableNotExists(command, schema, table string) string
- func (d SqlServerDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error)
- func (d SqlServerDialect) QuerySuffix() string
- func (d SqlServerDialect) QuoteField(f string) string
- func (d SqlServerDialect) QuotedTableForQuery(schema string, table string) string
- func (d SqlServerDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string
- func (d SqlServerDialect) TruncateClause() string
- type SqlTyper
- type SqliteDialect
- func (d SqliteDialect) AutoIncrBindValue() string
- func (d SqliteDialect) AutoIncrInsertSuffix(col *ColumnMap) string
- func (d SqliteDialect) AutoIncrStr() string
- func (d SqliteDialect) BindVar(i int) string
- func (d SqliteDialect) CreateIndexSuffix() string
- func (d SqliteDialect) CreateTableSuffix() string
- func (d SqliteDialect) DropIndexSuffix() string
- func (d SqliteDialect) IfSchemaNotExists(command, schema string) string
- func (d SqliteDialect) IfTableExists(command, schema, table string) string
- func (d SqliteDialect) IfTableNotExists(command, schema, table string) string
- func (d SqliteDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error)
- func (d SqliteDialect) QuerySuffix() string
- func (d SqliteDialect) QuoteField(f string) string
- func (d SqliteDialect) QuotedTableForQuery(schema string, table string) string
- func (d SqliteDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string
- func (d SqliteDialect) TruncateClause() string
- type TableMap
- func (t *TableMap) AddIndex(name string, idxtype string, columns []string) *IndexMap
- func (t *TableMap) ColMap(field string) *ColumnMap
- func (t *TableMap) DropIndex(name string) error
- func (t *TableMap) IdxMap(field string) *IndexMap
- func (t *TableMap) ResetSql()
- func (t *TableMap) SetKeys(isAutoIncr bool, fieldNames ...string) *TableMap
- func (t *TableMap) SetUniqueTogether(fieldNames ...string) *TableMap
- func (t *TableMap) SetVersionCol(field string) *ColumnMap
- func (t *TableMap) SqlForCreate(ifNotExists bool) string
- type TargetQueryInserter
- type TargetedAutoIncrInserter
- type Transaction
- func (t *Transaction) Commit() error
- func (t *Transaction) Delete(list ...interface{}) (int64, error)
- func (t *Transaction) Exec(query string, args ...interface{}) (sql.Result, error)
- func (t *Transaction) Get(i interface{}, keys ...interface{}) (interface{}, error)
- func (t *Transaction) Insert(list ...interface{}) error
- func (t *Transaction) Prepare(query string) (*sql.Stmt, error)
- func (t *Transaction) ReleaseSavepoint(savepoint string) error
- func (t *Transaction) Rollback() error
- func (t *Transaction) RollbackToSavepoint(savepoint string) error
- func (t *Transaction) Savepoint(name string) error
- func (t *Transaction) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error)
- func (t *Transaction) SelectFloat(query string, args ...interface{}) (float64, error)
- func (t *Transaction) SelectInt(query string, args ...interface{}) (int64, error)
- func (t *Transaction) SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error)
- func (t *Transaction) SelectNullInt(query string, args ...interface{}) (sql.NullInt64, error)
- func (t *Transaction) SelectNullStr(query string, args ...interface{}) (sql.NullString, error)
- func (t *Transaction) SelectOne(holder interface{}, query string, args ...interface{}) error
- func (t *Transaction) SelectStr(query string, args ...interface{}) (string, error)
- func (t *Transaction) Update(list ...interface{}) (int64, error)
- type TypeConverter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NonFatalError ¶
returns true if the error is non-fatal (ie, we shouldn't immediately return)
func SelectFloat ¶
func SelectFloat(e SqlExecutor, query string, args ...interface{}) (float64, error)
SelectFloat executes the given query, which should be a SELECT statement for a single float column, and returns the value of the first row returned. If no rows are found, zero is returned.
func SelectInt ¶
func SelectInt(e SqlExecutor, query string, args ...interface{}) (int64, error)
SelectInt executes the given query, which should be a SELECT statement for a single integer column, and returns the value of the first row returned. If no rows are found, zero is returned.
func SelectNullFloat ¶
func SelectNullFloat(e SqlExecutor, query string, args ...interface{}) (sql.NullFloat64, error)
SelectNullFloat executes the given query, which should be a SELECT statement for a single float column, and returns the value of the first row returned. If no rows are found, the empty sql.NullInt64 value is returned.
func SelectNullInt ¶
func SelectNullInt(e SqlExecutor, query string, args ...interface{}) (sql.NullInt64, error)
SelectNullInt executes the given query, which should be a SELECT statement for a single integer column, and returns the value of the first row returned. If no rows are found, the empty sql.NullInt64 value is returned.
func SelectNullStr ¶
func SelectNullStr(e SqlExecutor, query string, args ...interface{}) (sql.NullString, error)
SelectNullStr executes the given query, which should be a SELECT statement for a single char/varchar column, and returns the value of the first row returned. If no rows are found, the empty sql.NullString is returned.
func SelectOne ¶
func SelectOne(m *DbMap, e SqlExecutor, holder interface{}, query string, args ...interface{}) error
SelectOne executes the given query (which should be a SELECT statement) and binds the result to holder, which must be a pointer.
If no row is found, an error (sql.ErrNoRows specifically) will be returned ¶
If more than one row is found, an error will be returned.
func SelectStr ¶
func SelectStr(e SqlExecutor, query string, args ...interface{}) (string, error)
SelectStr executes the given query, which should be a SELECT statement for a single char/varchar column, and returns the value of the first row returned. If no rows are found, an empty string is returned.
Types ¶
type ColumnMap ¶
type ColumnMap struct { // Column name in db table ColumnName string // If true, this column is skipped in generated SQL statements Transient bool // If true, " unique" is added to create table statements. // Not used elsewhere Unique bool // Query used for getting generated id after insert GeneratedIdQuery string // Passed to Dialect.ToSqlType() to assist in informing the // correct column type to map to in CreateTables() MaxSize int DefaultValue string // contains filtered or unexported fields }
ColumnMap represents a mapping between a Go struct field and a single column in a table. Unique and MaxSize only inform the CreateTables() function and are not used by Insert/Update/Delete/Get.
func (*ColumnMap) Rename ¶
Rename allows you to specify the column name in the table
Example: table.ColMap("Updated").Rename("date_updated")
func (*ColumnMap) SetMaxSize ¶
SetMaxSize specifies the max length of values of this column. This is passed to the dialect.ToSqlType() function, which can use the value to alter the generated type for "create table" statements
func (*ColumnMap) SetNotNull ¶
SetNotNull adds "not null" to the create table statements for this column, if nn is true.
func (*ColumnMap) SetTransient ¶
SetTransient allows you to mark the column as transient. If true this column will be skipped when SQL statements are generated
type CustomScanner ¶
type CustomScanner struct { // After a row is scanned, Holder will contain the value from the database column. // Initialize the CustomScanner with the concrete Go type you wish the database // driver to scan the raw column into. Holder interface{} // Target typically holds a pointer to the target struct field to bind the Holder // value to. Target interface{} // Binder is a custom function that converts the holder value to the target type // and sets target accordingly. This function should return error if a problem // occurs converting the holder to the target. Binder func(holder interface{}, target interface{}) error }
CustomScanner binds a database column value to a Go type
func (CustomScanner) Bind ¶
func (me CustomScanner) Bind() error
Bind is called automatically by gorp after Scan()
type DbMap ¶
type DbMap struct { // Db handle to use with this map Db *sql.DB // Dialect implementation to use with this map Dialect Dialect TypeConverter TypeConverter // contains filtered or unexported fields }
DbMap is the root gorp mapping object. Create one of these for each database schema you wish to map. Each DbMap contains a list of mapped tables.
Example:
dialect := gorp.MySQLDialect{"InnoDB", "UTF8"} dbmap := &gorp.DbMap{Db: db, Dialect: dialect}
func (*DbMap) AddTable ¶
AddTable registers the given interface type with gorp. The table name will be given the name of the TypeOf(i). You must call this function, or AddTableWithName, for any struct type you wish to persist with the given DbMap.
This operation is idempotent. If i's type is already mapped, the existing *TableMap is returned
func (*DbMap) AddTableWithName ¶
AddTableWithName has the same behavior as AddTable, but sets table.TableName to name.
func (*DbMap) AddTableWithNameAndSchema ¶
AddTableWithNameAndSchema has the same behavior as AddTable, but sets table.TableName to name.
func (*DbMap) CreateIndex ¶
func (*DbMap) CreateTables ¶
CreateTables iterates through TableMaps registered to this DbMap and executes "create table" statements against the database for each.
This is particularly useful in unit tests where you want to create and destroy the schema automatically.
func (*DbMap) CreateTablesIfNotExists ¶
CreateTablesIfNotExists is similar to CreateTables, but starts each statement with "create table if not exists" so that existing tables do not raise errors
func (*DbMap) Delete ¶
Delete runs a SQL DELETE statement for each element in list. List items must be pointers.
The hook functions PreDelete() and/or PostDelete() will be executed before/after the DELETE statement if the interface defines them.
Returns the number of rows deleted.
Returns an error if SetKeys has not been called on the TableMap Panics if any interface in the list has not been registered with AddTable
func (*DbMap) DropTable ¶
DropTable drops an individual table. Returns an error when the table does not exist.
func (*DbMap) DropTableIfExists ¶
DropTableIfExists drops an individual table when the table exists.
func (*DbMap) DropTables ¶
DropTables iterates through TableMaps registered to this DbMap and executes "drop table" statements against the database for each.
func (*DbMap) DropTablesIfExists ¶
DropTablesIfExists is the same as DropTables, but uses the "if exists" clause to avoid errors for tables that do not exist.
func (*DbMap) Exec ¶
Exec runs an arbitrary SQL statement. args represent the bind parameters. This is equivalent to running: Exec() using database/sql
func (*DbMap) Get ¶
Get runs a SQL SELECT to fetch a single row from the table based on the primary key(s)
i should be an empty value for the struct to load. keys should be the primary key value(s) for the row to load. If multiple keys exist on the table, the order should match the column order specified in SetKeys() when the table mapping was defined.
The hook function PostGet() will be executed after the SELECT statement if the interface defines them.
Returns a pointer to a struct that matches or nil if no row is found.
Returns an error if SetKeys has not been called on the TableMap Panics if any interface in the list has not been registered with AddTable
func (*DbMap) Insert ¶
Insert runs a SQL INSERT statement for each element in list. List items must be pointers.
Any interface whose TableMap has an auto-increment primary key will have its last insert id bound to the PK field on the struct.
The hook functions PreInsert() and/or PostInsert() will be executed before/after the INSERT statement if the interface defines them.
Panics if any interface in the list has not been registered with AddTable
func (*DbMap) Prepare ¶
Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. This is equivalent to running: Prepare() using database/sql
func (*DbMap) Select ¶
Select runs an arbitrary SQL query, binding the columns in the result to fields on the struct specified by i. args represent the bind parameters for the SQL statement.
Column names on the SELECT statement should be aliased to the field names on the struct i. Returns an error if one or more columns in the result do not match. It is OK if fields on i are not part of the SQL statement.
The hook function PostGet() will be executed after the SELECT statement if the interface defines them.
Values are returned in one of two ways: 1. If i is a struct or a pointer to a struct, returns a slice of pointers to matching rows of type i. 2. If i is a pointer to a slice, the results will be appended to that slice and nil returned.
i does NOT need to be registered with AddTable()
func (*DbMap) SelectFloat ¶
SelectFloat is a convenience wrapper around the gorp.SelectFloat function
func (*DbMap) SelectNullFloat ¶
func (m *DbMap) SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error)
SelectNullFloat is a convenience wrapper around the gorp.SelectNullFloat function
func (*DbMap) SelectNullInt ¶
SelectNullInt is a convenience wrapper around the gorp.SelectNullInt function
func (*DbMap) SelectNullStr ¶
func (m *DbMap) SelectNullStr(query string, args ...interface{}) (sql.NullString, error)
SelectNullStr is a convenience wrapper around the gorp.SelectNullStr function
func (*DbMap) TableFor ¶
TableFor returns the *TableMap corresponding to the given Go Type If no table is mapped to that type an error is returned. If checkPK is true and the mapped table has no registered PKs, an error is returned.
func (*DbMap) TraceOn ¶
func (m *DbMap) TraceOn(prefix string, logger GorpLogger)
TraceOn turns on SQL statement logging for this DbMap. After this is called, all SQL statements will be sent to the logger. If prefix is a non-empty string, it will be written to the front of all logged strings, which can aid in filtering log lines.
Use TraceOn if you want to spy on the SQL statements that gorp generates.
Note that the base log.Logger type satisfies GorpLogger, but adapters can easily be written for other logging packages (e.g., the golang-sanctioned glog framework).
func (*DbMap) TruncateTables ¶
TruncateTables iterates through TableMaps registered to this DbMap and executes "truncate table" statements against the database for each, or in the case of sqlite, a "delete from" with no "where" clause, which uses the truncate optimization (http://www.sqlite.org/lang_delete.html)
func (*DbMap) Update ¶
Update runs a SQL UPDATE statement for each element in list. List items must be pointers.
The hook functions PreUpdate() and/or PostUpdate() will be executed before/after the UPDATE statement if the interface defines them.
Returns the number of rows updated.
Returns an error if SetKeys has not been called on the TableMap Panics if any interface in the list has not been registered with AddTable
type Dialect ¶
type Dialect interface { // adds a suffix to any query, usually ";" QuerySuffix() string // ToSqlType returns the SQL column type to use when creating a // table of the given Go Type. maxsize can be used to switch based on // size. For example, in MySQL []byte could map to BLOB, MEDIUMBLOB, // or LONGBLOB depending on the maxsize ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string // string to append to primary key column definitions AutoIncrStr() string // string to bind autoincrement columns to. Empty string will // remove reference to those columns in the INSERT statement. AutoIncrBindValue() string AutoIncrInsertSuffix(col *ColumnMap) string // string to append to "create table" statement for vendor specific // table attributes CreateTableSuffix() string // string to append to "create index" statement CreateIndexSuffix() string // string to append to "drop index" statement DropIndexSuffix() string // string to truncate tables TruncateClause() string // bind variable string to use when forming SQL statements // in many dbs it is "?", but Postgres appears to use $1 // // i is a zero based index of the bind variable in this statement // BindVar(i int) string // Handles quoting of a field name to ensure that it doesn't raise any // SQL parsing exceptions by using a reserved word as a field name. QuoteField(field string) string // Handles building up of a schema.database string that is compatible with // the given dialect // // schema - The schema that <table> lives in // table - The table name QuotedTableForQuery(schema string, table string) string // Existance clause for table creation / deletion IfSchemaNotExists(command, schema string) string IfTableExists(command, schema, table string) string IfTableNotExists(command, schema, table string) string }
The Dialect interface encapsulates behaviors that differ across SQL databases. At present the Dialect is only used by CreateTables() but this could change in the future
type GorpLogger ¶
type GorpLogger interface {
Printf(format string, v ...interface{})
}
type HasPostDelete ¶
type HasPostDelete interface {
PostDelete(SqlExecutor) error
}
PostUpdate() will be executed after the DELETE statement
type HasPostGet ¶
type HasPostGet interface {
PostGet(SqlExecutor) error
}
PostUpdate() will be executed after the GET statement.
type HasPostInsert ¶
type HasPostInsert interface {
PostInsert(SqlExecutor) error
}
PostInsert() will be executed after the INSERT statement
type HasPostUpdate ¶
type HasPostUpdate interface {
PostUpdate(SqlExecutor) error
}
PostUpdate() will be executed after the UPDATE statement
type HasPreDelete ¶
type HasPreDelete interface {
PreDelete(SqlExecutor) error
}
PreDelete() will be executed before the DELETE statement.
type HasPreInsert ¶
type HasPreInsert interface {
PreInsert(SqlExecutor) error
}
PreInsert() will be executed before INSERT statement.
type HasPreUpdate ¶
type HasPreUpdate interface {
PreUpdate(SqlExecutor) error
}
PreUpdate() will be executed before UPDATE statement.
type IndexMap ¶
type IndexMap struct { // Index name in db table IndexName string // If true, " unique" is added to create index statements. // Not used elsewhere Unique bool // Index type supported by Dialect // Postgres: B-tree, Hash, GiST and GIN. // Mysql: Btree, Hash. // Sqlite: nil. IndexType string // contains filtered or unexported fields }
IndexMap represents a mapping between a Go struct field and a single index in a table. Unique and MaxSize only inform the CreateTables() function and are not used by Insert/Update/Delete/Get.
func (*IndexMap) Rename ¶
Rename allows you to specify the index name in the table
Example: table.IndMap("customer_test_idx").Rename("customer_idx")
func (*IndexMap) SetIndexType ¶
SetIndexType specifies the index type supported by chousen SQL Dialect
type IntegerAutoIncrInserter ¶
type IntegerAutoIncrInserter interface {
InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error)
}
IntegerAutoIncrInserter is implemented by dialects that can perform inserts with automatically incremented integer primary keys. If the dialect can handle automatic assignment of more than just integers, see TargetedAutoIncrInserter.
type MySQLDialect ¶
type MySQLDialect struct { // Engine is the storage engine to use "InnoDB" vs "MyISAM" for example Engine string // Encoding is the character encoding to use for created tables Encoding string }
Implementation of Dialect for MySQL databases.
func (MySQLDialect) AutoIncrBindValue ¶
func (d MySQLDialect) AutoIncrBindValue() string
func (MySQLDialect) AutoIncrInsertSuffix ¶
func (d MySQLDialect) AutoIncrInsertSuffix(col *ColumnMap) string
func (MySQLDialect) CreateIndexSuffix ¶
func (m MySQLDialect) CreateIndexSuffix() string
func (MySQLDialect) CreateTableSuffix ¶
func (d MySQLDialect) CreateTableSuffix() string
Returns engine=%s charset=%s based on values stored on struct
func (MySQLDialect) DropIndexSuffix ¶
func (m MySQLDialect) DropIndexSuffix() string
func (MySQLDialect) IfSchemaNotExists ¶
func (d MySQLDialect) IfSchemaNotExists(command, schema string) string
func (MySQLDialect) IfTableExists ¶
func (d MySQLDialect) IfTableExists(command, schema, table string) string
func (MySQLDialect) IfTableNotExists ¶
func (d MySQLDialect) IfTableNotExists(command, schema, table string) string
func (MySQLDialect) InsertAutoIncr ¶
func (d MySQLDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error)
func (MySQLDialect) QuerySuffix ¶
func (d MySQLDialect) QuerySuffix() string
func (MySQLDialect) QuoteField ¶
func (d MySQLDialect) QuoteField(f string) string
func (MySQLDialect) QuotedTableForQuery ¶
func (d MySQLDialect) QuotedTableForQuery(schema string, table string) string
func (MySQLDialect) TruncateClause ¶
func (m MySQLDialect) TruncateClause() string
type NoFieldInTypeError ¶
A non-fatal error, when a select query returns columns that do not exist as fields in the struct it is being mapped to TODO: discuss wether this needs an error. encoding/json silently ignores missing fields
func (*NoFieldInTypeError) Error ¶
func (err *NoFieldInTypeError) Error() string
type NullTime ¶
A nullable Time value
type OptimisticLockError ¶
type OptimisticLockError struct { // Table name where the lock error occurred TableName string // Primary key values of the row being updated/deleted Keys []interface{} // true if a row was found with those keys, indicating the // LocalVersion is stale. false if no value was found with those // keys, suggesting the row has been deleted since loaded, or // was never inserted to begin with RowExists bool // Version value on the struct passed to Update/Delete. This value is // out of sync with the database. LocalVersion int64 }
OptimisticLockError is returned by Update() or Delete() if the struct being modified has a Version field and the value is not equal to the current value in the database
func (OptimisticLockError) Error ¶
func (e OptimisticLockError) Error() string
Error returns a description of the cause of the lock error
type OracleDialect ¶
type OracleDialect struct{}
Implementation of Dialect for Oracle databases.
func (OracleDialect) AutoIncrBindValue ¶
func (d OracleDialect) AutoIncrBindValue() string
func (OracleDialect) AutoIncrInsertSuffix ¶
func (d OracleDialect) AutoIncrInsertSuffix(col *ColumnMap) string
func (OracleDialect) CreateIndexSuffix ¶
func (d OracleDialect) CreateIndexSuffix() string
func (OracleDialect) CreateTableSuffix ¶
func (d OracleDialect) CreateTableSuffix() string
Returns suffix
func (OracleDialect) DropIndexSuffix ¶
func (d OracleDialect) DropIndexSuffix() string
func (OracleDialect) IfSchemaNotExists ¶
func (d OracleDialect) IfSchemaNotExists(command, schema string) string
func (OracleDialect) IfTableExists ¶
func (d OracleDialect) IfTableExists(command, schema, table string) string
func (OracleDialect) IfTableNotExists ¶
func (d OracleDialect) IfTableNotExists(command, schema, table string) string
func (OracleDialect) InsertQueryToTarget ¶
func (d OracleDialect) InsertQueryToTarget(exec SqlExecutor, insertSql, idSql string, target interface{}, params ...interface{}) error
After executing the insert uses the ColMap IdQuery to get the generated id
func (OracleDialect) QuerySuffix ¶
func (d OracleDialect) QuerySuffix() string
func (OracleDialect) QuoteField ¶
func (d OracleDialect) QuoteField(f string) string
func (OracleDialect) QuotedTableForQuery ¶
func (d OracleDialect) QuotedTableForQuery(schema string, table string) string
func (OracleDialect) TruncateClause ¶
func (d OracleDialect) TruncateClause() string
type OracleString ¶
type OracleString struct {
sql.NullString
}
OracleString (empty string is null) TODO: move to dialect/oracle?, rename to String?
func (*OracleString) Scan ¶
func (os *OracleString) Scan(value interface{}) error
Scan implements the Scanner interface.
type PostgresDialect ¶
type PostgresDialect struct {
// contains filtered or unexported fields
}
func (PostgresDialect) AutoIncrBindValue ¶
func (d PostgresDialect) AutoIncrBindValue() string
func (PostgresDialect) AutoIncrInsertSuffix ¶
func (d PostgresDialect) AutoIncrInsertSuffix(col *ColumnMap) string
func (PostgresDialect) AutoIncrStr ¶
func (d PostgresDialect) AutoIncrStr() string
Returns empty string
func (PostgresDialect) CreateIndexSuffix ¶
func (d PostgresDialect) CreateIndexSuffix() string
func (PostgresDialect) CreateTableSuffix ¶
func (d PostgresDialect) CreateTableSuffix() string
Returns suffix
func (PostgresDialect) DropIndexSuffix ¶
func (d PostgresDialect) DropIndexSuffix() string
func (PostgresDialect) IfSchemaNotExists ¶
func (d PostgresDialect) IfSchemaNotExists(command, schema string) string
func (PostgresDialect) IfTableExists ¶
func (d PostgresDialect) IfTableExists(command, schema, table string) string
func (PostgresDialect) IfTableNotExists ¶
func (d PostgresDialect) IfTableNotExists(command, schema, table string) string
func (PostgresDialect) InsertAutoIncrToTarget ¶
func (d PostgresDialect) InsertAutoIncrToTarget(exec SqlExecutor, insertSql string, target interface{}, params ...interface{}) error
func (PostgresDialect) QuerySuffix ¶
func (d PostgresDialect) QuerySuffix() string
func (PostgresDialect) QuoteField ¶
func (d PostgresDialect) QuoteField(f string) string
func (PostgresDialect) QuotedTableForQuery ¶
func (d PostgresDialect) QuotedTableForQuery(schema string, table string) string
func (PostgresDialect) TruncateClause ¶
func (d PostgresDialect) TruncateClause() string
type SqlExecutor ¶
type SqlExecutor interface { Get(i interface{}, keys ...interface{}) (interface{}, error) Insert(list ...interface{}) error Update(list ...interface{}) (int64, error) Delete(list ...interface{}) (int64, error) Exec(query string, args ...interface{}) (sql.Result, error) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error) SelectInt(query string, args ...interface{}) (int64, error) SelectNullInt(query string, args ...interface{}) (sql.NullInt64, error) SelectFloat(query string, args ...interface{}) (float64, error) SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error) SelectStr(query string, args ...interface{}) (string, error) SelectNullStr(query string, args ...interface{}) (sql.NullString, error) SelectOne(holder interface{}, query string, args ...interface{}) error // contains filtered or unexported methods }
SqlExecutor exposes gorp operations that can be run from Pre/Post hooks. This hides whether the current operation that triggered the hook is in a transaction.
See the DbMap function docs for each of the functions below for more information.
type SqlServerDialect ¶
type SqlServerDialect struct { // If set to "2005" legacy datatypes will be used Version string }
func (SqlServerDialect) AutoIncrBindValue ¶
func (d SqlServerDialect) AutoIncrBindValue() string
Empty string removes autoincrement columns from the INSERT statements.
func (SqlServerDialect) AutoIncrInsertSuffix ¶
func (d SqlServerDialect) AutoIncrInsertSuffix(col *ColumnMap) string
func (SqlServerDialect) AutoIncrStr ¶
func (d SqlServerDialect) AutoIncrStr() string
Returns auto_increment
func (SqlServerDialect) CreateIndexSuffix ¶
func (d SqlServerDialect) CreateIndexSuffix() string
func (SqlServerDialect) CreateTableSuffix ¶
func (d SqlServerDialect) CreateTableSuffix() string
func (SqlServerDialect) DropIndexSuffix ¶
func (d SqlServerDialect) DropIndexSuffix() string
func (SqlServerDialect) IfSchemaNotExists ¶
func (d SqlServerDialect) IfSchemaNotExists(command, schema string) string
func (SqlServerDialect) IfTableExists ¶
func (d SqlServerDialect) IfTableExists(command, schema, table string) string
func (SqlServerDialect) IfTableNotExists ¶
func (d SqlServerDialect) IfTableNotExists(command, schema, table string) string
func (SqlServerDialect) InsertAutoIncr ¶
func (d SqlServerDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error)
func (SqlServerDialect) QuerySuffix ¶
func (d SqlServerDialect) QuerySuffix() string
func (SqlServerDialect) QuoteField ¶
func (d SqlServerDialect) QuoteField(f string) string
func (SqlServerDialect) QuotedTableForQuery ¶
func (d SqlServerDialect) QuotedTableForQuery(schema string, table string) string
func (SqlServerDialect) TruncateClause ¶
func (d SqlServerDialect) TruncateClause() string
type SqlTyper ¶
SqlTyper is a type that returns its database type. Most of the time, the type can just use "database/sql/driver".Valuer; but when it returns nil for its empty value, it needs to implement SqlTyper to have its column type detected properly during table creation.
type SqliteDialect ¶
type SqliteDialect struct {
// contains filtered or unexported fields
}
func (SqliteDialect) AutoIncrBindValue ¶
func (d SqliteDialect) AutoIncrBindValue() string
func (SqliteDialect) AutoIncrInsertSuffix ¶
func (d SqliteDialect) AutoIncrInsertSuffix(col *ColumnMap) string
func (SqliteDialect) AutoIncrStr ¶
func (d SqliteDialect) AutoIncrStr() string
Returns autoincrement
func (SqliteDialect) CreateIndexSuffix ¶
func (d SqliteDialect) CreateIndexSuffix() string
func (SqliteDialect) CreateTableSuffix ¶
func (d SqliteDialect) CreateTableSuffix() string
Returns suffix
func (SqliteDialect) DropIndexSuffix ¶
func (d SqliteDialect) DropIndexSuffix() string
func (SqliteDialect) IfSchemaNotExists ¶
func (d SqliteDialect) IfSchemaNotExists(command, schema string) string
func (SqliteDialect) IfTableExists ¶
func (d SqliteDialect) IfTableExists(command, schema, table string) string
func (SqliteDialect) IfTableNotExists ¶
func (d SqliteDialect) IfTableNotExists(command, schema, table string) string
func (SqliteDialect) InsertAutoIncr ¶
func (d SqliteDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error)
func (SqliteDialect) QuerySuffix ¶
func (d SqliteDialect) QuerySuffix() string
func (SqliteDialect) QuoteField ¶
func (d SqliteDialect) QuoteField(f string) string
func (SqliteDialect) QuotedTableForQuery ¶
func (d SqliteDialect) QuotedTableForQuery(schema string, table string) string
sqlite does not have schemas like PostgreSQL does, so just escape it like normal
func (SqliteDialect) TruncateClause ¶
func (d SqliteDialect) TruncateClause() string
With sqlite, there technically isn't a TRUNCATE statement, but a DELETE FROM uses a truncate optimization: http://www.sqlite.org/lang_delete.html
type TableMap ¶
type TableMap struct { // Name of database table. TableName string SchemaName string Columns []*ColumnMap // contains filtered or unexported fields }
TableMap represents a mapping between a Go struct and a database table Use dbmap.AddTable() or dbmap.AddTableWithName() to create these
func (*TableMap) AddIndex ¶
AddIndex registers the index with gorp for specified table with given parameters. This operation is idempotent. If index is already mapped, the existing *IndexMap is returned Function will panic if one of the given for index columns does not exists
Automatically calls ResetSql() to ensure SQL statements are regenerated.
func (*TableMap) ColMap ¶
ColMap returns the ColumnMap pointer matching the given struct field name. It panics if the struct does not contain a field matching this name.
func (*TableMap) ResetSql ¶
func (t *TableMap) ResetSql()
ResetSql removes cached insert/update/select/delete SQL strings associated with this TableMap. Call this if you've modified any column names or the table name itself.
func (*TableMap) SetKeys ¶
SetKeys lets you specify the fields on a struct that map to primary key columns on the table. If isAutoIncr is set, result.LastInsertId() will be used after INSERT to bind the generated id to the Go struct.
Automatically calls ResetSql() to ensure SQL statements are regenerated.
Panics if isAutoIncr is true, and fieldNames length != 1
func (*TableMap) SetUniqueTogether ¶
SetUniqueTogether lets you specify uniqueness constraints across multiple columns on the table. Each call adds an additional constraint for the specified columns.
Automatically calls ResetSql() to ensure SQL statements are regenerated.
Panics if fieldNames length < 2.
func (*TableMap) SetVersionCol ¶
SetVersionCol sets the column to use as the Version field. By default the "Version" field is used. Returns the column found, or panics if the struct does not contain a field matching this name.
Automatically calls ResetSql() to ensure SQL statements are regenerated.
func (*TableMap) SqlForCreate ¶
SqlForCreateTable gets a sequence of SQL commands that will create the specified table and any associated schema
type TargetQueryInserter ¶
type TargetQueryInserter interface { // TargetQueryInserter runs an insert operation and assigns the // automatically generated primary key retrived by the query // extracted from the GeneratedIdQuery field of the id column. InsertQueryToTarget(exec SqlExecutor, insertSql, idSql string, target interface{}, params ...interface{}) error }
TargetQueryInserter is implemented by dialects that can perform assignment of integer primary key type by executing a query like "select sequence.currval from dual".
type TargetedAutoIncrInserter ¶
type TargetedAutoIncrInserter interface { // InsertAutoIncrToTarget runs an insert operation and assigns the // automatically generated primary key directly to the passed in // target. The target should be a pointer to the primary key // field of the value being inserted. InsertAutoIncrToTarget(exec SqlExecutor, insertSql string, target interface{}, params ...interface{}) error }
TargetedAutoIncrInserter is implemented by dialects that can perform automatic assignment of any primary key type (i.e. strings for uuids, integers for serials, etc).
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction represents a database transaction. Insert/Update/Delete/Get/Exec operations will be run in the context of that transaction. Transactions should be terminated with a call to Commit() or Rollback()
func (*Transaction) Commit ¶
func (t *Transaction) Commit() error
Commit commits the underlying database transaction.
func (*Transaction) Delete ¶
func (t *Transaction) Delete(list ...interface{}) (int64, error)
Delete has the same behavior as DbMap.Delete(), but runs in a transaction.
func (*Transaction) Exec ¶
func (t *Transaction) Exec(query string, args ...interface{}) (sql.Result, error)
Exec has the same behavior as DbMap.Exec(), but runs in a transaction.
func (*Transaction) Get ¶
func (t *Transaction) Get(i interface{}, keys ...interface{}) (interface{}, error)
Get has the same behavior as DbMap.Get(), but runs in a transaction.
func (*Transaction) Insert ¶
func (t *Transaction) Insert(list ...interface{}) error
Insert has the same behavior as DbMap.Insert(), but runs in a transaction.
func (*Transaction) Prepare ¶
func (t *Transaction) Prepare(query string) (*sql.Stmt, error)
Prepare has the same behavior as DbMap.Prepare(), but runs in a transaction.
func (*Transaction) ReleaseSavepoint ¶
func (t *Transaction) ReleaseSavepoint(savepoint string) error
ReleaseSavepint releases the savepoint with the given name. The name is interpolated directly into the SQL SAVEPOINT statement, so you must sanitize it if it is derived from user input.
func (*Transaction) Rollback ¶
func (t *Transaction) Rollback() error
Rollback rolls back the underlying database transaction.
func (*Transaction) RollbackToSavepoint ¶
func (t *Transaction) RollbackToSavepoint(savepoint string) error
RollbackToSavepoint rolls back to the savepoint with the given name. The name is interpolated directly into the SQL SAVEPOINT statement, so you must sanitize it if it is derived from user input.
func (*Transaction) Savepoint ¶
func (t *Transaction) Savepoint(name string) error
Savepoint creates a savepoint with the given name. The name is interpolated directly into the SQL SAVEPOINT statement, so you must sanitize it if it is derived from user input.
func (*Transaction) Select ¶
func (t *Transaction) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error)
Select has the same behavior as DbMap.Select(), but runs in a transaction.
func (*Transaction) SelectFloat ¶
func (t *Transaction) SelectFloat(query string, args ...interface{}) (float64, error)
SelectFloat is a convenience wrapper around the gorp.SelectFloat function.
func (*Transaction) SelectInt ¶
func (t *Transaction) SelectInt(query string, args ...interface{}) (int64, error)
SelectInt is a convenience wrapper around the gorp.SelectInt function.
func (*Transaction) SelectNullFloat ¶
func (t *Transaction) SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error)
SelectNullFloat is a convenience wrapper around the gorp.SelectNullFloat function.
func (*Transaction) SelectNullInt ¶
func (t *Transaction) SelectNullInt(query string, args ...interface{}) (sql.NullInt64, error)
SelectNullInt is a convenience wrapper around the gorp.SelectNullInt function.
func (*Transaction) SelectNullStr ¶
func (t *Transaction) SelectNullStr(query string, args ...interface{}) (sql.NullString, error)
SelectNullStr is a convenience wrapper around the gorp.SelectNullStr function.
func (*Transaction) SelectOne ¶
func (t *Transaction) SelectOne(holder interface{}, query string, args ...interface{}) error
SelectOne is a convenience wrapper around the gorp.SelectOne function.
func (*Transaction) SelectStr ¶
func (t *Transaction) SelectStr(query string, args ...interface{}) (string, error)
SelectStr is a convenience wrapper around the gorp.SelectStr function.
func (*Transaction) Update ¶
func (t *Transaction) Update(list ...interface{}) (int64, error)
Update had the same behavior as DbMap.Update(), but runs in a transaction.
type TypeConverter ¶
type TypeConverter interface { // ToDb converts val to another type. Called before INSERT/UPDATE operations ToDb(val interface{}) (interface{}, error) // FromDb returns a CustomScanner appropriate for this type. This will be used // to hold values returned from SELECT queries. // // In particular the CustomScanner returned should implement a Binder // function appropriate for the Go type you wish to convert the db value to // // If bool==false, then no custom scanner will be used for this field. FromDb(target interface{}) (CustomScanner, bool) }
The TypeConverter interface provides a way to map a value of one type to another type when persisting to, or loading from, a database.
Example use cases: Implement type converter to convert bool types to "y"/"n" strings, or serialize a struct member as a JSON blob.