Documentation ¶
Index ¶
- Constants
- Variables
- func Delete(ctx context.Context, dbx *sqlx.DB, record DeleteRecord) (int64, error)
- func Get[T any](ctx context.Context, dbx *sqlx.DB, record GetRecord, opts ...SelectOption) (*T, error)
- func GetMap(ctx context.Context, dbx *sqlx.DB, record GetRecord, opts ...SelectOption) (map[string]any, error)
- func Insert(ctx context.Context, dbx *sqlx.DB, record InsertRecord, opts ...InsertOption) (int64, error)
- func Update(ctx context.Context, dbx *sqlx.DB, record UpdateRecord, opts ...UpdateOption) (int64, error)
- func UseDefaultMasterDB(master *sqlx.DB)
- func UseDefaultReadDB(read *sqlx.DB)
- type Condition
- type ConditionType
- type DB
- type Dao
- func (dao *Dao) BatchReplaceInto(models interface{}, omitColumns ...string) (int64, error)
- func (dao *Dao) BatchSave(models interface{}, omitColumns ...string) (int64, error)
- func (dao *Dao) DBColumns(omitColumns ...string) []string
- func (dao *Dao) DeleteByColumn(kv *KV) (int64, error)
- func (dao *Dao) DeleteByColumnTx(tx *sqlx.Tx, kv *KV) (int64, error)
- func (dao *Dao) DeleteByColumns(kvs *MultiKV) (int64, error)
- func (dao *Dao) DeleteByColumnsTx(tx *sqlx.Tx, kvs *MultiKV) (int64, error)
- func (dao *Dao) DeleteByCond(where sqlbuilder.ConditionBuilder) (int64, error)
- func (dao *Dao) DeleteByCondTx(tx *sqlx.Tx, where sqlbuilder.ConditionBuilder) (int64, error)
- func (dao *Dao) DeleteByID(id interface{}) (bool, error)
- func (dao *Dao) DeleteByIDTx(tx *sqlx.Tx, id interface{}) (bool, error)
- func (dao *Dao) Get(dest interface{}, selector *sqlbuilder.Selector) (bool, error)
- func (dao *Dao) GetByColumn(kv *KV, dest Model) (bool, error)
- func (dao *Dao) GetByColumnTx(tx *sqlx.Tx, kv *KV, dest Model) (bool, error)
- func (dao *Dao) GetByID(id interface{}, dest Model) (bool, error)
- func (dao *Dao) GetColumnsByModel(model interface{}, omitColumns ...string) []string
- func (dao *Dao) GetColumnsByType(typ reflect.Type, omitColumns ...string) []string
- func (dao *Dao) GetMasterDB() *DB
- func (dao *Dao) GetReadDB() *DB
- func (dao *Dao) GetTx(tx *sqlx.Tx, dest interface{}, selector *sqlbuilder.Selector) (bool, error)
- func (dao *Dao) List(kv *KV, dest interface{}) error
- func (dao *Dao) ListByColumns(kvs *MultiKV, dest interface{}) error
- func (dao *Dao) ListByColumnsTx(tx *sqlx.Tx, kvs *MultiKV, dest interface{}) error
- func (dao *Dao) ListByIDs(dest interface{}, ids ...interface{}) error
- func (dao *Dao) ListByIdsTx(tx *sqlx.Tx, dest interface{}, ids ...interface{}) error
- func (dao *Dao) ListTx(tx *sqlx.Tx, kv *KV, dest interface{}) error
- func (dao *Dao) ReplaceInto(dest Model, omitColumns ...string) (int64, error)
- func (dao *Dao) SQLBuilder() *sqlbuilder.Builder
- func (dao *Dao) Save(dest Model, omitColumns ...string) (int64, error)
- func (dao *Dao) SaveTx(tx *sqlx.Tx, dest Model, omitColumns ...string) (int64, error)
- func (dao *Dao) Select(dest interface{}, selector *sqlbuilder.Selector) error
- func (dao *Dao) SelectTx(tx *sqlx.Tx, dest interface{}, selector *sqlbuilder.Selector) error
- func (dao *Dao) Selector(columns ...string) *sqlbuilder.Selector
- func (dao *Dao) TableName() string
- func (dao *Dao) Update(m Model, omitColumns ...string) (bool, error)
- func (dao *Dao) UpdateByCond(attr map[string]interface{}, where sqlbuilder.ConditionBuilder) (int64, error)
- func (dao *Dao) UpdateByCondTx(tx *sqlx.Tx, attr map[string]interface{}, where sqlbuilder.ConditionBuilder) (int64, error)
- func (dao *Dao) UpdateField(idValue interface{}, fieldMap map[string]interface{}) (bool, error)
- func (dao *Dao) UpdateFieldTx(tx *sqlx.Tx, idValue interface{}, fieldMap map[string]interface{}) (bool, error)
- func (dao *Dao) UpdateTx(tx *sqlx.Tx, m Model, omitColumns ...string) (bool, error)
- func (dao *Dao) With(master, read *sqlx.DB, opts ...Option) *Dao
- type DataWrapper
- type DeleteRecord
- type FieldsFilter
- type GetRecord
- type InsertOption
- type InsertOptions
- type InsertRecord
- type KV
- type Model
- type MultiKV
- type Op
- type Option
- type OrderField
- type OrderType
- type Page
- type QueryRecord
- type SelectOption
- type SelectOptions
- type TableMeta
- type UpdateOption
- type UpdateOptions
- type UpdateRecord
Constants ¶
const ( OpAnd Op = "and" OpOr Op = "or" ConditionTypeEq ConditionType = "eq" // 等于 ConditionTypeNotEq ConditionType = "not_eq" // 不等于 ConditionTypeLike ConditionType = "like" // 模糊匹配 ConditionTypeNotLike ConditionType = "not_like" // 不包含 ConditionTypeIn ConditionType = "in" // in ConditionTypeNotIn ConditionType = "not_in" // not in ConditionTypeGt ConditionType = "gt" // 大于 ConditionTypeLt ConditionType = "lt" // 小于 ConditionTypeGte ConditionType = "gte" // 大于等于 ConditionTypeLte ConditionType = "lte" // 小于等于 OrderTypeAsc OrderType = "asc" // 升序 OrderTypeDesc OrderType = "desc" // 降序 )
Variables ¶
var ( ErrUpdatePrimaryKeyRequire = errors.New("[daox] Primary key require for update") ErrTxNil = errors.New("[daox] Tx is nil") )
Functions ¶
func Get ¶
func Get[T any](ctx context.Context, dbx *sqlx.DB, record GetRecord, opts ...SelectOption) (*T, error)
Get 查询单条记录
func GetMap ¶
func GetMap(ctx context.Context, dbx *sqlx.DB, record GetRecord, opts ...SelectOption) (map[string]any, error)
GetMap 查询单条记录,返回 map
func Insert ¶
func Insert(ctx context.Context, dbx *sqlx.DB, record InsertRecord, opts ...InsertOption) (int64, error)
Insert 通用 insert 操作
func Update ¶
func Update(ctx context.Context, dbx *sqlx.DB, record UpdateRecord, opts ...UpdateOption) (int64, error)
Update 通用 update 操作
func UseDefaultMasterDB ¶
func UseDefaultReadDB ¶
Types ¶
type Condition ¶
type Condition struct { Disable bool `json:"disable"` // true 禁用该条件 Op Op `json:"op"` // and or 连接符 Field string `json:"field"` // 查询条件字段 Vals []any `json:"vals"` // 查询字段值 ConditionType ConditionType `json:"condition_type"` // 查找类型 }
Condition 条件语句
type ConditionType ¶
type ConditionType string
type Dao ¶
type Dao struct { ReadDB *DB Mapper *reflectx.Mapper TableMeta TableMeta // contains filtered or unexported fields }
func CreateDAO ¶
CreateDAO 函数用于创建一个新的Dao对象 tableName 参数表示表名 primaryKey 参数表示主键 structType 参数表示数据结构类型 opts 参数表示可选的选项 返回值为创建的Dao对象指针
func NewDAO ¶
func NewDAO(master *sqlx.DB, tableName string, primaryKey string, structType reflect.Type, opts ...Option) *Dao
NewDAO 函数用于创建一个新的Dao对象 master 参数用于连接数据库 tableName 参数表示表名 primaryKey 参数表示主键 structType 参数表示数据结构类型 opts 参数表示可选的选项 返回值为创建的Dao对象指针
func (*Dao) BatchReplaceInto ¶
BatchReplaceInto 批量新增,使用 replace into 方式 omitColumns 不需要 insert 的字段
func (*Dao) DeleteByColumn ¶
DeleteByColumn 按字段名删除
func (*Dao) DeleteByColumnTx ¶
DeleteByColumnTx 按字段名删除,支持事务
func (*Dao) DeleteByColumns ¶
DeleteByColumns 指定字段删除多个值
func (*Dao) DeleteByColumnsTx ¶
DeleteByColumnsTx 指定字段多个值删除
func (*Dao) DeleteByCond ¶
func (dao *Dao) DeleteByCond(where sqlbuilder.ConditionBuilder) (int64, error)
DeleteByCond 根据where条件删除
func (*Dao) DeleteByCondTx ¶
func (dao *Dao) DeleteByCondTx(tx *sqlx.Tx, where sqlbuilder.ConditionBuilder) (int64, error)
DeleteByCondTx 根据where条件删除,支持事务
func (*Dao) DeleteByID ¶
DeleteByID 根据id删除数据
func (*Dao) DeleteByIDTx ¶
DeleteByIDTx 根据id删除数据,支持事务
func (*Dao) Get ¶
func (dao *Dao) Get(dest interface{}, selector *sqlbuilder.Selector) (bool, error)
Get 根据查询条件查询单条记录 dest 必须是一个指针
func (*Dao) GetByColumn ¶
GetByColumn 按指定字段查询单条数据 bool 数据是否存在
func (*Dao) GetByColumnTx ¶
GetByColumnTx 按指定字段查询单条数据,支持事务 bool 数据是否存在
func (*Dao) GetColumnsByModel ¶
GetColumnsByModel 根据 model 结构获取数据库字段 omitColumns 表示需要忽略的字段
func (*Dao) GetColumnsByType ¶
GetColumnsByType 通过字段 tag 解析数据库字段
func (*Dao) GetMasterDB ¶
func (*Dao) ListByColumns ¶
ListByColumns 指定字段多个值查询多条数据 dest: slice pointer
func (*Dao) ListByColumnsTx ¶
ListByColumnsTx 指定字段多个值查询多条数据,支持事务
func (*Dao) ListByIdsTx ¶
ListByIdsTx 查询多个id值
func (*Dao) ReplaceInto ¶
ReplaceInto replace into table omitColumns 不需要 insert 的字段
func (*Dao) SQLBuilder ¶
func (dao *Dao) SQLBuilder() *sqlbuilder.Builder
SQLBuilder 创建当前表的 sqlbuilder
func (*Dao) Select ¶
func (dao *Dao) Select(dest interface{}, selector *sqlbuilder.Selector) error
Select 根据查询条件查询列表 dest 必须是一个 slice 指针
func (*Dao) Selector ¶
func (dao *Dao) Selector(columns ...string) *sqlbuilder.Selector
Selector 创建当前表的 selector columns 是查询指定字段,为空则是全部字段
func (*Dao) UpdateByCond ¶
func (dao *Dao) UpdateByCond(attr map[string]interface{}, where sqlbuilder.ConditionBuilder) (int64, error)
UpdateByCond 根据条件更新字段 attr 字段更新值
func (*Dao) UpdateByCondTx ¶
func (dao *Dao) UpdateByCondTx(tx *sqlx.Tx, attr map[string]interface{}, where sqlbuilder.ConditionBuilder) (int64, error)
UpdateByCondTx 根据条件更新字段,支持事务
func (*Dao) UpdateField ¶
UpdateField 部分字段更新
func (*Dao) UpdateFieldTx ¶
func (dao *Dao) UpdateFieldTx(tx *sqlx.Tx, idValue interface{}, fieldMap map[string]interface{}) (bool, error)
UpdateFieldTx 部分字段更新,支持事务
type DeleteRecord ¶
type DeleteRecord struct { TableName string `json:"table_name"` // 表名 Conditions []Condition `json:"conditions"` // 条件字段 }
DeleteRecord 删除记录
type FieldsFilter ¶
type GetRecord ¶
type GetRecord struct { TableName string `json:"table_name"` // 查询表 Fields []string `json:"fields"` // 投影字段 Conditions []Condition `json:"conditions,omitempty"` // 查找字段 }
GetRecord 单条记录查询
type InsertOption ¶
type InsertOption func(*InsertOptions)
func WithInsertDataWrapper ¶
func WithInsertDataWrapper(dataWrapper DataWrapper[map[string]any, map[string]any]) InsertOption
WithInsertDataWrapper 数据转换
func WithInsertFieldsFilter ¶
func WithInsertFieldsFilter(fieldsFilter FieldsFilter) InsertOption
WithInsertFieldsFilter 过滤 insert 字段
type InsertOptions ¶
type InsertOptions struct { DataWrapper DataWrapper[map[string]any, map[string]any] FieldsFilter FieldsFilter }
type InsertRecord ¶
type InsertRecord struct { TableName string `json:"table_name"` // 表名 Row map[string]any `json:"row"` // 行数据 }
InsertRecord 插入记录
type OrderField ¶
OrderField 排序字段
type Page ¶
type Page struct { Offset int64 `json:"offset"` // 游标起始位置 Limit int64 `json:"limit"` // 每页记录数 HasNext bool `json:"has_next"` // 是否有下一页 Count int64 `json:"count"` // 总记录数 QueryCount bool `json:"query_count"` // 是否查询总数 }
Page 分页参数
func Find ¶
func Find[T any](ctx context.Context, dbx *sqlx.DB, query QueryRecord, opts ...SelectOption) (list []T, page *Page, err error)
Find 通用查询封装
func FindListMap ¶
func FindListMap(ctx context.Context, dbx *sqlx.DB, query QueryRecord, opts ...SelectOption) (list []map[string]any, page *Page, err error)
FindListMap 通用查询封装,返回 map 类型
type QueryRecord ¶
type QueryRecord struct { TableName string `json:"table_name"` // 查询表 Fields []string `json:"fields"` // 投影字段 Conditions []Condition `json:"conditions,omitempty"` // 查找字段 OrderFields []OrderField `json:"order_fields,omitempty"` // 排序字段 Page *Page `json:"page,omitempty"` // 分页参数 }
QueryRecord 查询参数
func (QueryRecord) ToCountSQLArgs ¶
func (q QueryRecord) ToCountSQLArgs() (sql string, args []any, err error)
ToCountSQLArgs 返回 count 查询 sql 语句和参数
type SelectOption ¶
type SelectOption func(*SelectOptions)
func WithSelectDataWrapper ¶
func WithSelectDataWrapper(resultWrapper DataWrapper[any, any]) SelectOption
WithSelectDataWrapper 返回结果转换
func WithSelectFieldsFilter ¶
func WithSelectFieldsFilter(fieldsFilter FieldsFilter) SelectOption
WithSelectFieldsFilter 过滤 select 字段
type SelectOptions ¶
type SelectOptions struct { FieldsFilter FieldsFilter ResultWrapper DataWrapper[any, any] }
type TableMeta ¶
type TableMeta struct { TableName string StructType reflect.Type Columns []string PrimaryKey string IsAutoIncrement bool }
func (*TableMeta) OmitColumns ¶
OmitColumns 数据库表字段 omit 包含的字段
type UpdateOption ¶
type UpdateOption func(*UpdateOptions)
func WithUpdateDataWrapper ¶
func WithUpdateDataWrapper(dataWrapper DataWrapper[map[string]any, map[string]any]) UpdateOption
WithUpdateDataWrapper 数据转换
func WithUpdateFieldsFilter ¶
func WithUpdateFieldsFilter(fieldsFilter FieldsFilter) UpdateOption
WithUpdateFieldsFilter 过滤 update 字段
type UpdateOptions ¶
type UpdateOptions struct { DataWrapper DataWrapper[map[string]any, map[string]any] FieldsFilter FieldsFilter }