Documentation ¶
Index ¶
- Constants
- func AddRelationFn(getVal, join bool, q *QueryCondition, r RelationFn) func() (bool, bool, *QueryCondition, RelationFn)
- func BuildQuerySql(q *QueryCondition) (r string, args []any, err error)
- func Chunk[T Model, R any](ctx context.Context, perLimit int, fn func(rows T) (R, bool), ...) (r []R, err error)
- func ChunkFind[T Model](ctx context.Context, perLimit int, q *QueryCondition) (r []T, err error)
- func ChunkFindFromDB[T Model](db dbQuery, ctx context.Context, perLimit int, q *QueryCondition) (r []T, err error)
- func ChunkFromDB[T Model, R any](db dbQuery, ctx context.Context, perLimit int, fn func(rows T) (R, bool), ...) (r []R, err error)
- func Column[V Model, T any](ctx context.Context, fn func(V) (T, bool), q *QueryCondition) ([]T, error)
- func ColumnFromDB[V Model, T any](db dbQuery, ctx context.Context, fn func(V) (T, bool), q *QueryCondition) (r []T, err error)
- func DBWithOther(db dbQuery, ctx context.Context, r any, q *QueryCondition) error
- func Find[T Model](ctx context.Context, where ParseWhere, fields, group string, order SqlBuilder, ...) (r []T, err error)
- func FindFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (r []T, err error)
- func FindOneById[T Model, I constraints.Integer](ctx context.Context, id I) (T, error)
- func FindScanner[T Model](ctx context.Context, fn func(T), q *QueryCondition) error
- func FindScannerFromDB[T Model](db dbQuery, ctx context.Context, fn func(T), q *QueryCondition) error
- func FindToStringMap[T Model](ctx context.Context, q *QueryCondition) (r []map[string]string, err error)
- func FindToStringMapFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (r []map[string]string, err error)
- func Finds[T Model](ctx context.Context, q *QueryCondition) (r []T, err error)
- func FirstOne[T Model](ctx context.Context, where ParseWhere, fields string, order SqlBuilder, ...) (T, error)
- func FormatSql(sql string, params ...any) string
- func Get[T Model](ctx context.Context, sql string, params ...any) (r T, err error)
- func GetField[T Model](ctx context.Context, field string, q *QueryCondition) (r string, err error)
- func GetFieldFromDB[T Model](db dbQuery, ctx context.Context, field string, q *QueryCondition) (r string, err error)
- func GetToMap[V any](ctx context.Context, db *sqlx.DB, dest *map[string]V, sql string, ...) (err error)
- func GetToStringMap[T Model](ctx context.Context, q *QueryCondition) (r map[string]string, err error)
- func GetToStringMapFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (r map[string]string, err error)
- func GetWithID[T, V any](fn func(*T) V) func(any) []any
- func Gets[T Model](ctx context.Context, q *QueryCondition) (T, error)
- func GetsFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (T, error)
- func InitDB(db dbQuery)
- func LastOne[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (T, error)
- func Pagination[T Model](ctx context.Context, q *QueryCondition, page, pageSize int) ([]T, int, error)
- func PaginationFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition, page, pageSize int) ([]T, int, error)
- func PaginationToMap[T Model](ctx context.Context, q *QueryCondition, page, pageSize int) (r []map[string]string, total int, err error)
- func PaginationToMapFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition, page, pageSize int) (r []map[string]string, total int, err error)
- func ParseRelation(isMultiple bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) (err error)
- func PrimaryKey[T Model]() string
- func Relation(isPlural bool, db dbQuery, ctx context.Context, r any, q *QueryCondition) ([]func(), []func() error)
- func Scanner[T any](ctx context.Context, db *sqlx.DB, v T, s string, params ...any) func(func(T)) error
- func Select[T Model](ctx context.Context, sql string, params ...any) ([]T, error)
- func SetGet(db *SqlxQuery, fn func(context.Context, any, string, ...any) error)
- func SetHasMany[T, V any, K comparable](assignmentFn func(*T, *[]V), pIdFn func(*T) K, mIdFn func(*V) K) func(any, any)
- func SetHasOne[T, V any, K comparable](assignmentFn func(*T, *V), mIdFn func(*T) K, pIdFn func(*V) K) func(any, any)
- func SetSelect(db *SqlxQuery, fn func(context.Context, any, string, ...any) error)
- func SimpleFind[T Model](ctx context.Context, where ParseWhere, fields string, in ...[]any) (r []T, err error)
- func Table[T Model]() string
- func ToMapSlice[V any](ctx context.Context, db *sqlx.DB, dest *[]map[string]V, sql string, ...) (err error)
- func WithOther(ctx context.Context, r any, q *QueryCondition) error
- type AndWhere
- type Condition
- func Fields(fields string) Condition
- func From(from string) Condition
- func Group(group string) Condition
- func Having[T ~[][]string](having T) Condition
- func In(in ...[]any) Condition
- func Join[T ~[][]string](join T) Condition
- func Limit(limit int) Condition
- func Offset(offset int) Condition
- func Order[T ~[][]string](order T) Condition
- func TotalRaw(total int) Condition
- func Where(where ParseWhere) Condition
- func WithCtx(ctx *context.Context) Condition
- func WithFn(getVal, isJoin bool, q *QueryCondition, ...) Condition
- type Model
- type ParseWhere
- type QueryCondition
- type QueryFn
- type RelationFn
- 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
- 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
- type Relationship
- type SqlBuilder
- type SqlxQuery
- type UniversalDb
Constants ¶
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 ¶
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 ColumnFromDB ¶
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 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 FindToStringMapFromDB ¶
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 GetFieldFromDB ¶
func GetToStringMap ¶
func GetToStringMapFromDB ¶
func GetsFromDB ¶
func GetsFromDB[T Model](db dbQuery, ctx context.Context, q *QueryCondition) (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 PaginationToMapFromDB ¶
func ParseRelation ¶
func PrimaryKey ¶
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 SimpleFind ¶
func ToMapSlice ¶
Types ¶
type AndWhere ¶
type AndWhere interface {
AndWhere(field, operator, val, fieldType string) ParseWhere
}
type Condition ¶
type Condition func(c *QueryCondition)
func Where ¶
func Where(where ParseWhere) Condition
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 RelationFn ¶
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 ¶
type UniversalDb ¶
type UniversalDb struct {
// contains filtered or unexported fields
}
func NewUniversalDb ¶
func NewUniversalDb(selects QueryFn, gets QueryFn) UniversalDb