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) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (d *DB) SelectContext(ctx context.Context, dest any, query string, args ...any) error
- type Dao
- func (d *Dao[T]) BatchReplaceInto(models []T, opts ...InsertOption) (sql.Result, error)
- func (d *Dao[T]) BatchReplaceIntoContext(ctx context.Context, models []T, opts ...InsertOption) (sql.Result, error)
- func (d *Dao[T]) BatchSave(models []T, opts ...InsertOption) (sql.Result, error)
- func (d *Dao[T]) BatchSaveContext(ctx context.Context, models []T, opts ...InsertOption) (sql.Result, error)
- func (d *Dao[T]) DBColumns(omitColumns ...string) []string
- func (d *Dao[T]) DeleteByColumn(kv *KV) (int64, error)
- func (d *Dao[T]) DeleteByColumnContext(ctx context.Context, kv *KV) (int64, error)
- func (d *Dao[T]) DeleteByColumns(kvs *MultiKV) (int64, error)
- func (d *Dao[T]) DeleteByColumnsContext(ctx context.Context, kvs *MultiKV) (int64, error)
- func (d *Dao[T]) DeleteByID(id any) (bool, error)
- func (d *Dao[T]) DeleteByIDContext(ctx context.Context, id any) (bool, error)
- func (d *Dao[T]) Deleter() *sqlbuilder.Deleter
- func (d *Dao[T]) GetByColumn(kv *KV) (T, error)
- func (d *Dao[T]) GetByColumnContext(ctx context.Context, kv *KV) (T, error)
- func (d *Dao[T]) GetByID(id any) (T, error)
- func (d *Dao[T]) GetByIDContext(ctx context.Context, id any) (T, error)
- func (d *Dao[T]) GetColumnsByModel(model any, omitColumns ...string) []string
- func (d *Dao[T]) GetColumnsByType(typ reflect.Type, omitColumns ...string) []string
- func (d *Dao[T]) GetMasterDB() engine.Execer
- func (d *Dao[T]) GetReadDB() engine.Queryer
- func (d *Dao[T]) IgnoreInto(model T, opts ...InsertOption) (sql.Result, error)
- func (d *Dao[T]) IgnoreIntoContext(ctx context.Context, model T, opts ...InsertOption) (sql.Result, error)
- func (d *Dao[T]) Inserter(opts ...InsertOption) *sqlbuilder.Inserter
- func (d *Dao[T]) List(kv *KV) ([]T, error)
- func (d *Dao[T]) ListByColumns(kvs *MultiKV) ([]T, error)
- func (d *Dao[T]) ListByColumnsContext(ctx context.Context, kvs *MultiKV) ([]T, error)
- func (d *Dao[T]) ListByIDs(ids ...any) ([]T, error)
- func (d *Dao[T]) ListByIDsContext(ctx context.Context, ids ...any) ([]T, error)
- func (d *Dao[T]) ListContext(ctx context.Context, kv *KV) ([]T, error)
- func (d *Dao[T]) ReplaceInto(dest T, opts ...InsertOption) (sql.Result, error)
- func (d *Dao[T]) ReplaceIntoContext(ctx context.Context, model T, opts ...InsertOption) (sql.Result, error)
- func (d *Dao[T]) SQLBuilder() *sqlbuilder.Builder
- func (d *Dao[T]) Save(dest T, opts ...InsertOption) (int64, error)
- func (d *Dao[T]) SaveContext(ctx context.Context, dest T, opts ...InsertOption) (int64, error)
- func (d *Dao[T]) Selector(columns ...string) *sqlbuilder.Selector
- func (d *Dao[T]) TableName() string
- func (d *Dao[T]) Update(model T, omitColumns ...string) (bool, error)
- func (d *Dao[T]) UpdateByCond(model T, where sqlbuilder.ConditionBuilder, omitColumns ...string) (int64, error)
- func (d *Dao[T]) UpdateByCondContext(ctx context.Context, model T, where sqlbuilder.ConditionBuilder, ...) (int64, error)
- func (d *Dao[T]) UpdateContext(ctx context.Context, model T, omitColumns ...string) (bool, error)
- func (d *Dao[T]) UpdateField(idValue any, fieldMap map[string]any) (int64, error)
- func (d *Dao[T]) UpdateFieldContext(ctx context.Context, idValue any, fieldMap map[string]any) (int64, error)
- func (d *Dao[T]) Updater() *sqlbuilder.Updater
- func (d *Dao[T]) WithExecutor(executor engine.Executor) *Dao[T]
- func (d *Dao[T]) WithMaster() *Dao[T]
- func (d *Dao[T]) WithRead() *Dao[T]
- func (d *Dao[T]) WithTableName(tableName string) *Dao[T]
- 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 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) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, 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") )
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 ¶
DB 包装 sqlx.DB
func (*DB) ExecContext ¶
ExecContext 使用数组参数执行sql
func (*DB) GetContext ¶
GetContext 查询单条数据
func (*DB) NamedExecContext ¶
NamedExecContext 使用命名参数执行sql
func (*DB) QueryContext ¶
QueryContext 查询多条数据,返回 sql.Rows
type Dao ¶
Dao 数据访问对象,封装了数据库操作的基础方法
func (*Dao[T]) BatchReplaceInto ¶
func (d *Dao[T]) BatchReplaceInto(models []T, opts ...InsertOption) (sql.Result, error)
BatchReplaceInto 批量新增,使用 replace into 方式 models 是一个 slice omitColumns 不需要 insert 的字段
func (*Dao[T]) BatchReplaceIntoContext ¶
func (d *Dao[T]) BatchReplaceIntoContext(ctx context.Context, models []T, opts ...InsertOption) (sql.Result, error)
BatchReplaceIntoContext 批量新增,使用 replace into 方式,携带上下文 models 是一个 slice omitColumns 不需要 insert 的字段
func (*Dao[T]) BatchSave ¶
func (d *Dao[T]) BatchSave(models []T, opts ...InsertOption) (sql.Result, error)
BatchSave 批量新增,携带上下文 omitColumns 不需要 insert 的字段
func (*Dao[T]) BatchSaveContext ¶
func (d *Dao[T]) BatchSaveContext(ctx context.Context, models []T, opts ...InsertOption) (sql.Result, error)
BatchSaveContext 批量新增 omitColumns 不需要 insert 的字段 models 是一个批量 insert 的 slice
func (*Dao[T]) DeleteByColumn ¶
DeleteByColumn 按字段名删除
func (*Dao[T]) DeleteByColumnContext ¶
DeleteByColumnContext 按字段名删除,携带上下文
func (*Dao[T]) DeleteByColumns ¶
DeleteByColumns 指定字段删除多个值
func (*Dao[T]) DeleteByColumnsContext ¶
DeleteByColumnsContext 指定字段删除多个值,携带上下文
func (*Dao[T]) DeleteByIDContext ¶
DeleteByIDContext 根据id删除数据,携带上下文
func (*Dao[T]) Deleter ¶
func (d *Dao[T]) Deleter() *sqlbuilder.Deleter
Deleter 创建当前表的删除构建器 返回值: 删除构建器对象
func (*Dao[T]) GetByColumn ¶
GetByColumn 按指定字段查询单条数据 bool 数据是否存在
func (*Dao[T]) GetByColumnContext ¶
GetByColumnContext 按指定字段查询单条数据,携带上下文 bool 数据是否存在
func (*Dao[T]) GetByIDContext ¶
GetByIDContext 根据 id 查询单条数据,携带上下文
func (*Dao[T]) GetColumnsByModel ¶
GetColumnsByModel 根据 model 结构获取数据库字段 model: 模型结构体 omitColumns: 需要忽略的字段列表 返回值: 字段名列表
func (*Dao[T]) GetColumnsByType ¶
GetColumnsByType 通过字段 tag 解析数据库字段 typ: 结构体类型 omitColumns: 需要忽略的字段列表 返回值: 字段名列表
func (*Dao[T]) GetMasterDB ¶
GetMasterDB 返回主库连接 返回值: 主库连接对象
func (*Dao[T]) IgnoreInto ¶
func (d *Dao[T]) IgnoreInto(model T, opts ...InsertOption) (sql.Result, error)
IgnoreInto 使用 INSERT IGNORE INTO 如果记录已存在则忽略 omitColumns 不需要 insert 的字段
func (*Dao[T]) IgnoreIntoContext ¶
func (d *Dao[T]) IgnoreIntoContext(ctx context.Context, model T, opts ...InsertOption) (sql.Result, error)
IgnoreIntoContext 使用 INSERT IGNORE INTO 如果记录已存在则忽略,携带上下文 omitColumns 不需要 insert 的字段
func (*Dao[T]) Inserter ¶
func (d *Dao[T]) Inserter(opts ...InsertOption) *sqlbuilder.Inserter
Inserter 创建当前表的插入构建器 opts: 插入选项,如忽略字段等 返回值: 插入构建器对象
func (*Dao[T]) ListByColumns ¶
ListByColumns 指定字段多个值查询多条数据 dest: slice pointer
func (*Dao[T]) ListByColumnsContext ¶
ListByColumnsContext 指定字段多个值查询多条数据,携带上下文 dest: slice pointer
func (*Dao[T]) ListByIDsContext ¶
ListByIDsContext 根据 id 查询多条数据,携带上下文
func (*Dao[T]) ListContext ¶
ListContext 指定字段查询多条数据,携带上下文
func (*Dao[T]) ReplaceInto ¶
func (d *Dao[T]) ReplaceInto(dest T, opts ...InsertOption) (sql.Result, error)
ReplaceInto replace into table omitColumns 不需要 insert 的字段
func (*Dao[T]) ReplaceIntoContext ¶
func (d *Dao[T]) ReplaceIntoContext(ctx context.Context, model T, opts ...InsertOption) (sql.Result, error)
ReplaceIntoContext replace into table,携带上下文 omitColumns 不需要 insert 的字段
func (*Dao[T]) SQLBuilder ¶
func (d *Dao[T]) SQLBuilder() *sqlbuilder.Builder
SQLBuilder 创建当前表的 SQL 构建器 返回值: SQL构建器对象
func (*Dao[T]) Save ¶
func (d *Dao[T]) Save(dest T, opts ...InsertOption) (int64, error)
Save 插入数据 dest: 要插入的数据对象 opts: 插入选项 返回值: 插入ID,错误信息
func (*Dao[T]) SaveContext ¶
SaveContext 插入数据,携带上下文 ctx: 上下文 dest: 要插入的数据对象 opts: 插入选项 返回值: 插入ID,错误信息
func (*Dao[T]) Selector ¶
func (d *Dao[T]) Selector(columns ...string) *sqlbuilder.Selector
Selector 创建当前表的查询构建器 columns: 查询的字段列表,为空则查询全部字段 返回值: 查询构建器对象
func (*Dao[T]) UpdateByCond ¶
func (d *Dao[T]) UpdateByCond(model T, where sqlbuilder.ConditionBuilder, omitColumns ...string) (int64, error)
UpdateByCond 按条件更新全部字段
func (*Dao[T]) UpdateByCondContext ¶
func (d *Dao[T]) UpdateByCondContext(ctx context.Context, model T, where sqlbuilder.ConditionBuilder, omitColumns ...string) (int64, error)
UpdateByCondContext 按条件更新全部字段
func (*Dao[T]) UpdateContext ¶
UpdateContext 根据 ID 全字段更新,携带上下文
func (*Dao[T]) UpdateField ¶
UpdateField 部分字段更新
func (*Dao[T]) UpdateFieldContext ¶
func (d *Dao[T]) UpdateFieldContext(ctx context.Context, idValue any, fieldMap map[string]any) (int64, error)
UpdateFieldContext 部分字段更新,携带上下文
func (*Dao[T]) Updater ¶
func (d *Dao[T]) Updater() *sqlbuilder.Updater
Updater 创建当前表的更新构建器 返回值: 更新构建器对象
func (*Dao[T]) WithExecutor ¶
WithExecutor 使用新的执行器创建 Dao
func (*Dao[T]) WithTableName ¶
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 ¶
func DisableGlobalInsertOmits(disable bool) InsertOption
DisableGlobalInsertOmits insert 数据时,禁用全局忽略字段
func WithInsertOmits ¶
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 ¶
type LogHook struct {
Print engine.AfterHandler
}
LogHook 打印日志中间件
func (LogHook) After ¶
func (l LogHook) After(ctx context.Context, ec *engine.ExecutorContext, er *engine.ExecutorResult)
After 执行后
type Meta ¶
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 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 (TableMeta) OmitColumns ¶
OmitColumns 获取排除指定字段后的字段列表 omit: 需要排除的字段列表 返回值: 排除指定字段后的字段列表
func (TableMeta) WithTableName ¶
WithTableName 设置表名,一般用在分表的场景,设置实际物理表名 tableName: 新的表名
type Tx ¶
func (*Tx) ExecContext ¶
ExecContext 使用数组参数执行sql
func (*Tx) GetContext ¶
GetContext 查询单条数据
func (*Tx) NamedExecContext ¶
NamedExecContext 使用命名参数执行sql
func (*Tx) QueryContext ¶
QueryContext 查询多条数据,返回 sql.Rows