You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// select * from userdatabase.select('user').execute()// select * from user where id = 1database.select('user',{id: 1}).execute()// select * from user limit 10 offset 10database.select('user').limit(10).offset(10).execute()// select * from user order by money desc// 这里的 desc 通过 orderBy 的第二个参数表达database.select('user').orderBy('money',true).execute()// select id, name from userdatabase.select('user').project(['id','name']).execute()
import{$}from'koishi'// select * from user order by money + savingsdatabase.select('user').orderBy(row=>$.add(row.money,row.savings)).execute()// select id as foo, money + savings as bar from userdatabase.select('user').project({foo: 'id',bar: row=>$.add(row.money,row.savings),}).execute()
Group Operation
// select * from user group by foodatabase.select('user').groupBy('foo').execute()// select * from user group by foo, bardatabase.select('user').groupBy(['foo','bar']).execute()// select * from user group by foo + bardatabase.select('user').groupBy(row=>$.add(row.foo,row.bar)).execute()// select foo, sum(bar) as bar from user group by foo having count(id) > 1database.select('user').groupBy('foo',row=>$.gt($.count(row.id),1)).project({foo: 'foo',bar: row=>$.sum(row.bar),}).execute()
Subquery
子查询可能出现在多个位置,不过它们的语法是类似的。
// 获取用户的 money 排行// select (select count(*) from user where money > user1.money) as rank from user user1database.select('user').project({rank: u1=>database.select('user',u2=>$.gt(u2.money,u1.money)).evaluate(u2=>$.count(u2.id)),}).execute()
以上是子查询出现在 select 后的情况。除此以外还有出现在 from 和 where 中的情况:
// select * from user where foo in (select ...)database.select('user',{foo: database.select(...),}).execute()// select * from (select ...) as barconstselectionA=database.select(...)constselectionB=database.select(selectionA).execute()
Join Operation
constselectionA=database.select('foo')constselectionB=database.select('bar')// select * from foo inner join bardatabase.select([selectionA,selectionB])// select * from foo left join bardatabase.select([selectionA,selectionB],[true,false])// select * from foo right join bardatabase.select([selectionA,selectionB],[false,true])// select * from foo full join bardatabase.select([selectionA,selectionB],[true,true])// select * from foo inner join bar on foo.qux = bar.quxdatabase.select([selectionA,selectionB],(foo,bar)=>$.eq(foo.qux,bar.qux))
The text was updated successfully, but these errors were encountered:
The first parameter can be a single named field, an array of named fields, or a dict of field expressions.
// select foo from user group by foodatabase.select('user').groupBy('foo').execute()// select foo, bar from user group by foo, bardatabase.select('user').groupBy(['foo','bar']).execute()// select foo + bar as qux from user group by quxdatabase.select('user').groupBy({qux: row=>$.add(row.foo,row.bar)),}).execute()
Aggregations
The second parameter can be used to add additional aggregation fields.
// select foo, sum(bar) as bar, max(qux) as qux from user group by foodatabase.select('user').groupBy('foo',{bar: row=>$.sum(row.bar),qux: row=>$.max(row.qux),})
And the third parameter is used for having clause. Both parameters can be omitted.
// select foo from user group by foo having count(id) > 1database.select('user').groupBy('foo',row=>$.gt($.count(row.id),1)).execute()
Chaining
groupBy can be chained multiple times. Thanks to @undefined-moe for the real-world example.
尽管目前的 ORM API 已经覆盖了大部分使用场景,但仍然有一些不被支持的行为。为此,我们需要引入更强大的 Query Builder 来实现更多的数据库操作。此提案为 Koishi 补全了常见数据库 select 特性的支持,包括下列特性:
在实现过程中需要满足以下要求:
由于这个题目比较复杂,可能会分为多个阶段完成。
Selection
Selection 表达了一种类似 Table 的数据结构,也是大部分操作的基础。除了可以通过
database.select()
创建外,你还会在 group 和 join 等操作中遇到它。下面是一些典型 SQL 的对应写法:
Evaluation Callback
Evaluation Callback 允许通过回调函数的方式定义请求中的表达式。你可以在 project 和 orderBy 等方法中使用这种语法。函数相比对象不仅更接近我们熟悉的书写风格,而且能提供更好的类型提示。
Group Operation
Subquery
子查询可能出现在多个位置,不过它们的语法是类似的。
以上是子查询出现在 select 后的情况。除此以外还有出现在 from 和 where 中的情况:
Join Operation
The text was updated successfully, but these errors were encountered: