Documentation
¶
Index ¶
- Variables
- func Batch(ormArray ...*OrmModel) error
- func BatchArray(ormArray []*OrmModel) error
- func BatchFunc(f func(tx *sqlx.Tx)) error
- func BindDB(DB *sqlx.DB) error
- func ConvertArray[T int | string](array []T) []string
- func Exec(sqlStr string) error
- func ExecRawSQL(sql string, args ...any) error
- func JsonTagToJsonbKeys(obj interface{}, prefix string, igTags ...string) string
- func JsonbBuildObjString(obj interface{}, prefix ...string) string
- func Query(query string, dest any) error
- func StructToMap(item any) (map[string]any, map[string]string)
- func ValueTypeToStr(v any) string
- type CRUDInterface
- type ConditionGroup
- func And(tag ...string) ConditionGroup
- func And2F(tag string, arg any) ConditionGroup
- func AndAuto(tag ...string) ConditionGroup
- func AndGroup(cgs ...ConditionGroup) ConditionGroup
- func Asc(tag string) ConditionGroup
- func AutoFill(zero ...bool) ConditionGroup
- func Between(tag string, min, max any) ConditionGroup
- func Desc(tag string) ConditionGroup
- func Eq(tag string, args ...any) ConditionGroup
- func Exists(sql string, args ...any) ConditionGroup
- func Exp(express string, args ...any) ConditionGroup
- func Fields(tag ...string) ConditionGroup
- func Gt(tag string, args ...any) ConditionGroup
- func Gte(tag string, args ...any) ConditionGroup
- func IN[T any](tag string, args ...T) ConditionGroup
- func JoinOn(express string) ConditionGroup
- func Like(tag ...string) ConditionGroup
- func LikeOR(tag ...string) ConditionGroup
- func Lt(tag string, args ...any) ConditionGroup
- func Lte(tag string, args ...any) ConditionGroup
- func NEq(tag string, args ...any) ConditionGroup
- func NEqLike(tag ...string) ConditionGroup
- func NEqNull(tag ...string) ConditionGroup
- func NotExists(sql string, args ...any) ConditionGroup
- func NotIN[T any](tag string, args ...T) ConditionGroup
- func Null(tag ...string) ConditionGroup
- func NullOR(tag ...string) ConditionGroup
- func Or(tag ...string) ConditionGroup
- func Or2F(tag string, args ...any) ConditionGroup
- func OrAuto(tag ...string) ConditionGroup
- func OrGroup(cgs ...ConditionGroup) ConditionGroup
- func Raw(express string, args ...any) ConditionGroup
- func SubQuery(tag, symbol, sql string, args ...any) ConditionGroup
- type ConditionType
- type Error
- type JoinTable
- func INNER_JOIN(i ORMInterface, alias string) *JoinTable
- func InnerJoin(table, alias string) *JoinTable
- func LEFT_JOIN(i ORMInterface, alias string) *JoinTable
- func LeftJoin(table, alias string) *JoinTable
- func NewJoin(joinType JoinType, table, alias string) *JoinTable
- func RIGHT_JOIN(i ORMInterface, alias string) *JoinTable
- func RightJoin(table, alias string) *JoinTable
- type JoinType
- type ORMInterface
- type OrmModel
- func (o *OrmModel) AllowEmpty(jsonTag ...string) *OrmModel
- func (o *OrmModel) Asc(jsonTag ...string) *OrmModel
- func (o *OrmModel) Avg(column string) (float64, error)
- func (o *OrmModel) BuildSQL() SQLParams
- func (o *OrmModel) Count(column string) (int64, error)
- func (o *OrmModel) Delete(i interface{}) *OrmModel
- func (o *OrmModel) Desc(jsonTag ...string) *OrmModel
- func (o *OrmModel) DoNothing() *OrmModel
- func (o *OrmModel) DoUpdate(jsonTags ...string) *OrmModel
- func (o *OrmModel) Error() error
- func (o *OrmModel) ExcludeFields(jsonTag ...string) *OrmModel
- func (o *OrmModel) Exec() error
- func (o *OrmModel) Fields(jsonTag ...string) *OrmModel
- func (o *OrmModel) FullSQL() SQLParams
- func (o *OrmModel) GroupBy(cgs ...ConditionGroup) *OrmModel
- func (o *OrmModel) Having(exp string, args ...any) *OrmModel
- func (o *OrmModel) If(ifFunc func(o *OrmModel)) *OrmModel
- func (o *OrmModel) Insert(i interface{}) *OrmModel
- func (o *OrmModel) Join(join *JoinTable) *OrmModel
- func (o *OrmModel) JsonbList(dest interface{}) error
- func (o *OrmModel) JsonbListString() (string, error)
- func (o *OrmModel) JsonbMap(dest interface{}, columns ...string) error
- func (o *OrmModel) JsonbMapString(keys ...string) (string, error)
- func (o *OrmModel) Limit(row int64) *OrmModel
- func (o *OrmModel) Log(l bool) *OrmModel
- func (o *OrmModel) Many(dest interface{}) error
- func (o *OrmModel) Max(column string) (float64, error)
- func (o *OrmModel) Min(column string) (float64, error)
- func (o *OrmModel) Offset(row int64) *OrmModel
- func (o *OrmModel) OnConflict(jsonTags ...string) *OrmModel
- func (o *OrmModel) One(dest interface{}) error
- func (o *OrmModel) RETURNING(single any, list any, jsonTag ...string) error
- func (o *OrmModel) Select(i interface{}, distinct ...bool) *OrmModel
- func (o *OrmModel) SetField(jsonTag string, arg any) *OrmModel
- func (o *OrmModel) Sum(column string) (float64, error)
- func (o *OrmModel) Update(i interface{}) *OrmModel
- func (o *OrmModel) Where(cgs ...ConditionGroup) *OrmModel
- func (o *OrmModel) WherePK() *OrmModel
- func (o *OrmModel) With(t string) *OrmModel
- func (o *OrmModel) WithAsc(fields ...string) *OrmModel
- func (o *OrmModel) WithDesc(fields ...string) *OrmModel
- type PageResult
- type SQLParams
Constants ¶
This section is empty.
Variables ¶
var ( // SqlxDB 数据库连接对象 SqlxDB *sqlx.DB // TagName 结构体 tag 名称 TagName = "db" DebugMode bool )
var ( ErrInvalidPageSize = &Error{Code: 1001, Message: "page size must be greater than zero"} ErrNilDB = &Error{Code: 1002, Message: "database connection is nil"} ErrEmptySQL = &Error{Code: 1003, Message: "SQL statement is empty"} ErrNoEffect = &Error{Code: 1004, Message: "no rows affected"} )
定义常见错误
Functions ¶
func ConvertArray ¶ added in v0.1.6
func ExecRawSQL ¶ added in v0.2.0
ExecRawSQL 执行原生 SQL
func JsonTagToJsonbKeys ¶ added in v0.3.7
func JsonbBuildObjString ¶ added in v0.2.8
func ValueTypeToStr ¶ added in v0.3.3
Types ¶
type CRUDInterface ¶
type ConditionGroup ¶
type ConditionGroup struct {
Logic string // Logic: 逻辑运算符(AND/OR)
Symbol string // Symbol: 比较符号(=, >, < 等)
JsonTags []string // JsonTags: 参与条件的字段名
Args []any // Args: 参数值
InArgs []string // InArgs: IN 查询参数
Express string // Express: 表达式
SubGroups []ConditionGroup // SubGroups: 子条件分组(用于嵌套 OR/AND)
// contains filtered or unexported fields
}
ConditionGroup 条件分组结构体,描述 SQL 查询的条件
func AndGroup ¶ added in v0.4.3
func AndGroup(cgs ...ConditionGroup) ConditionGroup
AndGroup 构造 AND 嵌套分组,如 AndGroup(Gt("a",1), Lt("b",10)) → (a>1 AND b<10)
func Between ¶ added in v0.4.3
func Between(tag string, min, max any) ConditionGroup
Between 构造 BETWEEN 范围查询条件分组,如 Between("age", 18, 60) → age BETWEEN 18 AND 60
func Exists ¶ added in v0.4.3
func Exists(sql string, args ...any) ConditionGroup
Exists 构造 EXISTS 子查询条件,sql 为完整的子查询语句
func Exp ¶
func Exp(express string, args ...any) ConditionGroup
Exp 条件表达式 {table_column_field}=:{name}
func Fields ¶ added in v0.3.39
func Fields(tag ...string) ConditionGroup
func JoinOn ¶ added in v0.3.8
func JoinOn(express string) ConditionGroup
JoinOn 创建 JOIN ON 条件 (实际上是 Raw 的别名,为了语义更清晰)
func NEqLike ¶ added in v0.3.25
func NEqLike(tag ...string) ConditionGroup
NEqLike 构造 AND LIKE != 条件分组
func NotExists ¶ added in v0.4.3
func NotExists(sql string, args ...any) ConditionGroup
NotExists 构造 NOT EXISTS 子查询条件
func NotIN ¶ added in v0.4.3
func NotIN[T any](tag string, args ...T) ConditionGroup
NotIN 构造 NOT IN 查询条件分组
func OrGroup ¶ added in v0.4.3
func OrGroup(cgs ...ConditionGroup) ConditionGroup
OrGroup 构造 OR 嵌套分组,如 OrGroup(Eq("a",1), Eq("b",2)) → (a=1 OR b=2)
func Raw ¶ added in v0.3.15
func Raw(express string, args ...any) ConditionGroup
Raw 条件表达式 column1 = 2 AND column2 = 'abc' 或 column1 = $1 AND column2 = $2
func SubQuery ¶ added in v0.4.3
func SubQuery(tag, symbol, sql string, args ...any) ConditionGroup
SubQuery 构造 WHERE 子查询条件,如 SubQuery("id", "IN", "SELECT user_id FROM orders WHERE amount > $1", 100)
func (ConditionGroup) Transform ¶
func (cg ConditionGroup) Transform() string
Transform 转换为 SQL 字符串(未实现)
type ConditionType ¶ added in v0.3.1
type ConditionType int // ConditionType 条件类型枚举
type JoinTable ¶ added in v0.3.8
type JoinTable struct {
Type JoinType // JOIN 类型
Table string // 表名
Alias string // 表别名
Conditions []ConditionGroup // JOIN 条件
SelectField []string // 需要查询的字段
}
JoinTable 连接表的结构
func INNER_JOIN ¶ added in v0.4.0
func INNER_JOIN(i ORMInterface, alias string) *JoinTable
INNER_JOIN 通过 ORMInterface 创建 INNER JOIN
func LEFT_JOIN ¶ added in v0.4.0
func LEFT_JOIN(i ORMInterface, alias string) *JoinTable
LEFT_JOIN 通过 ORMInterface 创建 LEFT JOIN
func RIGHT_JOIN ¶ added in v0.4.0
func RIGHT_JOIN(i ORMInterface, alias string) *JoinTable
RIGHT_JOIN 通过 ORMInterface 创建 RIGHT JOIN
func (*JoinTable) On ¶ added in v0.4.0
func (j *JoinTable) On(cgs ...ConditionGroup) *JoinTable
On 添加连接条件 推荐使用 mworm.Raw("t.id = a.user_id") 或 mworm.JoinOn("t.id = a.user_id")
type ORMInterface ¶
type ORMInterface interface {
TableName() string
}
ORMInterface 数据库表结构体接口,需实现 TableName 方法 CRUDInterface 数据库操作接口,需实现 CRUDMode 方法
type OrmModel ¶
type OrmModel struct {
// contains filtered or unexported fields
}
func (*OrmModel) AllowEmpty ¶ added in v0.3.12
func (*OrmModel) ExcludeFields ¶ added in v0.1.17
func (*OrmModel) GroupBy ¶ added in v0.3.39
func (o *OrmModel) GroupBy(cgs ...ConditionGroup) *OrmModel
func (*OrmModel) JsonbListString ¶
func (*OrmModel) OnConflict ¶ added in v0.4.3
OnConflict 指定 Upsert 冲突列(json tag),用于 INSERT ON CONFLICT
func (*OrmModel) Where ¶
func (o *OrmModel) Where(cgs ...ConditionGroup) *OrmModel
type PageResult ¶ added in v0.3.1
type PageResult[T any] struct { Total int `json:"total" db:"total"` //总记录数 TotalPage int `json:"totalPage" db:"total_page"` //总页数 Page int `json:"page" db:"page"` //当前页 PageSize int `json:"pageSize" db:"page_size"` //页面数量 List []T `json:"list" db:"list"` //分页数据 }
PageResult 用于分页查询结果的结构体,包含总数、总页数、当前页、每页数量和数据列表。
func DebugPAGE ¶ added in v0.3.3
func DebugPAGE[T ORMInterface](entity T, debug bool, page, pageSize int, excludeTags []string, cgs ...ConditionGroup) (PageResult[T], error)
DebugPAGE 分页查询方法,支持调试和排除指定的json tag字段
func PAGE ¶ added in v0.3.1
func PAGE[T ORMInterface](entity T, page, pageSize int, excludeTags []string, cgs ...ConditionGroup) (PageResult[T], error)
PAGE 分页查询方法,支持排除指定的json tag字段
func (PageResult[T]) CalcTotalPage ¶ added in v0.3.1
func (pr PageResult[T]) CalcTotalPage() int
CalcTotalPage 计算总页数