sq

package
v0.0.368 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: Apache-2.0 Imports: 18 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 ConverterExErrCategoryToString = NewDBTypeConverter[exerr.ErrorCategory, string](func(v exerr.ErrorCategory) (string, error) {
	return v.Category, nil
}, func(v string) (exerr.ErrorCategory, error) {
	for _, cat := range exerr.AllCategories {
		if cat.Category == v {
			return cat, nil
		}
	}
	return exerr.CatUser, errors.New("failed to convert '" + v + "' to exerr.ErrorCategory")
})
View Source
var ConverterExErrSeverityToString = NewDBTypeConverter[exerr.ErrorSeverity, string](func(v exerr.ErrorSeverity) (string, error) {
	return v.Severity, nil
}, func(v string) (exerr.ErrorSeverity, error) {
	for _, sev := range exerr.AllSeverities {
		if sev.Severity == v {
			return sev, nil
		}
	}
	return exerr.SevErr, errors.New("failed to convert '" + v + "' to exerr.ErrorSeverity")
})
View Source
var ConverterExErrTypeToString = NewDBTypeConverter[exerr.ErrorType, string](func(v exerr.ErrorType) (string, error) {
	return v.Key, nil
}, func(v string) (exerr.ErrorType, error) {
	for _, etp := range exerr.ListRegisteredTypes() {
		if etp.Key == v {
			return etp, nil
		}
	}

	return exerr.NewType(v, nil), nil
})
View Source
var ConverterJsonArrToString = NewDBTypeConverter[JsonArr, string](func(v JsonArr) (string, error) {
	mrsh, err := json.Marshal(v)
	if err != nil {
		return "", err
	}
	return string(mrsh), nil
}, func(v string) (JsonArr, error) {
	var mrsh JsonArr
	if err := json.Unmarshal([]byte(v), &mrsh); err != nil {
		return JsonArr{}, err
	}
	return mrsh, nil
})
View Source
var ConverterJsonObjToString = NewDBTypeConverter[JsonObj, string](func(v JsonObj) (string, error) {
	mrsh, err := json.Marshal(v)
	if err != nil {
		return "", err
	}
	return string(mrsh), nil
}, func(v string) (JsonObj, error) {
	var mrsh JsonObj
	if err := json.Unmarshal([]byte(v), &mrsh); err != nil {
		return JsonObj{}, err
	}
	return mrsh, nil
})
View Source
var ConverterRFC339NanoTimeToString = NewDBTypeConverter[rfctime.RFC3339NanoTime, string](func(v rfctime.RFC3339NanoTime) (string, error) {
	return v.Time().In(time.UTC).Format("2006-01-02 15:04:05.999999999"), nil
}, func(v string) (rfctime.RFC3339NanoTime, error) {
	t, err := time.ParseInLocation("2006-01-02 15:04:05.999999999", v, time.UTC)
	if err != nil {
		return rfctime.RFC3339NanoTime{}, err
	}
	return rfctime.NewRFC3339Nano(t), nil
})

ConverterRFC339NanoTimeToString Does not really use RFC339 - but sqlite does not understand timezones and the `T` delimiter

View Source
var ConverterRFC339TimeToString = NewDBTypeConverter[rfctime.RFC3339Time, string](func(v rfctime.RFC3339Time) (string, error) {
	return v.Time().In(time.UTC).Format("2006-01-02 15:04:05"), nil
}, func(v string) (rfctime.RFC3339Time, error) {
	t, err := time.Parse("2006-01-02 15:04:05", v)
	if err != nil {
		return rfctime.RFC3339Time{}, err
	}
	return rfctime.NewRFC3339(t), nil
})

ConverterRFC339TimeToString Does not really use RFC339 - but sqlite does not understand timezones and the `T` delimiter

View Source
var ConverterRFCUnixMilliTimeToUnixMillis = NewDBTypeConverter[rfctime.UnixMilliTime, int64](func(v rfctime.UnixMilliTime) (int64, error) {
	return v.UnixMilli(), nil
}, func(v int64) (rfctime.UnixMilliTime, error) {
	return rfctime.NewUnixMilli(time.UnixMilli(v)), nil
})
View Source
var ConverterRFCUnixNanoTimeToUnixNanos = NewDBTypeConverter[rfctime.UnixNanoTime, int64](func(v rfctime.UnixNanoTime) (int64, error) {
	return v.UnixNano(), nil
}, func(v int64) (rfctime.UnixNanoTime, error) {
	return rfctime.NewUnixNano(time.Unix(0, v)), nil
})
View Source
var ConverterRFCUnixTimeToUnixSeconds = NewDBTypeConverter[rfctime.UnixTime, int64](func(v rfctime.UnixTime) (int64, error) {
	return v.Unix(), nil
}, func(v int64) (rfctime.UnixTime, error) {
	return rfctime.NewUnix(time.Unix(v, 0)), 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 HashGoSqliteSchema added in v0.0.350

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

HashGoSqliteSchema use if mattn/go-sqlite3

func HashMattnSqliteSchema added in v0.0.350

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

HashMattnSqliteSchema use if github.com/glebarez/go-sqlite

func HashSqliteDatabase added in v0.0.126

func HashSqliteDatabase(ctx context.Context, db Queryable) (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 PPID added in v0.0.358

func PPID() string

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](ctx context.Context, q Queryable, rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool) ([]TData, error)

func ScanSingle added in v0.0.34

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

Types

type DB

type DB interface {
	Queryable

	Ping(ctx context.Context) error
	BeginTransaction(ctx context.Context, iso sql.IsolationLevel) (Tx, error)
	AddListener(listener Listener)
	Exit() error
	RegisterConverter(DBTypeConverter)
	RegisterDefaultConverter()
}

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 JsonArr added in v0.0.355

type JsonArr []any

type JsonObj added in v0.0.355

type JsonObj map[string]any

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 BuildUpdateStatement added in v0.0.358

func BuildUpdateStatement(q Queryable, tableName string, obj any, idColumn string) (string, PP, error)

func Join added in v0.0.51

func Join(pps ...PP) PP

func (*PP) Add added in v0.0.358

func (pp *PP) Add(v any) string

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)
	ListConverter() []DBTypeConverter
}

type StructScanMode added in v0.0.44

type StructScanMode string
const (
	SModeFast     StructScanMode = "FAST"     // Use default sq.Scan, does not work with joined/resolved types and/or custom value converter
	SModeExtended StructScanMode = "EXTENDED" // Fully featured perhaps (?) a tiny bit slower - default
)

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(q Queryable, dest any) error

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

type Tx

type Tx interface {
	Queryable

	Rollback() error
	Commit() error
	Status() TxStatus
}

func NewTransaction

func NewTransaction(xtx *sqlx.Tx, txid uint16, db *database) 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