Documentation ¶
Index ¶
- Constants
- Variables
- func Init(dbConnStr string, logger *logrus.Entry) error
- func Migrate(connStr string, dir MigrateDir) (int, error)
- func SetAssets(assets migrate.AssetMigrationSource)
- type Config
- type ConnectionParams
- type MigrateDir
- type PageQuery
- type SQLConn
- func (conn *SQLConn) Begin() error
- func (conn *SQLConn) Clone() *SQLConn
- func (conn *SQLConn) Commit() error
- func (conn *SQLConn) Exec(sqq sq.Sqlizer) error
- func (conn *SQLConn) ExecRaw(query string, args ...interface{}) error
- func (conn *SQLConn) Get(sqq sq.Sqlizer, dest interface{}) error
- func (conn *SQLConn) GetRaw(dest interface{}, query string, args ...interface{}) error
- func (conn *SQLConn) InTx() bool
- func (conn *SQLConn) Insert(sqq sq.InsertBuilder) (id interface{}, err error)
- func (conn *SQLConn) IsInTx() bool
- func (conn *SQLConn) Rollback() error
- func (conn *SQLConn) Select(sqq sq.Sqlizer, dest interface{}) error
- func (conn *SQLConn) SelectRaw(dest interface{}, query string, args ...interface{}) error
- func (conn *SQLConn) SetConnMaxLifetime(d int64)
- func (conn *SQLConn) SetConnParams(params *ConnectionParams)
- func (conn *SQLConn) SetMaxIdleConns(n int)
- func (conn *SQLConn) SetMaxOpenConns(n int)
- func (conn *SQLConn) SetTx(tx *sqlx.Tx)
- func (conn *SQLConn) Stats() sql.DBStats
- func (conn *SQLConn) Transaction(fn func() error) (err error)
- type Table
- type Transactional
- type WBase
Constants ¶
const ( DefaultPageSize uint64 = 20 MaxPageSize uint64 = 500 OrderAscending = "asc" OrderDescending = "desc" )
Variables ¶
var ( ErrInvalidOrder = func(val string) error { return fmt.Errorf("order(%s): accept only %s|%s", val, OrderAscending, OrderDescending) } ErrTooBigPage = func(val uint64) error { return fmt.Errorf("pageSize(%d): shoud be less or equal %d", val, MaxPageSize) } )
Functions ¶
func Migrate ¶
func Migrate(connStr string, dir MigrateDir) (int, error)
Migrate connects to the database and applies migrations.
func SetAssets ¶
func SetAssets(assets migrate.AssetMigrationSource)
SetAssets is a function for injection of precompiled by bindata migrations files.
Types ¶
type Config ¶
type Config struct { ConnURL string `json:"conn_url" yaml:"conn_url"` //The database connection string. InitTimeout int `json:"dbInitTimeout" yaml:"init_timeout"` // AutoMigrate if `true` execute db migrate up on start. AutoMigrate bool `json:"auto_migrate" yaml:"auto_migrate"` WaitForDB bool `json:"wait_for_db" yaml:"wait_for_db"` Params *ConnectionParams `json:"params" yaml:"params"` }
type ConnectionParams ¶ added in v1.7.0
type MigrateDir ¶
type MigrateDir string
MigrateDir represents a direction in which to perform schema migrations.
const ( // MigrateUp causes migrations to be run in the "up" direction. MigrateUp MigrateDir = "up" // MigrateDown causes migrations to be run in the "down" direction. MigrateDown MigrateDir = "down" )
type PageQuery ¶
type PageQuery struct { Order string `json:"order"` Page uint64 `json:"page"` PageSize uint64 `json:"pageSize"` }
PageQuery is the structure for building query with pagination.
func ParsePageQuery ¶
ParsePageQuery extracts `PageQuery` from the url Query Values.
func (*PageQuery) Apply ¶
func (pq *PageQuery) Apply(query sq.SelectBuilder, orderColumn string) sq.SelectBuilder
Apply sets limit and ordering params to SelectBuilder.
func (*PageQuery) FromRQuery ¶
FromRQuery extracts `PageQuery` from the url Query Values and validate.
type SQLConn ¶
type SQLConn struct {
// contains filtered or unexported fields
}
SQLConn is a connector for interacting with the database.
func NewConnector ¶ added in v1.7.0
NewConnector returns an new instance of the SQLConn.
func NewSQLConn ¶
NewSQLConn create new connector by passed connection params
func (*SQLConn) Clone ¶
Clone clones the receiver, returning a new instance backed by the same context and db. The result will not be bound to any transaction that the source is currently within.
func (*SQLConn) Get ¶
Get compile `sqq` to raw sql query, executes it and write first row into the `dest`.
func (*SQLConn) InTx ¶ added in v1.6.0
InTx checks is transaction started. Return true if it is a transaction, and false if it is not a transaction
func (*SQLConn) Insert ¶
func (conn *SQLConn) Insert(sqq sq.InsertBuilder) (id interface{}, err error)
Insert compile `sqq` to SQL and runs query. Return last inserted id
func (*SQLConn) Select ¶
Select compile `sqq` to raw sql query, executes it, and write each row into dest, which must be a slice.
func (*SQLConn) SelectRaw ¶
SelectRaw executes a raw sql query, and write each row into dest, which must be a slice.
func (*SQLConn) SetConnMaxLifetime ¶
func (*SQLConn) SetConnParams ¶ added in v1.7.0
func (conn *SQLConn) SetConnParams(params *ConnectionParams)
func (*SQLConn) SetMaxIdleConns ¶
func (*SQLConn) SetMaxOpenConns ¶
func (*SQLConn) Transaction ¶
Transaction is generic helper method for specific Q's to implement Transaction capabilities
type Table ¶
type Table struct { Name string Alias string DB sq.BaseRunner QBuilder sq.SelectBuilder GQBuilder sq.SelectBuilder IQBuilder sq.InsertBuilder UQBuilder sq.UpdateBuilder DQBuilder sq.DeleteBuilder Page *PageQuery }
Table is the basis for implementing Querier for some model or table.
func (Table) AliasedName ¶
AliasedName returns table name with the alias postfix.
type Transactional ¶
type Transactional interface { // Begin starts a database transaction. Begin() error // Commit commits the transaction. Commit() error // Rollback aborts the transaction. Rollback() error // IsInTx checks is transaction started. // DEPRECATED: IsInTx works wrong IsInTx() bool // InTx checks is transaction started. Return true if it is a transaction, and false if it is not a transaction InTx() bool }
Transactional is the interface for representing a db connector/query builder that support database transactions.
type WBase ¶
WBase is a base structure for worker which uses database and need transactions support.
func (*WBase) DBTxRollback ¶
func (s *WBase) DBTxRollback()