Documentation ¶
Overview ¶
Package vanilla provides a re-usable table API.
Index ¶
- Constants
- type Record
- type RecordTable
- func (tbl RecordTable) BeginTx(opts *sql.TxOptions) (RecordTable, error)
- func (tbl RecordTable) Constraints() constraint.Constraints
- func (tbl RecordTable) Count(wh where.Expression) (count int64, err error)
- func (tbl RecordTable) CountWhere(where string, args ...interface{}) (count int64, err error)
- func (tbl RecordTable) Ctx() context.Context
- func (tbl RecordTable) DB() *sql.DB
- func (tbl RecordTable) Database() *sqlapi.Database
- func (tbl RecordTable) Delete(req require.Requirement, wh where.Expression) (int64, error)
- func (tbl RecordTable) DeleteRecords(req require.Requirement, id ...int64) (int64, error)
- func (tbl RecordTable) Dialect() schema.Dialect
- func (tbl RecordTable) Exec(req require.Requirement, query string, args ...interface{}) (int64, error)
- func (tbl RecordTable) Execer() sqlapi.Execer
- func (tbl RecordTable) Fetch(req require.Requirement, query string, args ...interface{}) ([]*Record, error)
- func (tbl RecordTable) GetRecordById(req require.Requirement, id int64) (*Record, error)
- func (tbl RecordTable) GetRecordsById(req require.Requirement, id ...int64) (list []*Record, err error)
- func (tbl RecordTable) IsTx() bool
- func (tbl RecordTable) Logger() *log.Logger
- func (tbl RecordTable) Name() sqlapi.TableName
- func (tbl RecordTable) PkColumn() string
- func (tbl RecordTable) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (tbl RecordTable) QueryOneNullFloat64(req require.Requirement, query string, args ...interface{}) (result sql.NullFloat64, err error)
- func (tbl RecordTable) QueryOneNullInt64(req require.Requirement, query string, args ...interface{}) (result sql.NullInt64, err error)
- func (tbl RecordTable) QueryOneNullString(req require.Requirement, query string, args ...interface{}) (result sql.NullString, err error)
- func (tbl RecordTable) Select(req require.Requirement, wh where.Expression, qc where.QueryConstraint) ([]*Record, error)
- func (tbl RecordTable) SelectOne(req require.Requirement, wh where.Expression, qc where.QueryConstraint) (*Record, error)
- func (tbl RecordTable) SelectOneWhere(req require.Requirement, where, orderBy string, args ...interface{}) (*Record, error)
- func (tbl RecordTable) SelectWhere(req require.Requirement, where, orderBy string, args ...interface{}) ([]*Record, error)
- func (tbl RecordTable) SetPkColumn(pk string) RecordTable
- func (tbl RecordTable) SliceFloat32Column(req require.Requirement, column string, wh where.Expression, ...) ([]float32, error)
- func (tbl RecordTable) SliceFloat64Column(req require.Requirement, column string, wh where.Expression, ...) ([]float64, error)
- func (tbl RecordTable) SliceId(req require.Requirement, wh where.Expression, qc where.QueryConstraint) ([]int64, error)
- func (tbl RecordTable) SliceInt16Column(req require.Requirement, column string, wh where.Expression, ...) ([]int16, error)
- func (tbl RecordTable) SliceInt32Column(req require.Requirement, column string, wh where.Expression, ...) ([]int32, error)
- func (tbl RecordTable) SliceInt64Column(req require.Requirement, column string, wh where.Expression, ...) ([]int64, error)
- func (tbl RecordTable) SliceInt8Column(req require.Requirement, column string, wh where.Expression, ...) ([]int8, error)
- func (tbl RecordTable) SliceIntColumn(req require.Requirement, column string, wh where.Expression, ...) ([]int, error)
- func (tbl RecordTable) SliceStringColumn(req require.Requirement, column string, wh where.Expression, ...) ([]string, error)
- func (tbl RecordTable) SliceUint16Column(req require.Requirement, column string, wh where.Expression, ...) ([]uint16, error)
- func (tbl RecordTable) SliceUint32Column(req require.Requirement, column string, wh where.Expression, ...) ([]uint32, error)
- func (tbl RecordTable) SliceUint64Column(req require.Requirement, column string, wh where.Expression, ...) ([]uint64, error)
- func (tbl RecordTable) SliceUint8Column(req require.Requirement, column string, wh where.Expression, ...) ([]uint8, error)
- func (tbl RecordTable) SliceUintColumn(req require.Requirement, column string, wh where.Expression, ...) ([]uint, error)
- func (tbl RecordTable) Tx() *sql.Tx
- func (tbl RecordTable) Using(tx *sql.Tx) RecordTable
- func (tbl RecordTable) WithConstraint(cc ...constraint.Constraint) RecordTable
- func (tbl RecordTable) WithContext(ctx context.Context) RecordTable
- func (tbl RecordTable) WithPrefix(pfx string) RecordTable
Constants ¶
const NumRecordColumns = 1
const NumRecordDataColumns = 0
const RecordColumnNames = "id"
const RecordDataColumnNames = ""
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Record ¶
type Record struct {
Id int64 `sql:"pk: true, auto: true"`
}
Record provides access to the primary key only; all other database columns are ignored. This is useful in situations where identity is the only concern.
type RecordTable ¶
type RecordTable struct {
// contains filtered or unexported fields
}
RecordTable holds a given table name with the database reference, providing access methods below. The Prefix field is often blank but can be used to hold a table name prefix (e.g. ending in '_'). Or it can specify the name of the schema, in which case it should have a trailing '.'.
func CopyTableAsRecordTable ¶
func CopyTableAsRecordTable(origin sqlapi.Table) RecordTable
CopyTableAsRecordTable copies a table instance, retaining the name etc but providing methods appropriate for 'Record'. It doesn't copy the constraints of the original table.
It serves to provide methods appropriate for 'Record'. This is most useful when this is used to represent a join result. In such cases, there won't be any need for DDL methods, nor Exec, Insert, Update or Delete.
func NewRecordTable ¶
func NewRecordTable(name string, d *sqlapi.Database) RecordTable
NewRecordTable returns a new table instance. If a blank table name is supplied, the default name "records" will be used instead. The request context is initialised with the background.
func (RecordTable) BeginTx ¶
func (tbl RecordTable) BeginTx(opts *sql.TxOptions) (RecordTable, error)
BeginTx starts a transaction using the table's context. This context is used until the transaction is committed or rolled back.
If this context is cancelled, the sql package will roll back the transaction. In this case, Tx.Commit will then return an error.
The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.
Panics if the Execer is not TxStarter.
func (RecordTable) Constraints ¶
func (tbl RecordTable) Constraints() constraint.Constraints
Constraints returns the table's constraints.
func (RecordTable) Count ¶
func (tbl RecordTable) Count(wh where.Expression) (count int64, err error)
Count counts the Records in the table that match a 'where' clause. Use a nil value for the 'wh' argument if it is not needed.
func (RecordTable) CountWhere ¶
func (tbl RecordTable) CountWhere(where string, args ...interface{}) (count int64, err error)
CountWhere counts Records in the table that match a 'where' clause. Use a blank string for the 'where' argument if it is not needed.
The args are for any placeholder parameters in the query.
func (RecordTable) Ctx ¶
func (tbl RecordTable) Ctx() context.Context
Ctx gets the current request context.
func (RecordTable) DB ¶
func (tbl RecordTable) DB() *sql.DB
DB gets the wrapped database handle, provided this is not within a transaction. Panics if it is in the wrong state - use IsTx() if necessary.
func (RecordTable) Database ¶
func (tbl RecordTable) Database() *sqlapi.Database
Database gets the shared database information.
func (RecordTable) Delete ¶
func (tbl RecordTable) Delete(req require.Requirement, wh where.Expression) (int64, error)
Delete deletes one or more rows from the table, given a 'where' clause. Use a nil value for the 'wh' argument if it is not needed (very risky!).
func (RecordTable) DeleteRecords ¶
func (tbl RecordTable) DeleteRecords(req require.Requirement, id ...int64) (int64, error)
DeleteRecords deletes rows from the table, given some primary keys. The list of ids can be arbitrarily long.
func (RecordTable) Dialect ¶
func (tbl RecordTable) Dialect() schema.Dialect
Dialect gets the database dialect.
func (RecordTable) Exec ¶
func (tbl RecordTable) Exec(req require.Requirement, query string, args ...interface{}) (int64, error)
Exec executes a query without returning any rows. It returns the number of rows affected (if the database driver supports this).
The args are for any placeholder parameters in the query.
func (RecordTable) Execer ¶
func (tbl RecordTable) Execer() sqlapi.Execer
Execer gets the wrapped database or transaction handle.
func (RecordTable) Fetch ¶
func (tbl RecordTable) Fetch(req require.Requirement, query string, args ...interface{}) ([]*Record, error)
Fetch fetches a list of Record based on a supplied query. This is mostly used for join queries that map its result columns to the fields of Record. Other queries might be better handled by GetXxx or Select methods.
func (RecordTable) GetRecordById ¶
func (tbl RecordTable) GetRecordById(req require.Requirement, id int64) (*Record, error)
GetRecordById gets the record with a given primary key value. If not found, *Record will be nil.
func (RecordTable) GetRecordsById ¶
func (tbl RecordTable) GetRecordsById(req require.Requirement, id ...int64) (list []*Record, err error)
GetRecordsById gets records from the table according to a list of primary keys. Although the list of ids can be arbitrarily long, there are practical limits; note that Oracle DB has a limit of 1000.
It places a requirement, which may be nil, on the size of the expected results: in particular, require.All controls whether an error is generated not all the ids produce a result.
func (RecordTable) IsTx ¶
func (tbl RecordTable) IsTx() bool
IsTx tests whether this is within a transaction.
func (RecordTable) Logger ¶
func (tbl RecordTable) Logger() *log.Logger
Logger gets the trace logger.
func (RecordTable) PkColumn ¶
func (tbl RecordTable) PkColumn() string
PkColumn gets the column name used as a primary key.
func (RecordTable) Query ¶
func (tbl RecordTable) Query(query string, args ...interface{}) (*sql.Rows, error)
Query is the low-level request method for this table. The query is logged using whatever logger is configured. If an error arises, this too is logged.
If you need a context other than the background, use WithContext before calling Query.
The args are for any placeholder parameters in the query.
The caller must call rows.Close() on the result.
Wrap the result in *sqlapi.Rows if you need to access its data as a map.
func (RecordTable) QueryOneNullFloat64 ¶
func (tbl RecordTable) QueryOneNullFloat64(req require.Requirement, query string, args ...interface{}) (result sql.NullFloat64, err error)
QueryOneNullFloat64 is a low-level access method for one float64. This can be used for 'AVG(...)' queries and such like. If the query selected many rows, only the first is returned; the rest are discarded. If not found, the result will be invalid.
Note that this applies ReplaceTableName to the query string.
The args are for any placeholder parameters in the query.
func (RecordTable) QueryOneNullInt64 ¶
func (tbl RecordTable) QueryOneNullInt64(req require.Requirement, query string, args ...interface{}) (result sql.NullInt64, err error)
QueryOneNullInt64 is a low-level access method for one int64. This can be used for 'COUNT(1)' queries and such like. If the query selected many rows, only the first is returned; the rest are discarded. If not found, the result will be invalid.
Note that this applies ReplaceTableName to the query string.
The args are for any placeholder parameters in the query.
func (RecordTable) QueryOneNullString ¶
func (tbl RecordTable) QueryOneNullString(req require.Requirement, query string, args ...interface{}) (result sql.NullString, err error)
QueryOneNullString is a low-level access method for one string. This can be used for function queries and such like. If the query selected many rows, only the first is returned; the rest are discarded. If not found, the result will be invalid.
Note that this applies ReplaceTableName to the query string.
The args are for any placeholder parameters in the query.
func (RecordTable) Select ¶
func (tbl RecordTable) Select(req require.Requirement, wh where.Expression, qc where.QueryConstraint) ([]*Record, error)
Select allows Records to be obtained from the table that match a 'where' clause. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
It places a requirement, which may be nil, on the size of the expected results: for example require.AtLeastOne controls whether an error is generated when no result is found.
func (RecordTable) SelectOne ¶
func (tbl RecordTable) SelectOne(req require.Requirement, wh where.Expression, qc where.QueryConstraint) (*Record, error)
SelectOne allows a single Record to be obtained from the sqlapi. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed. If not found, *Example will be nil.
It places a requirement, which may be nil, on the size of the expected results: for example require.One controls whether an error is generated when no result is found.
func (RecordTable) SelectOneWhere ¶
func (tbl RecordTable) SelectOneWhere(req require.Requirement, where, orderBy string, args ...interface{}) (*Record, error)
SelectOneWhere allows a single Example to be obtained from the table that match a 'where' clause and some limit. Any order, limit or offset clauses can be supplied in 'orderBy'. Use blank strings for the 'where' and/or 'orderBy' arguments if they are not needed. If not found, *Example will be nil.
It places a requirement, which may be nil, on the size of the expected results: for example require.One controls whether an error is generated when no result is found.
The args are for any placeholder parameters in the query.
func (RecordTable) SelectWhere ¶
func (tbl RecordTable) SelectWhere(req require.Requirement, where, orderBy string, args ...interface{}) ([]*Record, error)
SelectWhere allows Records to be obtained from the table that match a 'where' clause. Any order, limit or offset clauses can be supplied in 'orderBy'. Use blank strings for the 'where' and/or 'orderBy' arguments if they are not needed.
It places a requirement, which may be nil, on the size of the expected results: for example require.AtLeastOne controls whether an error is generated when no result is found.
The args are for any placeholder parameters in the query.
func (RecordTable) SetPkColumn ¶
func (tbl RecordTable) SetPkColumn(pk string) RecordTable
SetPkColumn sets the name of the primary key column. It defaults to "id". The result is a modified copy of the table; the original is unchanged.
func (RecordTable) SliceFloat32Column ¶
func (tbl RecordTable) SliceFloat32Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]float32, error)
SliceFloat32Column gets a float32 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceFloat64Column ¶
func (tbl RecordTable) SliceFloat64Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]float64, error)
SliceFloat64Column gets a float64 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceId ¶
func (tbl RecordTable) SliceId(req require.Requirement, wh where.Expression, qc where.QueryConstraint) ([]int64, error)
SliceId gets the id column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceInt16Column ¶
func (tbl RecordTable) SliceInt16Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]int16, error)
SliceInt16Column gets an int16 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceInt32Column ¶
func (tbl RecordTable) SliceInt32Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]int32, error)
SliceInt32Column gets an int32 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceInt64Column ¶
func (tbl RecordTable) SliceInt64Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]int64, error)
SliceInt64Column gets an int64 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceInt8Column ¶
func (tbl RecordTable) SliceInt8Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]int8, error)
SliceInt8Column gets an int8 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceIntColumn ¶
func (tbl RecordTable) SliceIntColumn(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]int, error)
SliceIntColumn gets an int column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceStringColumn ¶
func (tbl RecordTable) SliceStringColumn(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]string, error)
SliceStringColumn gets a string column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceUint16Column ¶
func (tbl RecordTable) SliceUint16Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]uint16, error)
SliceUint16Column gets a uint16 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceUint32Column ¶
func (tbl RecordTable) SliceUint32Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]uint32, error)
SliceUint32Column gets a uint32 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceUint64Column ¶
func (tbl RecordTable) SliceUint64Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]uint64, error)
SliceUint64Column gets a uint64 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceUint8Column ¶
func (tbl RecordTable) SliceUint8Column(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]uint8, error)
SliceUint8Column gets a uint8 column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) SliceUintColumn ¶
func (tbl RecordTable) SliceUintColumn(req require.Requirement, column string, wh where.Expression, qc where.QueryConstraint) ([]uint, error)
SliceUintColumn gets a uint column for all rows that match the 'where' condition. Any order, limit or offset clauses can be supplied in query constraint 'qc'. Use nil values for the 'wh' and/or 'qc' arguments if they are not needed.
func (RecordTable) Tx ¶
func (tbl RecordTable) Tx() *sql.Tx
Tx gets the wrapped transaction handle, provided this is within a transaction. Panics if it is in the wrong state - use IsTx() if necessary.
func (RecordTable) Using ¶
func (tbl RecordTable) Using(tx *sql.Tx) RecordTable
Using returns a modified Table using the transaction supplied. This is needed when making multiple queries across several tables within a single transaction. The result is a modified copy of the table; the original is unchanged.
func (RecordTable) WithConstraint ¶
func (tbl RecordTable) WithConstraint(cc ...constraint.Constraint) RecordTable
WithConstraint returns a modified Table with added data consistency constraints.
func (RecordTable) WithContext ¶
func (tbl RecordTable) WithContext(ctx context.Context) RecordTable
WithContext sets the context for subsequent queries via this table. The result is a modified copy of the table; the original is unchanged.
The shared context in the *Database is not altered by this method. So it is possible to use different contexts for different (groups of) queries.
func (RecordTable) WithPrefix ¶
func (tbl RecordTable) WithPrefix(pfx string) RecordTable
WithPrefix sets the table name prefix for subsequent queries. The result is a modified copy of the table; the original is unchanged.