dbt

package module
v0.0.0-...-04658fc Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2019 License: GPL-3.0 Imports: 14 Imported by: 0

README

pipeline status coverage report

Coverage Report

BDT

DBT is a library use for making queries to relational databases. Currently Postgres is the only database supported. It provides a means to manage database environments and issue SQL queries within closures that manage database resources, such as connections, statements, and result sets.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoOpRowHandler = func(map[string]interface{}) (interface{}, error) {
	return nil, nil
}

NoOpRowHandler is a row handler to use that does nothing.

Functions

This section is empty.

Types

type Account

type Account struct {
	AccountID  string     `db-column:"account_id"db-size:"36"db-not-null:"true"db-constraint:"primaryKey"`
	Name       string     `db-column:"name"db-size:"32"db-not-null:"true"db-constraint:"unique"`
	CreateDate *time.Time `db-column:"create_date"db-not-null:"true"`
	Active     bool       `db-column:"active"db-not-null:"true"`
	CreateIP   []byte     `db-column:"create_ip"db-not-null:"true"`
	// contains filtered or unexported fields
}

Account this is a dummy comment to make golint behave well on generated code.

type CommandTag

type CommandTag pgx.CommandTag

CommandTag is the result of an Execute function

func (CommandTag) Empty

func (commandTag CommandTag) Empty() bool

Empty returns true if CommandTag is empty.

type Connection

type Connection interface {

	// WithTransaction provides a closure where database queries can be executed within a database transaction.
	// Returns the results of the TransactionHandler.
	WithTransaction(transactionHandler TransactionHandler) (interface{}, DbError)

	// Query issues a parameterized query, passing each row of the result set to the RowHandler f.
	// Returns a slice of  all the return values of the row handler. If at any point
	// the row handler returns an error, Query immediately returns
	MapSlice(sql string, args []interface{}, f RowHandler) ([]interface{}, error)

	// Issue a query against the database getting only the fist row returned in the result set. The query can have
	// placeholders (e.g. "$1, $2, $3...) for each value in args. The first row is passed to rowHandler whose return value
	// is the return value of Select
	Select(sql string, args []interface{}, f RowHandler) (interface{}, DbError)

	// Execute executes sql. sql can be either a prepared statement name or an SQL string.
	// arguments should be referenced positionally from the sql string as $1, $2, etc.
	Execute(sql string, args []interface{}) (CommandTag, DbError)

	// ForEach executes parameterized SQL. For each row in the result set rowHandler is called. If rowHandler returns
	// an error, processing the result set stops and ForEach returns the error.
	ForEach(sql string, args []interface{}, rowHandler RowForEachFunc) DbError
}

Connection represents a database connection to a generic database.

type Database

type Database interface {
	// Issue a query against a PostgreSql database and process only the first row. The query can be parameterized by using
	// placeholders in the form of "$1, $2, $3...etc" which corresponds to the values in args. On the first row of the
	// result set is fetched and passed to rowHandler. Select returns the value and error that rowHandler returns.
	Select(sql string, args []interface{}, f RowHandler) (interface{}, DbError)

	// Issue a query against a PostgreSql database and call rowHandler with each row of the result set. The query can be
	// parameterized via placeholders (e.g. "$1, $2, $3...etc") which corresponds to the respective args values.
	// If rowHandler returns an error, the processing stops and Query returns the error. Otherwise when rowHandler returns
	// no error, Query returns a slice containing all the values rowHandler returned.
	MapSlice(sql string, args []interface{}, f RowHandler) ([]interface{}, DbError)

	// Issue a query against a PostgreSQL database. The query can be parameterized by using placeholders (e.g. $1, $2..etc).
	// StructValue must be an instances with fields tagged with "db" that must correspond to the fields present
	// in the result set. Returns a structure populated with the mapped fields.
	SelectStruct(sql string, args []interface{}, structValue interface{}) (interface{}, error)

	//   sql := "SELECT value from things where id = $1 or id = $2"
	//   ret, err := db.QueryStructSlice(sql, []interface{}{id1, id2}, structValue)
	//
	//   if err != nil {
	//      t.Error(err)
	//   }
	//   structList := ret.([]TestStruct)
	MapStructSlice(sql string, args []interface{}, structType interface{}) (interface{}, error)

	// Exec executes sql. sql can be either a prepared statement name or an SQL string.
	// arguments should be referenced positionally from the sql string as $1, $2, etc.
	Execute(sql string, args []interface{}) (CommandTag, DbError)

	// Fetches a connection object from the connection pool and calls a function with it.
	WithConnection(f func(Connection) (interface{}, error)) (interface{}, error)

	// WithTransaction accepts an handler function, returning any values the handler returns.
	// If transactionHandler returns an error, the transaction is rolled back and the error gets propagated
	// to the caller.
	WithTransaction(transactionHandler TransactionHandler) (interface{}, DbError)

	// ForEach executes parameterized SQL. For each row in the result set rowHandler is called. If rowHandler returns
	// an error, processing the result set stops and ForEach returns the error.
	ForEach(sql string, args []interface{}, rowHandler RowForEachFunc) DbError

	InsertStruct(i ...interface{}) DbError

	UpdateStruct(obj ...interface{}) DbError
}

Database is an interface describing how to interact with a database

type DatabaseConfig

type DatabaseConfig struct {
	// Host is the hostname of the database
	Host string

	// Port is the port the database is running on.
	Port uint

	// Database is the name of the Postgres database/
	Database string

	// Username is the name of the user used to access the database.
	Username string

	// Password is the password use to access the database.
	Password string

	// PoolSize is the maximum number of connections to use for connecting to the database.
	PoolSize uint
}

DatabaseConfig contains detailed information on a database configuration.

type DatabaseEnvironmentConfig

type DatabaseEnvironmentConfig map[string]DatabaseConfig

DatabaseEnvironmentConfig is used for modeling different environment configurations. Where the key is the name of an environment and the key is the configration specific for that environment

func (*DatabaseEnvironmentConfig) Empty

func (config *DatabaseEnvironmentConfig) Empty() bool

Empty returns true if a DatabaseEnvironmentConfig is empty.

func (*DatabaseEnvironmentConfig) GetEnvironment

func (config *DatabaseEnvironmentConfig) GetEnvironment(environment string) DatabaseConfig

GetEnvironment returns the database configuration of a particular environment.

func (*DatabaseEnvironmentConfig) LookupEnvironment

func (config *DatabaseEnvironmentConfig) LookupEnvironment(environment string) (*DatabaseConfig, bool)

LookupEnvironment returns the database configuration of a particular environment as defined a YAML file and a boolean whether the environment exists in the configuration.

type DbError

type DbError error

DbError is an error that occurs when an unexpected situation occurs when interacting with a database.

type DbErrorBase

type DbErrorBase struct {
	DbError

	// SQL is the sql statement that caused the error
	SQL string

	// The arguments that were supplied
	Args []interface{}
	// contains filtered or unexported fields
}

DbErrorBase is an that was caused during execution of an database query.

func (*DbErrorBase) Error

func (error *DbErrorBase) Error() string

Error returns a string representation of a database error.

type DbStructError

type DbStructError struct {
	DbError
	// contains filtered or unexported fields
}

DbStructError is an error that is used when inserting, updating or selecting data from the database using structs

func NewDbStructError

func NewDbStructError(structName string, message string) *DbStructError

NewDbStructError create a new error that denotes something went wrong when dealing with structs.

func (*DbStructError) Error

func (error *DbStructError) Error() string

Error returns an error string describing the details of the error

type Email

type Email struct {
	EmailID    string     `db-column:"email_id"db-size:"36"db-not-null:"true"db-constraint:"primaryKey"`
	Address    string     `db-column:"address"db-size:"254"db-not-null:"true"db-constraint:"unique"`
	Type       string     `db-column:"type"db-size:"32"`
	CreateDate *time.Time `db-column:"create_date"db-not-null:"true"`
	// contains filtered or unexported fields
}

Email this is a dummy comment to make golint behave well on generated code.

type EnvironmentConfiguration

type EnvironmentConfiguration interface {
	// GetEnvironment get the environment configuration corresponding to a particular environment name
	// If no such environment exists, return nil.
	GetEnvironment(environment string) DatabaseConfig

	// LookupEnvironment gets the environment configuration corresponding to a particular environment name,
	// returning the environment if it exists and a boolean indication whether the environment
	// configuration was found.
	LookupEnvironment(environment string) (*DatabaseConfig, bool)
}

EnvironmentConfiguration defines a set of functions that return database configuration

func LoadDatabaseConfiguration

func LoadDatabaseConfiguration(pathname string) (EnvironmentConfiguration, error)

LoadDatabaseConfiguration reads a yaml file from pathname returns a EnvironmentConfiguration

func LoadEnvironmentConfiguration

func LoadEnvironmentConfiguration() (EnvironmentConfiguration, error)

LoadEnvironmentConfiguration looks for environment variables as defined by constants based on EnvironmentDatabaseConfig and returns a EnvironmentConfiguration.

type EnvironmentDatabaseConfig

type EnvironmentDatabaseConfig string

EnvironmentDatabaseConfig is a type of database environment configuration variable.

const (
	// DatabaseHostEnv is the name of the environment variable for configuring the database host.
	DatabaseHostEnv EnvironmentDatabaseConfig = "DATABASE_HOST"

	// DatabaseUserEnv is the name of the environment variable for configuring the database user.
	DatabaseUserEnv EnvironmentDatabaseConfig = "DATABASE_USER"

	// DatabasePortEnv is the name of the environment variable for configuring the database port.
	DatabasePortEnv EnvironmentDatabaseConfig = "DATABASE_PORT"

	// DatabaseDatabaseEnv is the name of the environment variable for configuring the database name.
	DatabaseDatabaseEnv EnvironmentDatabaseConfig = "DATABASE_DATABASE"

	// DatabasePasswordEnv is the name of the environment variable for configuring the database password.
	DatabasePasswordEnv EnvironmentDatabaseConfig = "DATABASE_PASSWORD"

	// DatabasePoolSizeEnv is the name of the environment variable for configuring the maximum number
	// of database connections.
	DatabasePoolSizeEnv EnvironmentDatabaseConfig = "DATABASE_POOL_SIZE"
)

type EnvironmentVariableConfig

type EnvironmentVariableConfig DatabaseEnvironmentConfig

EnvironmentVariableConfig is the the type use for describing an configuration solely determined from environment variables. More useful in production and container configurations were environment variables are used for configuration.

func (*EnvironmentVariableConfig) GetEnvironment

func (config *EnvironmentVariableConfig) GetEnvironment(environment string) DatabaseConfig

GetEnvironment returns the environment

func (*EnvironmentVariableConfig) LookupEnvironment

func (config *EnvironmentVariableConfig) LookupEnvironment(environment string) (*DatabaseConfig, bool)

LookupEnvironment returns the environment and a boolean indicating whether the environment exists.

type Marshaler

type Marshaler interface {
	// MarshalDatabase converts a struct to a row in a database
	MarshalDatabase() (map[string]interface{}, error)
}

Marshaler describes how to convert a struct to a row in a database

type NullConstraintError

type NullConstraintError struct {
	DbErrorBase
}

NullConstraintError occurs when null inserted into a field that does not allow nulls.

type Password

type Password struct {
	PasswordID string     `db-column:"password_id"db-size:"36"db-not-null:"true"db-constraint:"primaryKey"`
	Password   string     `db-column:"password"db-size:"64"db-not-null:"true"db-constraint:"unique"`
	CreateDate *time.Time `db-column:"create_date"db-not-null:"true"`
	// contains filtered or unexported fields
}

Password this is a dummy comment to make golint behave well on generated code.

type PostgresConnection

type PostgresConnection struct {
	// Conn is is the postgress connection
	Conn *pgx.Conn
	// contains filtered or unexported fields
}

PostgresConnection is a connection containing the pgx connection and the configuration that was used to create the connection.

func (*PostgresConnection) Execute

func (conn *PostgresConnection) Execute(sql string, args []interface{}) (CommandTag, DbError)

Execute executes sql. sql can be either a prepared statement name or an SQL string. arguments should be referenced positionally from the sql string as $1, $2, etc.

func (*PostgresConnection) ForEach

func (conn *PostgresConnection) ForEach(sql string, args []interface{}, rowHandler RowForEachFunc) DbError

ForEach issues a query against a PostgreSQl database. The query can be parameterized by using placeholders in the form of "$1, $2, $3...etc" which correspond to the values in args. For each row returned rowHandler is called. If rowHandler returns an error the processing of the result set halts and ForEach returns the error. When every row has been processes ForEach returns.

func (*PostgresConnection) MapSlice

func (conn *PostgresConnection) MapSlice(sql string, args []interface{}, rowHandler RowHandler) ([]interface{}, error)

MapSlice issues a query against a PostgreSQl database. The query can be parameterized by using placeholders in the form of "$1, $2, $3...etc" which correspond to the values in args. For each row returned rowHandler is called. If rowHandler returns an error the processing of the result set halts and SelectSlice returns the error and all accumulated return values of the row handler up until the error occurred. When every row has been processes Query returns a slice of the values rowHandler returned.

func (*PostgresConnection) Select

func (conn *PostgresConnection) Select(sql string, args []interface{}, rowHandler RowHandler) (interface{}, DbError)

Select issues a query against the database getting only the fist row returned in the result set. The query can have placeholders (e.g. "$1, $2, $3...) for each value in args. The first row is passed to rowHandler whose return value is the return value of Select

func (*PostgresConnection) WithTransaction

func (conn *PostgresConnection) WithTransaction(transactionHandler TransactionHandler) (interface{}, DbError)

WithTransaction provides a closure where database queries can be executed within a database transaction. Returns the results of the TransactionHandler.

type PostgresDatabase

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

PostgresDatabase contains the context needed to make queries to a Postgres database.

func NewPostgreSQLDatabase

func NewPostgreSQLDatabase(config *DatabaseConfig) (*PostgresDatabase, error)

NewPostgreSQLDatabase creates a new PostgreSql database with the provided configuration. Returns an PostgreSQL database object if successfully created, otherwise an error.

func (*PostgresDatabase) Close

func (postgresDatabase *PostgresDatabase) Close()

Close closes the database connections.

func (*PostgresDatabase) Execute

func (postgresDatabase *PostgresDatabase) Execute(sql string, args []interface{}) (CommandTag, DbError)

Execute executes sql. sql can be either a prepared statement name or an SQL string. arguments should be referenced positionally from the sql string as $1, $2, etc.

func (*PostgresDatabase) ForEach

func (postgresDatabase *PostgresDatabase) ForEach(sql string, args []interface{}, rowHandler RowForEachFunc) DbError

ForEach issues a query against a PostgreSQl database. The query can be parameterized by using placeholders in the form of "$1, $2, $3...etc" which correspond to the values in args. For each row returned rowHandler is called. If rowHandler returns an error the processing of the result set halts and ForEach returns the error and all accumulated return values of the row handler up until the error occurred. When every row has been processes ForEach returns a slice of the values rowHandler returned.

func (*PostgresDatabase) InsertStruct

func (postgresDatabase *PostgresDatabase) InsertStruct(obj ...interface{}) DbError

InsertStruct inserts a strucutre in the database.

func (*PostgresDatabase) MapSlice

func (postgresDatabase *PostgresDatabase) MapSlice(sql string, args []interface{}, rowHandler RowHandler) ([]interface{}, DbError)

MapSlice issues a query against a PostgreSQl database. The query can be parameterized by using placeholders in the form of "$1, $2, $3...etc" which correspond to the values in args. For each row returned rowHandler is called. If rowHandler returns an error the processing of the result set halts and MapSlice returns the error and all accumulated return values of the row handler up until the error occurred. When every row has been processes MapSlice returns a slice of the values rowHandler returned.

func (*PostgresDatabase) MapStructSlice

func (postgresDatabase *PostgresDatabase) MapStructSlice(sql string, args []interface{}, obj interface{}) (interface{}, error)

MapStructSlice issues a query against a PostgreSQL database. The query can be paramterized by using placeholder values (e.g. $1, $2...etc). An array of the structType variable is returned where each field tagged with db is mapped to the value in the result set.

func (*PostgresDatabase) Select

func (postgresDatabase *PostgresDatabase) Select(sql string, args []interface{}, rowHandler RowHandler) (interface{}, DbError)

Select executes a parameterized sql statement. If the query returns a result set, rowHandler is called with the row. Select returns the values from rowHandler.

func (*PostgresDatabase) SelectStruct

func (postgresDatabase *PostgresDatabase) SelectStruct(sql string, args []interface{}, obj interface{}) (interface{}, error)

SelectStruct issue a query against a PostgreSQL database. The query can be parameterized by using placeholders (e.g. $1, $2..etc). StructValue must be an instances with fields tagged with "db" that must correspond to the fields present in the result set. Returns a structure populated with the mapped fields.

func (*PostgresDatabase) UpdateStruct

func (postgresDatabase *PostgresDatabase) UpdateStruct(obj ...interface{}) DbError

UpdateStruct updates a struct using the tagged values of the structure to generate a update statement. The struct passed must define a single primary key. Tables with multiple/composite or no primary key is not supported.

func (*PostgresDatabase) WithConnection

func (postgresDatabase *PostgresDatabase) WithConnection(f func(Connection) (interface{}, error)) (interface{}, error)

WithConnection accepts a closure that is provided a database connection.

func (*PostgresDatabase) WithTransaction

func (postgresDatabase *PostgresDatabase) WithTransaction(transactionHandler TransactionHandler) (interface{}, DbError)

WithTransaction accepts an handler function, returning any values the handler returns. If transactionHandler returns an error, the transaction is rolled back and the error gets propagated to the caller.

type PostgresTransaction

type PostgresTransaction struct {

	// The psx transaction object
	Trans *pgx.Tx
	// contains filtered or unexported fields
}

PostgresTransaction maintains the state of a Postgres database transaction..

func (*PostgresTransaction) Connection

func (trans *PostgresTransaction) Connection() Connection

Connection returns the database connection of a PostgreSql transaction

func (*PostgresTransaction) Database

func (trans *PostgresTransaction) Database() Database

Database returns the database of a PostgreSql transaction

func (*PostgresTransaction) WithTransaction

func (trans *PostgresTransaction) WithTransaction(f func(tx *Database) error) DbError

WithTransaction would be called within a transaction for nested transactions. Nested transactions currently not supported.

type RelationAlreadyExistsError

type RelationAlreadyExistsError struct {
	DbErrorBase
}

RelationAlreadyExistsError is raised when trying to create a table that already exists.

func (*RelationAlreadyExistsError) Error

func (error *RelationAlreadyExistsError) Error() string

Error returns a string representation of RelationAlreadyExistsError

type RowForEachFunc

type RowForEachFunc func(map[string]interface{}) error

RowForEachFunc is a function type that specifies a function that is called when examining a row from a result set, but does return a result.

type RowHandler

type RowHandler func(map[string]interface{}) (interface{}, error)

RowHandler is a function type that specifies a function that is called when examining a row from a result set.

type SyntaxError

type SyntaxError struct {
	DbErrorBase
}

SyntaxError is an error that occurs when there is a SQL syntax error.

type Transaction

type Transaction interface {
	// Connection returns the database connection that can be used within a database transaction.
	Connection() Connection

	// Database returns the database struct that can be used within a database transaction.
	Database() Database
}

Transaction represents a database transaction.

type TransactionHandler

type TransactionHandler func(tx Transaction) (interface{}, error)

TransactionHandler is a function that is called when handling the body of a database transaction

type UniqueConstraintError

type UniqueConstraintError struct {
	DbErrorBase
}

UniqueConstraintError is raised when unique constraint violoation occurs, such as when trying to insert a row with an already existing primary key.

func (*UniqueConstraintError) Error

func (error *UniqueConstraintError) Error() string

Error returns a string representation of UniqueConstraintError

type Unmarshaler

type Unmarshaler interface {

	// UnmarshalDatabase converts a database row to a struct
	UnmarshalDatabase(row map[string]interface{}) error
}

Unmarshaler describes how to convert a database row into a struct

type User

type User struct {
	UserID     string     `db-column:"user_id"db-size:"36"db-not-null:"true"db-constraint:"primaryKey"`
	EmailID    string     `db-column:"email_id"db-size:"36"db-not-null:"true"`
	Name       string     `db-column:"name"db-size:"32"db-not-null:"true"db-constraint:"unique"`
	Active     bool       `db-column:"active"db-not-null:"true"`
	CreateDate *time.Time `db-column:"create_date"db-not-null:"true"`
	CreateIP   []byte     `db-column:"create_ip"db-not-null:"true"`
	// contains filtered or unexported fields
}

User this is a dummy comment to make golint behave well on generated code.

type UserEmail

type UserEmail struct {
	UserID   string `db-column:"user_id"db-size:"36"db-not-null:"true"db-constraint:"primaryKey"`
	EmailID  string `db-column:"email_id"db-size:"36"db-not-null:"true"db-constraint:"primaryKey"`
	CreateIP []byte `db-column:"create_ip"db-not-null:"true"`
	// contains filtered or unexported fields
}

UserEmail this is a dummy comment to make golint behave well on generated code.

type UserPassword

type UserPassword struct {
	UserID     string     `db-column:"user_id"db-size:"36"db-not-null:"true"db-constraint:"primaryKey"`
	PasswordID string     `db-column:"password_id"db-size:"36"db-not-null:"true"db-constraint:"primaryKey"`
	CreateDate *time.Time `db-column:"create_date"db-not-null:"true"`
	// contains filtered or unexported fields
}

UserPassword this is a dummy comment to make golint behave well on generated code.

type ValueTooLongError

type ValueTooLongError struct {
	DbErrorBase
}

ValueTooLongError is an error that occurs when a value is too long for its database field.

func (*ValueTooLongError) Error

func (error *ValueTooLongError) Error() string

Error returns a string representation of ValueTooLongError

Jump to

Keyboard shortcuts

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