Documentation ¶
Index ¶
- Variables
- func Columns(cs ...string) columns
- type Aggregate
- func (a Aggregate) As(alias string) Selectable
- func (a Aggregate) EQ(val interface{}) Predicate
- func (a Aggregate) GT(val interface{}) Predicate
- func (a Aggregate) GTEQ(val interface{}) Predicate
- func (a Aggregate) LT(val interface{}) Predicate
- func (a Aggregate) LTEQ(val interface{}) Predicate
- func (a Aggregate) NEQ(val interface{}) Predicate
- type Assignable
- type Assignment
- type Column
- func (c Column) Add(val interface{}) MathExpr
- func (c Column) As(alias string) Selectable
- func (c Column) EQ(val interface{}) Predicate
- func (c Column) GT(val interface{}) Predicate
- func (c Column) GTEQ(val interface{}) Predicate
- func (c Column) In(data ...any) Predicate
- func (c Column) LT(val interface{}) Predicate
- func (c Column) LTEQ(val interface{}) Predicate
- func (c Column) Like(val interface{}) Predicate
- func (c Column) Multi(val interface{}) MathExpr
- func (c Column) NEQ(val interface{}) Predicate
- func (c Column) NotIn(data ...any) Predicate
- func (c Column) NotLike(val interface{}) Predicate
- type DB
- type DBOption
- type Deleter
- type Executor
- type Expr
- type Inserter
- type MathExpr
- type OrderBy
- type Predicate
- type Querier
- type Query
- type QueryBuilder
- type RawExpr
- type Result
- type Selectable
- type Selector
- func (s *Selector[T]) Build() (*Query, error)
- func (s *Selector[T]) Distinct() *Selector[T]
- func (s *Selector[T]) From(table interface{}) *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(columns ...string) *Selector[T]
- func (s *Selector[T]) Having(predicates ...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(columns ...Selectable) *Selector[T]
- func (s *Selector[T]) Where(predicates ...Predicate) *Selector[T]
- type Tx
- type Updater
Examples ¶
- Aggregate.As
- Assign
- AssignNotNilColumns
- AssignNotZeroColumns
- Avg
- C
- Column.Add
- Column.As
- Column.EQ
- Column.GT
- Column.GTEQ
- Column.LT
- Column.LTEQ
- Column.Multi
- Column.NEQ
- Columns
- Count
- DB.BeginTx
- Deleter.Build
- Deleter.From
- Deleter.Where
- Inserter.Build
- Inserter.Columns
- Inserter.Values
- Max
- Min
- NewDeleter
- NewInserter
- NewUpdater
- Not
- Open
- Predicate.And
- Predicate.Or
- Querier.Exec
- RawExpr.AsPredicate
- RawQuery
- Selector.Having
- Selector.OrderBy
- Selector.Select
- Sum
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoRows 代表没有找到数据 ErrNoRows = errs.ErrNoRows )
哨兵错误,或者说预定义错误,谨慎添加
Functions ¶
func Columns ¶
func Columns(cs ...string) columns
Columns specify columns
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewSelector[TestModel](db).Select(Columns("Id", "Age")).From(tm).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: SELECT `id`,`age` FROM `test_model`; Args: []
Types ¶
type Aggregate ¶
type Aggregate struct {
// contains filtered or unexported fields
}
Aggregate represents aggregate expression, including AVG, MAX, MIN...
func Avg ¶
Avg represents AVG
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).Select(Avg("Age").As("avg_age")).From(&TestModel{}).Build() fmt.Println(query.SQL)
Output: SELECT AVG(`age`) AS `avg_age` FROM `test_model`;
func Count ¶
Count represents COUNT
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).Select(Count("Age")).From(&TestModel{}).Build() fmt.Println(query.SQL)
Output: SELECT COUNT(`age`) FROM `test_model`;
func Max ¶
Max represents MAX
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).Select(Max("Age")).From(&TestModel{}).Build() fmt.Println(query.SQL)
Output: SELECT MAX(`age`) FROM `test_model`;
func Min ¶
Min represents MIN
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).Select(Min("Age")).From(&TestModel{}).Build() fmt.Println(query.SQL)
Output: SELECT MIN(`age`) FROM `test_model`;
func Sum ¶
Sum represents SUM
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).Select(Sum("Age")).From(&TestModel{}).Build() fmt.Println(query.SQL)
Output: SELECT SUM(`age`) FROM `test_model`;
type Assignable ¶
type Assignable interface {
// contains filtered or unexported methods
}
Assignable represents that something could be used as "assignment" statement
func AssignColumns ¶
func AssignColumns(entity interface{}, filter func(typ reflect.StructField, val reflect.Value) bool) []Assignable
AssignColumns will check all columns and then apply the filter function. If the returned value is true, this column will be updated.
func AssignNotNilColumns ¶
func AssignNotNilColumns(entity interface{}) []Assignable
AssignNotNilColumns uses the non-nil value to construct the Assignable instances.
Example ¶
db := memoryDB() query, _ := NewUpdater[TestModel](db).Set(AssignNotNilColumns(&TestModel{Id: 13})...).Build() fmt.Println(query.string())
Output: SQL: UPDATE `test_model` SET `id`=?,`first_name`=?,`age`=?; Args: []interface {}{13, "", 0}
func AssignNotZeroColumns ¶
func AssignNotZeroColumns(entity interface{}) []Assignable
AssignNotZeroColumns uses the non-zero value to construct the Assignable instances.
Example ¶
db := memoryDB() query, _ := NewUpdater[TestModel](db).Set(AssignNotZeroColumns(&TestModel{Id: 13})...).Build() fmt.Println(query.string())
Output: SQL: UPDATE `test_model` SET `id`=?; Args: []interface {}{13}
type Assignment ¶
type Assignment binaryExpr
Assignment represents assignment statement
func Assign ¶
func Assign(column string, value interface{}) Assignment
Example ¶
db := memoryDB() tm := &TestModel{} examples := []struct { assign Assignment assignStr string wantSQL string wantArgs []interface{} }{ { assign: Assign("Age", 18), assignStr: `Assign("Age", 18)`, wantSQL: "UPDATE `test_model` SET `age`=?;", wantArgs: []interface{}{18}, }, { assign: Assign("Age", C("Id")), assignStr: `Assign("Age", C("Id"))`, wantSQL: "UPDATE `test_model` SET `age`=`id`;", }, { assign: Assign("Age", C("Age").Add(1)), assignStr: `Assign("Age", C("Age").Add(1))`, wantSQL: "UPDATE `test_model` SET `age`=`age`+?;", wantArgs: []interface{}{1}, }, { assign: Assign("Age", Raw("`age`+`id`+1")), assignStr: "Assign(\"Age\", Raw(\"`age`+`id`+1\"))", wantSQL: "UPDATE `test_model` SET `age`=`age`+`id`+1;", }, } for _, exp := range examples { query, _ := NewUpdater[TestModel](db).Update(tm).Set(exp.assign).Build() fmt.Printf(` Assignment: %s SQL: %s Args: %v `, exp.assignStr, query.SQL, query.Args) }
Output: Assignment: Assign("Age", 18) SQL: UPDATE `test_model` SET `age`=?; Args: [18] Assignment: Assign("Age", C("Id")) SQL: UPDATE `test_model` SET `age`=`id`; Args: [] Assignment: Assign("Age", C("Age").Add(1)) SQL: UPDATE `test_model` SET `age`=(`age`+?); Args: [1] Assignment: Assign("Age", Raw("`age`+`id`+1")) SQL: UPDATE `test_model` SET `age`=`age`+`id`+1; Args: []
type Column ¶
type Column struct {
// contains filtered or unexported fields
}
Column represents column it could have alias in general, we use it in two ways 1. specify the column in query 2. it's the start point of building complex expression
func C ¶
C specify column
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewSelector[TestModel](db).Select(C("Id")).From(tm).Where(C("Id").EQ(18)).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: SELECT `id` FROM `test_model` WHERE `id`=?; Args: [18]
func (Column) Add ¶
Add generate an additive expression
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewUpdater[TestModel](db).Update(tm).Set(Assign("Age", C("Age").Add(1))).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: UPDATE `test_model` SET `age`=(`age`+?); Args: [1]
func (Column) As ¶
func (c Column) As(alias string) Selectable
As means alias
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewSelector[TestModel](db).Select(C("Id").As("my_id")).From(tm).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: SELECT `id` AS `my_id` FROM `test_model`; Args: []
func (Column) EQ ¶
EQ =
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewSelector[TestModel](db).Select(C("Id")).From(tm).Where(C("Id").EQ(18)).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: SELECT `id` FROM `test_model` WHERE `id`=?; Args: [18]
func (Column) GT ¶
GT >
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewSelector[TestModel](db).Select(C("Id")).From(tm).Where(C("Id").GT(18)).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: SELECT `id` FROM `test_model` WHERE `id`>?; Args: [18]
func (Column) GTEQ ¶
GTEQ >=
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewSelector[TestModel](db).Select(C("Id")).From(tm).Where(C("Id").GTEQ(18)).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: SELECT `id` FROM `test_model` WHERE `id`>=?; Args: [18]
func (Column) LT ¶
LT <
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewSelector[TestModel](db).Select(C("Id")).From(tm).Where(C("Id").LT(18)).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: SELECT `id` FROM `test_model` WHERE `id`<?; Args: [18]
func (Column) LTEQ ¶
LTEQ <=
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewSelector[TestModel](db).Select(C("Id")).From(tm).Where(C("Id").LTEQ(18)).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: SELECT `id` FROM `test_model` WHERE `id`<=?; Args: [18]
func (Column) Multi ¶
Multi generate a multiplication expression
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewUpdater[TestModel](db).Update(tm).Set(Assign("Age", C("Age").Multi(2))).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: UPDATE `test_model` SET `age`=(`age`*?); Args: [2]
func (Column) NEQ ¶
NEQ !=
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewSelector[TestModel](db).Select(C("Id")).From(tm).Where(C("Id").NEQ(18)).Build() fmt.Printf(` SQL: %s Args: %v `, query.SQL, query.Args)
Output: SQL: SELECT `id` FROM `test_model` WHERE `id`!=?; Args: [18]
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents a database
func Open ¶
Open 创建一个 ORM 实例 注意该实例是一个无状态的对象,你应该尽可能复用它
Example ¶
// case1 without DBOption db, _ := Open("sqlite3", "file:test.db?cache=shared&mode=memory") fmt.Printf("case1 dialect: %s\n", db.dialect.Name)
Output: case1 dialect: SQLite
func (*DB) BeginTx ¶
BeginTx 开启事务
Example ¶
db, _ := Open("sqlite3", "file:test.db?cache=shared&mode=memory") defer func() { _ = db.Close() }() tx, err := db.BeginTx(context.Background(), &sql.TxOptions{}) if err == nil { fmt.Println("Begin") } // 或者 tx.Rollback() err = tx.Commit() if err == nil { fmt.Println("Commit") }
Output: Begin Commit
type DBOption ¶
type DBOption func(db *DB)
DBOption configure DB
func UseReflection ¶
func UseReflection() DBOption
type Deleter ¶
type Deleter[T any] struct { // contains filtered or unexported fields }
Deleter builds DELETE query
func NewDeleter ¶
NewDeleter 开始构建一个 DELETE 查询
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewDeleter[TestModel](db).From(tm).Build() fmt.Printf("SQL: %s", query.SQL)
Output: SQL: DELETE FROM `test_model`;
func (*Deleter[T]) Build ¶
Build returns DELETE query
Example ¶
query, _ := NewDeleter[TestModel](memoryDB()).From(&TestModel{}).Build() fmt.Printf("SQL: %s", query.SQL)
Output: SQL: DELETE FROM `test_model`;
func (*Deleter[T]) From ¶
From accepts model definition
Example ¶
query, _ := NewDeleter[TestModel](memoryDB()).From(&TestModel{}).Build() fmt.Printf("SQL: %s", query.SQL)
Output: SQL: DELETE FROM `test_model`;
type Expr ¶
type Expr interface {
// contains filtered or unexported methods
}
Expr is the top interface. It represents everything.
type Inserter ¶
type Inserter[T any] struct { // contains filtered or unexported fields }
Inserter is used to construct an insert query More details check Build function
func NewInserter ¶
NewInserter 开始构建一个 INSERT 查询
Example ¶
db := memoryDB() tm := &TestModel{} query, _ := NewInserter[TestModel](db).Values(tm).Build() fmt.Printf("SQL: %s", query.SQL)
Output: SQL: INSERT INTO `test_model`(`id`,`first_name`,`age`,`last_name`) VALUES(?,?,?,?);
func (*Inserter[T]) Build ¶
Build function build the query notes: - All the values from function Values should have the same type. - It will insert all columns including auto-increment primary key
Example ¶
db := memoryDB() query, _ := NewInserter[TestModel](db).Values(&TestModel{ Id: 1, Age: 18, }).Build() fmt.Printf("case1\n%s", query.string()) query, _ = NewInserter[TestModel](db).Values(&TestModel{}).Build() fmt.Printf("case2\n%s", query.string())
Output: case1 SQL: INSERT INTO `test_model`(`id`,`first_name`,`age`,`last_name`) VALUES(?,?,?,?); Args: []interface {}{1, "", 18, (*sql.NullString)(nil)} case2 SQL: INSERT INTO `test_model`(`id`,`first_name`,`age`,`last_name`) VALUES(?,?,?,?); Args: []interface {}{0, "", 0, (*sql.NullString)(nil)}
func (*Inserter[T]) Columns ¶
Columns specifies the columns that need to be inserted if cs is empty, all columns will be inserted cs must be the same with the field name in model
Example ¶
db := memoryDB() query, _ := NewInserter[TestModel](db).Values(&TestModel{ Id: 1, Age: 18, }).Columns("Id", "Age").Build() fmt.Printf("case1\n%s", query.string()) query, _ = NewInserter[TestModel](db).Values(&TestModel{ Id: 1, Age: 18, }, &TestModel{}, &TestModel{FirstName: "Tom"}).Columns("Id", "Age").Build() fmt.Printf("case2\n%s", query.string())
Output: case1 SQL: INSERT INTO `test_model`(`id`,`age`) VALUES(?,?); Args: []interface {}{1, 18} case2 SQL: INSERT INTO `test_model`(`id`,`age`) VALUES(?,?),(?,?),(?,?); Args: []interface {}{1, 18, 0, 0, 0, 0}
func (*Inserter[T]) Values ¶
Values specify the rows all the elements must be the same type and users are supposed to passing at least one element
Example ¶
db := memoryDB() query, _ := NewInserter[TestModel](db).Values(&TestModel{ Id: 1, Age: 18, }, &TestModel{}).Build() fmt.Println(query.string())
Output: SQL: INSERT INTO `test_model`(`id`,`first_name`,`age`,`last_name`) VALUES(?,?,?,?),(?,?,?,?); Args: []interface {}{1, "", 18, (*sql.NullString)(nil), 0, "", 0, (*sql.NullString)(nil)}
type OrderBy ¶
type OrderBy struct {
// contains filtered or unexported fields
}
OrderBy specify fields and ASC
type Predicate ¶
type Predicate binaryExpr
Predicate will be used in Where Or Having
func Not ¶
Not indicates "NOT"
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).Select(Columns("Id")).From(&TestModel{Id: 10}).Where(Not(C("Id").EQ(18))).Build() fmt.Println(query.string())
Output: SQL: SELECT `id` FROM `test_model` WHERE NOT (`id`=?); Args: []interface {}{18}
func (Predicate) And ¶
And indicates "AND"
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).Select(Columns("Id")).From(&TestModel{ Id: 10, }).Where(C("Id").EQ(18).And(C("Age").GT(100))).Build() fmt.Println(query.string())
Output: SQL: SELECT `id` FROM `test_model` WHERE (`id`=?) AND (`age`>?); Args: []interface {}{18, 100}
func (Predicate) Or ¶
Or indicates "OR"
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).Select(Columns("Id")).From(&TestModel{ Id: 10, }).Where(C("Id").EQ(18).Or(C("Age").GT(100))).Build() fmt.Println(query.string())
Output: SQL: SELECT `id` FROM `test_model` WHERE (`id`=?) OR (`age`>?); Args: []interface {}{18, 100}
type Querier ¶
type Querier[T any] struct { // contains filtered or unexported fields }
Querier 查询器,代表最基本的查询
func RawQuery ¶
RawQuery 创建一个 Querier 实例 泛型参数 T 是目标类型。 例如,如果查询 User 的数据,那么 T 就是 User
Example ¶
orm := memoryDB() q := RawQuery[any](orm, `SELECT * FROM user_tab WHERE id = ?;`, 1) fmt.Printf(` SQL: %s Args: %v `, q.q.SQL, q.q.Args)
Output: SQL: SELECT * FROM user_tab WHERE id = ?; Args: [1]
func (Querier[T]) Exec ¶
Exec 执行 SQL
Example ¶
orm := memoryDB() // 在 Exec 的时候,泛型参数可以是任意的 q := RawQuery[any](orm, `CREATE TABLE IF NOT EXISTS groups ( group_id INTEGER PRIMARY KEY, name TEXT NOT NULL )`) res := q.Exec(context.Background()) if res.Err() == nil { fmt.Print("SUCCESS") }
Output: SUCCESS
type QueryBuilder ¶
QueryBuilder is used to build a query
type RawExpr ¶
type RawExpr struct {
// contains filtered or unexported fields
}
RawExpr uses string as Expr
func (RawExpr) AsPredicate ¶
AsPredicate 将会返回一个 Predicate,RawExpr 将会作为这个 Predicate 的左边部分 eorm 将不会校验任何从 RawExpr 生成的 Predicate
Example ¶
pred := Raw("`id`<?", 12).AsPredicate() query, _ := NewSelector[TestModel](memoryDB()).From(&TestModel{}).Where(pred).Build() fmt.Println(query.string())
Output: SQL: SELECT `id`,`first_name`,`age`,`last_name` FROM `test_model` WHERE `id`<?; Args: []interface {}{12}
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
func (Result) LastInsertId ¶
func (Result) RowsAffected ¶
type Selectable ¶
type Selectable interface {
// contains filtered or unexported methods
}
Selectable is a tag interface which represents SELECT XXX
type Selector ¶
type Selector[T any] struct { // contains filtered or unexported fields }
Selector represents a select query
func (*Selector[T]) Get ¶
Get 方法会执行查询,并且返回一条数据 注意,在不同的数据库情况下,第一条数据可能是按照不同的列来排序的 而且要注意,这个方法会强制设置 Limit 1 在没有查找到数据的情况下,会返回 ErrNoRows
func (*Selector[T]) Having ¶
Having accepts predicates
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).Select(Columns("Id"), Columns("FirstName"), Avg("Age").As("avg_age")).From(&TestModel{}).GroupBy("FirstName").Having(C("avg_age").LT(20)).Build() fmt.Printf("case1\n%s", query.string()) query, err := NewSelector[TestModel](db).Select(Columns("Id"), Columns("FirstName"), Avg("Age").As("avg_age")).From(&TestModel{}).GroupBy("FirstName").Having(C("Invalid").LT(20)).Build() fmt.Printf("case2\n%s", err)
Output: case1 SQL: SELECT `id`,`first_name`,AVG(`age`) AS `avg_age` FROM `test_model` GROUP BY `first_name` HAVING `avg_age`<?; Args: []interface {}{20} case2 eorm: 未知字段 Invalid
func (*Selector[T]) OrderBy ¶
OrderBy means "ORDER BY"
Example ¶
db := memoryDB() query, _ := NewSelector[TestModel](db).From(&TestModel{}).OrderBy(ASC("Age")).Build() fmt.Printf("case1\n%s", query.string()) query, _ = NewSelector[TestModel](db).From(&TestModel{}).OrderBy(ASC("Age", "Id")).Build() fmt.Printf("case2\n%s", query.string()) query, _ = NewSelector[TestModel](db).From(&TestModel{}).OrderBy(ASC("Age"), ASC("Id")).Build() fmt.Printf("case3\n%s", query.string()) query, _ = NewSelector[TestModel](db).From(&TestModel{}).OrderBy(ASC("Age"), DESC("Id")).Build() fmt.Printf("case4\n%s", query.string())
Output: case1 SQL: SELECT `id`,`first_name`,`age`,`last_name` FROM `test_model` ORDER BY `age` ASC; Args: []interface {}(nil) case2 SQL: SELECT `id`,`first_name`,`age`,`last_name` FROM `test_model` ORDER BY `age``id` ASC; Args: []interface {}(nil) case3 SQL: SELECT `id`,`first_name`,`age`,`last_name` FROM `test_model` ORDER BY `age` ASC,`id` ASC; Args: []interface {}(nil) case4 SQL: SELECT `id`,`first_name`,`age`,`last_name` FROM `test_model` ORDER BY `age` ASC,`id` DESC; Args: []interface {}(nil)
func (*Selector[T]) Select ¶
func (s *Selector[T]) Select(columns ...Selectable) *Selector[T]
Select 指定查询的列。 列可以是物理列,也可以是聚合函数,或者 RawExpr
Example ¶
db := memoryDB() tm := &TestModel{} cases := []*Selector[TestModel]{ // case0: all columns are included NewSelector[TestModel](db).From(tm), // case1: only query specific columns NewSelector[TestModel](db).Select(Columns("Id", "Age")).From(tm), // case2: using alias NewSelector[TestModel](db).Select(C("Id").As("my_id")).From(tm), // case3: using aggregation function and alias NewSelector[TestModel](db).Select(Avg("Age").As("avg_age")).From(tm), // case4: using raw expression NewSelector[TestModel](db).Select(Raw("COUNT(DISTINCT `age`) AS `age_cnt`")).From(tm), } for index, tc := range cases { query, _ := tc.Build() fmt.Printf("case%d:\n%s", index, query.string()) }
Output: case0: SQL: SELECT `id`,`first_name`,`age`,`last_name` FROM `test_model`; Args: []interface {}(nil) case1: SQL: SELECT `id`,`age` FROM `test_model`; Args: []interface {}(nil) case2: SQL: SELECT `id` AS `my_id` FROM `test_model`; Args: []interface {}(nil) case3: SQL: SELECT AVG(`age`) AS `avg_age` FROM `test_model`; Args: []interface {}(nil) case4: SQL: SELECT COUNT(DISTINCT `age`) AS `age_cnt` FROM `test_model`; Args: []interface {}(nil)
type Updater ¶
type Updater[T any] struct { // contains filtered or unexported fields }
Updater is the builder responsible for building UPDATE query
func NewUpdater ¶
NewUpdater 开始构建一个 UPDATE 查询
Example ¶
db := memoryDB() tm := &TestModel{ Age: 18, } query, _ := NewUpdater[TestModel](db).Update(tm).Build() fmt.Printf("SQL: %s", query.SQL)
Output: SQL: UPDATE `test_model` SET `id`=?,`first_name`=?,`age`=?,`last_name`=?;
func (*Updater[T]) Set ¶
func (u *Updater[T]) Set(assigns ...Assignable) *Updater[T]
Set represents SET clause