Documentation ¶
Overview ¶
Package sqorc implements orchestrator-specific SQL utilities like statement builders and transaction wrappers.
Index ¶
- Constants
- func ClearStatementCacheLogOnError(cache *squirrel.StmtCache, callsite string)
- func CloseRowsLogOnError(rows *sql.Rows, callsite string)
- func ExecInTx(db *sql.DB, opts *sql.TxOptions, initFn func(*sql.Tx) error, ...) (ret interface{}, err error)
- func Open(driver string, source string) (*sql.DB, error)
- func OpenCleanForTest(t *testing.T, dbName, dbDriver string) *sql.DB
- func OpenForTest(t *testing.T, dbName, dbDriver string) *sql.DB
- type ColumnBuilder
- func (b ColumnBuilder) Default(value interface{}) ColumnBuilder
- func (b ColumnBuilder) EndColumn() CreateTableBuilder
- func (b ColumnBuilder) Name(name string) ColumnBuilder
- func (b ColumnBuilder) NotNull() ColumnBuilder
- func (b ColumnBuilder) OnDelete(onDelete ColumnOnDeleteOption) ColumnBuilder
- func (b ColumnBuilder) PrimaryKey() ColumnBuilder
- func (b ColumnBuilder) References(table string, column string) ColumnBuilder
- func (b ColumnBuilder) ToSql() (string, error)
- func (b ColumnBuilder) Type(columnType ColumnType) ColumnBuilder
- type ColumnOnDeleteOption
- type ColumnType
- type CreateIndexBuilder
- func (b CreateIndexBuilder) Columns(columns ...string) CreateIndexBuilder
- func (b CreateIndexBuilder) Exec() (sql.Result, error)
- func (b CreateIndexBuilder) IfNotExists() CreateIndexBuilder
- func (b CreateIndexBuilder) Name(name string) CreateIndexBuilder
- func (b CreateIndexBuilder) On(table string) CreateIndexBuilder
- func (b CreateIndexBuilder) RunWith(runner squirrel.BaseRunner) CreateIndexBuilder
- func (b CreateIndexBuilder) ToSql() (string, []interface{}, error)
- type CreateTableBuilder
- func (b CreateTableBuilder) Column(name string) ColumnBuilder
- func (b CreateTableBuilder) Exec() (sql.Result, error)
- func (b CreateTableBuilder) ForeignKey(on string, columnMap map[string]string, onDelete ColumnOnDeleteOption) CreateTableBuilder
- func (b CreateTableBuilder) IfNotExists() CreateTableBuilder
- func (b CreateTableBuilder) Name(name string) CreateTableBuilder
- func (b CreateTableBuilder) PrimaryKey(columns ...string) CreateTableBuilder
- func (b CreateTableBuilder) RunWith(runner squirrel.BaseRunner) CreateTableBuilder
- func (b CreateTableBuilder) ToSql() (string, []interface{}, error)
- func (b CreateTableBuilder) Unique(columns ...string) CreateTableBuilder
- type InsertBuilder
- type StatementBuilder
- type UpsertValue
Constants ¶
const ( PostgresDialect = "psql" MariaDialect = "maria" )
const ( MariaDriver = "mysql" PostgresDriver = "postgres" SQLiteDriver = "sqlite3" )
Variables ¶
This section is empty.
Functions ¶
func CloseRowsLogOnError ¶
CloseRowsLogOnError will close the *Rows object and log if an error is returned by Rows.Close(). This function will no-op if rows is nil.
func ExecInTx ¶
func ExecInTx( db *sql.DB, opts *sql.TxOptions, initFn func(*sql.Tx) error, txFn func(*sql.Tx) (interface{}, error), ) (ret interface{}, err error)
ExecInTx executes a callback inside a sql transaction on the provided DB. The transaction is rolled back if any error is encountered. initFn is a callback to call before the main txFn, commonly used in our codebase to execute a CREATE TABLE IF NOT EXISTS.
func Open ¶
Open is a wrapper for sql.Open which sets the max open connections to 1 for in memory sqlite3 dbs. In memory sqlite3 creates a new database on each connection, so the number of open connections must be limited to 1 for thread safety. Otherwise, there is a race condition between threads using a cached connection to the original database or opening a new connection to a new database.
func OpenCleanForTest ¶
OpenCleanForTest is the same as OpenForTest, except it also drops then creates the underlying DB name before returning.
func OpenForTest ¶
OpenForTest returns a new connection to a shared test DB. Does not guarantee the existence of the underlying DB name. The shared DB is part of the shared testing infrastructure, so care must be taken to avoid racing on the same DB name across testing code. Supported DB drivers include:
- postgres
- mysql
Environment variables:
- SQL_DRIVER overrides the Go SQL driver
- TEST_DATABASE_HOST overrides the DB connection host
- TEST_DATABASE_PORT_POSTGRES overrides the port connected to for postgres driver
- TEST_DATABASE_PORT_MARIA overrides the port connected to for maria driver
Types ¶
type ColumnBuilder ¶
ColumnBuilder is a builder for columns within a table creation statement This builder is immutable and all methods will return a new instance of the builder with the requested fields set.
func (ColumnBuilder) Default ¶
func (b ColumnBuilder) Default(value interface{}) ColumnBuilder
Default sets the default value for the column. This value is not escaped so SQL expressions are valid.
func (ColumnBuilder) EndColumn ¶
func (b ColumnBuilder) EndColumn() CreateTableBuilder
EndColumn returns the parent CreateTableBuilder to continue building the table creation statement.
func (ColumnBuilder) Name ¶
func (b ColumnBuilder) Name(name string) ColumnBuilder
Name sets the name of the column.
func (ColumnBuilder) NotNull ¶
func (b ColumnBuilder) NotNull() ColumnBuilder
NotNull marks the column as not nullable.
func (ColumnBuilder) OnDelete ¶
func (b ColumnBuilder) OnDelete(onDelete ColumnOnDeleteOption) ColumnBuilder
OnDelete sets the deletion behavior for a foreign key column.
func (ColumnBuilder) PrimaryKey ¶
func (b ColumnBuilder) PrimaryKey() ColumnBuilder
PrimaryKey marks the column as a PK for the table.
func (ColumnBuilder) References ¶
func (b ColumnBuilder) References(table string, column string) ColumnBuilder
References marks the column as a foreign key to the specified table and foreign column.
func (ColumnBuilder) ToSql ¶
func (b ColumnBuilder) ToSql() (string, error)
ToSql returns the column creation as a SQL string.
func (ColumnBuilder) Type ¶
func (b ColumnBuilder) Type(columnType ColumnType) ColumnBuilder
Type sets the type of the column.
type ColumnOnDeleteOption ¶
type ColumnOnDeleteOption uint8
ColumnOnDeleteOption is an enum type to specify ON DELETE behavior for foreign keys
const ( ColumnOnDeleteDoNothing ColumnOnDeleteOption = iota ColumnOnDeleteCascade )
type ColumnType ¶
type ColumnType uint8
ColumnType is an enum type to specify table column types
const ( ColumnTypeText ColumnType = iota ColumnTypeInt ColumnTypeBytes ColumnTypeBool )
type CreateIndexBuilder ¶
CreateIndexBuilder is a builder for CREATE INDEX statements
func (CreateIndexBuilder) Columns ¶
func (b CreateIndexBuilder) Columns(columns ...string) CreateIndexBuilder
Columns sets the columns the index is on
func (CreateIndexBuilder) Exec ¶
func (b CreateIndexBuilder) Exec() (sql.Result, error)
Exec runs the statement using the set runner
func (CreateIndexBuilder) IfNotExists ¶
func (b CreateIndexBuilder) IfNotExists() CreateIndexBuilder
IfNotExists sets the index creation to run only if it doesn't already exist
func (CreateIndexBuilder) Name ¶
func (b CreateIndexBuilder) Name(name string) CreateIndexBuilder
Name sets the name of the index
func (CreateIndexBuilder) On ¶
func (b CreateIndexBuilder) On(table string) CreateIndexBuilder
On sets the table for the index
func (CreateIndexBuilder) RunWith ¶
func (b CreateIndexBuilder) RunWith(runner squirrel.BaseRunner) CreateIndexBuilder
RunWith sets the runner to Exec the statement with
func (CreateIndexBuilder) ToSql ¶
func (b CreateIndexBuilder) ToSql() (string, []interface{}, error)
ToSql returns the sql string and args for to the statement
type CreateTableBuilder ¶
CreateTableBuilder is a builder for DDL table creation statements. This builder is immutable and all operations will return a new instance with the requested fields set.
func (CreateTableBuilder) Column ¶
func (b CreateTableBuilder) Column(name string) ColumnBuilder
Column returns a ColumnBuilder to build a column for the table. The returned ColumnBuilder will have a reference back to this CreateTableBuilder which will be returned when EndColumn() is called so you can chain column creation into table creation.
func (CreateTableBuilder) Exec ¶
func (b CreateTableBuilder) Exec() (sql.Result, error)
Exec runs the statement using the set runner.
func (CreateTableBuilder) ForeignKey ¶
func (b CreateTableBuilder) ForeignKey(on string, columnMap map[string]string, onDelete ColumnOnDeleteOption) CreateTableBuilder
ForeignKey adds a foreign key constraint on a table. Note that the column builder also supports foreign key constraints for individual columns for engines which support it. TODO: pull this into a builder
func (CreateTableBuilder) IfNotExists ¶
func (b CreateTableBuilder) IfNotExists() CreateTableBuilder
IfNotExists sets the table creation to run only if the table does not already exist
func (CreateTableBuilder) Name ¶
func (b CreateTableBuilder) Name(name string) CreateTableBuilder
Name sets the name of the table to be created
func (CreateTableBuilder) PrimaryKey ¶
func (b CreateTableBuilder) PrimaryKey(columns ...string) CreateTableBuilder
PrimaryKey specifies columns to create a primary key on Note that the column builder also has a PrimaryKey. We do not cross-validate primary key constraints, so avoid specifying PKs in multiple places.
func (CreateTableBuilder) RunWith ¶
func (b CreateTableBuilder) RunWith(runner squirrel.BaseRunner) CreateTableBuilder
RunWith sets the runner for the statement.
func (CreateTableBuilder) ToSql ¶
func (b CreateTableBuilder) ToSql() (string, []interface{}, error)
ToSql returns the SQL string and arguments for the statement.
func (CreateTableBuilder) Unique ¶
func (b CreateTableBuilder) Unique(columns ...string) CreateTableBuilder
Unique adds a unique constraint on a set of columns.
type InsertBuilder ¶
type InsertBuilder interface { ExecContext(ctx context.Context) (sql.Result, error) QueryContext(ctx context.Context) (*sql.Rows, error) QueryRowContext(ctx context.Context) squirrel.RowScanner ScanContext(ctx context.Context, dest ...interface{}) error PlaceholderFormat(f squirrel.PlaceholderFormat) InsertBuilder RunWith(runner squirrel.BaseRunner) InsertBuilder Exec() (sql.Result, error) Query() (*sql.Rows, error) QueryRow() squirrel.RowScanner Scan(dest ...interface{}) error ToSql() (string, []interface{}, error) Prefix(sql string, args ...interface{}) InsertBuilder Options(options ...string) InsertBuilder Into(from string) InsertBuilder Columns(columns ...string) InsertBuilder Values(values ...interface{}) InsertBuilder Suffix(sql string, args ...interface{}) InsertBuilder SetMap(clauses map[string]interface{}) InsertBuilder Select(sb squirrel.SelectBuilder) InsertBuilder // OnConflict builds an upsert clause for the insert query. // An empty value for the setValues param indicates do nothing on conflict. OnConflict(setValues []UpsertValue, columns ...string) InsertBuilder }
InsertBuilder is an interface which tracks squirrel's InsertBuilder struct but returns InsertBuilder on all self-referencing returns and adds an OnConflict method to support upserts.
type StatementBuilder ¶
type StatementBuilder interface { Select(columns ...string) squirrel.SelectBuilder Insert(into string) InsertBuilder Update(table string) squirrel.UpdateBuilder Delete(from string) squirrel.DeleteBuilder PlaceholderFormat(f squirrel.PlaceholderFormat) squirrel.StatementBuilderType RunWith(runner squirrel.BaseRunner) squirrel.StatementBuilderType // CreateTable returns a CreateTableBuilder for building DDL table creation // statements. // IMPORTANT: the returned builder will NOT respect the runner set via // RunWith on this StatementBuilder due to a reflection bug that's // tricky to chase down. CreateTable(name string) CreateTableBuilder // CreateIndex returns a CreateIndexBuilder for building index creation // statements. // IMPORTANT: the returned builder will NOT respect the runner set via // RunWith on this StatementBuilder due to a reflection bug that's // tricky to chase down. CreateIndex(name string) CreateIndexBuilder }
StatementBuilder is an interface which tracks squirrel's StatementBuilderType with the difference that Insert returns this package's InsertBuilder interface type. This interface exists to support building DDL commands and upsert statements for multiple dialects.
func GetSqlBuilder ¶
func GetSqlBuilder() StatementBuilder
GetSqlBuilder returns a squirrel Builder for the configured SQL dialect as found in the SQL_DIALECT env var.
func NewMariaDBStatementBuilder ¶
func NewMariaDBStatementBuilder() StatementBuilder
NewMariaDBStatementBuilder returns an implementation of StatementBuilder for MariaDB dialect.
func NewPostgresStatementBuilder ¶
func NewPostgresStatementBuilder() StatementBuilder
NewPostgresStatementBuilder returns an implementation of StatementBuilder for PostgreSQL dialect.
type UpsertValue ¶
type UpsertValue struct { Column string Value interface{} }
UpsertValue wraps a column name and updated value