goquutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package goquutil provides auxiliary routines for working with goqu query builder. Warning: until this notice is removed, current state is "beta", i.e. it is being tested in selected projects for errors and completeness. This also means that APIs may change without any guarantees for backward compatibility.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound indicates that something was not found in db

View Source
var IsInsideTest bool

IsInsideTest when set to true enables some checks that are skipped for production code

Functions

func BuildSQLAndExec

func BuildSQLAndExec(q Querier, sqlExpression exp.SQLExpression) (sql.Result, error)

BuildSQLAndExec is a function for running DML not returning any data like UPDATE, DELETE, INSERT

func BuildSQLAndQuery

func BuildSQLAndQuery(q Querier, sqlExpression exp.SQLExpression) (*sql.Rows, error)

BuildSQLAndQuery is a function for running SELECT statements returning many rows

func BuildSQLAndQueryRow

func BuildSQLAndQueryRow(q Querier, sqlExpression exp.SQLExpression) (*sql.Row, error)

BuildSQLAndQueryRow is a function for running SELECT statements returning single row

func BuildSQLAndQueryScalar

func BuildSQLAndQueryScalar(q Querier, sqlExpression exp.SQLExpression, scalar interface{}) error

BuildSQLAndQueryScalar is a function for running SELECT statements returning single scalar value

func JSONDecoder

func JSONDecoder(i interface{}) sql.Scanner

JSONDecoder is convenience function for reading JSON values from db

func JSONEncoder

func JSONEncoder(i interface{}) driver.Valuer

JSONEncoder is convenience function for writing JSON values to db

func QueryAndScanStruct

func QueryAndScanStruct(q Querier, query *goqu.SelectDataset, composite interface{}) error

QueryAndScanStruct scans results into composite struct

func QueryAndScanStructs

func QueryAndScanStructs(q Querier, query *goqu.SelectDataset, composite interface{}) error

QueryAndScanStructs scans results into structs (using common goqu rules about tags) it allows scanning from queries that contain JOINs between tables other than INNER JOIN

func QueryAndScanValues

func QueryAndScanValues(q Querier, query *goqu.SelectDataset, result interface{}) error

QueryAndScanValues runs SELECT and scans its result into values list, result is a pointer to slice of values: SELECT attr FROM t WHERE t.id > 123

func ScanEachRow

func ScanEachRow(rows *sql.Rows, scanRow func(s Scanner) error) (rowsProcessed int, err error)

ScanEachRow is a helper for scanning multiple rows result set

Types

type ContextProvider

type ContextProvider interface {
	Context() context.Context
}

ContextProvider is an interface that defines a method for obtaining a context.Context. Implementing types should return the context.Context representing the execution context of the operation or task.

type DB

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

DB is a wrapper for goqu.Database

func NewDB

func NewDB(ctx context.Context, db *goqu.Database) *DB

NewDB returns tx wrapper for goqu.Database

func (*DB) DoInTx

func (d *DB) DoInTx(worker func(q Querier) error) error

DoInTx opens db tx and runs worker func within its context

func (*DB) WithLogging

func (d *DB) WithLogging(logger golibslog.FieldLogger, loggingCtx string, loggingTimeThresholdBeginTx time.Duration) *DB

WithLogging enables logging of time consumed on openning/getting DB connection from pool

func (*DB) WithTxOpts

func (d *DB) WithTxOpts(txOpts *sql.TxOptions) *DB

WithTxOpts allows passing additional options for opened tx

type NullTime

type NullTime struct {
	Valid bool
	Time  time.Time
}

NullTime is suitable in case of different functions working with time Note! It's suitable for in case you use goqu.MAX(date_column) on SQLite. It's not required on MySQL. MySQL can work with sql.NullTime sql.NullTime is not suitable in such case because on SQLite driver cannot detect type of MAX(date_column) expression as timestamp and handle it as a text, the problem can be in function (rc *SQLiteRows) declTypes() []string at github.com/mattn/go-sqlite3/sqlite3.go

func NullTimeFrom

func NullTimeFrom(t time.Time) NullTime

NullTimeFrom creates valid NullTime from time.Time

func (*NullTime) Scan

func (ns *NullTime) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*NullTime) SetInvalid

func (ns *NullTime) SetInvalid()

SetInvalid sets NullTime invalid

func (*NullTime) SetValid

func (ns *NullTime) SetValid(t time.Time)

SetValid sets NullTime valid

func (NullTime) Value

func (ns NullTime) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type PostQueryFuncT

type PostQueryFuncT func(ctx context.Context, startedAt time.Time, err error, query string, args ...interface{})

PostQueryFuncT is type for post query hook function

var PostQueryHook PostQueryFuncT

PostQueryHook will be executed after actual query execution

type PreQueryFuncT

type PreQueryFuncT func(ctx context.Context, query string, args ...interface{}) string

PreQueryFuncT is type for pre query hook function

var PreQueryHook PreQueryFuncT

PreQueryHook will be executed before actual query execution

type Querier

type Querier interface {
	QueryRow(query string, args ...interface{}) *sql.Row
	Query(query string, args ...interface{}) (*sql.Rows, error)
	Exec(query string, args ...interface{}) (sql.Result, error)
}

Querier is an interface to abstract details of db implementation

type QueryDurationObserverFunc

type QueryDurationObserverFunc func(preparedQueryString string, ctx context.Context, startTime time.Time, err error)

QueryDurationObserverFunc is a function type to observe query related stats

var ObserveSQLQueryDuration QueryDurationObserverFunc

ObserveSQLQueryDuration is an actual instance of QueryDurationObserverFunc that is used

type SQLBuilderSettings

type SQLBuilderSettings struct {
	Dialect goqu.DialectWrapper
}

SQLBuilderSettings is sql builder settings representation

type Scanner

type Scanner interface {
	Scan(dest ...interface{}) error
}

Scanner is an interface to abstract details of scanning db values

Jump to

Keyboard shortcuts

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