From 35ed13c94036e6e830260ddb84afa748386ef2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 6 Oct 2018 14:15:35 +0200 Subject: [PATCH] Test for annotations in HAVING clauses --- .../Association/Annotation.swift | 45 +++++- .../Association/HasManyAssociation.swift | 31 ++-- .../QueryInterfaceRequest+Association.swift | 25 +++- Tests/GRDBTests/AnnotationTests.swift | 137 +++++++++++++++--- 4 files changed, 202 insertions(+), 36 deletions(-) diff --git a/GRDB/QueryInterface/Association/Annotation.swift b/GRDB/QueryInterface/Association/Annotation.swift index ab377b47b..679704758 100644 --- a/GRDB/QueryInterface/Association/Annotation.swift +++ b/GRDB/QueryInterface/Association/Annotation.swift @@ -12,5 +12,48 @@ import Foundation public struct Annotation { let association: A let expression: SQLExpression - var alias: String + var alias: String? + + init(association: A, expression: SQLExpression) { + self.association = association + self.expression = expression + } +} + +extension Annotation { + func aliased(_ name: String) -> Annotation { + var annotation = self + annotation.alias = name + return annotation + } +} + +public prefix func ! (annotation: Annotation) -> Annotation { + return Annotation( + association: annotation.association, + expression: !annotation.expression) +} + +public func == (lhs: Annotation, rhs: SQLExpressible?) -> Annotation { + return Annotation( + association: lhs.association, + expression: lhs.expression == rhs) +} + +public func == (lhs: SQLExpressible?, rhs: Annotation) -> Annotation { + return Annotation( + association: rhs.association, + expression: lhs == rhs.expression) +} + +public func != (lhs: Annotation, rhs: SQLExpressible?) -> Annotation { + return Annotation( + association: lhs.association, + expression: lhs.expression != rhs) +} + +public func != (lhs: SQLExpressible?, rhs: Annotation) -> Annotation { + return Annotation( + association: rhs.association, + expression: lhs != rhs.expression) } diff --git a/GRDB/QueryInterface/Association/HasManyAssociation.swift b/GRDB/QueryInterface/Association/HasManyAssociation.swift index 2c4a7b073..bb16ff69e 100644 --- a/GRDB/QueryInterface/Association/HasManyAssociation.swift +++ b/GRDB/QueryInterface/Association/HasManyAssociation.swift @@ -111,8 +111,19 @@ extension HasManyAssociation where Destination: TableRecord { public func count(aliased name: String) -> Annotation { return Annotation( association: self, - expression: SQLExpressionCountDistinct(Column.rowID), - alias: name) + expression: SQLExpressionCountDistinct(Column.rowID)) + .aliased(name) + } + + /// An annotation that is true if there exists no associated records. + /// + /// For example: + /// + /// Team.having(Team.players.isEmpty()) + /// Team.having(!Team.players.isEmpty()) + /// Team.having(Team.players.isEmpty() == false) + public func isEmpty() -> Annotation { + return count() == 0 } /// An annotation that evaluates to the average value of the given column @@ -129,8 +140,8 @@ extension HasManyAssociation where Destination: TableRecord { public func average(_ value: SQLSpecificExpressible, aliased name: String) -> Annotation { return Annotation( association: self, - expression: SQLExpressionFunction(.avg, arguments: value), - alias: name) + expression: SQLExpressionFunction(.avg, arguments: value)) + .aliased(name) } /// An annotation that evaluates to the maximum value of the given column @@ -147,8 +158,8 @@ extension HasManyAssociation where Destination: TableRecord { public func max(_ value: SQLSpecificExpressible, aliased name: String) -> Annotation { return Annotation( association: self, - expression: SQLExpressionFunction(.max, arguments: value), - alias: name) + expression: SQLExpressionFunction(.max, arguments: value)) + .aliased(name) } /// An annotation that evaluates to the minimum value of the given column @@ -165,8 +176,8 @@ extension HasManyAssociation where Destination: TableRecord { public func min(_ value: SQLSpecificExpressible, aliased name: String) -> Annotation { return Annotation( association: self, - expression: SQLExpressionFunction(.min, arguments: value), - alias: name) + expression: SQLExpressionFunction(.min, arguments: value)) + .aliased(name) } /// An annotation that evaluates to the sum of the given column @@ -184,8 +195,8 @@ extension HasManyAssociation where Destination: TableRecord { public func sum(_ value: SQLSpecificExpressible, aliased name: String) -> Annotation { return Annotation( association: self, - expression: SQLExpressionFunction(.sum, arguments: value), - alias: name) + expression: SQLExpressionFunction(.sum, arguments: value)) + .aliased(name) } } diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index 6846a720c..2931ed91b 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -48,7 +48,25 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { // LEFT JOIN book ON ... // GROUP BY author.id let tableAlias = TableAlias() - return appendingSelection([tableAlias[annotation.expression].aliased(annotation.alias)]) + let selectable: SQLSelectable + if let alias = annotation.alias { + selectable = tableAlias[annotation.expression].aliased(alias) + } else { + selectable = tableAlias[annotation.expression] + } + return appendingSelection([selectable]) + .joining(optional: annotation.association.aliased(tableAlias)) + .groupByPrimaryKey() + } + + /// TODO + public func having(_ annotation: Annotation) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { + // SELECT author.* + // LEFT JOIN book ON ... + // GROUP BY author.id + // HAVING COUNT(book.rowID) > 0 + let tableAlias = TableAlias() + return having(tableAlias[annotation.expression]) .joining(optional: annotation.association.aliased(tableAlias)) .groupByPrimaryKey() } @@ -111,4 +129,9 @@ extension TableRecord { public static func annotated(with annotation: Annotation) -> QueryInterfaceRequest where A.OriginRowDecoder == Self { return all().annotated(with: annotation) } + + /// TODO + public static func having(_ annotation: Annotation) -> QueryInterfaceRequest where A.OriginRowDecoder == Self { + return all().having(annotation) + } } diff --git a/Tests/GRDBTests/AnnotationTests.swift b/Tests/GRDBTests/AnnotationTests.swift index 9d0d2c41b..ad4514f26 100644 --- a/Tests/GRDBTests/AnnotationTests.swift +++ b/Tests/GRDBTests/AnnotationTests.swift @@ -65,7 +65,7 @@ class AnnotationTests: GRDBTestCase { } } - func testDefaultAvg() throws { + func testAnnotatedWithDefaultAverage() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -98,7 +98,7 @@ class AnnotationTests: GRDBTestCase { } } - func testDefaultCount() throws { + func testAnnotatedWithDefaultCount() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -131,7 +131,7 @@ class AnnotationTests: GRDBTestCase { } } - func testDefaultMax() throws { + func testAnnotatedWithDefaultMax() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -164,7 +164,7 @@ class AnnotationTests: GRDBTestCase { } } - func testDefaultMin() throws { + func testAnnotatedWithDefaultMin() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -197,7 +197,7 @@ class AnnotationTests: GRDBTestCase { } } - func testDefaultSum() throws { + func testAnnotatedWithDefaultSum() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -230,7 +230,7 @@ class AnnotationTests: GRDBTestCase { } } - func testDefaultMultipleAnnotations() throws { + func testAnnotatedWithMultipleDefaultAnnotations() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -284,7 +284,7 @@ class AnnotationTests: GRDBTestCase { } } - func testCustomAvg() throws { + func testAnnotatedWithCustomAverage() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -317,7 +317,7 @@ class AnnotationTests: GRDBTestCase { } } - func testCustomCount() throws { + func testAnnotatedWithCustomCount() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -350,7 +350,7 @@ class AnnotationTests: GRDBTestCase { } } - func testCustomMax() throws { + func testAnnotatedWithCustomMax() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -383,7 +383,7 @@ class AnnotationTests: GRDBTestCase { } } - func testCustomMin() throws { + func testAnnotatedWithCustomMin() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -416,7 +416,7 @@ class AnnotationTests: GRDBTestCase { } } - func testCustomSum() throws { + func testAnnotatedWithCustomSum() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -449,7 +449,7 @@ class AnnotationTests: GRDBTestCase { } } - func testCustomMultipleAnnotations() throws { + func testAnnotatedWithMultipleCustomAnnotations() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -503,7 +503,7 @@ class AnnotationTests: GRDBTestCase { } } - func testAnnotationAlias() throws { + func testAnnotatedWithAnnotationAlias() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -512,8 +512,6 @@ class AnnotationTests: GRDBTestCase { .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")) - .orderByPrimaryKey() - .asRequest(of: CustomTeamInfo.self) try assertEqualSQL(db, request, """ SELECT "team".*, \ @@ -524,13 +522,12 @@ class AnnotationTests: GRDBTestCase { SUM("player"."score") AS "a5" \ FROM "team" \ LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \ - GROUP BY "team"."id" \ - ORDER BY "team"."id" + GROUP BY "team"."id" """) } } - func testAnnotationExpression() throws { + func testAnnotatedWithAnnotationExpression() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.read { db in let request = Team @@ -538,9 +535,6 @@ class AnnotationTests: GRDBTestCase { .annotated(with: Team.players.max(Column("score") * 10, aliased: "a3")) .annotated(with: Team.players.min(-Column("score"), aliased: "a4")) .annotated(with: Team.players.sum(Column("score") * Column("score"), aliased: "a5")) - .orderByPrimaryKey() - .asRequest(of: CustomTeamInfo.self) - try assertEqualSQL(db, request, """ SELECT "team".*, \ AVG(("player"."score" * "player"."score")) AS "a1", \ @@ -549,13 +543,12 @@ class AnnotationTests: GRDBTestCase { SUM(("player"."score" * "player"."score")) AS "a5" \ FROM "team" \ LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \ - GROUP BY "team"."id" \ - ORDER BY "team"."id" + GROUP BY "team"."id" """) } } - func testMultipleCount() throws { + func testAnnotatedWithMultipleCount() throws { struct TeamInfo: Decodable, FetchableRecord { var team: Team var lowPlayerCount: Int @@ -599,4 +592,100 @@ class AnnotationTests: GRDBTestCase { XCTAssertEqual(teamInfos[2].highPlayerCount, 0) } } + + func testHavingIsEmpty() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + do { + let request = Team.having(Team.players.isEmpty()) + + try assertEqualSQL(db, request, """ + SELECT "team".* \ + FROM "team" \ + LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \ + GROUP BY "team"."id" \ + HAVING (COUNT(DISTINCT "player"."rowid") = 0) + """) + } + do { + let request = Team.having(!Team.players.isEmpty()) + + try assertEqualSQL(db, request, """ + SELECT "team".* \ + FROM "team" \ + LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \ + GROUP BY "team"."id" \ + HAVING (COUNT(DISTINCT "player"."rowid") <> 0) + """) + } + do { + let request = Team.having(Team.players.isEmpty() == false) + + // TODO: this is not nice at all + try assertEqualSQL(db, request, """ + SELECT "team".* \ + FROM "team" \ + LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \ + GROUP BY "team"."id" \ + HAVING ((COUNT(DISTINCT "player"."rowid") = 0) = 0) + """) + } + } + } + + func testHavingEqual() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + do { + let request = Team.having(Team.players.count() == 2) + + try assertEqualSQL(db, request, """ + SELECT "team".* \ + FROM "team" \ + LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \ + GROUP BY "team"."id" \ + HAVING (COUNT(DISTINCT "player"."rowid") = 2) + """) + } + do { + let request = Team.having(2 == Team.players.count()) + + try assertEqualSQL(db, request, """ + SELECT "team".* \ + FROM "team" \ + LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \ + GROUP BY "team"."id" \ + HAVING (2 = COUNT(DISTINCT "player"."rowid")) + """) + } + } + } + + func testHavingNotEqual() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.read { db in + do { + let request = Team.having(Team.players.count() != 2) + + try assertEqualSQL(db, request, """ + SELECT "team".* \ + FROM "team" \ + LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \ + GROUP BY "team"."id" \ + HAVING (COUNT(DISTINCT "player"."rowid") <> 2) + """) + } + do { + let request = Team.having(2 != Team.players.count()) + + try assertEqualSQL(db, request, """ + SELECT "team".* \ + FROM "team" \ + LEFT JOIN "player" ON ("player"."teamId" = "team"."id") \ + GROUP BY "team"."id" \ + HAVING (2 <> COUNT(DISTINCT "player"."rowid")) + """) + } + } + } }