Documentation
¶
Overview ¶
Package sqlgen2 contains a small API plus a tool that generates SQL functions for specified struct types.
Lighter than a full-blown ORM and simpler than hand-written code, the output makes it easy to write flexible yet reliable and high-performance database code.
See the README for further details: https://github.com/rickb777/sqlgen2/blob/master/README.md
Index ¶
- func Named(name string, value interface{}) sql.NamedArg
- func NamedArgString(arg sql.NamedArg) string
- type CanPostGet
- type CanPreInsert
- type CanPreUpdate
- type Database
- func (database *Database) Begin() (*sql.Tx, error)
- func (database *Database) BeginTx(opts *sql.TxOptions) (*sql.Tx, error)
- func (database *Database) Ctx() context.Context
- func (database *Database) DB() Execer
- func (database *Database) Dialect() schema.Dialect
- func (database *Database) Exec(query string, args ...interface{}) (sql.Result, error)
- func (database *Database) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (database *Database) ListTables() (util.StringList, error)
- func (database *Database) LogError(err error) error
- func (database *Database) LogIfError(err error) error
- func (database *Database) LogQuery(query string, args ...interface{})
- func (database *Database) Logger() *log.Logger
- func (database *Database) Prepare(query string) (*sql.Stmt, error)
- func (database *Database) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (database *Database) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (database *Database) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (database *Database) QueryRow(query string, args ...interface{}) *sql.Row
- func (database *Database) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (database *Database) SetContext(ctx context.Context) *Database
- func (database *Database) SetLogger(logger *log.Logger) *Database
- func (database *Database) SetWrapper(wrapper interface{}) *Database
- func (database *Database) TableExists(name TableName) (yes bool, err error)
- func (database *Database) Wrapper() interface{}
- type Execer
- type NamedArgList
- func (list NamedArgList) Assignments(d schema.Dialect, from int) []string
- func (list NamedArgList) Contains(name string) bool
- func (list NamedArgList) Exists(fn func(sql.NamedArg) bool) bool
- func (list NamedArgList) Find(fn func(sql.NamedArg) bool) (sql.NamedArg, bool)
- func (list NamedArgList) FindByName(name string) (sql.NamedArg, bool)
- func (list NamedArgList) MkString(sep string) string
- func (list NamedArgList) Names() []string
- func (list NamedArgList) String() string
- func (list NamedArgList) Values() []interface{}
- type Table
- type TableCreator
- type TableName
- type TableWithCrud
- type TableWithIndexes
- type TxStarter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NamedArgString ¶
NamedArgString converts the argument to a string of the form "name=value".
Types ¶
type CanPostGet ¶ added in v0.8.0
type CanPostGet interface {
PostGet() error
}
CanPostGet is implemented by value types that need a hook to run just after their data is fetched from the database.
type CanPreInsert ¶
type CanPreInsert interface {
PreInsert() error
}
CanPreInsert is implemented by value types that need a hook to run just before their data is inserted into the database.
type CanPreUpdate ¶
type CanPreUpdate interface {
PreUpdate() error
}
CanPreUpdate is implemented by value types that need a hook to run just before their data is updated in the database.
type Database ¶ added in v0.12.0
type Database struct {
// contains filtered or unexported fields
}
func NewDatabase ¶ added in v0.12.0
NewDatabase createa a new database handler, which wraps the core *sql.DB.
func (*Database) Begin ¶ added in v0.15.0
Begin starts a transaction using default options. The default isolation level is dependent on the driver.
func (*Database) BeginTx ¶ added in v0.15.0
BeginTx starts a transaction.
The internal context, obtained using Ctx(), 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 (*Database) DB ¶ added in v0.12.0
DB gets the Execer, which is a *sql.DB except during testing.
func (*Database) ExecContext ¶ added in v0.12.0
func (*Database) ListTables ¶ added in v0.12.0
func (database *Database) ListTables() (util.StringList, error)
ListTables gets all the table names in the database/schema.
func (*Database) LogError ¶ added in v0.13.0
LogError writes error info to the logger, if the logger is not nil.
func (*Database) LogIfError ¶ added in v0.13.0
LogIfError writes error info to the logger, if neither the logger nor the error is nil.
func (*Database) LogQuery ¶ added in v0.13.0
LogQuery writes query info to the logger, if it is not nil.
func (*Database) PrepareContext ¶ added in v0.12.0
func (*Database) QueryContext ¶ added in v0.12.0
func (*Database) QueryRowContext ¶ added in v0.12.0
func (*Database) SetContext ¶ added in v0.12.0
SetContext sets the context for subsequent queries.
func (*Database) SetLogger ¶ added in v0.12.0
SetLogger sets the logger for subsequent queries, returning the interface.
func (*Database) SetWrapper ¶ added in v0.12.0
SetWrapper sets a user-defined wrapper or container.
func (*Database) TableExists ¶ added in v0.12.0
DoesTableExist gets all the table names in the database/schema.
type Execer ¶
type Execer interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
Execer describes the methods of the core database API. See database/sql/DB and database/sql/Tx.
type NamedArgList ¶
NamedArgList holds a slice of NamedArgs
func (NamedArgList) Assignments ¶
func (list NamedArgList) Assignments(d schema.Dialect, from int) []string
Assignments gets the assignment expressions.
func (NamedArgList) Contains ¶
func (list NamedArgList) Contains(name string) bool
Contains tests whether anything in the list has a certain name.
func (NamedArgList) Exists ¶
func (list NamedArgList) Exists(fn func(sql.NamedArg) bool) bool
Exists verifies that one or more elements of NamedArgList return true for the passed func.
func (NamedArgList) Find ¶
Find returns the first sql.NamedArg that returns true for some function. False is returned if none match.
func (NamedArgList) FindByName ¶
func (list NamedArgList) FindByName(name string) (sql.NamedArg, bool)
FindByName finds the first item with a particular name.
func (NamedArgList) MkString ¶
func (list NamedArgList) MkString(sep string) string
MkString produces a string ontainin all the values separated by sep.
func (NamedArgList) String ¶
func (list NamedArgList) String() string
String produces a string ontainin all the values separated by comma.
func (NamedArgList) Values ¶
func (list NamedArgList) Values() []interface{}
Values gets all the valules
type Table ¶
type Table interface {
// Name gets the table name. without prefix
Name() TableName
// Database gets the shared database information.
Database() *Database
// Execer gets the wrapped database or transaction handle.
Execer() Execer
// 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.
DB() *sql.DB
// Tx gets the wrapped transaction handle, provided this is within a transaction.
// Panics if it is in the wrong state - use IsTx() if necessary.
Tx() *sql.Tx
// IsTx tests whether this is within a transaction.
IsTx() bool
// Ctx gets the current request context.
Ctx() context.Context
// Dialect gets the database dialect.
Dialect() schema.Dialect
// Logger gets the trace logger.
Logger() *log.Logger
}
Table provides the generic features of each generated table handler.
type TableCreator ¶
type TableName ¶ added in v0.8.0
type TableName struct {
// Prefix on the table name. It can be used as the schema name, in which case
// it should include the trailing dot. Or it can be any prefix as needed.
Prefix string
// The principal name of the table.
Name string
}
TableName holds a two-part name. The prefix part is optional.
func (TableName) PrefixWithoutDot ¶ added in v0.8.0
PrefixWithoutDot return the prefix; if this ends with a dot, the dot is removed.
type TableWithCrud ¶
type TableWithCrud interface {
Table
// 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.
QueryOneNullString(query string, args ...interface{}) (result sql.NullString, err error)
// MustQueryOneNullString is a low-level access method for one string. This can be used for function queries and
// such like.
//
// It places a requirement that exactly one result must be found; an error is generated when this expectation is not met.
MustQueryOneNullString(query string, args ...interface{}) (result sql.NullString, 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.
QueryOneNullInt64(query string, args ...interface{}) (result sql.NullInt64, err error)
// MustQueryOneNullInt64 is a low-level access method for one int64. This can be used for 'COUNT(1)' queries and
// such like.
//
// It places a requirement that exactly one result must be found; an error is generated when this expectation is not met.
MustQueryOneNullInt64(query string, args ...interface{}) (result sql.NullInt64, 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.
QueryOneNullFloat64(query string, args ...interface{}) (result sql.NullFloat64, err error)
// MustQueryOneNullFloat64 is a low-level access method for one float64. This can be used for 'AVG(...)' queries and
// such like.
//
// It places a requirement that exactly one result must be found; an error is generated when this expectation is not met.
MustQueryOneNullFloat64(query string, args ...interface{}) (result sql.NullFloat64, err error)
// ReplaceTableName replaces all occurrences of "{TABLE}" with the table's name.
ReplaceTableName(query string) string
// Exec executes a query.
//
// It places a requirement, which may be nil, on the number of affected rows: this
// controls whether an error is generated when this expectation is not met.
//
// It returns the number of rows affected (if the DB supports that).
Exec(req require.Requirement, query string, args ...interface{}) (int64, error)
// CountWhere counts records that match a 'where' predicate.
CountWhere(where string, args ...interface{}) (count int64, err error)
// Count counts records that match a 'where' predicate.
Count(where where.Expression) (count int64, err error)
// UpdateFields writes new values to the specified columns for rows that match the 'where' predicate.
// It returns the number of rows affected (if the DB supports that).
UpdateFields(req require.Requirement, where where.Expression, fields ...sql.NamedArg) (int64, error)
// Delete deletes rows that match the 'where' predicate.
// It returns the number of rows affected (if the DB supports that).
Delete(req require.Requirement, wh where.Expression) (int64, error)
}
type TableWithIndexes ¶
type TableWithIndexes interface {
TableCreator
// CreateIndexes creates the indexes for the database table.
CreateIndexes(ifNotExist bool) (err error)
// DropIndexes executes a query that drops all the indexes on the database table.
DropIndexes(ifExist bool) (err error)
// CreateTableWithIndexes creates the database table and its indexes.
CreateTableWithIndexes(ifNotExist bool) (err error)
}
Directories
¶
| Path | Synopsis |
|---|---|
|
Package constraint provides types and methods to support foreign-key relationshipd between database tables.
|
Package constraint provides types and methods to support foreign-key relationshipd between database tables. |
|
Package require provides simple constraints to assist with detecting errors in database queries that arise from the wrong number of result (for example no result or too many results).
|
Package require provides simple constraints to assist with detecting errors in database queries that arise from the wrong number of result (for example no result or too many results). |
|
Package vanilla provides a re-usable table API.
|
Package vanilla provides a re-usable table API. |