mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
aliased(CodingKey), annotated(with: aggregate, ...), count and isEmpty are now properties
This commit is contained in:
1 parent
2774aaf292
commit
704da35576
6 files changed
+218
-87
No files matched your search
@@ -1415,7 +1415,7 @@ struct AuthorInfo: Decodable, FetchableRecord {
|
||||
// FROM author
|
||||
// LEFT JOIN book ON book.authorId = author.id
|
||||
// GROUP BY author.id
|
||||
let request = Author.annotated(with: Author.books.count())
|
||||
let request = Author.annotated(with: Author.books.count)
|
||||
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)
|
||||
|
||||
for info in authorInfos {
|
||||
@@ -1428,8 +1428,8 @@ for info in authorInfos {
|
||||
|
||||
**HasMany** associations let you build the following aggregates:
|
||||
|
||||
- `books.count()`
|
||||
- `books.isEmpty()`
|
||||
- `books.count`
|
||||
- `books.isEmpty`
|
||||
- `books.min(column)`
|
||||
- `books.max(column)`
|
||||
- `books.average(column)`
|
||||
@@ -1459,8 +1459,7 @@ struct AuthorInfo: Decodable, FetchableRecord {
|
||||
// WHERE author.id = 1
|
||||
// GROUP BY author.id
|
||||
let request = Author
|
||||
.annotated(with: Author.books.count())
|
||||
.annotated(with: Author.books.max(Column("year")))
|
||||
.annotated(with: Author.books.count, Author.books.max(Column("year")))
|
||||
|
||||
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)
|
||||
|
||||
@@ -1471,17 +1470,18 @@ for info in authorInfos {
|
||||
}
|
||||
```
|
||||
|
||||
As seen in the above example, aggregated values are given a **default name**, such as "bookCount" or "maxBookYear". The default name is built from the aggregating method, the **[association key](#the-structure-of-a-joined-request)**, the aggregated column name:
|
||||
As seen in the above example, some aggregated values are given a **default name**, such as "bookCount" or "maxBookYear". The default name is built from the aggregating method, the **[association key](#the-structure-of-a-joined-request)**, and the aggregated column name:
|
||||
|
||||
| Method | Key | Column | Default name |
|
||||
| --------- | --- | ------ | ------------- |
|
||||
| `Author.books.count()` | `book` | | `bookCount` |
|
||||
| `Author.books.min(Column("year"))` | `book` | `year` | `minBookYear` |
|
||||
| `Author.books.max(Column("year"))` | `book` | `year` | `maxBookYear` |
|
||||
| `Author.books.isEmpty ` | `book` | | - |
|
||||
| `Author.books.count ` | `book` | | `bookCount` |
|
||||
| `Author.books.min(Column("year"))` | `book` | `year` | `minBookYear` |
|
||||
| `Author.books.max(Column("year"))` | `book` | `year` | `maxBookYear` |
|
||||
| `Author.books.average(Column("price"))` | `book` | `price` | `averageBookPrice` |
|
||||
| `Author.books.sum(Column("awards"))` | `book` | `awards` | `bookAwardsSum` |
|
||||
| `Author.books.sum(Column("awards")) ` | `book` | `awards` | `bookAwardsSum` |
|
||||
|
||||
You can rename aggregated values with the `aliased` method:
|
||||
You give a custom name to an aggregated value with the `aliased` method:
|
||||
|
||||
```swift
|
||||
struct AuthorInfo: Decodable, FetchableRecord {
|
||||
@@ -1493,24 +1493,134 @@ struct AuthorInfo: Decodable, FetchableRecord {
|
||||
// FROM author
|
||||
// LEFT JOIN book ON book.authorId = author.id
|
||||
// GROUP BY author.id
|
||||
let request = Author.annotated(with: Author.books.count().aliased("numberOfBooks"))
|
||||
let request = Author.annotated(with: Author.books.count.aliased("numberOfBooks"))
|
||||
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)
|
||||
```
|
||||
|
||||
Custom names help consuming complex aggregates:
|
||||
|
||||
```swift
|
||||
struct AuthorInfo: Decodable, FetchableRecord {
|
||||
var author: Author
|
||||
var workCount: Int
|
||||
}
|
||||
|
||||
// SELECT author.*,
|
||||
// (COUNT(DISTINCT book.rowid) + COUNT(DISTINCT painting.rowid)) AS workCount
|
||||
// FROM author
|
||||
// LEFT JOIN book ON book.authorId = author.id
|
||||
// LEFT JOIN painting ON painting.authorId = author.id
|
||||
// GROUP BY author.id
|
||||
let aggregate = Author.books.count + Author.paintings.count
|
||||
let request = Author.annotated(with: aggregate.aliased("workCount"))
|
||||
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)
|
||||
```
|
||||
|
||||
The `aliased` method also accept coding keys:
|
||||
|
||||
```swift
|
||||
struct AuthorInfo: Decodable, FetchableRecord {
|
||||
var author: Author
|
||||
var workCount: Int
|
||||
|
||||
static func fetchAll(_ db: Database) throws -> [AuthorInfo] {
|
||||
let aggregate = Author.books.count + Author.paintings.count
|
||||
let request = Author.annotated(with: aggregate.aliased(CodingKey.workCount))
|
||||
return try AuthorInfo.fetchAll(db, request)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Filtering a Request with Aggregates
|
||||
|
||||
The `having(_:)` method filters a request according to an aggregated value. You can append as many aggregate conditions as needed, from one or several associations:
|
||||
The `having(_:)` method filters a request according to an aggregated value. You can append as many aggregate conditions as needed, from one or several associations.
|
||||
|
||||
```swift
|
||||
// SELECT author.*
|
||||
// FROM author
|
||||
// LEFT JOIN book ON book.authorId = author.id
|
||||
// GROUP BY author.id
|
||||
// HAVING COUNT(DISTINCT book.rowid) > 0
|
||||
let request = Author.having(Author.books.isEmpty() == false)
|
||||
let authors: [Author] = try AuthorInfo.fetchAll(db, request)
|
||||
```
|
||||
- Authors without books:
|
||||
|
||||
```swift
|
||||
// SELECT author.*
|
||||
// FROM author
|
||||
// LEFT JOIN book ON book.authorId = author.id
|
||||
// GROUP BY author.id
|
||||
// HAVING COUNT(DISTINCT book.rowid) > 0
|
||||
let request = Author
|
||||
.having(Author.books.isEmpty == false)
|
||||
```
|
||||
|
||||
- Authors with at least one book:
|
||||
|
||||
```swift
|
||||
// SELECT author.*
|
||||
// FROM author
|
||||
// LEFT JOIN book ON book.authorId = author.id
|
||||
// GROUP BY author.id
|
||||
// HAVING COUNT(DISTINCT book.rowid) > 0
|
||||
let request = Author
|
||||
.having(Author.books.isEmpty == false)
|
||||
```
|
||||
|
||||
- Authors with at least two books:
|
||||
|
||||
```swift
|
||||
// SELECT author.*
|
||||
// FROM author
|
||||
// LEFT JOIN book ON book.authorId = author.id
|
||||
// GROUP BY author.id
|
||||
// HAVING COUNT(DISTINCT book.rowid) >= 2
|
||||
let request = Author
|
||||
.having(Author.books.count >= 2)
|
||||
```
|
||||
|
||||
- Authors with more books than paitings:
|
||||
|
||||
```swift
|
||||
// SELECT author.*
|
||||
// FROM author
|
||||
// LEFT JOIN book ON book.authorId = author.id
|
||||
// LEFT JOIN painting ON painting.authorId = author.id
|
||||
// GROUP BY author.id
|
||||
// HAVING COUNT(DISTINCT book.rowid) > COUNT(DISTINCT painting.rowid)
|
||||
let request = Author
|
||||
.having(Author.books.count > Author.painting.count)
|
||||
```
|
||||
|
||||
- Authors without books but with at least one painting:
|
||||
|
||||
```swift
|
||||
// SELECT author.*
|
||||
// FROM author
|
||||
// LEFT JOIN book ON book.authorId = author.id
|
||||
// LEFT JOIN painting ON painting.authorId = author.id
|
||||
// GROUP BY author.id
|
||||
// HAVING ((COUNT(DISTINCT book.rowid) = 0) AND (COUNT(DISTINCT painting.rowid) > 0))
|
||||
let request = Author
|
||||
.having(Author.books.isEmpty && !Author.painting.isEmpty)
|
||||
```
|
||||
|
||||
When you need to compute aggregates on *several distinct sets* of associated records, make sure all of them use distinct **[association keys](#the-structure-of-a-joined-request)**.
|
||||
|
||||
In the example above, the `Author.books` and `Author.paintings` are assumed to have the distinct `book` and `painting` keys. But in the example below, which uses the same association `Author.books` *twice*, for distinct purposes, we need to provide explicit distinct keys:
|
||||
|
||||
- Authors with more novels than theatre plays:
|
||||
|
||||
```swift
|
||||
// SELECT author.*
|
||||
// FROM author
|
||||
// LEFT JOIN book book1 ON book1.authorId = author.id AND book1.kind = 'novel'
|
||||
// LEFT JOIN book book2 ON book2.authorId = author.id AND book2.kind = 'theatrePlay'
|
||||
// GROUP BY author.id
|
||||
// HAVING COUNT(DISTINCT book1.rowid) > COUNT(DISTINCT book2.rowid)
|
||||
let novelCount = Author.books
|
||||
.filter(Column("kind") == "novel")
|
||||
.forKey("novel")
|
||||
.count
|
||||
let theatrePlayCount = Author.books
|
||||
.filter(Column("kind") == "theatrePlay")
|
||||
.forKey("theatrePlay")
|
||||
.count
|
||||
let request = Author.having(novelCount > theatrePlayCount)
|
||||
```
|
||||
|
||||
|
||||
### Aggregates on Subsets of Associated Records
|
||||
@@ -1549,15 +1659,15 @@ struct AuthorInfo: Decodable, FetchableRecord {
|
||||
// LEFT JOIN book book1 ON book1.authorId = author.id AND book1.kind = 'novel'
|
||||
// LEFT JOIN book book2 ON book2.authorId = author.id AND book2.kind = 'theatrePlay'
|
||||
// GROUP BY author.id
|
||||
let request = Author
|
||||
.annotated(with: Author.books
|
||||
.filter(Column("kind") == "novel")
|
||||
.forKey("novel")
|
||||
.count())
|
||||
.annotated(with: Author.books
|
||||
.filter(Column("kind") == "theatrePlay")
|
||||
.forKey("theatrePlay")
|
||||
.count())
|
||||
let novelCount = Author.books
|
||||
.filter(Column("kind") == "novel")
|
||||
.forKey("novel")
|
||||
.count
|
||||
let theatrePlayCount = Author.books
|
||||
.filter(Column("kind") == "theatrePlay")
|
||||
.forKey("theatrePlay")
|
||||
.count
|
||||
let request = Author.annotated(with: novelCount, theatrePlayCount)
|
||||
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)
|
||||
```
|
||||
|
||||
|
||||
@@ -25,6 +25,11 @@ extension AssociationAggregate {
|
||||
aggregate.alias = name
|
||||
return aggregate
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func aliased(_ key: CodingKey) -> AssociationAggregate<RowDecoder> {
|
||||
return aliased(key.stringValue)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Logical Operators (AND, OR, NOT)
|
||||
|
||||
@@ -110,7 +110,7 @@ extension HasManyAssociation where Origin: TableRecord, Destination: TableRecord
|
||||
/// For example:
|
||||
///
|
||||
/// Team.annotated(with: Team.players.count())
|
||||
public func count() -> AssociationAggregate<Origin> {
|
||||
public var count: AssociationAggregate<Origin> {
|
||||
return makeAggregate(SQLExpressionCountDistinct(Column.rowID)).aliased("\(key)Count")
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ extension HasManyAssociation where Origin: TableRecord, Destination: TableRecord
|
||||
/// Team.having(Team.players.isEmpty())
|
||||
/// Team.having(!Team.players.isEmpty())
|
||||
/// Team.having(Team.players.isEmpty() == false)
|
||||
public func isEmpty() -> AssociationAggregate<Origin> {
|
||||
public var isEmpty: AssociationAggregate<Origin> {
|
||||
return makeAggregate(SQLExpressionIsEmpty(SQLExpressionCountDistinct(Column.rowID)))
|
||||
}
|
||||
|
||||
|
||||
@@ -42,8 +42,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
|
||||
|
||||
// MARK: - Association Aggregates
|
||||
|
||||
/// TODO
|
||||
public func annotated(with aggregate: AssociationAggregate<RowDecoder>) -> QueryInterfaceRequest<RowDecoder> {
|
||||
private func annotated(with aggregate: AssociationAggregate<RowDecoder>) -> QueryInterfaceRequest<RowDecoder> {
|
||||
let (request, expression) = aggregate.prepare(self)
|
||||
if let alias = aggregate.alias {
|
||||
return request.appendingSelection([expression.aliased(alias)])
|
||||
@@ -52,6 +51,18 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func annotated(with aggregates: AssociationAggregate<RowDecoder>...) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return annotated(with: aggregates)
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func annotated(with aggregates: [AssociationAggregate<RowDecoder>]) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return aggregates.reduce(self) { request, aggregate in
|
||||
request.annotated(with: aggregate)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func having(_ aggregate: AssociationAggregate<RowDecoder>) -> QueryInterfaceRequest<RowDecoder> {
|
||||
let (request, expression) = aggregate.prepare(self)
|
||||
|
||||
@@ -30,6 +30,13 @@ extension SQLSpecificExpressible {
|
||||
public func aliased(_ name: String) -> SQLSelectable {
|
||||
return SQLAliasedExpression(sqlExpression, name: name)
|
||||
}
|
||||
|
||||
/// Returns a value that can be used as an argument to QueryInterfaceRequest.select()
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift/#the-query-interface
|
||||
public func aliased(_ key: CodingKey) -> SQLSelectable {
|
||||
return aliased(key.stringValue)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
try dbQueue.read { db in
|
||||
let tableAlias = TableAlias(name: "custom")
|
||||
let request = Team
|
||||
.annotated(with: Team.players.count())
|
||||
.annotated(with: Team.players.count)
|
||||
.joining(required: Team.players.aliased(tableAlias).filter(sql: "custom.score < ?", arguments: [500]))
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
@@ -136,7 +136,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
let request = Team
|
||||
.annotated(with: Team.players.count())
|
||||
.annotated(with: Team.players.count)
|
||||
.orderByPrimaryKey()
|
||||
.asRequest(of: TeamInfo.self)
|
||||
|
||||
@@ -269,9 +269,8 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
try dbQueue.read { db in
|
||||
let request = Team
|
||||
.annotated(with: Team.players.average(Column("score")))
|
||||
.annotated(with: Team.players.count())
|
||||
.annotated(with: Team.players.max(Column("score")))
|
||||
.annotated(with: Team.players.min(Column("score")))
|
||||
.annotated(with: Team.players.count)
|
||||
.annotated(with: Team.players.min(Column("score")), Team.players.max(Column("score")))
|
||||
.annotated(with: Team.players.sum(Column("score")))
|
||||
.orderByPrimaryKey()
|
||||
.asRequest(of: TeamInfo.self)
|
||||
@@ -280,8 +279,8 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
SELECT "team".*, \
|
||||
AVG("player"."score") AS "averagePlayerScore", \
|
||||
COUNT(DISTINCT "player"."rowid") AS "playerCount", \
|
||||
MAX("player"."score") AS "maxPlayerScore", \
|
||||
MIN("player"."score") AS "minPlayerScore", \
|
||||
MAX("player"."score") AS "maxPlayerScore", \
|
||||
SUM("player"."score") AS "playerScoreSum" \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
||||
@@ -355,7 +354,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
let request = Team
|
||||
.annotated(with: Team.customPlayers.count())
|
||||
.annotated(with: Team.customPlayers.count)
|
||||
.orderByPrimaryKey()
|
||||
.asRequest(of: CustomTeamInfo.self)
|
||||
|
||||
@@ -488,9 +487,8 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
try dbQueue.read { db in
|
||||
let request = Team
|
||||
.annotated(with: Team.customPlayers.average(Column("score")))
|
||||
.annotated(with: Team.customPlayers.count())
|
||||
.annotated(with: Team.customPlayers.max(Column("score")))
|
||||
.annotated(with: Team.customPlayers.min(Column("score")))
|
||||
.annotated(with: Team.customPlayers.count)
|
||||
.annotated(with: Team.customPlayers.min(Column("score")), Team.customPlayers.max(Column("score")))
|
||||
.annotated(with: Team.customPlayers.sum(Column("score")))
|
||||
.orderByPrimaryKey()
|
||||
.asRequest(of: CustomTeamInfo.self)
|
||||
@@ -499,8 +497,8 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
SELECT "team".*, \
|
||||
AVG("player"."score") AS "averageCustomScore", \
|
||||
COUNT(DISTINCT "player"."rowid") AS "customCount", \
|
||||
MAX("player"."score") AS "maxCustomScore", \
|
||||
MIN("player"."score") AS "minCustomScore", \
|
||||
MAX("player"."score") AS "maxCustomScore", \
|
||||
SUM("player"."score") AS "customScoreSum" \
|
||||
FROM "team" \
|
||||
LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \
|
||||
@@ -542,7 +540,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
try dbQueue.read { db in
|
||||
let request = Team
|
||||
.annotated(with: Team.players.average(Column("score")).aliased("a1"))
|
||||
.annotated(with: Team.players.count().aliased("a2"))
|
||||
.annotated(with: Team.players.count.aliased("a2"))
|
||||
.annotated(with: Team.players.max(Column("score")).aliased("a3"))
|
||||
.annotated(with: Team.players.min(Column("score")).aliased("a4"))
|
||||
.annotated(with: Team.players.sum(Column("score")).aliased("a5"))
|
||||
@@ -591,8 +589,8 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
let request = Team
|
||||
.annotated(with: Team.players.filter(Column("score") < 500).forKey("lowPlayer").count())
|
||||
.annotated(with: Team.players.filter(Column("score") >= 500).forKey("highPlayer").count())
|
||||
.annotated(with: Team.players.filter(Column("score") < 500).forKey("lowPlayer").count)
|
||||
.annotated(with: Team.players.filter(Column("score") >= 500).forKey("highPlayer").count)
|
||||
.orderByPrimaryKey()
|
||||
.asRequest(of: TeamInfo.self)
|
||||
|
||||
@@ -631,7 +629,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.having(Team.players.isEmpty())
|
||||
let request = Team.having(Team.players.isEmpty)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -642,7 +640,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(!Team.players.isEmpty())
|
||||
let request = Team.having(!Team.players.isEmpty)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -653,7 +651,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(Team.players.isEmpty() == false)
|
||||
let request = Team.having(Team.players.isEmpty == false)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -664,7 +662,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(Team.players.isEmpty() == true)
|
||||
let request = Team.having(Team.players.isEmpty == true)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -681,7 +679,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.having(Team.players.count() == 2)
|
||||
let request = Team.having(Team.players.count == 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -692,7 +690,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(2 == Team.players.count())
|
||||
let request = Team.having(2 == Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -703,7 +701,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(Team.players.count() == Team.awards.count())
|
||||
let request = Team.having(Team.players.count == Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -721,7 +719,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.having(Team.players.count() != 2)
|
||||
let request = Team.having(Team.players.count != 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -732,7 +730,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(2 != Team.players.count())
|
||||
let request = Team.having(2 != Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -743,7 +741,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(Team.players.count() != Team.awards.count())
|
||||
let request = Team.having(Team.players.count != Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -761,7 +759,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.having(Team.players.count() >= 2)
|
||||
let request = Team.having(Team.players.count >= 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -772,7 +770,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(2 >= Team.players.count())
|
||||
let request = Team.having(2 >= Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -783,7 +781,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(Team.players.count() >= Team.awards.count())
|
||||
let request = Team.having(Team.players.count >= Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -801,7 +799,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.having(Team.players.count() > 2)
|
||||
let request = Team.having(Team.players.count > 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -812,7 +810,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(2 > Team.players.count())
|
||||
let request = Team.having(2 > Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -823,7 +821,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(Team.players.count() > Team.awards.count())
|
||||
let request = Team.having(Team.players.count > Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -841,7 +839,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.having(Team.players.count() <= 2)
|
||||
let request = Team.having(Team.players.count <= 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -852,7 +850,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(2 <= Team.players.count())
|
||||
let request = Team.having(2 <= Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -863,7 +861,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(Team.players.count() <= Team.awards.count())
|
||||
let request = Team.having(Team.players.count <= Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -881,7 +879,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.having(Team.players.count() < 2)
|
||||
let request = Team.having(Team.players.count < 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -892,7 +890,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(2 < Team.players.count())
|
||||
let request = Team.having(2 < Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -903,7 +901,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(Team.players.count() < Team.awards.count())
|
||||
let request = Team.having(Team.players.count < Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -921,7 +919,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.having(Team.players.isEmpty() && Team.awards.isEmpty())
|
||||
let request = Team.having(Team.players.isEmpty && Team.awards.isEmpty)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -933,7 +931,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(Team.players.isEmpty() || Team.awards.isEmpty())
|
||||
let request = Team.having(Team.players.isEmpty || Team.awards.isEmpty)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -945,7 +943,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.having(!(Team.players.isEmpty() || Team.awards.isEmpty()))
|
||||
let request = Team.having(!(Team.players.isEmpty || Team.awards.isEmpty))
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".* \
|
||||
@@ -963,7 +961,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.annotated(with: -Team.players.count())
|
||||
let request = Team.annotated(with: -Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, -COUNT(DISTINCT "player"."rowid") \
|
||||
@@ -979,7 +977,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.annotated(with: Team.players.count() + 2)
|
||||
let request = Team.annotated(with: Team.players.count + 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (COUNT(DISTINCT "player"."rowid") + 2) \
|
||||
@@ -989,7 +987,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.annotated(with: 2 + Team.players.count())
|
||||
let request = Team.annotated(with: 2 + Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (2 + COUNT(DISTINCT "player"."rowid")) \
|
||||
@@ -999,7 +997,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.annotated(with: Team.players.count() + Team.awards.count())
|
||||
let request = Team.annotated(with: Team.players.count + Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (COUNT(DISTINCT "player"."rowid") + COUNT(DISTINCT "award"."rowid")) \
|
||||
@@ -1016,7 +1014,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.annotated(with: Team.players.count() - 2)
|
||||
let request = Team.annotated(with: Team.players.count - 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (COUNT(DISTINCT "player"."rowid") - 2) \
|
||||
@@ -1026,7 +1024,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.annotated(with: 2 - Team.players.count())
|
||||
let request = Team.annotated(with: 2 - Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (2 - COUNT(DISTINCT "player"."rowid")) \
|
||||
@@ -1036,7 +1034,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.annotated(with: Team.players.count() - Team.awards.count())
|
||||
let request = Team.annotated(with: Team.players.count - Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (COUNT(DISTINCT "player"."rowid") - COUNT(DISTINCT "award"."rowid")) \
|
||||
@@ -1053,7 +1051,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.annotated(with: Team.players.count() * 2)
|
||||
let request = Team.annotated(with: Team.players.count * 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (COUNT(DISTINCT "player"."rowid") * 2) \
|
||||
@@ -1063,7 +1061,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.annotated(with: 2 * Team.players.count())
|
||||
let request = Team.annotated(with: 2 * Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (2 * COUNT(DISTINCT "player"."rowid")) \
|
||||
@@ -1073,7 +1071,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.annotated(with: Team.players.count() * Team.awards.count())
|
||||
let request = Team.annotated(with: Team.players.count * Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (COUNT(DISTINCT "player"."rowid") * COUNT(DISTINCT "award"."rowid")) \
|
||||
@@ -1090,7 +1088,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
do {
|
||||
let request = Team.annotated(with: Team.players.count() / 2)
|
||||
let request = Team.annotated(with: Team.players.count / 2)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (COUNT(DISTINCT "player"."rowid") / 2) \
|
||||
@@ -1100,7 +1098,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.annotated(with: 2 / Team.players.count())
|
||||
let request = Team.annotated(with: 2 / Team.players.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (2 / COUNT(DISTINCT "player"."rowid")) \
|
||||
@@ -1110,7 +1108,7 @@ class AssociationAggregateTests: GRDBTestCase {
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let request = Team.annotated(with: Team.players.count() / Team.awards.count())
|
||||
let request = Team.annotated(with: Team.players.count / Team.awards.count)
|
||||
|
||||
try assertEqualSQL(db, request, """
|
||||
SELECT "team".*, (COUNT(DISTINCT "player"."rowid") / COUNT(DISTINCT "award"."rowid")) \
|
||||
|
||||
Reference in new issue
Block a user