文章目录
支持能力
ORM框架支持基于Builder模式构建SQL查询,利用reflect与unsafe技术处理结果集,同时具备元数据维护、中间件集成、链式调用、事务管理和分库分表查询能力
sql builder
.
|____common
| |____sentence_builder.go // 公共Builder 存放Core和strings.Builder
|____executor
| |____inserter
| | |____inserter.go
| | |____updater.go // insert冲突部分处理 on duplicate key update
|____querior
| |____selector
| | |____order_by
| | |____selector.go
| |____sharding_selector // 分库分表selector
|____raw_query // 万金油 支持传入原始SQL
|____types.go
executer
onduplicateKey
type OnDuplicateKeyBuilder[T any] struct {
i *Inserter[T]
conflictColumns []predicate.Column
}
func (o *OnDuplicateKeyBuilder[T]) Update(assigns ...predicate.Assignable) *Inserter[T] {
o.i.OnDuplicateKey = &dialect.OnDuplicateKey{
Assigns: assigns,
ConflictColumns: o.conflictColumns,
}
return o.i
}
哇塞用法
insertor => duplicateBuilder => insertor
NewInserter[data.User](db).GetOnDuplicateKeyBuilder().Update(predicate.Assign("Email", "更新重复1357"))
predicate
.
|____aggregate.go // 聚合函数,如SUM、AVG
|____assignment.go // 赋值语句 用于on duplicate key update [assignment]
|____column.go // 列
|____join.go // join语句
|____predicate.go // predicate语句,用在select * from tableNane where []predicate
|____raw_expr.go // 原始sql
|____subQuery.go // 子查询语句
|____table.go // select * from tableReference(包括:table、join、subQuery)
|____valuer.go // 值 column = [valuer]
Expression
// Expression 标记 表示 语句 或 语句的部分
type Expression interface {
expr()
}
var _ Expression = Column{}
var _ Expression = Aggregate{}
var _ Expression = RawExpr{}
var _ Expression = Predicate{}
var _ Expression = Valuer{}
Selectable
// Selectable selector中通过.Select(select selectable)使用
// Selectable 标记 canSelect 列、聚合函数、子查询、表达式(是 Expression)
// Selectable 的部分都可以设置别名alias
type Selectable interface {
selectable()
}
db
dialect
type Dialect interface {
Quoter() string
BuildOnDuplicateKey(OnDuplicateKey *OnDuplicateKey) (*OnDuplicateKeyStatement, error)
}
var _ Dialect = &StandardSQL{}
var _ Dialect = &MysqlSQL{}
standardSql
quoter -> 双引号
duplicateKey语句: on conflict columnName do update set col1=exclued.col1
mysqlSql
quoter -> `
duplicateKey语句:on duplicate key update col1=values(col1)
onduplicatekey
type OnDuplicateKey struct {
Assigns []predicate.Assignable // 支持两种形式 col=values(col)、col=具体值
ConflictColumns []predicate.Column // 冲突列 standardSql 语句构造使用
}
session(上下文)
// Session 是 db、tx的顶层抽象
type Session interface {
GetCore() Core
ExecContext(ctx context.Context, query string, args ...any) *sql2.QueryResult
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}
var _ Session = &DB{}
var _ Session = &Tx{}
db
支持doTx(闭包事务)、beginTx
sharding_db
支持主从数据库
model
-
维护数据结构(如User)和数据库中相应表、字段的关系
-
register(注册中心)
- 维护models,支持注册model、缓存model、get model
-
维护分库分表键和规则
valuer
type Value interface {
// SetColumns 反射设置值;selector中将db返回结果通过row.Scan塞进&user{}中
SetColumns(rows *sql.Rows, quote string) error
// Field 反射获取值;inserter中遍历&user{Name: "zly", Birthdate: "1999-06-06"}获取每个字段及其值生成相应的sql语句
Field(colName string) (any, error)
}
// Creator 支持unsafe、reflect两种方式,unsafe性能更好,默认使用unsafe
type Creator func(val any, model *model.Model) (Value, error)
type Values interface {
// SetValues 反射设置多值
SetValues(rows *sql.Rows) error
}
aop
支持数据观测、统计
type Handler func(ctx context.Context, qc *orm.QueryContext) *orm.QueryResult
type Middleware func(next Handler) Handler
分库分表中间件设计思路梳理
源码
gitee: https://gitee.com/go_web_1/server/tree/master/framework/orm
测试
普通orm
单元测试
e2e测试
分库分表orm
相关资料
没有tree命令生成目录树
find . -type d \( -name 'readme.assets' -o -name 'test' \) -prune -o -print | sed -e 's;[^/]*/;|____;g' -e 's;____|; |;g'