Documentation
¶
Index ¶
- Variables
- func BeginTx(ctx context.Context, dbname string, opts *sql.TxOptions) (tx *sql.Tx, err error)
- func Conn(ctx context.Context, dbname string, mode DBMode) (*sql.Conn, error)
- func Exec(dbname string, query string, params ...any) (sql.Result, error)deprecated
- func ExecCtx(ctx context.Context, dbname string, query string, params ...any) (sql.Result, error)
- func FetchByPK[T any](dbname string, tableName string, pkName []string, val ...any) *Tdeprecated
- func FetchByPKCtx[T any](ctx context.Context, dbname string, tableName string, pkName []string, ...) (*T, error)
- func FetchByPKFromMaster[T any](dbname string, tableName string, pkName []string, val ...any) *Tdeprecated
- func FetchByPKFromMasterCtx[T any](ctx context.Context, dbname string, tableName string, pkName []string, ...) (*T, error)
- func FetchByPKs[T any](dbname string, tableName string, pkName string, vals []any) []*Tdeprecated
- func FetchByPKsCtx[T any](ctx context.Context, dbname string, tableName string, pkName string, ...) ([]*T, error)
- func FetchByPKsFromMaster[T any](dbname string, tableName string, pkName string, vals []any) []*Tdeprecated
- func FetchByPKsFromMasterCtx[T any](ctx context.Context, dbname string, tableName string, pkName string, ...) ([]*T, error)
- func Find[T any](dbname string, tableName string, where WhereQuery) ([]*T, error)deprecated
- func FindCtx[T any](ctx context.Context, dbname string, tableName string, where WhereQuery) ([]*T, error)
- func FindFromMaster[T any](dbname string, tableName string, where WhereQuery) ([]*T, error)deprecated
- func FindFromMasterCtx[T any](ctx context.Context, dbname string, tableName string, where WhereQuery) ([]*T, error)
- func FindOne[T any](dbname string, tableName string, where WhereQuery) *Tdeprecated
- func FindOneCtx[T any](ctx context.Context, dbname string, tableName string, where WhereQuery) (*T, error)
- func FindOneFromMaster[T any](dbname string, tableName string, where WhereQuery) *Tdeprecated
- func FindOneFromMasterCtx[T any](ctx context.Context, dbname string, tableName string, where WhereQuery) (*T, error)
- func GetAnySlice[T any](data []T) []any
- func GetColumnsNames[T any]() (joinedColumnNames string)
- func GetColumnsNamesReflect(o any) (joinedColumnNames string)
- func GetParamPlaceHolder(count int) string
- func MapSlice[T, K any](data []T, convert func(T) K) []K
- func MustParseTime(timestr string) time.Time
- func Query[T any](dbname string, query string, params ...any) (result []*T, err error)deprecated
- func QueryCtx[T any](ctx context.Context, dbname string, query string, params ...any) (result []*T, err error)
- func QueryFromMaster[T any](dbname string, query string, params ...any) (result []*T, err error)deprecated
- func QueryFromMasterCtx[T any](ctx context.Context, dbname string, query string, params ...any) (result []*T, err error)
- func QueryInt(dbname string, query string, params ...any) (result int, err error)deprecated
- func QueryIntCtx(ctx context.Context, dbname string, query string, params ...any) (result int, err error)
- func QueryIntFromMaster(dbname string, query string, params ...any) (result int, err error)deprecated
- func QueryIntFromMasterCtx(ctx context.Context, dbname string, query string, params ...any) (result int, err error)
- func RowToStruct[T any](row *sql.Row) (result *T, err error)
- func RowToStructReflect(row *sql.Row, v any) (err error)
- func RowsToStructSlice[T any](rows *sql.Rows) (result []*T, err error)
- func RowsToStructSliceReflect(rows *sql.Rows, out any) (err error)
- func Setup(dbp DBProvider)
- func StrutForScan(u any) (pointers []any)
- func ValueInSet(set []string, s string) bool
- type ColumnNamer
- type ColumnVal
- type ColumnValPointer
- type ConnectionGetter
- type DBMode
- type DBProvider
- type InvalidScanError
- type Ops
- type Params
- type ReadOneQuery
- type ReadQuery
- type RowStruct
- type TableType
- type TxContext
- type TxProvider
- type TxStarter
- type TxStarterWithConnection
- type WhereQuery
Constants ¶
This section is empty.
Variables ¶
var DefaultTxOpts = sql.TxOptions{ Isolation: sql.LevelDefault, ReadOnly: false, }
DefaultTxOpts is package variable with default transaction level
var ErrAvoidInsert = errors.New("ErrAvoidInsertion")
ErrAvoidInsert represent the error if insertion failed
var ErrAvoidUpdate = errors.New("ErrAvoidUpdate")
ErrAvoidUpdate represent the error if update failed, i.e. no affected row
Functions ¶
func BeginTx ¶ added in v2.2.0
BeginTx returns a custom db.Tx based on opts. This method exists for flexibility. Make sure you call Commit or Rollback on the returned Tx. Refer to https://go.dev/doc/database/execute-transactions on how to use the returned Tx.
func Conn ¶ added in v2.5.1
Conn returns a single connection by either opening a new connection or returning an existing connection from the connection pool. Conn will block until either a connection is returned or ctx is canceled. Queries run on the same Conn will be run in the same database session.
Every Conn must be returned to the database pool after use by calling [Conn.Close].
func FetchByPKCtx ¶
func FetchByPKCtx[T any](ctx context.Context, dbname string, tableName string, pkName []string, val ...any) (*T, error)
FetchByPKCtx returns a row of T type with given primary key value
func FetchByPKFromMasterCtx ¶
func FetchByPKFromMasterCtx[T any](ctx context.Context, dbname string, tableName string, pkName []string, val ...any) (*T, error)
FetchByPKFromMasterCtx returns a row of T type with given primary key value
func FetchByPKsCtx ¶
func FetchByPKsCtx[T any](ctx context.Context, dbname string, tableName string, pkName string, vals []any) ([]*T, error)
FetchByPKsCtx returns rows of T type with given primary key values
func FetchByPKsFromMasterCtx ¶
func FetchByPKsFromMasterCtx[T any](ctx context.Context, dbname string, tableName string, pkName string, vals []any) ([]*T, error)
FetchByPKsFromMasterCtx returns rows of T type with given primary key values
func FindCtx ¶
func FindCtx[T any](ctx context.Context, dbname string, tableName string, where WhereQuery) ([]*T, error)
FindCtx returns rows from given table type with where query
func FindFromMaster
deprecated
func FindFromMaster[T any](dbname string, tableName string, where WhereQuery) ([]*T, error)
FindFromMaster using master DB returns rows from given table type with where query
Deprecated: use the function with context
func FindFromMasterCtx ¶
func FindFromMasterCtx[T any](ctx context.Context, dbname string, tableName string, where WhereQuery) ([]*T, error)
FindFromMasterCtx using master DB returns rows from given table type with where query
func FindOne
deprecated
func FindOne[T any](dbname string, tableName string, where WhereQuery) *T
FindOne returns a row from given table type with where query
Deprecated: use the function with context
func FindOneCtx ¶
func FindOneCtx[T any](ctx context.Context, dbname string, tableName string, where WhereQuery) (*T, error)
FindOneCtx returns a row from given table type with where query. If no rows found, *T will be nil. No error will be returned.
func FindOneFromMaster
deprecated
func FindOneFromMaster[T any](dbname string, tableName string, where WhereQuery) *T
FindOneFromMaster using master DB returns a row from given table type with where query
Deprecated: use the function with context
func FindOneFromMasterCtx ¶
func FindOneFromMasterCtx[T any](ctx context.Context, dbname string, tableName string, where WhereQuery) (*T, error)
FindOneFromMasterCtx using master DB returns a row from given table type with where query If no rows found, *T will be nil. No error will be returned.
func GetColumnsNames ¶
GetColumnsNames returns column names joined by `,` of given type
func GetColumnsNamesReflect ¶ added in v2.2.0
GetColumnsNamesReflect returns column names joined by `,` of given type
func GetParamPlaceHolder ¶
GetParamPlaceHolder returns string for param place holder in sql with given count
func MustParseTime ¶
func QueryCtx ¶
func QueryCtx[T any](ctx context.Context, dbname string, query string, params ...any) (result []*T, err error)
QueryCtx rows from given table type with where query & params
func QueryFromMasterCtx ¶
func QueryFromMasterCtx[T any](ctx context.Context, dbname string, query string, params ...any) (result []*T, err error)
QueryFromMasterCtx rows from master DB from given table type with where query & params
func QueryIntCtx ¶
func QueryIntCtx(ctx context.Context, dbname string, query string, params ...any) (result int, err error)
QueryIntCtx single int result by query, handy for count(*) querys
func QueryIntFromMasterCtx ¶
func QueryIntFromMasterCtx(ctx context.Context, dbname string, query string, params ...any) (result int, err error)
QueryIntFromMasterCtx single int result by query, handy for count(*) querys
func RowToStruct ¶ added in v2.2.0
RowToStruct converts a database row into a struct.
It takes a pointer to a sql.Row and returns a pointer to the converted struct and an error.
func RowsToStructSlice ¶ added in v2.2.0
RowsToStructSlice converts the rows of a SQL query result into a slice of structs.
It takes a pointer to a sql.Rows object as input. The function also uses a generic type T, which represents the type of the struct.
The function returns a slice of pointers to T structs and an error.
func RowsToStructSliceReflect ¶ added in v2.2.0
func StrutForScan ¶
StrutForScan returns value pointers of given obj
func ValueInSet ¶
Types ¶
type ColumnNamer ¶ added in v2.2.2
type ColumnNamer interface {
GetColumnName() string
}
type ColumnVal ¶ added in v2.2.2
type ColumnVal[T any] struct { // contains filtered or unexported fields }
func (*ColumnVal[T]) GetValPointer ¶ added in v2.2.2
type ColumnValPointer ¶ added in v2.2.2
type ColumnValPointer interface {
GetValPointer() any
}
ColumnType defines the generated type of a table column
type ConnectionGetter ¶ added in v2.3.0
type InvalidScanError ¶ added in v2.2.0
An InvalidScanError describes an invalid argument passed to Scan.
func (*InvalidScanError) Error ¶ added in v2.2.0
func (e *InvalidScanError) Error() string
type ReadOneQuery ¶
type ReadOneQuery[T any] interface { One() *T }
ReadOneQuery defines interface which support reading one object
type ReadQuery ¶
type ReadQuery[T any] interface { // Deprecated: use the function with context All() []*T // Deprecated: use the function with context Limit(offset, limit int) []*T // Deprecated: use the function with context AllFromMaster() []*T // Deprecated: use the function with context LimitFromMaster(offset, limit int) []*T AllCtx(context.Context) ([]*T, error) LimitCtx(ctx context.Context, offset, limit int) ([]*T, error) AllFromMasterCtx(context.Context) ([]*T, error) LimitFromMasterCtx(ctx context.Context, offset, limit int) ([]*T, error) }
ReadQuery defines interface which support reading multiple objects
type TableType ¶
type TableType interface {
GetTableName() string
}
TableType defines the generated type of a table
type TxContext ¶ added in v2.2.0
type TxContext interface { // Exec executes a query without returning any rows. // The args are for any placeholder parameters in the query. Exec(query string, args ...any) (sql.Result, error) // Query executes a SELECT query and scans the resulting rows into the provided 'results' destination. // It accepts a SQL query and an optional list of parameters for placeholder substitution. // NOTE: results must be a pointer to a slice of struct pointers. Query(results any, query string, params ...any) (err error) // QueryInt executes a SELECT query expected to return a single integer value. // Commonly used for COUNT(*) operations or where the result is inherently an integer. // Multiple params for query placeholders are supported. QueryInt(query string, params ...any) (result int, err error) // FindOne fetches a single record from the database and populates 'result'. // It requires the name of the table, an optional WHERE clause ('whereSQL'), and // parameters to substitute into the WHERE clause's placeholders. // NOTE: result must be a non-nil pointer to a struct. FindOne(result any, tableName string, whereSQL string, params ...any) error // Find executes a SELECT query based on the given 'tableName' and 'whereSQL', // placing all matching records into the 'results' slice. // Parameters for the WHERE clause's placeholders can be passed with 'params'. // NOTE: results must be a pointer to a slice of struct pointers. Find(results any, tableName string, whereSQL string, params ...any) error }
TxContext interface for DAO operations with context.
type TxProvider ¶ added in v2.2.0
type TxProvider struct {
// contains filtered or unexported fields
}
TxProvider ...
func NewTxProvider ¶ added in v2.2.0
func NewTxProvider(dbname string) *TxProvider
NewTxProvider ...
func (*TxProvider) TxWithLock ¶ added in v2.3.0
func (t *TxProvider) TxWithLock(ctx context.Context, lock string, durationInSec int, fn func(txContext TxContext) error) (err error)
TxWithLock executes a transaction with a lock and a specified duration in seconds.
type TxStarterWithConnection ¶ added in v2.3.0
type TxStarterWithConnection interface { TxStarter ConnectionGetter }
type WhereQuery ¶
WhereQuery defines interface which support forming query query
func NewWhere ¶
func NewWhere(whereSQL string, params ...any) WhereQuery
NewWhere returns WhereQuery with given whereSQL and params