orm

package
v0.0.0-...-2af02f3 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoRows error from the beego orm
	ErrNoRows = orm.ErrNoRows

	// ErrOptimisticLock error when update object failed
	ErrOptimisticLock = errors.New("the object has been modified; please apply your changes to the latest version and try again")
)
View Source
var (
	// Crt is a global instance of ORM creator
	Crt = NewCreator()
)
View Source
var NewCondition = orm.NewCondition

NewCondition alias function of orm.NewCondition

Functions

func AsConflictError

func AsConflictError(err error, messageFormat string, args ...interface{}) *errors.Error

AsConflictError checks whether the err is duplicate key error. If it is, wrap it as a src/internal/error.Error with conflict error code, else return nil

func AsForeignKeyError

func AsForeignKeyError(err error, messageFormat string, args ...interface{}) *errors.Error

AsForeignKeyError checks whether the err is violating foreign key constraint error. If it it, wrap it as a src/internal/error.Error with violating foreign key constraint error code, else return nil

func AsNotFoundError

func AsNotFoundError(err error, messageFormat string, args ...interface{}) *errors.Error

AsNotFoundError checks whether the err is orm.ErrNoRows. If it it, wrap it as a src/internal/error.Error with not found error code, else return nil

func Clone

func Clone(ctx context.Context) context.Context

Clone returns new context with orm for ctx

func Context

func Context() context.Context

Context returns a context with an orm

func Copy

func Copy(ctx context.Context) context.Context

Copy returns new context with orm and value from parent context but no linkage of parent.

func CreateInClause

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

CreateInClause creates an IN clause with the provided sql and args to avoid the sql injection The sql should return the ID list with the specific condition(e.g. select id from table1 where column1=?) The sql runs as a prepare statement with the "?" be populated rather than concat string directly The returning in clause is a string like "IN (id1, id2, id3, ...)"

func Escape

func Escape(str string) string

Escape ..

func FromContext

func FromContext(ctx context.Context) (orm.QueryExecutor, error)

FromContext returns orm from context

func GetTransactionOpNameFromContext

func GetTransactionOpNameFromContext(ctx context.Context) string

GetTransactionOpNameFromContext returns the transaction operation name from context

func HasCommittedKey

func HasCommittedKey(ctx context.Context) bool

HasCommittedKey checks whether exist committed key in context.

func IsDuplicateKeyError

func IsDuplicateKeyError(err error) bool

IsDuplicateKeyError check the duplicate key error

func NewContext

func NewContext(ctx context.Context, o orm.QueryExecutor) context.Context

NewContext returns new context with orm

func PaginationOnRawSQL

func PaginationOnRawSQL(query *q.Query, sql string, params []interface{}) (string, []interface{})

PaginationOnRawSQL append page information to the raw sql It should be called after the order by e.g. select a, b, c from mytable order by a limit ? offset ? it appends the " limit ? offset ? " to sql, and appends the limit value and offset value to the params of this query

func ParamPlaceholderForIn

func ParamPlaceholderForIn(n int) string

ParamPlaceholderForIn returns a string that contains placeholders for sql keyword "in" e.g. n=3, returns "?,?,?"

func QuerySetter

func QuerySetter(ctx context.Context, model interface{}, query *q.Query) (orm.QuerySeter, error)

QuerySetter generates the query setter according to the provided model and query. e.g.

type Foo struct{
  Field1 string `orm:"-"`                         // can not filter/sort
  Field2 string `orm:"column(customized_field2)"` // support filter by "Field2", "customized_field2"
  Field3 string `sort:"false"`                    // cannot be sorted
  Field4 string `sort:"default:desc"`             // the default field/order(asc/desc) to sort if no sorting specified in query.
  Field5 string `filter:"false"`                  // cannot be filtered
}

// support filter by "Field6", "field6"

func (f *Foo) FilterByField6(ctx context.Context, qs orm.QuerySetter, key string, value interface{}) orm.QuerySetter {
  ...
	 return qs
}

Defining the method "GetDefaultSorts() []*q.Sort" for the model whose default sorting contains more than one fields

type Bar struct{
  Field1 string
  Field2 string
}

// Sort by "Field1" desc, "Field2"

func (b *Bar) GetDefaultSorts() []*q.Sort {
	return []*q.Sort{
		{
			Key:  "Field1",
			DESC: true,
		},
		{
			Key:  "Field2",
			DESC: false,
		},
	 }
}

func QuerySetterForCount

func QuerySetterForCount(ctx context.Context, model interface{}, query *q.Query, _ ...string) (orm.QuerySeter, error)

QuerySetterForCount creates the query setter used for count with the sort and pagination information ignored

func QuoteLiteral

func QuoteLiteral(literal string) string

QuoteLiteral quotes a 'literal' (e.g. a parameter, often used to pass literal to DDL and other statements that do not accept parameters) to be used as part of an SQL statement. For example:

exp_date := pq.QuoteLiteral("2023-01-05 15:00:00Z")
err := db.Exec(fmt.Sprintf("CREATE ROLE my_user VALID UNTIL %s", exp_date))

Any single quotes in name will be escaped. Any backslashes (i.e. "\") will be replaced by two backslashes (i.e. "\\") and the C-style escape identifier that PostgreSQL provides ('E') will be prepended to the string.

func ReadOrCreate

func ReadOrCreate(ctx context.Context, md interface{}, col1 string, cols ...string) (created bool, id int64, err error)

ReadOrCreate read or create instance to database, retry to read when met a duplicate key error after the creating

func RegisterModel

func RegisterModel(models ...interface{})

RegisterModel ...

func SetTransactionOpNameToContext

func SetTransactionOpNameToContext(ctx context.Context, name string) context.Context

SetTransactionOpNameToContext sets the transaction operation name

func WithTransaction

func WithTransaction(f func(ctx context.Context) error) func(ctx context.Context) error

WithTransaction a decorator which make f run in transaction

func WrapConflictError

func WrapConflictError(err error, format string, args ...interface{}) error

WrapConflictError wrap error as ConflictError when it is duplicate key error otherwise return err

func WrapNotFoundError

func WrapNotFoundError(err error, format string, args ...interface{}) error

WrapNotFoundError wrap error as NotFoundError when it is orm.ErrNoRows otherwise return err

Types

type CommittedKey

type CommittedKey struct{}

type Condition

type Condition = orm.Condition

Condition alias to orm.Condition

type Creator

type Creator interface {
	Create() orm.Ormer
}

Creator creates ORMer Introducing the "Creator" interface to eliminate the dependency on database

func NewCreator

func NewCreator() Creator

NewCreator creates an ORM creator

type Params

type Params = orm.Params

Params alias to orm.Params

type ParamsList

type ParamsList = orm.ParamsList

ParamsList alias to orm.ParamsList

type QuerySeter

type QuerySeter = orm.QuerySeter

QuerySeter alias to orm.QuerySeter

Jump to

Keyboard shortcuts

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