mehen

package module
v0.0.0-...-ce0d072 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2019 License: Apache-2.0 Imports: 12 Imported by: 0

README

Mehen

codecov

SQL wrapper in Golang.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextInterface

type ContextInterface interface {
	// NoACL declares that no ACL-checks are needed.
	NoACL() SqlInterface

	// SetAuditableContext sets database setting name into current transaction's variable for auditing purposes.
	SetAuditableContext(databaseSettingName string, value interface{}) error
}

type Database

type Database interface {
	sqlbuilder.Database
	Settings
}

Database is the wrapper-interface for upper.io/db that also contains our custom database settings.

func Connect

func Connect(dbConfig config.Database) (Database, error)

Connect opens a connection with the postgres DB.

type DatabaseTransactionContext

type DatabaseTransactionContext interface {
	sqlbuilder.SQLBuilder
	Settings
}

type InsertUpdateDeleteInterface

type InsertUpdateDeleteInterface interface {
	// Insert executes a sql insert query and returns inserted records ID (only if query ends with "returning id;").
	Insert() (int, error)
	// Update executes a sql update query and returns affected rows count (do not use `;` in the sql).
	Update() (int, error)
	// Delete executes a sql delete query.
	Delete() error
}

InsertUpdateDeleteInterface describes insert, update, delete methods

type Interface

type Interface interface {
	// Context sets an query context:
	// tx - database or transaction reference
	Context(tx DatabaseTransactionContext) ContextInterface
}

Interface for SqlWrapper

func NewSqlWrapper

func NewSqlWrapper() Interface

NewSqlWrapper returns a new SqlWrapper

type Pagination

type Pagination struct {
	Sort           string
	SortDescending bool
	Offset         int
	Limit          int
}

Pagination holds general pagination input data

type QueryInterface

type QueryInterface interface {
	// QueryOne executes a sql select query to retrieve one object.
	QueryOne(result interface{}) error
	// Query executes a sql select query to retrieve many objects.
	Query(result interface{}) error
	// QueryWithCount executes a count and a select query to retrieve many objects and the total count.
	// Meant to be used with pagination.
	QueryWithCount(result interface{}) (int, error)
}

QueryInterface describes querying methods

type Settings

type Settings interface {
	// SetStatementTimeout sets the default statement timeout (in seconds).
	// Database queries will return with an error after this time. 0 = no timeout.
	SetStatementTimeout(int)

	// StatementTimeout returns the default statement timeout (in seconds).
	// Database queries will return with an error after this time. 0 = no timeout.
	StatementTimeout() int
}

Settings is an interface for reading and setting custom database settings that are not included in upper.io/db.

type SqlContextInterface

type SqlContextInterface interface {
	InsertUpdateDeleteInterface
	QueryInterface

	// Parameter declares an sql parameter
	// key - key of the parameter (i.e. "application_id").
	// value - value of the parameter.
	Parameter(key string, value interface{}) SqlContextInterface

	// ParameterNullable declares an sql parameter
	// key - key of the parameter (i.e. "application_id").
	// value - value of the parameter. If value is default, then parameter will be used as NULL
	ParameterNullable(key string, value interface{}) SqlContextInterface

	// Pagination declares pagination settings (with automatic count query).
	// pagination - pagination settings.
	// selectFields - these fields will be selected when performing query.
	Pagination(pagination Pagination, selectFields []string) QueryInterface

	// PaginationWithCountSQLTemplate declares pagination settings (with implicit count sql template).
	// pagination - pagination settings.
	// countSQLTemplateOverride - this sql template will be used for count query (optional).
	PaginationWithCountSQLTemplate(pagination Pagination, countSQLTemplateOverride string) QueryInterface
}

SqlContextInterface describes methods for SQL context

type SqlInterface

type SqlInterface interface {
	// SQL sets the sql template to be used for the query.
	// Possible placeholders:
	// {{select}} - use when querying with count.
	// {{footer}} - use when using pagination or querying with count.
	// Custom parameters are also allowed (i.e. {{id}})) but have to be provided by Parameter-declarations.
	SQL(sqlTemplate string) SqlContextInterface
}

SqlInterface describes methods for SQL template defining

type SqlWrapper

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

SqlWrapper wraps db query making

func (SqlWrapper) Context

Context sets an query context: tx - database or transaction reference

func (SqlWrapper) Delete

func (sqlWrapper SqlWrapper) Delete() error

Delete executes a sql delete query.

func (SqlWrapper) Insert

func (sqlWrapper SqlWrapper) Insert() (int, error)

Insert executes a sql insert query and returns inserted records ID (only if query ends with "returning id;").

func (SqlWrapper) NoACL

func (sqlWrapper SqlWrapper) NoACL() SqlInterface

NoACL declares that no ACL-checks are needed.

func (SqlWrapper) Pagination

func (sqlWrapper SqlWrapper) Pagination(pagination Pagination, selectFields []string) QueryInterface

Pagination declares pagination settings (with automatic count query). pagination - pagination settings. selectFields - these fields will be selected when performing query.

func (SqlWrapper) PaginationWithCountSQLTemplate

func (sqlWrapper SqlWrapper) PaginationWithCountSQLTemplate(pagination Pagination, countSQLTemplateOverride string) QueryInterface

PaginationWithCountSQLTemplate declares pagination settings (with implicit count sql template). pagination - pagination settings. countSQLTemplateOverride - this sql template will be used for count query (optional).

func (SqlWrapper) Parameter

func (sqlWrapper SqlWrapper) Parameter(key string, value interface{}) SqlContextInterface

Parameter declares an sql parameter key - key of the parameter (i.e. "id"). value - value of the parameter.

func (SqlWrapper) ParameterNullable

func (sqlWrapper SqlWrapper) ParameterNullable(key string, value interface{}) SqlContextInterface

ParameterNullable declares an sql parameter key - key of the parameter (i.e. "id"). value - value of the parameter. If value is default, then parameter will be used as NULL

func (SqlWrapper) Query

func (sqlWrapper SqlWrapper) Query(result interface{}) error

Query executes a sql select query to retrieve many objects.

func (SqlWrapper) QueryOne

func (sqlWrapper SqlWrapper) QueryOne(result interface{}) error

QueryOne executes a sql select query to retrieve one object.

func (SqlWrapper) QueryWithCount

func (sqlWrapper SqlWrapper) QueryWithCount(result interface{}) (int, error)

QueryWithCount executes a count and a select query to retrieve many objects and the total count. Meant to be used with pagination.

func (SqlWrapper) SQL

func (sqlWrapper SqlWrapper) SQL(sqlTemplate string) SqlContextInterface

SQL sets the sql template to be used for the query. Possible placeholders: {{select}} - use when querying with count. {{footer}} - use when using pagination or querying with count. Custom parameters are also allowed (i.e. {{id}})) but have to be provided by Parameter-declarations.

func (SqlWrapper) SetAuditableContext

func (sqlWrapper SqlWrapper) SetAuditableContext(databaseSettingName string, value interface{}) error

SetAuditableContext sets database setting name into current transaction's variable for auditing purposes.

func (SqlWrapper) Update

func (sqlWrapper SqlWrapper) Update() (int, error)

Update executes a sql update query.

type TransactionContext

type TransactionContext interface {
	sqlbuilder.Tx
	Settings
}

func NewTx

func NewTx(db Database) (TransactionContext, error)

NewTx creates a new transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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