orm

package
v0.0.0-...-8e1e73d Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package orm NOTES

Package orm NOTES

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRecordNotFound returns a "record not found error".
	// Occurs only when attempting to query the database with a struct,
	// querying with a slice won't return this error.
	ErrRecordNotFound = sql.ErrNoRows
	// ErrDeadLock concurrent exec deadlock, error message returned by db.
	ErrDeadLock = "Error 1213: Deadlock found when trying to get lock"
)
View Source
var (
	// EndBracketsReg ....
	EndBracketsReg = regexp.MustCompile(`\([^()]*\)\s*$`)
)

Functions

func GetNamedSelectColumns

func GetNamedSelectColumns(table interface{}) (expr string, err error)

GetNamedSelectColumns get 'all' the named field with 'db' tag and embedded if it's an embedded struct. Use table.App as an example, the returned expr should as follows: id, biz_id, name as 'spec.name', memo as 'spec.memo' Note: define the embedded columns in the table.go manually. Deprecated: GetNamedSelectColumns will panic if there is a nil value.

func RearrangeSQLDataWithOption

func RearrangeSQLDataWithOption(data interface{}, opts *FieldOption) (
	expr string, toUpdate map[string]interface{}, err error)

RearrangeSQLDataWithOption parse a *struct into a sql expression, and returned with the update sql expression and the to be updated data.

  1. the input FieldOption only works for the returned 'expr', not controls the returned 'toUpdate', so the returned 'toUpdate' contains all the flatted tagged 'db' field and value.
  2. Obviously, a data field need to be updated if the field value is not blank(as is not "ZERO"),
  3. If the field is defined in the blank options deliberately, then update it to blank value as required.
  4. see the test case to know the exact data returned.

func RecursiveGetDBTags

func RecursiveGetDBTags(table interface{}) ([]string, error)

RecursiveGetDBTags get a table's all the db tag recursively.

func RecursiveGetTaggedFieldValues

func RecursiveGetTaggedFieldValues(v interface{}) (map[string]interface{}, error)

RecursiveGetTaggedFieldValues get all the tagged db kv in the struct to a flat map except ptr and struct tag. Note:

  1. if the embedded tag is same, then it will be overlapped.
  2. use this function carefully, it not supports all the type, such as array, slice, map is not supported.
  3. see the test case to know the output data example.

Types

type DoOrm

type DoOrm interface {
	Get(ctx context.Context, dest interface{}, expr string, args ...interface{}) error
	Select(ctx context.Context, dest interface{}, expr string, args ...interface{}) error
	Count(ctx context.Context, expr string, args ...interface{}) (uint32, error)
	Delete(ctx context.Context, expr string, args ...interface{}) (int64, error)
	Update(ctx context.Context, expr string, args interface{}) (int64, error)
	Insert(ctx context.Context, expr string, data interface{}) error
	BulkInsert(ctx context.Context, expr string, args interface{}) error
	Exec(ctx context.Context, expr string) (int64, error)
}

DoOrm defines all the orm method.

type DoOrmWithTransaction

type DoOrmWithTransaction interface {
	Insert(ctx context.Context, expr string, args interface{}) error
	BulkInsert(ctx context.Context, expr string, args interface{}) error
	Delete(ctx context.Context, expr string, args ...interface{}) error
	Update(ctx context.Context, expr string, args interface{}) (int64, error)
}

DoOrmWithTransaction defines all the orm method with transaction.

type FieldOption

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

FieldOption is to define which field need to be:

  1. updated to blank(as is ZERO) value.
  2. be ignored, which means not be updated even its value is not blank(as is not ZERO).

NOTE: 1. A field can not in the blanked and ignore fields at the same time. if a field does, then it will be ignored without being updated. 2. The map's key is the structs' 'db' tag of that field.

func NewFieldOptions

func NewFieldOptions() *FieldOption

NewFieldOptions create a blank option instances for add keys to be updated when update data.

func (*FieldOption) AddBlankedFields

func (f *FieldOption) AddBlankedFields(fields ...string) *FieldOption

AddBlankedFields add fields to be updated to blank values.

func (*FieldOption) AddIgnoredFields

func (f *FieldOption) AddIgnoredFields(fields ...string) *FieldOption

AddIgnoredFields add fields which do not need to be updated even it do has a value.

func (*FieldOption) NeedBlanked

func (f *FieldOption) NeedBlanked(field string) bool

NeedBlanked check if this field need to be updated with blank

func (*FieldOption) NeedIgnored

func (f *FieldOption) NeedIgnored(field string) bool

NeedIgnored check if this field does not need to be updated.

type Interface

type Interface interface {
	Do(db *sqlx.DB) DoOrm
	Txn(tx *sqlx.Tx) DoOrmWithTransaction
}

Interface defines all the orm related operations.

func Do

func Do(opt cc.Sharding) Interface

Do return orm operations.

Jump to

Keyboard shortcuts

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