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-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-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-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-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-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
|
|
|
|
|
```
|
|
|
|
|
#### Count
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
2013-08-01 01:23:44 +00:00
|
|
|
|
查询当前条件下的行数
|
|
|
|
|
```go
|
|
|
|
|
o.QueryTable("user").Count() // SELECT COUNT(*) FROM USER
|
|
|
|
|
```
|
2013-07-31 14:11:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|