Documentation ¶
Overview ¶
Package sqlapi contains a small API for a tool (sqlgen2) 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/sqlapi/blob/master/README.md
Index ¶
- func Named(name string, value interface{}) sql.NamedArg
- func NamedArgString(arg sql.NamedArg) string
- type Batcher
- type CanPostGet
- type CanPreInsert
- type CanPreUpdate
- type DBStats
- type Database
- type Execer
- type Getter
- type Logger
- type NamedArgList
- func (list NamedArgList) Assignments(q quote.Quoter, 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 RowData
- type Rows
- type SqlDB
- type SqlRow
- type SqlRows
- type SqlStmt
- type SqlTx
- type Table
- type TableCreator
- type TableName
- type TableWithCrud
- type TableWithIndexes
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 ¶
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 ¶
type Database interface { DB() Execer Dialect() dialect.Dialect Logger() Logger Wrapper() interface{} PingContext(ctx context.Context) error Ping() error Stats() DBStats ListTables(re *regexp.Regexp) (util.StringList, error) }
Database typically wraps a *pgx.ConnPool with a dialect and (optionally) a logger. It's safe for concurrent use by multiple goroutines. See NewDatabase.
func NewDatabase ¶
NewDatabase creates a new database handler, which wraps the core *sql.DB along with the appropriate dialect.
The wrapper holds some associated data your application needs for this database, if any. Otherwise this should be nil. As with the logger, it cannot be changed after construction.
type Execer ¶
type Execer interface { Getter Batcher InsertContext(ctx context.Context, query string, args ...interface{}) (int64, error) ExecContext(ctx context.Context, sql string, arguments ...interface{}) (int64, error) PrepareContext(ctx context.Context, name, sql string) (*pgx.PreparedStatement, error) IsTx() bool Logger() Logger }
type Getter ¶
type Getter interface { QueryContext(ctx context.Context, sql string, args ...interface{}) (SqlRows, error) QueryExRaw(ctx context.Context, sql string, options *pgx.QueryExOptions, args ...interface{}) (SqlRows, error) QueryRowContext(ctx context.Context, query string, args ...interface{}) SqlRow QueryRowExRaw(ctx context.Context, query string, options *pgx.QueryExOptions, args ...interface{}) SqlRow }
type Logger ¶
type NamedArgList ¶
NamedArgList holds a slice of NamedArgs
func (NamedArgList) Assignments ¶
func (list NamedArgList) Assignments(q quote.Quoter, 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 RowData ¶
type RowData struct { Fields []pgx.FieldDescription Data util.StringAnyMap }
RowData holds a single row result from the database.
type Rows ¶
type Rows struct { Rows SqlRows // contains filtered or unexported fields }
Rows provides a tool for scanning result *sql.Rows of arbitrary or varying length. The internal *sql.Rows field is exported and is usable as per normal via its Next and Scan methods, or the Next and ScanToMap methods can be used instead.
func WrapRows ¶
WrapRows wraps a *sql.Rows result so that its data can be scanned into a series of maps, one for each row.
func (*Rows) Close ¶
func (rams *Rows) Close()
Close closes the Rows, preventing further enumeration. If Next is called and returns false and there are no further result sets, the Rows are closed automatically and it will suffice to check the result of Err. Close is idempotent and does not affect the result of Err.
func (*Rows) Err ¶
Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close.
func (*Rows) FieldDescriptions ¶
func (rams *Rows) FieldDescriptions() []pgx.FieldDescription
func (*Rows) Next ¶
Next prepares the next result row for reading with the Scan method. It returns true on success, or false if there is no next result row or an error happened while preparing it. Err should be consulted to distinguish between the two cases.
Every call to Scan, even the first one, must be preceded by a call to Next.
type SqlDB ¶
type SqlDB interface { Execer BeginTx(ctx context.Context, opts *pgx.TxOptions) (SqlTx, error) Transact(ctx context.Context, txOptions *pgx.TxOptions, fn func(Execer) error) error PingContext(ctx context.Context) error Stats() sql.DBStats Close() }
SqlDB is able to make queries and begin transactions.
type SqlRow ¶
type SqlRow interface {
Scan(dest ...interface{}) error
}
SqlRow is a precis of *sql.Row.
type SqlRows ¶
type SqlRows interface { SqlRow // Next prepares the next row for reading. It returns true if there is another // row and false if no more rows are available. It automatically closes rows // when all rows are read. Next() bool FieldDescriptions() []pgx.FieldDescription // Values returns an array of the row values Values() ([]interface{}, error) // Close closes the rows, making the connection ready for use again. It is safe // to call Close after rows is already closed. Close() Err() error }
SqlRows is a precis of *sql.Rows.
type SqlStmt ¶
type SqlStmt interface { // ExecContext executes a query without returning any rows. // The args are for any placeholder parameters in the query. ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error) // QueryContext executes a query that returns rows, typically a SELECT. // The args are for any placeholder parameters in the query. QueryContext(ctx context.Context, args ...interface{}) (*pgx.Rows, error) // QueryRowContext executes a query that is expected to return at most one row. // QueryRowContext always returns a non-nil value. Errors are deferred until // Row's Scan method is called. // If the query selects no rows, the *Row's Scan will return ErrNoRows. // Otherwise, the *Row's Scan scans the first selected row and discards // the rest. QueryRowContext(ctx context.Context, args ...interface{}) *pgx.Row Close() error }
SqlStmt is a precis of *sql.Stmt
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() SqlDB // 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() SqlTx // 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() dialect.Dialect // Logger gets the trace logger. Logger() Logger // 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. Query(query string, args ...interface{}) (SqlRows, error) }
Table provides the generic features of each generated table handler.
type TableCreator ¶
type TableCreator interface { Table // CreateTable creates the database table. CreateTable(ifNotExists bool) (int64, error) // DropTable drops the database table. DropTable(ifExists bool) (int64, error) // Truncate empties the table Truncate(force bool) (err error) }
TableCreator is a table with create/delete/truncate methods.
type TableName ¶
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 ¶
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(req require.Requirement, 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(req require.Requirement, 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(req require.Requirement, query string, args ...interface{}) (result sql.NullFloat64, err error) // 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) }
TableWithCrud is a table with a selection of generic access methods. Note that most access methods on concrete table types are strongly-typed so don't appear here.
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) }
TableWithIndexes is a table creator with create/delete methods for the indexes.
Source Files ¶
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 logadapter provides a logger that writes to a log.Logger log.
|
Package logadapter provides a logger that writes to a log.Logger log. |
Package vanilla provides a re-usable table API.
|
Package vanilla provides a re-usable table API. |