Documentation
¶
Index ¶
- Constants
- Variables
- func Delete(ctx context.Context, execer engine.Execer, record DeleteRecord) (int64, error)
- func Get[T any](ctx context.Context, dbx *sqlx.DB, record GetRecord) (*T, error)
- func GetMap(ctx context.Context, dbx *sqlx.DB, record GetRecord) (map[string]any, error)
- func Insert(ctx context.Context, execer engine.Execer, record InsertRecord, ...) (int64, error)
- func PrintSQL(p engine.AfterHandler)
- func Update(ctx context.Context, execer engine.Execer, record UpdateRecord) (int64, error)
- func UseDefaultMasterDB(master *sqlx.DB)
- func UseDefaultReadDB(read *sqlx.DB)
- func UseHooks(hooks ...engine.Hook)
- func UseOmits(omits ...string)
- type Condition
- type ConditionType
- type DB
- func (d *DB) Beginx() (*Tx, error)
- func (d *DB) ExecContext(ctx context.Context, execSQL string, args ...any) (sql.Result, error)
- func (d *DB) GetContext(ctx context.Context, dest any, query string, args ...any) error
- func (d *DB) NamedExecContext(ctx context.Context, execSQL string, arg any) (sql.Result, error)
- func (d *DB) SelectContext(ctx context.Context, dest any, query string, args ...any) error
- type Dao
- func (d *Dao) BatchReplaceInto(models any, opts ...InsertOption) (sql.Result, error)
- func (d *Dao) BatchReplaceIntoContext(ctx context.Context, models any, opts ...InsertOption) (sql.Result, error)
- func (d *Dao) BatchSave(models any, opts ...InsertOption) (sql.Result, error)
- func (d *Dao) BatchSaveContext(ctx context.Context, models any, opts ...InsertOption) (sql.Result, error)
- func (d *Dao) DBColumns(omitColumns ...string) []string
- func (d *Dao) DeleteByColumn(kv *KV) (int64, error)
- func (d *Dao) DeleteByColumnContext(ctx context.Context, kv *KV) (int64, error)
- func (d *Dao) DeleteByColumns(kvs *MultiKV) (int64, error)
- func (d *Dao) DeleteByColumnsContext(ctx context.Context, kvs *MultiKV) (int64, error)
- func (d *Dao) DeleteByID(id any) (bool, error)
- func (d *Dao) DeleteByIDContext(ctx context.Context, id any) (bool, error)
- func (d *Dao) Deleter() *sqlbuilder.Deleter
- func (d *Dao) GetByColumn(kv *KV, dest Model) (bool, error)
- func (d *Dao) GetByColumnContext(ctx context.Context, kv *KV, dest Model) (bool, error)
- func (d *Dao) GetByID(id any, dest Model) (bool, error)
- func (d *Dao) GetByIDContext(ctx context.Context, id any, dest Model) (bool, error)
- func (d *Dao) GetColumnsByModel(model any, omitColumns ...string) []string
- func (d *Dao) GetColumnsByType(typ reflect.Type, omitColumns ...string) []string
- func (d *Dao) GetMasterDB() *DB
- func (d *Dao) GetReadDB() *DB
- func (d *Dao) IgnoreInto(model Model, opts ...InsertOption) (sql.Result, error)
- func (d *Dao) IgnoreIntoContext(ctx context.Context, model Model, opts ...InsertOption) (sql.Result, error)
- func (d *Dao) Inserter(opts ...InsertOption) *sqlbuilder.Inserter
- func (d *Dao) List(kv *KV, dest any) error
- func (d *Dao) ListByColumns(kvs *MultiKV, dest any) error
- func (d *Dao) ListByColumnsContext(ctx context.Context, kvs *MultiKV, dest any) error
- func (d *Dao) ListByIDs(dest any, ids ...any) error
- func (d *Dao) ListByIDsContext(ctx context.Context, dest any, ids ...any) error
- func (d *Dao) ListContext(ctx context.Context, kv *KV, dest any) error
- func (d *Dao) ReplaceInto(dest Model, opts ...InsertOption) (sql.Result, error)
- func (d *Dao) ReplaceIntoContext(ctx context.Context, model Model, opts ...InsertOption) (sql.Result, error)
- func (d *Dao) SQLBuilder() *sqlbuilder.Builder
- func (d *Dao) Save(dest Model, opts ...InsertOption) (int64, error)
- func (d *Dao) SaveContext(ctx context.Context, dest Model, opts ...InsertOption) (int64, error)
- func (d *Dao) Selector(columns ...string) *sqlbuilder.Selector
- func (d *Dao) TableName() string
- func (d *Dao) Update(m Model, omitColumns ...string) (bool, error)
- func (d *Dao) UpdateByCond(model Model, where sqlbuilder.ConditionBuilder, omitColumns ...string) (bool, error)
- func (d *Dao) UpdateByCondContext(ctx context.Context, model Model, where sqlbuilder.ConditionBuilder, ...) (bool, error)
- func (d *Dao) UpdateContext(ctx context.Context, model Model, omitColumns ...string) (bool, error)
- func (d *Dao) UpdateField(idValue any, fieldMap map[string]any) (bool, error)
- func (d *Dao) UpdateFieldContext(ctx context.Context, idValue any, fieldMap map[string]any) (bool, error)
- func (d *Dao) Updater() *sqlbuilder.Updater
- func (d *Dao) With(master, read *sqlx.DB) *Dao
- func (d *Dao) WithExecutor(executor engine.Executor) *Dao
- func (d *Dao) WithTableName(tableName string) *Dao
- type DeleteRecord
- type GetRecord
- type InsertOption
- type InsertOptions
- type InsertRecord
- type KV
- type LogHook
- type Meta
- type Model
- type MultiKV
- type Op
- type Option
- func IsAutoIncrement() Option
- func WithDBMaster(master *sqlx.DB) Option
- func WithDBRead(read *sqlx.DB) Option
- func WithHooks(hooks ...engine.Hook) Option
- func WithIfNullVal(col string, val string) Option
- func WithIfNullVals(vals map[string]string) Option
- func WithMapper(mapper *reflectx.Mapper) Option
- func WithOmitColumns(omitColumns ...string) Option
- func WithPrintSQL(printSQL engine.AfterHandler) Option
- func WithTableName(tableName string) Option
- type Options
- type OrderField
- type OrderType
- type Page
- type QueryRecord
- type TableMeta
- type Tx
- func (t *Tx) ExecContext(ctx context.Context, execSQL string, args ...any) (sql.Result, error)
- func (t *Tx) GetContext(ctx context.Context, dest any, query string, args ...any) error
- func (t *Tx) NamedExecContext(ctx context.Context, execSQL string, arg any) (sql.Result, error)
- func (t *Tx) SelectContext(ctx context.Context, dest any, query string, args ...any) error
- type TxFun
- type TxManager
- 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 更新操作必须提供主键值 ErrUpdatePrimaryKeyRequire = errors.New("[daox] Primary key require for update") // ErrTxNil 事务对象为空 ErrTxNil = errors.New("[daox] Tx is nil") )
Functions ¶
func Insert ¶
func Insert(ctx context.Context, execer engine.Execer, record InsertRecord, opts ...InsertOption) (int64, error)
Insert 通用 insert 操作
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 DB ¶ added in v1.1.0
DB 包装 sqlx.DB
func (*DB) ExecContext ¶ added in v1.1.0
ExecContext 使用数组参数执行sql
func (*DB) GetContext ¶ added in v1.1.0
GetContext 查询单条数据
func (*DB) NamedExecContext ¶ added in v1.1.0
NamedExecContext 使用命名参数执行sql
type Dao ¶
type Dao struct { TableMeta *TableMeta // 表元数据 // contains filtered or unexported fields }
Dao 数据访问对象,封装了数据库操作的基础方法
func NewDao ¶ added in v1.1.0
NewDao 创建一个新的 dao 对象 tableName: 表名 primaryKey: 主键字段名 opts: 可选配置项,如自增主键、字段映射等 返回值: 创建的Dao对象指针
func NewDaoByMeta ¶ added in v1.1.0
NewDaoByMeta 根据 meta 接口创建 dao 对象 m: 表元数据接口 opts: 可选配置项 返回值: 创建的Dao对象指针
func (*Dao) BatchReplaceInto ¶
BatchReplaceInto 批量新增,使用 replace into 方式 models 是一个 slice omitColumns 不需要 insert 的字段
func (*Dao) BatchReplaceIntoContext ¶
func (d *Dao) BatchReplaceIntoContext(ctx context.Context, models any, opts ...InsertOption) (sql.Result, error)
BatchReplaceIntoContext 批量新增,使用 replace into 方式,携带上下文 models 是一个 slice omitColumns 不需要 insert 的字段
func (*Dao) BatchSaveContext ¶
func (d *Dao) BatchSaveContext(ctx context.Context, models any, opts ...InsertOption) (sql.Result, error)
BatchSaveContext 批量新增 omitColumns 不需要 insert 的字段 models 是一个批量 insert 的 slice
func (*Dao) DeleteByColumn ¶
DeleteByColumn 按字段名删除
func (*Dao) DeleteByColumnContext ¶
DeleteByColumnContext 按字段名删除,携带上下文
func (*Dao) DeleteByColumns ¶
DeleteByColumns 指定字段删除多个值
func (*Dao) DeleteByColumnsContext ¶
DeleteByColumnsContext 指定字段删除多个值,携带上下文
func (*Dao) DeleteByIDContext ¶
DeleteByIDContext 根据id删除数据,携带上下文
func (*Dao) Deleter ¶ added in v1.1.0
func (d *Dao) Deleter() *sqlbuilder.Deleter
Deleter 创建当前表的删除构建器 返回值: 删除构建器对象
func (*Dao) GetByColumn ¶
GetByColumn 按指定字段查询单条数据 bool 数据是否存在
func (*Dao) GetByColumnContext ¶
GetByColumnContext 按指定字段查询单条数据,携带上下文 bool 数据是否存在
func (*Dao) GetByIDContext ¶
GetByIDContext 根据 id 查询单条数据,携带上下文
func (*Dao) GetColumnsByModel ¶
GetColumnsByModel 根据 model 结构获取数据库字段 model: 模型结构体 omitColumns: 需要忽略的字段列表 返回值: 字段名列表
func (*Dao) GetColumnsByType ¶
GetColumnsByType 通过字段 tag 解析数据库字段 typ: 结构体类型 omitColumns: 需要忽略的字段列表 返回值: 字段名列表
func (*Dao) IgnoreInto ¶
IgnoreInto 使用 INSERT IGNORE INTO 如果记录已存在则忽略 omitColumns 不需要 insert 的字段
func (*Dao) IgnoreIntoContext ¶
func (d *Dao) IgnoreIntoContext(ctx context.Context, model Model, opts ...InsertOption) (sql.Result, error)
IgnoreIntoContext 使用 INSERT IGNORE INTO 如果记录已存在则忽略,携带上下文 omitColumns 不需要 insert 的字段
func (*Dao) Inserter ¶ added in v1.1.0
func (d *Dao) Inserter(opts ...InsertOption) *sqlbuilder.Inserter
Inserter 创建当前表的插入构建器 opts: 插入选项,如忽略字段等 返回值: 插入构建器对象
func (*Dao) ListByColumns ¶
ListByColumns 指定字段多个值查询多条数据 dest: slice pointer
func (*Dao) ListByColumnsContext ¶
ListByColumnsContext 指定字段多个值查询多条数据,携带上下文 dest: slice pointer
func (*Dao) ListByIDsContext ¶
ListByIDsContext 根据 id 查询多条数据,携带上下文
func (*Dao) ListContext ¶
ListContext 指定字段查询多条数据,携带上下文
func (*Dao) ReplaceInto ¶
ReplaceInto replace into table omitColumns 不需要 insert 的字段
func (*Dao) ReplaceIntoContext ¶
func (d *Dao) ReplaceIntoContext(ctx context.Context, model Model, opts ...InsertOption) (sql.Result, error)
ReplaceIntoContext replace into table,携带上下文 omitColumns 不需要 insert 的字段
func (*Dao) SQLBuilder ¶
func (d *Dao) SQLBuilder() *sqlbuilder.Builder
SQLBuilder 创建当前表的 SQL 构建器 返回值: SQL构建器对象
func (*Dao) Save ¶
func (d *Dao) Save(dest Model, opts ...InsertOption) (int64, error)
Save 插入数据 dest: 要插入的数据对象 opts: 插入选项 返回值: 插入ID,错误信息
func (*Dao) SaveContext ¶
SaveContext 插入数据,携带上下文 ctx: 上下文 dest: 要插入的数据对象 opts: 插入选项 返回值: 插入ID,错误信息
func (*Dao) Selector ¶
func (d *Dao) Selector(columns ...string) *sqlbuilder.Selector
Selector 创建当前表的查询构建器 columns: 查询的字段列表,为空则查询全部字段 返回值: 查询构建器对象
func (*Dao) UpdateByCond ¶
func (d *Dao) UpdateByCond(model Model, where sqlbuilder.ConditionBuilder, omitColumns ...string) (bool, error)
UpdateByCond 按条件更新全部字段
func (*Dao) UpdateByCondContext ¶
func (d *Dao) UpdateByCondContext(ctx context.Context, model Model, where sqlbuilder.ConditionBuilder, omitColumns ...string) (bool, error)
UpdateByCondContext 按条件更新全部字段
func (*Dao) UpdateContext ¶
UpdateContext 全字段更新,携带上下文
func (*Dao) UpdateField ¶
UpdateField 部分字段更新
func (*Dao) UpdateFieldContext ¶
func (d *Dao) UpdateFieldContext(ctx context.Context, idValue any, fieldMap map[string]any) (bool, error)
UpdateFieldContext 部分字段更新,携带上下文
func (*Dao) Updater ¶ added in v1.1.0
func (d *Dao) Updater() *sqlbuilder.Updater
Updater 创建当前表的更新构建器 返回值: 更新构建器对象
func (*Dao) WithExecutor ¶ added in v1.1.0
func (*Dao) WithTableName ¶ added in v1.1.0
WithTableName 使用新的数据库连接创建 Dao
type DeleteRecord ¶
type DeleteRecord struct { TableName string `json:"table_name"` // 表名 Conditions []Condition `json:"conditions"` // 条件字段 }
DeleteRecord 删除记录
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 DisableGlobalInsertOmits ¶ added in v1.1.0
func DisableGlobalInsertOmits(disable bool) InsertOption
DisableGlobalInsertOmits insert 数据时,禁用全局忽略字段
func WithInsertOmits ¶ added in v1.1.0
func WithInsertOmits(omits ...string) InsertOption
WithInsertOmits 当前 insert 时,忽略的字段
type InsertOptions ¶
type InsertOptions struct {
// contains filtered or unexported fields
}
InsertOptions insert 选项
type InsertRecord ¶
type InsertRecord struct { TableName string `json:"table_name"` // 表名 Row map[string]any `json:"row"` // 行数据 }
InsertRecord 插入记录
type LogHook ¶ added in v1.1.0
type LogHook struct {
Print engine.AfterHandler
}
LogHook 打印日志中间件
func NewLogHook ¶ added in v1.1.0
func NewLogHook(p engine.AfterHandler) *LogHook
NewLogHook 创建打印日志中间件
func (LogHook) After ¶ added in v1.1.0
func (l LogHook) After(ctx context.Context, ec *engine.ExecutorContext, er *engine.ExecutorResult)
After 执行后
type Meta ¶ added in v1.1.0
type Meta interface { // TableName 获取表名 TableName() string // PrimaryKey 获取主键字段名 PrimaryKey() string // IsAutoIncrement 判断主键是否自增 IsAutoIncrement() bool // Columns 获取表的所有字段 Columns() []string }
Meta 数据库表元信息定义接口,用于自定义表元信息的获取方式
type Model ¶
type Model interface { // GetID 获取模型的主键值 GetID() any }
Model 数据库模型接口,所有数据库模型结构体都需要实现此接口
type MultiKV ¶
MultiKV 表示一个字段对应多个值的结构,用于 IN 查询等场景
type Option ¶
type Option func(*Options)
func WithIfNullVal ¶ added in v1.1.0
WithIfNullVal 设置字段为null时的默认值
func WithIfNullVals ¶ added in v1.1.0
WithIfNullVals 设置字段(多个)为null时的默认值
func WithOmitColumns ¶ added in v1.1.0
WithOmitColumns 设置忽略字段
func WithPrintSQL ¶ added in v1.1.0
func WithPrintSQL(printSQL engine.AfterHandler) Option
WithPrintSQL 打印 sql 回调
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 分页参数
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 TableMeta ¶
type TableMeta struct { TableName string // 表名 Columns []string // 表字段列表 PrimaryKey string // 主键字段名 IsAutoIncrement bool // 主键是否自增 }
TableMeta 数据库表元信息,包含表的基本信息和字段定义
func GetMetaInfo ¶ added in v1.1.0
GetMetaInfo 根据表名获得元信息
func (TableMeta) OmitColumns ¶
OmitColumns 获取排除指定字段后的字段列表 omit: 需要排除的字段列表 返回值: 排除指定字段后的字段列表
func (TableMeta) WithTableName ¶ added in v1.1.0
WithTableName 设置表名,一般用在分表的场景,设置实际物理表名 tableName: 新的表名
type Tx ¶ added in v1.1.0
func (*Tx) ExecContext ¶ added in v1.1.0
ExecContext 使用数组参数执行sql
func (*Tx) GetContext ¶ added in v1.1.0
GetContext 查询单条数据
func (*Tx) NamedExecContext ¶ added in v1.1.0
NamedExecContext 使用命名参数执行sql