Documentation
¶
Index ¶
- Variables
- type Aggregate
- type Assignable
- type Assignment
- type Column
- func (c Column) Add(delta int) MathExpr
- func (c Column) As(alias string) Column
- func (c Column) EQ(arg any) Predicate
- func (c Column) GT(arg any) Predicate
- func (c Column) In(vals ...any) Predicate
- func (c Column) InQuery(sub Subquery) Predicate
- func (c Column) LT(arg any) Predicate
- func (c Column) Multi(delta int) MathExpr
- type DB
- type DBOption
- type Deleter
- type Dialect
- type Executor
- type Expression
- type Handler
- type Inserter
- type Join
- type JoinBuilder
- type MathExpr
- type Middleware
- type OrderBy
- type Predicate
- type Querier
- type Query
- type QueryBuilder
- type QueryContext
- type QueryResult
- type RawExpr
- type RawQuerier
- type Result
- type Selectable
- type Selector
- func (s *Selector[T]) Build() (*Query, error)
- func (s *Selector[T]) From(tbl TableReference) *Selector[T]
- func (s *Selector[T]) Get(ctx context.Context) (*T, error)
- func (s *Selector[T]) GetMulti(ctx context.Context) ([]*T, error)
- func (s *Selector[T]) GroupBy(cols ...Column) *Selector[T]
- func (s *Selector[T]) Having(ps ...Predicate) *Selector[T]
- func (s *Selector[T]) Limit(limit int) *Selector[T]
- func (s *Selector[T]) Offset(offset int) *Selector[T]
- func (s *Selector[T]) OrderBy(orderBys ...OrderBy) *Selector[T]
- func (s *Selector[T]) Select(cols ...Selectable) *Selector[T]
- func (s *Selector[T]) Where(ps ...Predicate) *Selector[T]
- type Session
- type Subquery
- type SubqueryExpr
- type Table
- type TableReference
- type TestModel
- type TestModelWithTableName
- type Tx
- type Updater
- type Upsert
- type UpsertBuilder
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoRows 代表没有找到数据 ErrNoRows = errs.ErrNoRows )
将内部的 sentinel error 暴露出去
Functions ¶
This section is empty.
Types ¶
type Aggregate ¶
type Aggregate struct {
// contains filtered or unexported fields
}
Aggregate 代表聚合函数, 例如 AVG, MAX, MIN 等 以及别名
type Assignable ¶
type Assignable interface {
// contains filtered or unexported methods
}
Assignable 标记接口, 实现该接口意味着可以用于赋值语句, 用于在 UPDATE 和 UPSERT 中 Assign("FirstName", "DaMing") -> SET`first_name`=?
func AssignColumns ¶
func AssignColumns(entity interface{}, filter func(typ reflect.StructField, val reflect.Value) bool) []Assignable
AssignColumns returns a list of Assignable values for the given entity, filtered by the provided filter function.
func AssignNotNilColumns ¶
func AssignNotNilColumns(entity interface{}) []Assignable
AssignNotNilColumns
@Description: 判断是否是 Nil, 如果是 nil 那么调用 assign 的时候,就不处理这个字段 @param entity @return []Assignable
func AssignNotZeroColumns ¶
func AssignNotZeroColumns(entity interface{}) []Assignable
AssignNotZeroColumns
@Description: 判断是否是零值, 如果是零值 那么调用 assign 的时候,就不处理这个字段 @param entity @return []Assignable
type Assignment ¶
type Assignment struct {
// contains filtered or unexported fields
}
func Assign ¶
func Assign(column string, val any) Assignment
type Column ¶
type Column struct {
// contains filtered or unexported fields
}
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
func MustNewDB ¶
MustNewDB creates a new DB with the provided options. If the creation fails, it panics.
func Open ¶
Open opens a database connection using the specified driver and DSN. It also accepts optional DBOptions. 可以传入不同的 driver 连接不同的 db
type DBOption ¶
type DBOption func(*DB)
func DBUseReflectValuer ¶
func DBUseReflectValuer() DBOption
DBUseReflectValuer 如果想使用反射进行获取相关信息,则调用这个 option
func DBWithMiddlewares ¶
func DBWithMiddlewares(mdls ...Middleware) DBOption
type Deleter ¶
type Deleter[T any] struct { // contains filtered or unexported fields }
func NewDeleter ¶
NewSelector creates a new instance of Selector.
func (*Deleter[T]) Build ¶
Build generates a DELETE query based on the provided parameters. It returns the generated query string and any associated arguments, or an error if there was a problem building the query.
func (*Deleter[T]) From ¶
From sets the table for the Deleter and returns a pointer to the Deleter. The table parameter specifies the name of the table to delete from.
type Expression ¶
type Expression interface {
// contains filtered or unexported methods
}
Expression 代表语句,或者语句的部分 暂时没想好怎么设计方法,所以直接做成标记接口
type Handler ¶
type Handler func(ctx context.Context, qc *QueryContext) *QueryResult
type Inserter ¶
type Inserter[T any] struct { // contains filtered or unexported fields }
func NewInserter ¶
func (*Inserter[T]) Columns ¶
Columns Fields 指定要插入的列 TODO 目前我们只支持指定具体的列,但是不支持复杂的表达式 例如不支持 VALUES(..., now(), now()) 这种在 MySQL 里面常用的
@Description: 更新指定的字段 @receiver i @param cols @return *Inserter[T]
func (*Inserter[T]) OnDuplicateKey ¶
func (i *Inserter[T]) OnDuplicateKey() *UpsertBuilder[T]
type Join ¶
type Join struct {
// contains filtered or unexported fields
}
func (Join) Join ¶
func (j Join) Join(target TableReference) *JoinBuilder
func (Join) LeftJoin ¶
func (j Join) LeftJoin(target TableReference) *JoinBuilder
func (Join) RightJoin ¶
func (j Join) RightJoin(target TableReference) *JoinBuilder
type JoinBuilder ¶
type JoinBuilder struct {
// contains filtered or unexported fields
}
func (*JoinBuilder) On ¶
func (j *JoinBuilder) On(ps ...Predicate) Join
func (*JoinBuilder) Using ¶
func (j *JoinBuilder) Using(cs ...string) Join
type Middleware ¶
type Predicate ¶
type Predicate binaryExpr
Predicate 代表一个查询条件 Predicate 可以通过和 Predicate 组合构成复杂的查询条件
type Predicate struct { left Expression op op right Expression }
type QueryBuilder ¶
type QueryContext ¶
type QueryContext struct { // Type 声明查询类型。即 SELECT, UPDATE, DELETE 和 INSERT Type string // builder 使用的时候,大多数情况下你需要转换到具体的类型 // 才能篡改查询 Builder QueryBuilder // qc.Model.TableName 为了有的中间件在拦截时需要 Model 信息 // 所以需要冗余一份在 middleware 的上下文中 Model *model.Model }
QueryContext 中间件的上下文,冗余了 Builder model 等,是因为还没有执行 sql 前,有的中间件,需要使用这些信息, 这里优化还可以考虑 构建 builder 后的 sql 拼接结果,省的每次调用都需要调用 builder 进行拼接 sql, 这里没这么做,可能因为怕别人篡改?
type QueryResult ¶
type RawExpr ¶
type RawExpr struct {
// contains filtered or unexported fields
}
RawExpr 代表一个原生表达式 意味着 ORM 不会对它进行任何处理
func (RawExpr) AsPredicate ¶
type RawQuerier ¶
type RawQuerier[T any] struct { // contains filtered or unexported fields }
func (*RawQuerier[T]) Build ¶
func (r *RawQuerier[T]) Build() (*Query, error)
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
func (Result) LastInsertId ¶
LastInsertId 重新 database sql 的 Result 方法 做一层拦截
func (Result) RowsAffected ¶
type Selectable ¶
type Selectable interface {
// contains filtered or unexported methods
}
Selectable 暂时没什么作用只是用作标记,可检索指定字段的标记 让结构体实现这个接口,就可以传入 使用接口为的是:让 聚合函数, columns, 以及 RawExpr(原生sql) 都能作为参数传入统一个函数,做统一处理
type Selector ¶
type Selector[T any] struct { // contains filtered or unexported fields }
Selector represents a query selector that allows building SQL SELECT statements. It holds the necessary information to construct the query.
func NewSelector ¶
NewSelector creates a new instance of Selector.
func (*Selector[T]) Build ¶
Build generates a SQL query for selecting all columns from a table. It returns the generated query as a *Query struct or an error if there was any.
func (*Selector[T]) From ¶
func (s *Selector[T]) From(tbl TableReference) *Selector[T]
From sets the table name for the selector. It returns the updated selector.
func (*Selector[T]) Select ¶
func (s *Selector[T]) Select(cols ...Selectable) *Selector[T]
Select 检索指定 column
type Session ¶
type Session interface {
// contains filtered or unexported methods
}
Session 代表一个抽象的概念,即会话 暂时做成私有的,后面考虑重构,因为这个东西用户可能有点难以理解
type Subquery ¶
type Subquery struct {
// contains filtered or unexported fields
}
func (Subquery) Join ¶
func (s Subquery) Join(target TableReference) *JoinBuilder
func (Subquery) LeftJoin ¶
func (s Subquery) LeftJoin(target TableReference) *JoinBuilder
func (Subquery) RightJoin ¶
func (s Subquery) RightJoin(target TableReference) *JoinBuilder
type SubqueryExpr ¶
type SubqueryExpr struct {
// contains filtered or unexported fields
}
SubqueryExpr 注意,这个谓词这种不是在所有的数据库里面都支持的 这里采取的是和 Upsert 不同的做法 Upsert 里面我们是属于用 dialect 来区别不同的实现 这里我们采用另外一种方案,就是直接生成,依赖于数据库来报错 实际中两种方案你可以自由替换
func All ¶
func All(sub Subquery) SubqueryExpr
func Any ¶
func Any(sub Subquery) SubqueryExpr
func Some ¶
func Some(sub Subquery) SubqueryExpr
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
func (Table) Join ¶
func (t Table) Join(target TableReference) *JoinBuilder
func (Table) LeftJoin ¶
func (t Table) LeftJoin(target TableReference) *JoinBuilder
func (Table) RightJoin ¶
func (t Table) RightJoin(target TableReference) *JoinBuilder
type TableReference ¶
type TableReference interface {
// contains filtered or unexported methods
}
type TestModel ¶
type TestModel struct { Id int64 FirstName string Age int8 LastName *sql.NullString }
type TestModelWithTableName ¶
type TestModelWithTableName struct { Id int64 FirstName string Age int8 LastName *sql.NullString }
func (TestModelWithTableName) TableName ¶
func (t TestModelWithTableName) TableName() string
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
func (*Tx) RollbackIfNotCommit ¶
type Updater ¶
type Updater[T any] struct { // contains filtered or unexported fields }
func NewUpdater ¶
func (*Updater[T]) Set ¶
func (u *Updater[T]) Set(assigns ...Assignable) *Updater[T]
type UpsertBuilder ¶
type UpsertBuilder[T any] struct { // contains filtered or unexported fields }
func (*UpsertBuilder[T]) ConflictColumns ¶
func (o *UpsertBuilder[T]) ConflictColumns(cols ...string) *UpsertBuilder[T]
func (*UpsertBuilder[T]) Update ¶
func (o *UpsertBuilder[T]) Update(assigns ...Assignable) *Inserter[T]
Update
@Description: 如果存在,则更新指定字段 @receiver o @param assigns @return *Inserter[T]