sq

package
v0.0.267 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConverterBoolToBit = NewDBTypeConverter[bool, int](func(v bool) (int, error) {
	return langext.Conditional(v, 1, 0), nil
}, func(v int) (bool, error) {
	if v == 0 {
		return false, nil
	}
	if v == 1 {
		return true, nil
	}
	return false, errors.New(fmt.Sprintf("invalid valud for boolean: '%d'", v))
})
View Source
var ConverterOptTimeToUnixMillis = NewDBTypeConverter[*time.Time, *int64](func(v *time.Time) (*int64, error) {
	if v == nil {
		return nil, nil
	}
	return langext.Ptr(v.UnixMilli()), nil
}, func(v *int64) (*time.Time, error) {
	if v == nil {
		return nil, nil
	}
	return langext.Ptr(time.UnixMilli(*v)), nil
})
View Source
var ConverterTimeToUnixMillis = NewDBTypeConverter[time.Time, int64](func(v time.Time) (int64, error) {
	return v.UnixMilli(), nil
}, func(v int64) (time.Time, error) {
	return time.UnixMilli(v), nil
})

Functions

func CreateSqliteDatabaseSchemaString added in v0.0.125

func CreateSqliteDatabaseSchemaString(ctx context.Context, db Queryable) (string, error)

func HashSqliteDatabase added in v0.0.126

func HashSqliteDatabase(ctx context.Context, db Queryable) (string, error)

func HashSqliteSchema added in v0.0.125

func HashSqliteSchema(ctx context.Context, schemaStr string) (string, error)

func InsertSingle added in v0.0.125

func InsertSingle[TData any](ctx context.Context, q Queryable, tableName string, v TData) (sql.Result, error)

func QueryAll added in v0.0.125

func QueryAll[TData any](ctx context.Context, q Queryable, sql string, pp PP, mode StructScanMode, sec StructScanSafety) ([]TData, error)

func QuerySingle added in v0.0.125

func QuerySingle[TData any](ctx context.Context, q Queryable, sql string, pp PP, mode StructScanMode, sec StructScanSafety) (TData, error)

func ScanAll added in v0.0.34

func ScanAll[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool) ([]TData, error)

func ScanSingle added in v0.0.34

func ScanSingle[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool) (TData, error)

Types

type DB

type DB interface {
	Exec(ctx context.Context, sql string, prep PP) (sql.Result, error)
	Query(ctx context.Context, sql string, prep PP) (*sqlx.Rows, error)
	Ping(ctx context.Context) error
	BeginTransaction(ctx context.Context, iso sql.IsolationLevel) (Tx, error)
	AddListener(listener Listener)
	Exit() error
}

func NewDB

func NewDB(db *sqlx.DB) DB

type DBTypeConverter added in v0.0.127

type DBTypeConverter interface {
	ModelTypeString() string
	DBTypeString() string
	ModelToDB(v any) (any, error)
	DBToModel(v any) (any, error)
}

func NewDBTypeConverter added in v0.0.127

func NewDBTypeConverter[TModelData any, TDBData any](todb func(v TModelData) (TDBData, error), tomodel func(v TDBData) (TModelData, error)) DBTypeConverter

type Listener

type Listener interface {
	PrePing(ctx context.Context) error
	PreTxBegin(ctx context.Context, txid uint16) error
	PreTxCommit(txid uint16) error
	PreTxRollback(txid uint16) error
	PreQuery(ctx context.Context, txID *uint16, sql *string, params *PP) error
	PreExec(ctx context.Context, txID *uint16, sql *string, params *PP) error

	PostPing(result error)
	PostTxBegin(txid uint16, result error)
	PostTxCommit(txid uint16, result error)
	PostTxRollback(txid uint16, result error)
	PostQuery(txID *uint16, sqlOriginal string, sqlReal string, params PP)
	PostExec(txID *uint16, sqlOriginal string, sqlReal string, params PP)
}

type PP

type PP map[string]any

func Join added in v0.0.51

func Join(pps ...PP) PP

type Queryable

type Queryable interface {
	Exec(ctx context.Context, sql string, prep PP) (sql.Result, error)
	Query(ctx context.Context, sql string, prep PP) (*sqlx.Rows, error)
}

type StructScanMode added in v0.0.44

type StructScanMode string
const (
	SModeFast     StructScanMode = "FAST"
	SModeExtended StructScanMode = "EXTENDED"
)

type StructScanSafety added in v0.0.44

type StructScanSafety string
const (
	Safe   StructScanSafety = "SAFE"   // return error for missing fields
	Unsafe StructScanSafety = "UNSAFE" // ignore missing fields
)

type StructScanner added in v0.0.44

type StructScanner struct {
	Mapper *reflectx.Mapper
	// contains filtered or unexported fields
}

func NewStructScanner added in v0.0.44

func NewStructScanner(rows *sqlx.Rows, unsafe bool) *StructScanner

func (*StructScanner) Start added in v0.0.44

func (r *StructScanner) Start(dest any) error

func (*StructScanner) StructScanBase added in v0.0.44

func (r *StructScanner) StructScanBase(dest any) error

StructScanBase forked from github.com/jmoiron/sqlx@v1.3.5/sqlx.go without (relevant) changes

func (*StructScanner) StructScanExt added in v0.0.44

func (r *StructScanner) StructScanExt(dest any) error

StructScanExt forked from github.com/jmoiron/sqlx@v1.3.5/sqlx.go does also wok with nullabel structs (from LEFT JOIN's)

type Tx

type Tx interface {
	Rollback() error
	Commit() error
	Status() TxStatus
	Exec(ctx context.Context, sql string, prep PP) (sql.Result, error)
	Query(ctx context.Context, sql string, prep PP) (*sqlx.Rows, error)
}

func NewTransaction

func NewTransaction(xtx *sqlx.Tx, txid uint16, lstr []Listener) Tx

type TxStatus added in v0.0.217

type TxStatus string
const (
	TxStatusInitial  TxStatus = "INITIAL"
	TxStatusActive   TxStatus = "ACTIVE"
	TxStatusComitted TxStatus = "COMMITTED"
	TxStatusRollback TxStatus = "ROLLBACK"
)

Jump to

Keyboard shortcuts

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