model

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

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

Go to latest
Published: Aug 31, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HasOne  = "hasOne"
	HasMany = "hasMany"
)

Variables

This section is empty.

Functions

func AddRelationFn

func AddRelationFn(getVal, join bool, q *QueryCondition, r RelationFn) func() (bool, bool, *QueryCondition, RelationFn)

func BuildQuerySql

func BuildQuerySql(q *QueryCondition) (r string, args []any, err error)

func Chunk

func Chunk[T Model, R any](ctx context.Context, perLimit int, fn func(rows T) (R, bool), q *QueryCondition) (r []R, err error)

Chunk 分片查询并函数过虑返回新类型的切片

Conditions 中可用 Where Fields Group Having Join Order Limit In 函数

func ChunkFind

func ChunkFind[T Model](ctx context.Context, perLimit int, q *QueryCondition) (r []T, err error)

ChunkFind 分片查询并直接返回所有结果

Conditions 中可用 Where Fields Group Having Join Order Limit In 函数

func ChunkFindFromDB

func ChunkFindFromDB[T Model](db dbQuery, ctx context.Context, perLimit int, q *QueryCondition) (r []T, err error)

ChunkFindFromDB 同 ChunkFind

Conditions 中可用 Where Fields Group Having Join Order Limit In 函数

func ChunkFromDB

func ChunkFromDB[T Model, R any](db dbQuery, ctx context.Context, perLimit int, fn func(rows T) (R, bool), q *QueryCondition) (r []R, err error)

ChunkFromDB 同 Chunk

Conditions 中可用 Where Fields Group Having Join Order Limit In 函数

func Column

func Column[V Model, T any](ctx context.Context, fn func(V) (T, bool), q *QueryCondition) ([]T, error)

func ColumnFromDB

func ColumnFromDB[V Model, T any](db dbQuery, ctx context.Context, fn func(V) (T, bool), q *QueryCondition) (r []T, err error)

func DBWithOther

func DBWithOther(db dbQuery, ctx context.Context, r any, q *QueryCondition) error

func Find

func Find[T Model](ctx context.Context, where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, having SqlBuilder, limit int, in ...[]any) (r []T, err error)

func FindFromDB

func FindFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (r []T, err error)

FindFromDB 同 Finds 使用指定 db 查询

Conditions 中可用 Where Fields Group Having Join Order Offset Limit In 函数

func FindOneById

func FindOneById[T Model, I constraints.Integer](ctx context.Context, id I) (T, error)

func FindScanner

func FindScanner[T Model](ctx context.Context, fn func(T), q *QueryCondition) error

func FindScannerFromDB

func FindScannerFromDB[T Model](db dbQuery, ctx context.Context, fn func(T), q *QueryCondition) error

func FindToStringMap

func FindToStringMap[T Model](ctx context.Context, q *QueryCondition) (r []map[string]string, err error)

func FindToStringMapFromDB

func FindToStringMapFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (r []map[string]string, err error)

func Finds

func Finds[T Model](ctx context.Context, q *QueryCondition) (r []T, err error)

Finds 比 Find 多一个offset

Conditions 中可用 Where Fields Group Having Join Order Offset Limit In 函数

func FirstOne

func FirstOne[T Model](ctx context.Context, where ParseWhere, fields string, order SqlBuilder, in ...[]any) (T, error)

func FormatSql

func FormatSql(sql string, params ...any) string

func Get

func Get[T Model](ctx context.Context, sql string, params ...any) (r T, err error)

Get 可以使用 {table}来替代 T的表名

func GetField

func GetField[T Model](ctx context.Context, field string, q *QueryCondition) (r string, err error)

func GetFieldFromDB

func GetFieldFromDB[T Model](db dbQuery, ctx context.Context, field string, q *QueryCondition) (r string, err error)

func GetToMap

func GetToMap[V any](ctx context.Context, db *sqlx.DB, dest *map[string]V, sql string, params ...any) (err error)

func GetToStringMap

func GetToStringMap[T Model](ctx context.Context, q *QueryCondition) (r map[string]string, err error)

func GetToStringMapFromDB

func GetToStringMapFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (r map[string]string, err error)

func GetWithID

func GetWithID[T, V any](fn func(*T) V) func(any) []any

func Gets

func Gets[T Model](ctx context.Context, q *QueryCondition) (T, error)

func GetsFromDB

func GetsFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (T, error)

func InitDB

func InitDB(db dbQuery)

func LastOne

func LastOne[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (T, error)

func Pagination

func Pagination[T Model](ctx context.Context, q *QueryCondition, page, pageSize int) ([]T, int, error)

Pagination 同

Condition 中可使用 Where Fields From Group Having Join Order Limit In 函数

func PaginationFromDB

func PaginationFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition, page, pageSize int) ([]T, int, error)

PaginationFromDB 同 Pagination 方便多个db使用

Condition 中可使用 Where Fields Group Having Join Order Limit In 函数

func PaginationToMap

func PaginationToMap[T Model](ctx context.Context, q *QueryCondition, page, pageSize int) (r []map[string]string, total int, err error)

func PaginationToMapFromDB

func PaginationToMapFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition, page, pageSize int) (r []map[string]string, total int, err error)

func ParseRelation

func ParseRelation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) (err error)

func PrimaryKey

func PrimaryKey[T Model]() string

func Relation

func Relation(isPlural bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) ([]func(), []func() error)

func Scanner

func Scanner[T any](ctx context.Context, db *sqlx.DB, v T, s string, params ...any) func(func(T)) error

func Select

func Select[T Model](ctx context.Context, sql string, params ...any) ([]T, error)

Select 如果查询的为T的表名,可以使用 {table}来代替

func SetGet

func SetGet(db *SqlxQuery, fn func(context.Context, any, string, ...any) error)

func SetHasMany

func SetHasMany[T, V any, K comparable](assignmentFn func(*T, *[]V), pIdFn func(*T) K, mIdFn func(*V) K) func(any, any)

SetHasMany eg: post has many comments,pIdFn is comment's postId, mIdFn is post's id

func SetHasOne

func SetHasOne[T, V any, K comparable](assignmentFn func(*T, *V), mIdFn func(*T) K, pIdFn func(*V) K) func(any, any)

SetHasOne mIdFn is main , pIdFn is part

eg: post has a user. mIdFn is post's userId, iddFn is user's id

func SetSelect

func SetSelect(db *SqlxQuery, fn func(context.Context, any, string, ...any) error)

func SimpleFind

func SimpleFind[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (r []T, err error)

func Table

func Table[T Model]() string

func ToMapSlice

func ToMapSlice[V any](ctx context.Context, db *sqlx.DB, dest *[]map[string]V, sql string, params ...any) (err error)

func WithOther

func WithOther(ctx context.Context, r any, q *QueryCondition) error

Types

type AndWhere

type AndWhere interface {
	AndWhere(field, operator, val, fieldType string) ParseWhere
}

type Condition

type Condition func(c *QueryCondition)

func Fields

func Fields(fields string) Condition

func From

func From(from string) Condition

func Group

func Group(group string) Condition

func Having

func Having[T ~[][]string](having T) Condition

func In

func In(in ...[]any) Condition

func Join

func Join[T ~[][]string](join T) Condition

func Limit

func Limit(limit int) Condition

func Offset

func Offset(offset int) Condition

func Order

func Order[T ~[][]string](order T) Condition

func TotalRaw

func TotalRaw(total int) Condition

TotalRaw only effect on Pagination, ChunkFind,when TotalRaw>0 ,will not query count

func Where

func Where(where ParseWhere) Condition

func WithCtx

func WithCtx(ctx *context.Context) Condition

func WithFn

func WithFn(getVal, isJoin bool, q *QueryCondition, fn func() (func(any) []any, func(any, any), func(bool) any, Relationship)) Condition

type Model

type Model interface {
	PrimaryKey() string
	Table() string
}

type ParseWhere

type ParseWhere interface {
	ParseWhere(*[][]any) (string, []any, error)
}

type QueryCondition

type QueryCondition struct {
	Where      ParseWhere
	From       string
	Fields     string
	Group      string
	Order      SqlBuilder
	Join       SqlBuilder
	Having     SqlBuilder
	Limit      int
	Offset     int
	TotalRow   int
	In         [][]any
	RelationFn []func() (bool, bool, *QueryCondition, RelationFn)
}

func Conditions

func Conditions(fns ...Condition) *QueryCondition

type QueryFn

type QueryFn func(context.Context, any, string, ...any) error

type RelationFn

type RelationFn func() (func(any) []any, func(any, any), func(bool) any, Relationship)

func RelationHasMany

func RelationHasMany[M, P any, I constraints.Integer | constraints.Unsigned](mId func(*M) I, pId func(*P) I, setVal func(*M, *[]P), r Relationship) RelationFn

RelationHasMany eg: post has many comments,mId is post's id, pId is comment's postId

func RelationHasOne

func RelationHasOne[M, P any, I constraints.Integer | constraints.Unsigned](fId func(*M) I, pId func(*P) I, setVal func(*M, *P), r Relationship) RelationFn

RelationHasOne eg: post has a user. fId is post's userId, pId is user's id

type Relationship

type Relationship struct {
	RelationType string
	Table        string
	ForeignKey   string
	Local        string
	On           string
	Middle       *Relationship
}

Relationship join table

RelationType HasOne| HasMany

eg: hasOne, post has a user. ForeignKey is user's id , Local is post's userId field

eg: hasMany, post has many comments,ForeignKey is comment's postId field, Local is post's id field

On is additional join on conditions

type SqlBuilder

type SqlBuilder [][]string

func (SqlBuilder) AndWhere

func (w SqlBuilder) AndWhere(field, operator, val, fieldType string) ParseWhere

func (SqlBuilder) ParseWhere

func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error)

ParseWhere 解析为where条件,支持3种风格,具体用法参照query_test中的 Find 的测试方法

1. 1个为一组 {{"field operator value"}} 为纯字符串条件,不对参数做处理

2. 2个为一组 {{"field1","value1"},{"field2","value2"}} => where field1='value1' and field2='value2'

3. 3个或4个为一组 {{"field","operator","value"[,"int|float"]}} => where field operator 'string'|int|float

{{"a",">","1","int"}} => where 'a'> 1

{{ "a",">","1"}} => where 'a'>'1'

另外如果是操作符为in的话为 {{"field","in",""}} => where field in (?,..) in的条件传给 in参数

4. 5的倍数为一组{{"and|or","field","operator","value","int|float"}}会忽然掉第一组的and|or

{{"and","field","=","value1","","and","field","=","value2",""}} => where (field = 'value1' and field = 'value2')

{{"and","field","=","num1","int","or","field","=","num2","int"}} => where (field = num1 or field = num2')

type SqlxQuery

type SqlxQuery struct {
	UniversalDb
	// contains filtered or unexported fields
}

func NewSqlxQuery

func NewSqlxQuery(sqlx *safety.Var[*sqlx.DB], u UniversalDb) *SqlxQuery

func (*SqlxQuery) Gets

func (r *SqlxQuery) Gets(ctx context.Context, dest any, sql string, params ...any) error

func (*SqlxQuery) Selects

func (r *SqlxQuery) Selects(ctx context.Context, dest any, sql string, params ...any) error

type UniversalDb

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

func NewUniversalDb

func NewUniversalDb(selects QueryFn, gets QueryFn) UniversalDb

func (UniversalDb) Get

func (u UniversalDb) Get(ctx context.Context, a any, s string, args ...any) error

func (UniversalDb) Select

func (u UniversalDb) Select(ctx context.Context, a any, s string, args ...any) error

Jump to

Keyboard shortcuts

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