sqorc

package
v0.0.0-...-89602ce Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2020 License: BSD-3-Clause Imports: 17 Imported by: 6

Documentation

Overview

Package sqorc implements orchestrator-specific SQL utilities like statement builders and transaction wrappers.

Index

Constants

View Source
const (
	PostgresDialect = "psql"
	MariaDialect    = "maria"
)
View Source
const (
	MariaDriver    = "mysql"
	PostgresDriver = "postgres"
	SQLiteDriver   = "sqlite3"
)

Variables

This section is empty.

Functions

func ClearStatementCacheLogOnError

func ClearStatementCacheLogOnError(cache *squirrel.StmtCache, callsite string)

func CloseRowsLogOnError

func CloseRowsLogOnError(rows *sql.Rows, callsite string)

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

func Open(driver string, source string) (*sql.DB, error)

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

func OpenCleanForTest(t *testing.T, dbName, dbDriver string) *sql.DB

OpenCleanForTest is the same as OpenForTest, except it also drops then creates the underlying DB name before returning.

func OpenForTest

func OpenForTest(t *testing.T, dbName, dbDriver string) *sql.DB

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

type ColumnBuilder builder.Builder

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

type CreateIndexBuilder builder.Builder

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

Name sets the name of the index

func (CreateIndexBuilder) On

On sets the table for the index

func (CreateIndexBuilder) RunWith

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

type CreateTableBuilder builder.Builder

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

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

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

Jump to

Keyboard shortcuts

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