coredb

package
v2.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTxOpts = sql.TxOptions{
	Isolation: sql.LevelDefault,
	ReadOnly:  false,
}

DefaultTxOpts is package variable with default transaction level

View Source
var ErrAvoidInsert = errors.New("ErrAvoidInsertion")

ErrAvoidInsert represent the error if insertion failed

View Source
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

func BeginTx(ctx context.Context, dbname string, opts *sql.TxOptions) (tx *sql.Tx, err error)

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

func Conn(ctx context.Context, dbname string, mode DBMode) (*sql.Conn, error)

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 Exec deprecated

func Exec(dbname string, query string, params ...any) (sql.Result, error)

Exec given query with given db info & params

Deprecated: use the function with context

func ExecCtx

func ExecCtx(ctx context.Context, dbname string, query string, params ...any) (sql.Result, error)

ExecCtx given query with given db info & params

func FetchByPK deprecated

func FetchByPK[T any](dbname string, tableName string, pkName []string, val ...any) *T

FetchByPK returns a row of T type with given primary key value

Deprecated: use the function with context

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 FetchByPKFromMaster deprecated

func FetchByPKFromMaster[T any](dbname string, tableName string, pkName []string, val ...any) *T

FetchByPKFromMaster returns a row of T type with given primary key value

Deprecated: use the function with context

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 FetchByPKs deprecated

func FetchByPKs[T any](dbname string, tableName string, pkName string, vals []any) []*T

FetchByPKs returns rows of T type with given primary key values

Deprecated: use the function with context

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 FetchByPKsFromMaster deprecated

func FetchByPKsFromMaster[T any](dbname string, tableName string, pkName string, vals []any) []*T

FetchByPKsFromMaster returns rows of T type with given primary key values

Deprecated: use the function with context

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 Find deprecated

func Find[T any](dbname string, tableName string, where WhereQuery) ([]*T, error)

Find returns rows from given table type with where query

Deprecated: use the function with context

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 GetAnySlice

func GetAnySlice[T any](data []T) []any

GetAnySlice converts []T to []any

func GetColumnsNames

func GetColumnsNames[T any]() (joinedColumnNames string)

GetColumnsNames returns column names joined by `,` of given type

func GetColumnsNamesReflect added in v2.2.0

func GetColumnsNamesReflect(o any) (joinedColumnNames string)

GetColumnsNamesReflect returns column names joined by `,` of given type

func GetParamPlaceHolder

func GetParamPlaceHolder(count int) string

GetParamPlaceHolder returns string for param place holder in sql with given count

func MapSlice

func MapSlice[T, K any](data []T, convert func(T) K) []K

func MustParseTime

func MustParseTime(timestr string) time.Time

func Query deprecated

func Query[T any](dbname string, query string, params ...any) (result []*T, err error)

Query rows from given table type with where query & params

Deprecated: use the function with context

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 QueryFromMaster deprecated

func QueryFromMaster[T any](dbname string, query string, params ...any) (result []*T, err error)

QueryFromMaster rows from master DB from given table type with where query & params

Deprecated: use the function with context

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 QueryInt deprecated

func QueryInt(dbname string, query string, params ...any) (result int, err error)

QueryInt single int result by query, handy for count(*) querys

Deprecated: use the function with context

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 QueryIntFromMaster deprecated

func QueryIntFromMaster(dbname string, query string, params ...any) (result int, err error)

QueryIntFromMaster single int result by query, handy for count(*) querys

Deprecated: use the function with context

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

func RowToStruct[T any](row *sql.Row) (result *T, err error)

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 RowToStructReflect added in v2.2.0

func RowToStructReflect(row *sql.Row, v any) (err error)

func RowsToStructSlice added in v2.2.0

func RowsToStructSlice[T any](rows *sql.Rows) (result []*T, err error)

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 RowsToStructSliceReflect(rows *sql.Rows, out any) (err error)

func Setup

func Setup(dbp DBProvider)

Setup default db provider for all db ops

func StrutForScan

func StrutForScan(u any) (pointers []any)

StrutForScan returns value pointers of given obj

func ValueInSet

func ValueInSet(set []string, s string) bool

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]) GetVal added in v2.2.2

func (c *ColumnVal[T]) GetVal() T

func (*ColumnVal[T]) GetValPointer added in v2.2.2

func (c *ColumnVal[T]) GetValPointer() any

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 ConnectionGetter interface {
	Conn(ctx context.Context) (*sql.Conn, error)
}

type DBMode

type DBMode int
const (
	// DBModeRead allow read from slave db
	DBModeRead DBMode = iota
	// DBModeRead set write to master db
	DBModeWrite
	// DBModeReadFromWrite is aka force read from master db
	DBModeReadFromWrite
)

type DBProvider

type DBProvider func(dbname string, mode DBMode) *sql.DB

type InvalidScanError added in v2.2.0

type InvalidScanError struct {
	Type reflect.Type
}

An InvalidScanError describes an invalid argument passed to Scan.

func (*InvalidScanError) Error added in v2.2.0

func (e *InvalidScanError) Error() string

type Ops

type Ops int

Ops defines operation in a where query

const (
	// OpInit is default
	OpInit Ops = iota
	// OpEqual is =
	OpEqual
	// OpIn is in
	OpIn
	// OpGreater is >
	OpGreater
	// OpSmaller is <
	OpSmaller
	// OpRange is < ? <
	OpRange
)

type Params added in v2.2.2

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

func NewParams added in v2.2.2

func NewParams(params ...any) *Params

func (*Params) Add added in v2.2.2

func (p *Params) Add(params ...any)

func (*Params) Get added in v2.2.2

func (p *Params) Get() []any

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 RowStruct

type RowStruct interface {
	GetColumnNames() string
	GetPointers() []any
}

RowStruct defines interface of an ORM row struct

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) Tx added in v2.2.0

func (t *TxProvider) Tx(ctx context.Context, fn func(TxContext) error) error

Tx runs fn in transaction.

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.

func (*TxProvider) TxWithOpts added in v2.2.0

func (t *TxProvider) TxWithOpts(ctx context.Context, fn func(TxContext) error, conn TxStarter, opts *sql.TxOptions) (err error)

TxWithOpts ...

type TxStarter added in v2.3.0

type TxStarter interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

TxStarter for sql database.

type TxStarterWithConnection added in v2.3.0

type TxStarterWithConnection interface {
	TxStarter
	ConnectionGetter
}

type WhereQuery

type WhereQuery interface {
	GetWhere() (whereSQL string, params []any)
}

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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