2013-07-31 14:11:35 +00:00
|
|
|
|
## Query
|
|
|
|
|
|
|
|
|
|
orm 以 **QuerySeter** 来组织查询,每个返回 **QuerySeter** 的方法都会获得一个新的 **QuerySeter** 对象。
|
|
|
|
|
|
|
|
|
|
基本使用方法:
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
o := orm.NewOrm()
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
// 获取 QuerySeter 对象,user 为表名
|
|
|
|
|
qs := o.QueryTable("user")
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
// 也可以直接使用对象作为表名
|
|
|
|
|
user := NewUser()
|
|
|
|
|
qs = o.QueryTable(user) // 返回 QuerySeter
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
### expr
|
|
|
|
|
|
|
|
|
|
QuerySeter 中用于描述字段和 sql 操作符使用简单的 expr 查询方法
|
|
|
|
|
|
|
|
|
|
字段组合的前后顺序依照表的关系,比如 User 表拥有 Profile 的外键,那么对 User 表查询对应的 Profile.Age 为条件,则使用 `Profile__Age` 注意,字段的分隔符号使用双下划线 `__`,除了描述字段, expr 的尾部可以增加操作符以执行对应的 sql 操作。比如 `Profile__Age__gt` 代表 Profile.Age > 18 的条件查询。
|
|
|
|
|
|
|
|
|
|
注释后面将描述对应的 sql 语句,仅仅是描述 expr 的类似结果,并不代表实际生成的语句。
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("id", 1) // WHERE id = 1
|
|
|
|
|
qs.Filter("profile__age", 18) // WHERE profile.age = 18
|
|
|
|
|
qs.Filter("Profile__Age", 18) // 使用字段名和Field名都是允许的
|
|
|
|
|
qs.Filter("profile__age", 18) // WHERE profile.age = 18
|
|
|
|
|
qs.Filter("profile__age__gt", 18) // WHERE profile.age > 18
|
|
|
|
|
qs.Filter("profile__age__gte", 18) // WHERE profile.age >= 18
|
|
|
|
|
qs.Filter("profile__age__in", 18, 20) // WHERE profile.age IN (18, 20)
|
|
|
|
|
|
|
|
|
|
qs.Filter("profile__age__in", 18, 20).Exclude("profile__money__lt", 1000)
|
|
|
|
|
// WHERE profile.age IN (18, 20) AND NOT profile.money < 1000
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
### Operators
|
|
|
|
|
|
|
|
|
|
当前支持的操作符号
|
|
|
|
|
|
|
|
|
|
#### exact
|
|
|
|
|
|
|
|
|
|
Filter / Exclude / Condition expr 的默认值
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("user_name", "slene") // WHERE user_name = 'slene'
|
|
|
|
|
qs.Filter("user_name__exact", "slene") // WHERE user_name = 'slene'
|
|
|
|
|
// 使用 = 匹配,大小写是否敏感取决于数据表使用的 collation
|
|
|
|
|
qs.Filter("profile", nil) // WHERE profile_id IS NULL
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### iexact
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("user_name__iexact", "slene")
|
|
|
|
|
// WHERE user_name LIKE 'slene'
|
|
|
|
|
// 大小写不敏感,匹配任意 'Slene' 'sLENE'
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### contains
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("user_name__contains", "slene")
|
|
|
|
|
// WHERE user_name LIKE BINARY '%slene%'
|
|
|
|
|
// 大小写敏感, 匹配包含 slene 的字符
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### icontains
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("user_name__icontains", "slene")
|
|
|
|
|
// WHERE user_name LIKE '%slene%'
|
|
|
|
|
// 大小写不敏感, 匹配任意 'im Slene', 'im sLENE'
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### in
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("profile__age__in", 17, 18, 19, 20)
|
|
|
|
|
// WHERE profile.age IN (17, 18, 19, 20)
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### gt / gte
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("profile__age__gt", 17)
|
|
|
|
|
// WHERE profile.age > 17
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
qs.Filter("profile__age__gte", 18)
|
|
|
|
|
// WHERE profile.age >= 18
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### lt / lte
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("profile__age__lt", 17)
|
|
|
|
|
// WHERE profile.age < 17
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
qs.Filter("profile__age__lte", 18)
|
|
|
|
|
// WHERE profile.age <= 18
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### startswith
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("user_name__startswith", "slene")
|
|
|
|
|
// WHERE user_name LIKE BINARY 'slene%'
|
|
|
|
|
// 大小写敏感, 匹配以 'slene' 起始的字符串
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### istartswith
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("user_name__istartswith", "slene")
|
|
|
|
|
// WHERE user_name LIKE 'slene%'
|
|
|
|
|
// 大小写不敏感, 匹配任意以 'slene', 'Slene' 起始的字符串
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### endswith
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("user_name__endswith", "slene")
|
|
|
|
|
// WHERE user_name LIKE BINARY '%slene'
|
|
|
|
|
// 大小写敏感, 匹配以 'slene' 结束的字符串
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### iendswith
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("user_name__startswith", "slene")
|
|
|
|
|
// WHERE user_name LIKE '%slene'
|
|
|
|
|
// 大小写不敏感, 匹配任意以 'slene', 'Slene' 结束的字符串
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### isnull
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("profile__isnull", true)
|
|
|
|
|
qs.Filter("profile_id__isnull", true)
|
|
|
|
|
// WHERE profile_id IS NULL
|
|
|
|
|
|
|
|
|
|
qs.Filter("profile__isnull", false)
|
|
|
|
|
// WHERE profile_id IS NOT NULL
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
## QuerySeter
|
|
|
|
|
|
|
|
|
|
#### Filter
|
|
|
|
|
|
|
|
|
|
多个 Filter 之间使用 `AND` 连接
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Filter("profile__isnull", true).Filter("user_name", "slene")
|
|
|
|
|
// WHERE profile_id IS NULL AND user_name = 'slene'
|
|
|
|
|
```
|
2013-08-01 07:52:05 +00:00
|
|
|
|
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### Exclude
|
|
|
|
|
|
|
|
|
|
使用 `NOT` 排除条件
|
|
|
|
|
|
|
|
|
|
多个 Exclude 之间使用 `AND` 连接
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.Exclude("profile__isnull", true).Filter("user_name", "slene")
|
|
|
|
|
// WHERE NOT profile_id IS NULL AND user_name = 'slene'
|
|
|
|
|
```
|
2013-08-01 07:52:05 +00:00
|
|
|
|
|
|
|
|
|
#### SetCond
|
|
|
|
|
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### Limit
|
|
|
|
|
|
|
|
|
|
限制最大返回数据行数,第二个参数可以设置 `Offset`
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
var DefaultRowsLimit = 1000 // orm 默认的 limit 值为 1000
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
// 默认情况下 select 查询的最大行数为 1000
|
|
|
|
|
// LIMIT 1000
|
|
|
|
|
|
|
|
|
|
qs.Limit(10)
|
|
|
|
|
// LIMIT 10
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
qs.Limit(10, 20)
|
|
|
|
|
// LIMIT 10 OFFSET 20
|
|
|
|
|
|
|
|
|
|
qs.Limit(-1)
|
|
|
|
|
// no limit
|
|
|
|
|
|
|
|
|
|
qs.Limit(-1, 100)
|
|
|
|
|
// LIMIT 18446744073709551615 OFFSET 100
|
|
|
|
|
// 18446744073709551615 是 1<<64 - 1 用来指定无 limit 限制 但有 offset 偏移的情况
|
|
|
|
|
```
|
2013-08-01 07:52:05 +00:00
|
|
|
|
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### Offset
|
|
|
|
|
|
|
|
|
|
设置 偏移行数
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.OFFSET(20)
|
|
|
|
|
// LIMIT 1000 OFFSET 20
|
|
|
|
|
```
|
2013-08-01 07:52:05 +00:00
|
|
|
|
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### OrderBy
|
|
|
|
|
|
|
|
|
|
参数使用 **expr**
|
|
|
|
|
|
|
|
|
|
在 expr 前使用减号 `-` 表示 `DESC` 的排列
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
qs.OrderBy("id", "-profile__age")
|
|
|
|
|
// ORDER BY id ASC, profile.age DESC
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
qs.OrderBy("-profile__money", "profile")
|
|
|
|
|
// ORDER BY profile.money DESC, profile_id ASC
|
|
|
|
|
```
|
2013-08-01 07:52:05 +00:00
|
|
|
|
|
2013-07-31 14:11:35 +00:00
|
|
|
|
#### RelatedSel
|
|
|
|
|
|
|
|
|
|
关系查询,参数使用 **expr**
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
|
|
|
|
var DefaultRelsDepth = 5 // 默认情况下直接调用 RelatedSel 将进行最大 5 层的关系查询
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
qs := o.QueryTable("post")
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
qs.RelateSel()
|
|
|
|
|
// INNER JOIN user ... LEFT OUTER JOIN profile ...
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
qs.RelateSel("user")
|
|
|
|
|
// INNER JOIN user ...
|
|
|
|
|
// 设置 expr 只对设置的字段进行关系查询
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
// 对设置 null 属性的 Field 将使用 LEFT OUTER JOIN
|
|
|
|
|
```
|
2013-08-01 07:52:05 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
#### Count
|
2013-08-01 07:52:05 +00:00
|
|
|
|
依据当前的查询条件,返回结果行数
|
|
|
|
|
```go
|
|
|
|
|
cnt, err := o.QueryTable("user").Count() // SELECT COUNT(*) FROM USER
|
|
|
|
|
fmt.Printf("Count Num: %s, %s", cnt, err)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Update
|
|
|
|
|
依据当前查询条件,进行批量更新操作
|
|
|
|
|
```go
|
|
|
|
|
num, err := o.QueryTable("user").Filter("user_name", "slene").Update(orm.Params{
|
|
|
|
|
"user_name": "astaxie",
|
|
|
|
|
})
|
|
|
|
|
fmt.Printf("Affected Num: %s, %s", num, err)
|
|
|
|
|
// SET user_name = "astaixe" WHERE user_name = "slene"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Delete
|
|
|
|
|
依据当前查询条件,进行批量删除操作
|
|
|
|
|
```go
|
|
|
|
|
num, err := o.QueryTable("user").Filter("user_name", "slene").Delete()
|
|
|
|
|
fmt.Printf("Affected Num: %s, %s", num, err)
|
|
|
|
|
// DELETE FROM user WHERE user_name = "slene"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### All
|
|
|
|
|
返回对应的结果集对象
|
|
|
|
|
```go
|
|
|
|
|
var users []*User
|
|
|
|
|
num, err := o.QueryTable("user").Filter("user_name", "slene").All(&users)
|
|
|
|
|
fmt.Printf("Returned Rows Num: %s, %s", num, err)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### One
|
|
|
|
|
尝试返回单个对象
|
|
|
|
|
```go
|
|
|
|
|
var user *User
|
|
|
|
|
err := o.QueryTable("user").Filter("user_name", "slene").One(&user)
|
|
|
|
|
if err == orm.ErrMultiRows {
|
|
|
|
|
fmt.Printf("Returned Multi Rows Not One")
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Values
|
|
|
|
|
返回结果集的 key => value 值
|
|
|
|
|
|
|
|
|
|
key 为 Model 里的 Field name,value 的值 以 string 保存
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
var maps []orm.Params
|
|
|
|
|
num, err := o.QueryTable("user").Values(&maps)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Result Nums: %d\n", num)
|
|
|
|
|
for _, m := range maps {
|
|
|
|
|
fmt.Println(m["Id"], m["UserName"])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
返回指定的 Field 数据
|
|
|
|
|
|
|
|
|
|
**TODO**: 暂不支持级联查询 **RelatedSel** 直接返回 Values
|
|
|
|
|
|
|
|
|
|
但可以直接指定 expr 级联返回需要的数据
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
var maps []orm.Params
|
|
|
|
|
num, err := o.QueryTable("user").Values(&maps, "id", "user_name", "profile", "profile__age")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Result Nums: %d\n", num)
|
|
|
|
|
for _, m := range maps {
|
|
|
|
|
fmt.Println(m["Id"], m["UserName"], m["Profile"], m["Profile__Age"])
|
|
|
|
|
// map 中的数据都是展开的,没有复杂的嵌套
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### ValuesList
|
|
|
|
|
|
|
|
|
|
顾名思义,返回的结果集以slice存储
|
|
|
|
|
|
|
|
|
|
结果的排列与 Model 中定义的 Field 顺序一致
|
|
|
|
|
|
|
|
|
|
返回的每个元素值以 string 保存
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```go
|
2013-08-01 07:52:05 +00:00
|
|
|
|
var lists []orm.ParamsList
|
|
|
|
|
num, err := o.QueryTable("user").ValuesList(&lists)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Result Nums: %d\n", num)
|
|
|
|
|
for _, row := range lists {
|
|
|
|
|
fmt.Println(row)
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-01 01:23:44 +00:00
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 07:52:05 +00:00
|
|
|
|
当然也可以指定 expr 返回指定的 Field
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
var lists []orm.ParamsList
|
|
|
|
|
num, err := o.QueryTable("user").ValuesList(&lists, "user_name", "profile__age")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Result Nums: %d\n", num)
|
|
|
|
|
for _, row := range lists {
|
|
|
|
|
fmt.Printf("UserName: %s, Age: %s\m", row[0], row[1])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### ValuesFlat
|
|
|
|
|
|
|
|
|
|
只返回特定的 Field 值,讲结果集展开到单个 slice 里
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
var list orm.ParamsList
|
|
|
|
|
num, err := o.QueryTable("user").ValuesFlat(&list, "user_name")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Result Nums: %d\n", num)
|
|
|
|
|
fmt.Printf("All User Names: %s", strings.Join(list, ", ")
|
|
|
|
|
}
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 07:52:05 +00:00
|
|
|
|
#### PrepareInsert
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 07:52:05 +00:00
|
|
|
|
用于批量插入 prepare -> insert -> insert
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|