Builder - XORM


本站和网页 https://xorm.io/zh/docs/chapter-13/readme/ 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

Builder - XORM
XORMMain文档API博客SocialsGiteaQQ
ZH
简体中文EnglishExpandCollapse
创建Engine创建 Engine创建 Engine 组设置 Engine 组策略
定义表结构体名称映射前缀映射,后缀映射和缓存映射使用 Table 和 Tag 改变名称映射Column 属性定义Go与字段类型对应表
表结构操作获取数据库信息表操作创建索引和唯一索引同步数据库结构导入导出数据库结构和数据
插入数据插入数据创建时间Created
查询和统计数据查询条件方法临时开关方法Get方法Find方法Count方法Exist系列方法Iterate方法Join的使用Rows方法Sum系列方法
更新数据更新数据乐观锁Version更新时间Updated
删除数据删除数据软删除Deleted
执行SQL查询执行SQL查询
执行SQL命令执行SQL命令
事务处理事务处理
缓存缓存
事件事件
BuilderBuilder
Reverse 工具Reverse 工具
常见问题常见问题
案例案例
创建Engine创建 Engine创建 Engine 组设置 Engine 组策略
定义表结构体名称映射前缀映射,后缀映射和缓存映射使用 Table 和 Tag 改变名称映射Column 属性定义Go与字段类型对应表
表结构操作获取数据库信息表操作创建索引和唯一索引同步数据库结构导入导出数据库结构和数据
插入数据插入数据创建时间Created
查询和统计数据查询条件方法临时开关方法Get方法Find方法Count方法Exist系列方法Iterate方法Join的使用Rows方法Sum系列方法
更新数据更新数据乐观锁Version更新时间Updated
删除数据删除数据软删除Deleted
执行SQL查询执行SQL查询
执行SQL命令执行SQL命令
事务处理事务处理
缓存缓存
事件事件
BuilderBuilder
Reverse 工具Reverse 工具
常见问题常见问题
案例案例首页DocsBuilderBuilderBuilderBuilder 是一个 XORM 内置的轻量级快速 SQL 构建引擎。请注意此包为独立安装,请确保你的 Go 版本在 1.8+ 以上可以如下安装。go get xorm.io/builder
Insert #sql, args, err := builder.Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()
// INSERT INTO table1 SELECT * FROM table2
sql, err := builder.Insert().Into("table1").Select().From("table2").ToBoundSQL()
// INSERT INTO table1 (a, b) SELECT b, c FROM table2
sql, err = builder.Insert("a, b").Into("table1").Select("b, c").From("table2").ToBoundSQL()
Select #// Simple Query
sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
// With join
sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
// From sub query
sql, args, err := Select("sub.id").From(Select("c").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
// From union query
sql, args, err = Select("sub.id").From(
Select("id").From("table1").Where(Eq{"a": 1}).Union("all", Select("id").From("table1").Where(Eq{"a": 2})),"sub").
Where(Eq{"b": 1}).ToSQL()
// With order by
sql, args, err = Select("a", "b", "c").From("table1").Where(Eq{"f1": "v1", "f2": "v2"}).
OrderBy("a ASC").ToSQL()
// With limit.
// Be careful! You should set up specific dialect for builder before performing a query with LIMIT
sql, args, err = Dialect(MYSQL).Select("a", "b", "c").From("table1").OrderBy("a ASC").
Limit(5, 10).ToSQL()
Update #sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()
Delete #sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()
Union #sql, args, err := Select("*").From("a").Where(Eq{"status": "1"}).
Union("all", Select("*").From("a").Where(Eq{"status": "2"})).
Union("distinct", Select("*").From("a").Where(Eq{"status": "3"})).
Union("", Select("*").From("a").Where(Eq{"status": "4"})).
ToSQL()
Conditions #Eq is a redefine of a map, you can give one or more conditions to Eqimport . "xorm.io/builder"
sql, args, _ := ToSQL(Eq{"a":1})
// a=? [1]
sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
// b=? AND c=? ["c", 0]
sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
// b=? AND c=? ["c", 0]
sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
// b=? OR b=? ["c", "d"]
sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
// b IN (?,?) ["c", "d"]
sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
// b=? AND c IN (?,?) [1, 2, 3]
Neq is the same to Eqimport . "xorm.io/builder"
sql, args, _ := ToSQL(Neq{"a":1})
// a<>? [1]
sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
// b<>? AND c<>? ["c", 0]
sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
// b<>? AND c<>? ["c", 0]
sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
// b<>? OR b<>? ["c", "d"]
sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
// b NOT IN (?,?) ["c", "d"]
sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
// b<>? AND c NOT IN (?,?) [1, 2, 3]
Gt, Gte, Lt, Lteimport . "xorm.io/builder"
sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
// a>? AND b>=? [1, 2]
sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
// a<? OR b<=? [1, 2]
Likeimport . "xorm.io/builder"
sql, args, _ := ToSQL(Like{"a", "c"})
// a LIKE ? [%c%]
Expr you can customerize your sql with Exprimport . "xorm.io/builder"
sql, args, _ := ToSQL(Expr("a = ? ", 1))
// a = ? [1]
sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
// a=(select id from table where c = ?) [1]
In and NotInimport . "xorm.io/builder"
sql, args, _ := ToSQL(In("a", 1, 2, 3))
// a IN (?,?,?) [1,2,3]
sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
// a IN (?,?,?) [1,2,3]
sql, args, _ := ToSQL(NotIn("a", Expr("select id from b where c = ?", 1))))
// a NOT IN (select id from b where c = ?) [1]
Exists and NotExistsimport . "xorm.io/builder"
sql, args, _ := ToSQL(Exists(Select("a").From("table")))
// EXISTS (SELECT a FROM table)
sql, args, _ := ToSQL(NotExists(Select("a").From("table")))
// NOT EXISTS (SELECT a FROM table)
IsNull and NotNullimport . "xorm.io/builder"
sql, args, _ := ToSQL(IsNull{"a"})
// a IS NULL []
sql, args, _ := ToSQL(NotNull{"b"})
// b IS NOT NULL []
And(conds ...Cond), And can connect one or more conditions via Andimport . "xorm.io/builder"
sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
// a=? AND b LIKE ? AND d<>? [1, %c%, 2]
Or(conds ...Cond), Or can connect one or more conditions via Orimport . "xorm.io/builder"
sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
// a=? OR b LIKE ? OR d<>? [1, %c%, 2]
sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
// a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
Betweenimport . "xorm.io/builder"
sql, args, _ := ToSQL(Between{"a", 1, 2})
// a BETWEEN 1 AND 2
Define yourself conditionsSince Cond is an interface.type Cond interface {
WriteTo(Writer) error
And(...Cond) Cond
Or(...Cond) Cond
IsValid() bool
You can define yourself conditions and compose with other Cond.在 Gitea 上编辑此页面&larr; Reverse 工具事件 &rarr;Powered by Netlify, Hugo, and Doks