vanilla

package
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2018 License: BSD-2-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package vanilla provides a re-usable table API.

Index

Constants

View Source
const NumPrimaryKeyColumns = 1
View Source
const NumPrimaryKeyDataColumns = 0
View Source
const PrimaryKeyColumnNames = "id"
View Source
const PrimaryKeyDataColumnNames = ""

Variables

This section is empty.

Functions

This section is empty.

Types

type PrimaryKey

type PrimaryKey struct {
	Id int64 `sql:"pk: true, auto: true"`
}

PrimaryKey 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 PrimaryKeyTable

type PrimaryKeyTable struct {
	// contains filtered or unexported fields
}

PrimaryKeyTable 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 CopyTableAsPrimaryKeyTable

func CopyTableAsPrimaryKeyTable(origin sqlgen2.Table) PrimaryKeyTable

CopyTableAsPrimaryKeyTable copies a table instance, retaining the name etc but providing methods appropriate for 'PrimaryKey'. It doesn't copy the constraints of the original table.

It serves to provide methods appropriate for 'PrimaryKey'. 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 NewPrimaryKeyTable

func NewPrimaryKeyTable(name string, d *sqlgen2.Database) PrimaryKeyTable

NewPrimaryKeyTable returns a new table instance. If a blank table name is supplied, the default name "primarykeys" will be used instead. The request context is initialised with the background.

func (PrimaryKeyTable) BeginTx

func (tbl PrimaryKeyTable) BeginTx(opts *sql.TxOptions) (PrimaryKeyTable, 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 (PrimaryKeyTable) Constraints added in v0.14.0

func (tbl PrimaryKeyTable) Constraints() constraint.Constraints

Constraints returns the table's constraints.

func (PrimaryKeyTable) Count

func (tbl PrimaryKeyTable) Count(wh where.Expression) (count int64, err error)

Count counts the PrimaryKeys in the table that match a 'where' clause. Use a nil value for the 'wh' argument if it is not needed.

func (PrimaryKeyTable) CountWhere

func (tbl PrimaryKeyTable) CountWhere(where string, args ...interface{}) (count int64, err error)

CountWhere counts PrimaryKeys 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 (PrimaryKeyTable) Ctx

func (tbl PrimaryKeyTable) Ctx() context.Context

Ctx gets the current request context.

func (PrimaryKeyTable) DB

func (tbl PrimaryKeyTable) 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 (PrimaryKeyTable) Database

func (tbl PrimaryKeyTable) Database() *sqlgen2.Database

Database gets the shared database information.

func (PrimaryKeyTable) Delete

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 (PrimaryKeyTable) DeletePrimaryKeys added in v0.25.0

func (tbl PrimaryKeyTable) DeletePrimaryKeys(req require.Requirement, id ...int64) (int64, error)

DeletePrimaryKeys deletes rows from the table, given some primary keys. The list of ids can be arbitrarily long.

func (PrimaryKeyTable) Dialect

func (tbl PrimaryKeyTable) Dialect() schema.Dialect

Dialect gets the database dialect.

func (PrimaryKeyTable) Exec

func (tbl PrimaryKeyTable) 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 (PrimaryKeyTable) Execer

func (tbl PrimaryKeyTable) Execer() sqlgen2.Execer

Execer gets the wrapped database or transaction handle.

func (PrimaryKeyTable) Fetch added in v0.25.0

func (tbl PrimaryKeyTable) Fetch(req require.Requirement, query string, args ...interface{}) ([]*PrimaryKey, error)

Fetch fetches a list of PrimaryKey based on a supplied query. This is mostly used for join queries that map its result columns to the fields of PrimaryKey. Other queries might be better handled by GetXxx or Select methods.

func (PrimaryKeyTable) GetPrimaryKeyById added in v0.25.0

func (tbl PrimaryKeyTable) GetPrimaryKeyById(req require.Requirement, id int64) (*PrimaryKey, error)

GetPrimaryKeyById gets the record with a given primary key value. If not found, *PrimaryKey will be nil.

func (PrimaryKeyTable) GetPrimaryKeysById added in v0.25.0

func (tbl PrimaryKeyTable) GetPrimaryKeysById(req require.Requirement, id ...int64) (list []*PrimaryKey, err error)

GetPrimaryKeysById 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 (PrimaryKeyTable) IsTx

func (tbl PrimaryKeyTable) IsTx() bool

IsTx tests whether this is within a transaction.

func (PrimaryKeyTable) Logger

func (tbl PrimaryKeyTable) Logger() *log.Logger

Logger gets the trace logger.

func (PrimaryKeyTable) Name

func (tbl PrimaryKeyTable) Name() sqlgen2.TableName

Name gets the table name.

func (PrimaryKeyTable) PkColumn added in v0.25.0

func (tbl PrimaryKeyTable) PkColumn() string

PkColumn gets the column name used as a primary key.

func (PrimaryKeyTable) Query

func (tbl PrimaryKeyTable) 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 *sqlgen2.Rows if you need to access its data as a map.

func (PrimaryKeyTable) QueryOneNullFloat64

func (tbl PrimaryKeyTable) 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 (PrimaryKeyTable) QueryOneNullInt64

func (tbl PrimaryKeyTable) 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 (PrimaryKeyTable) QueryOneNullString

func (tbl PrimaryKeyTable) 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 (PrimaryKeyTable) Select

Select allows PrimaryKeys 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 (PrimaryKeyTable) SelectOne

SelectOne allows a single PrimaryKey to be obtained from the sqlgen2. 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 (PrimaryKeyTable) SelectOneWhere

func (tbl PrimaryKeyTable) SelectOneWhere(req require.Requirement, where, orderBy string, args ...interface{}) (*PrimaryKey, 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 (PrimaryKeyTable) SelectWhere

func (tbl PrimaryKeyTable) SelectWhere(req require.Requirement, where, orderBy string, args ...interface{}) ([]*PrimaryKey, error)

SelectWhere allows PrimaryKeys 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 (PrimaryKeyTable) SetPkColumn

func (tbl PrimaryKeyTable) SetPkColumn(pk string) PrimaryKeyTable

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 (PrimaryKeyTable) SliceId

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 (PrimaryKeyTable) Tx

func (tbl PrimaryKeyTable) 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 (PrimaryKeyTable) Using

func (tbl PrimaryKeyTable) Using(tx *sql.Tx) PrimaryKeyTable

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 (PrimaryKeyTable) WithConstraint

func (tbl PrimaryKeyTable) WithConstraint(cc ...constraint.Constraint) PrimaryKeyTable

WithConstraint returns a modified Table with added data consistency constraints.

func (PrimaryKeyTable) WithContext

func (tbl PrimaryKeyTable) WithContext(ctx context.Context) PrimaryKeyTable

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 (PrimaryKeyTable) WithPrefix

func (tbl PrimaryKeyTable) WithPrefix(pfx string) PrimaryKeyTable

WithPrefix sets the table name prefix for subsequent queries. The result is a modified copy of the table; the original is unchanged.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL