ormx

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Raw = sb.Raw

Functions

func Any2Slice

func Any2Slice(data any) []any

func Any2Time

func Any2Time(i any) (time.Time, bool)

func Build

func Build(ctx context.Context, b Builder) (string, []any)

Build is same with builder.Build, but it will try to inject namespace(which defined in context) filter into where condition in sql

func Close

func Close() error

Close the connections to the database server in driver

func ColNamesWithTagOpt

func ColNamesWithTagOpt(d interface{}, tag string, opt *Option) []string

ColNamesWithTagOpt will column names from structure data, the type of d must be a struct, otherwise will return []string{}.

ColNamesWithTagOpt will try to filter the filter the struct field which having <tag> specified in StructField.Tag if <tag> is not empty

func Connect

func Connect(ctx context.Context) error

Connect to the database server by using the addr and password specified in flags

func Count

func Count(ctx context.Context, filter any, opt *Option) (int64, error)

Count select the count of rows in table which match the filter condition

func CountBy

func CountBy(ctx context.Context, dst any, filter any, group []string, opt *Option) error

Count select the count of rows in table which match the filter condition

func DefaultProvider

func DefaultProvider(isMaster bool) *sqlx.DB

DefaultProvider return the sqlx.DB created by Connect()

func DeleteByID

func DeleteByID(ctx context.Context, id any, opt *Option) error

DeleteWhere delete rows by id in transaction from the table

func DeleteWhere

func DeleteWhere(ctx context.Context, filter any, opt *Option) error

DeleteWhereTx delete rows that match the filter in transaction from the given table

func Distinct

func Distinct(ctx context.Context, column string, filter any, opt *Option) ([]any, error)

Distinct fetch distinct values of the column in table

func Exec

func Exec(ctx context.Context, sql string, args ...interface{}) (driver.Result, error)

Exec execute a sql on master DB

func ExecTx

func ExecTx(ctx context.Context, tx *sqlx.Tx, sql string, args ...interface{}) (driver.Result, error)

Exec execute a sql in transaction

func Exist

func Exist(ctx context.Context, filter any, opt *Option) (bool, error)

Exist return true if the at least one row found in table by using where condition

func Float64

func Float64(v interface{}) (f float64, err error)

Float64 将参数 value 转换成 float64 返回

func FromMaster

func FromMaster(ctx context.Context) context.Context

FromMaster force ormx execute sql on master instance when called by this context

func FromSlave

func FromSlave(ctx context.Context) context.Context

FromMaster force ormx execute sql on slave instance when called by this context

func Get

func Get(ctx context.Context, dest interface{}, sql string, args ...interface{}) error

Get will get one data into dest with raw sql and args.

it will auto query from master if the context having FromMaster

func GetByID

func GetByID(ctx context.Context, dst any, id any, opt *Option) error

func GetTx

func GetTx(ctx context.Context, tx *sqlx.Tx, dest interface{}, sql string, args ...interface{}) error

Get will get one data from tx by using raw sql and args.

func GetWhere

func GetWhere(ctx context.Context, dst any, filter any, opt *Option) error

GetWhere 使用自定义条件跟新数据

func Init

func Init(ctx context.Context, opt *Option) error

Init the ormx, setting the sqlx.DB getter and common table name prefix

func InsertIgnore

func InsertIgnore(ctx context.Context, data []any, opt *Option) error

InsertIgnore insert new data into database and ingore the rows on duplicate keys using transaction

func InsertMany

func InsertMany(ctx context.Context, data []any, opt *Option) error

InsertMany insert rows in transaction, the all data type should be same structure.

func InsertOne

func InsertOne(ctx context.Context, data any, opt *Option) (int64, error)

InsertOne insert rows in transaction, the data type should be structure.

func IsDuplicate

func IsDuplicate(err error) bool

IsDuplicate 判断查询错误是否是 未找到错误

func IsNotFound

func IsNotFound(err error) bool

IsNotFound 判断查询错误是否是 未找到错误

func Master

func Master() *sqlx.DB

Master return master *sqlx.DB which returned by DBProvider, panic if DBProvider is not Initilized

func NewInsertBuilderFromStruct

func NewInsertBuilderFromStruct(ctx context.Context, data []any, opt *Option) (*sb.InsertBuilder, error)

NewInsertBuilderFromStruct create a new insert builder from data, the struct field with 'insert' option in field tag will be inserted

such as: db:"columnName,insert" or db:",insert"

the struct field with no insert tag option, will be ignored

func NewSelectBuilderFromStruct

func NewSelectBuilderFromStruct(data any, opt *Option) (*sb.SelectBuilder, error)

NewSelectBuilderFromStruct create select sql builder by data

func NewUpdateBuilderFromStruct

func NewUpdateBuilderFromStruct(data any, opt *Option) (*sb.UpdateBuilder, bool)

NewUpdateBuilderFromStruct 使用 data 数据定义 update builder

func ParseOptionStr

func ParseOptionStr(str string) map[string]string

ParseOptionStr will decode key-value data from a string which format like k1:v1,k2:v2,k3:v3. it will always return a non-nil value map such as:

  • k1:v1,k2:v2 will parsed to {"k1":"v1","k2":"v2"}
  • k1,k2 will parsed to {"k1":"","k2":""}
  • k1:v2,k2 will parsed to {"k1":"v2","k2":""}

func PatchByID

func PatchByID(ctx context.Context, id any, data any, opt *Option) error

PatchByIDTx updates the data by id in the table using a transaction.

func PatchWhere

func PatchWhere(ctx context.Context, data any, filter any, opt *Option) (int64, error)

PatchWhereTx updates the data that matchthe filter in the table using a transaction. The filter is used as the condition and can be of type KVs, struct, []int64, int64.

func Replace

func Replace(ctx context.Context, data any, opt *Option) (int64, error)

Replace insert rows in transaction, the data type should be structure.

func RunInLock

func RunInLock(ctx context.Context, table string, f func(ctx context.Context) error) (err error)

RunInLock execute a sql in lock

func RunInRLock

func RunInRLock(ctx context.Context, table string, f func(ctx context.Context) error) (err error)

func RunTxContext

func RunTxContext(ctx context.Context, f func(ctx context.Context, tx *sqlx.Tx) error) error

RunTxContext execute a transiction

func RunTxWithOptionContext

func RunTxWithOptionContext(ctx context.Context, opts *sql.TxOptions, f func(ctx context.Context, tx *sqlx.Tx) error) error

RunTxContext execute a transiction with custom options

func Select

func Select(ctx context.Context, dest interface{}, sql string, args ...interface{}) error

Select will query data into dest with raw sql and args.

it will auto query from master if the context having FromMaster

func SelectTx

func SelectTx(ctx context.Context, tx *sqlx.Tx, dest interface{}, sql string, args ...interface{}) error

Select will query data into dest with raw sql and args.

it will auto query from master if the context having FromMaster

func SelectWhere

func SelectWhere(ctx context.Context, dst any, filter any, opt *Option) error

GetWhere 使用自定义条件跟新数据

func SetGlobalOption

func SetGlobalOption(opt *Option)

SetGlobalOption set option for all the sql execution

func SetLogger

func SetLogger(l zerolog.Logger)

func SetMetricHandler

func SetMetricHandler(h MetricHandler)

func Slave

func Slave() *sqlx.DB

Master return slave *sqlx.DB which returned by DBProvider, panic if DBProvider is not Initilized

func TableName

func TableName(d any, opt *Option) string

TableName auto recoganize the table name from data, it will auto prepend the tableNamePrefix which can be set by SetTableNamePrefix to the result.

  • having Table() method, it will call d.Table() to get the table name
  • type of struct, it will use the struct name, and snake case it
  • type of string, return the name.
  • type of other, return fmt.Sprintf("%s", d)

func WhereFrom

func WhereFrom(c *sb.Cond, filter any, dst []string, opt *Option) []string

func WhereFromID

func WhereFromID(c *sb.Cond, id int64, dst []string) []string

func WhereFromIDs

func WhereFromIDs(c *sb.Cond, idList []int64, dst []string) []string

func WhereFromKVs

func WhereFromKVs(c *sb.Cond, filter KVs, dst []string) []string

WhereFromStruct generate where exprs from []KV, the returned value can be used by builder.Where method

func WhereFromStruct

func WhereFromStruct(c *sb.Cond, data any, dst []string, opt *Option) []string

WhereFromStruct generate where exprs from data(type of struct), the returned value can be used by builder.Where method

Types

type Builder

type Builder interface {
	Build() (string, []any)
}

type DBProvider

type DBProvider func(isMaster bool) *sqlx.DB

DBProvider

type FieldOptioner

type FieldOptioner interface {
	// return the field's option by field name for structure
	OrmxFieldOption(string) string
}

type KV

type KV struct {
	Key   string
	Value any
	Extra string
}

type KVs

type KVs []KV

func KVsFromMap

func KVsFromMap(dst KVs, filter map[string]any) KVs

KVsFromMap generate KVs from map

type M

type M map[string]any

type MetricHandler

type MetricHandler interface {
	Emit(context.Context, string, bool)
}

type Option

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

Option for select or insert data

func CtxOption

func CtxOption(ctx context.Context) *Option

CtxOption try to get option from context, if not found, return NewOption()

func NewOption

func NewOption() *Option

NewOptionCtx return a new option for execute sql

func (*Option) Copy

func (opt *Option) Copy() *Option

func (*Option) Fields

func (opt *Option) Fields(value ...string) *Option

func (*Option) FromMaster

func (opt *Option) FromMaster() *Option

func (*Option) IgnoreNamespace

func (opt *Option) IgnoreNamespace() *Option

func (*Option) Isolation

func (opt *Option) Isolation(level sql.IsolationLevel) *Option

func (*Option) Namespace

func (opt *Option) Namespace(value string) *Option

func (*Option) NamespaceColumnName

func (opt *Option) NamespaceColumnName(value string) *Option

func (*Option) Page

func (opt *Option) Page(page, pageSize int) *Option

func (*Option) PrimaryKey

func (opt *Option) PrimaryKey(value string) *Option

func (*Option) Sorts

func (opt *Option) Sorts(value ...string) *Option

func (*Option) Table

func (opt *Option) Table(value any) *Option

func (*Option) TablePrefix

func (opt *Option) TablePrefix(value string) *Option

func (*Option) TagName

func (opt *Option) TagName(value string) *Option

func (*Option) Tx

func (opt *Option) Tx(tx *sqlx.Tx) *Option

func (*Option) With

func (opt *Option) With(ctx context.Context) context.Context

With put the Option into context by Context.WithValue

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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