db

package module
v0.0.0-...-3fd964c Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2025 License: BSD-3-Clause Imports: 7 Imported by: 0

README

A wrapper of xorm

How to use

The library will only support singlton application. That means only one engine will be created in the whole application.

First, create the xorm engine like below.

engine, err := xorm.NewEngine(...)

And then SetEngine before use any database operations

db.SetXORMEngine(engine)

Now you can use the follow functions like what use an engine

db.GetEngine(ctx)...Get()
db.GetEngine(ctx)...Find()

You can now use transaction like below.

db.WithTx(ctx, func(ctx context) error{
    db.GetEngine(ctx)....Get
})

And some useful functions are introduced

db.Find[MyModel](ctx, FindOptions{
    ...
})

db.Count[MyModel](ctx, FindOptions{
    ...
})

db.FindAndCount[MyModel](ctx, FindOptions{
    ...
})
db.GetByID[MyMode](ctx, id)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IsProd = true

Functions

func Count

func Count[T any](ctx context.Context, opts FindOptions) (int64, error)

Count represents a common count function which accept an options interface

func CountByBean

func CountByBean(ctx context.Context, bean any) (int64, error)

CountByBean counts the number of database records according non-empty fields of the bean as conditions.

func Delete

func Delete[T any](ctx context.Context, opts FindOptions) (int64, error)

func DeleteByBean

func DeleteByBean(ctx context.Context, bean any) (int64, error)

DeleteByBean deletes all records according non-empty fields of the bean as conditions.

func DeleteByBeans

func DeleteByBeans(ctx context.Context, beans ...any) (err error)

DeleteByBeans deletes all given beans, beans must contain delete conditions.

func DeleteByID

func DeleteByID[T any](ctx context.Context, id int64) (int64, error)

DeleteByID deletes the given bean with the given ID

func DeleteByIDs

func DeleteByIDs[T any](ctx context.Context, ids ...int64) error

func Exec

func Exec(ctx context.Context, sqlAndArgs ...any) (sql.Result, error)

Exec executes a sql with args

func Exist

func Exist[T any](ctx context.Context, cond builder.Cond) (bool, error)

func ExistByID

func ExistByID[T any](ctx context.Context, id int64) (bool, error)

func Find

func Find[T any](ctx context.Context, opts FindOptions) ([]*T, error)

Find represents a common find function which accept an options interface

func FindAndCount

func FindAndCount[T any](ctx context.Context, opts FindOptions) ([]*T, int64, error)

FindAndCount represents a common findandcount function which accept an options interface

func FindIDs

func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([]int64, error)

FindIDs finds the IDs for the given table name satisfying the given condition By passing a different value than "id" for "idCol", you can query for foreign IDs, i.e. the repo IDs which satisfy the condition

func Get

func Get[T any](ctx context.Context, cond builder.Cond) (object *T, exist bool, err error)

func GetByID

func GetByID[T any](ctx context.Context, id int64) (object *T, exist bool, err error)

func GetXORMEngine

func GetXORMEngine() *xorm.Engine

func InTransaction

func InTransaction(ctx context.Context) bool

InTransaction returns true if the engine is in a transaction otherwise return false

func Insert

func Insert(ctx context.Context, beans ...any) error

Insert inserts records into database

func SetDefaultFindSliceSize

func SetDefaultFindSliceSize(size int)

func SetXORMEngine

func SetXORMEngine(engine *xorm.Engine)

func TruncateBeans

func TruncateBeans(ctx context.Context, beans ...any) (err error)

TruncateBeans deletes all given beans, beans may contain delete conditions.

func WithTx

func WithTx(parentCtx context.Context, f func(ctx context.Context) error) error

WithTx represents executing database operations on a transaction, if the transaction exist, this function will reuse it otherwise will create a new one and close it when finished.

func WithTx2

func WithTx2[T any](parentCtx context.Context, f func(ctx context.Context) (T, error)) (ret T, errRet error)

WithTx2 is similar to WithTx, but it has two return values: result and error.

Types

type Committer

type Committer interface {
	Commit() error
	Close() error
}

Committer represents an interface to Commit or Close the Context

type Engine

type Engine interface {
	Table(tableNameOrBean any) *xorm.Session
	Count(...any) (int64, error)
	Decr(column string, arg ...any) *xorm.Session
	Delete(...any) (int64, error)
	Truncate(...any) (int64, error)
	Exec(...any) (sql.Result, error)
	Find(any, ...any) error
	Get(beans ...any) (bool, error)
	ID(any) *xorm.Session
	In(string, ...any) *xorm.Session
	Incr(column string, arg ...any) *xorm.Session
	Insert(...any) (int64, error)
	Iterate(any, xorm.IterFunc) error
	Join(joinOperator string, tablename, condition any, args ...any) *xorm.Session
	SQL(any, ...any) *xorm.Session
	Where(any, ...any) *xorm.Session
	Asc(colNames ...string) *xorm.Session
	Desc(colNames ...string) *xorm.Session
	Limit(limit int, start ...int) *xorm.Session
	NoAutoTime() *xorm.Session
	SumInt(bean any, columnName string) (res int64, err error)
	Sync(...any) error
	Select(string) *xorm.Session
	SetExpr(string, any) *xorm.Session
	NotIn(string, ...any) *xorm.Session
	OrderBy(any, ...any) *xorm.Session
	Exist(...any) (bool, error)
	Distinct(...string) *xorm.Session
	Query(...any) ([]map[string][]byte, error)
	Cols(...string) *xorm.Session
	Context(ctx context.Context) *xorm.Session
	Ping() error
	IsTableExist(tableNameOrBean any) (bool, error)
}

Engine represents a xorm engine or session.

func GetEngine

func GetEngine(ctx context.Context) Engine

GetEngine gets an existing db Engine/Statement or creates a new Session

type FindOptions

type FindOptions interface {
	GetPage() int
	GetPageSize() int
	IsListAll() bool
	ToConds() builder.Cond
}

FindOptions represents a find options

type FindOptionsJoin

type FindOptionsJoin interface {
	ToJoins() []JoinFunc
}

type FindOptionsOrder

type FindOptionsOrder interface {
	ToOrders() string
}

type JoinFunc

type JoinFunc func(sess Engine) error

Jump to

Keyboard shortcuts

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