Test for annotations in HAVING clauses

This commit is contained in:
Gwendal Roué committed 2018-10-06 14:15:35 +02:00
1 parent 1da41c7fe5
commit 35ed13c940
4 files changed
+202 -36

No files matched your search

@@ -12,5 +12,48 @@ import Foundation
public struct Annotation<A: Association> {
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<A> {
var annotation = self
annotation.alias = name
return annotation
}
}
public prefix func ! <A>(annotation: Annotation<A>) -> Annotation<A> {
return Annotation(
association: annotation.association,
expression: !annotation.expression)
}
public func == <A>(lhs: Annotation<A>, rhs: SQLExpressible?) -> Annotation<A> {
return Annotation(
association: lhs.association,
expression: lhs.expression == rhs)
}
public func == <A>(lhs: SQLExpressible?, rhs: Annotation<A>) -> Annotation<A> {
return Annotation(
association: rhs.association,
expression: lhs == rhs.expression)
}
public func != <A>(lhs: Annotation<A>, rhs: SQLExpressible?) -> Annotation<A> {
return Annotation(
association: lhs.association,
expression: lhs.expression != rhs)
}
public func != <A>(lhs: SQLExpressible?, rhs: Annotation<A>) -> Annotation<A> {
return Annotation(
association: rhs.association,
expression: lhs != rhs.expression)
}
@@ -111,8 +111,19 @@ extension HasManyAssociation where Destination: TableRecord {
public func count(aliased name: String) -> Annotation<HasManyAssociation> {
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<HasManyAssociation> {
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<HasManyAssociation> {
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<HasManyAssociation> {
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<HasManyAssociation> {
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<HasManyAssociation> {
return Annotation(
association: self,
expression: SQLExpressionFunction(.sum, arguments: value),
alias: name)
expression: SQLExpressionFunction(.sum, arguments: value))
.aliased(name)
}
}
@@ -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<A: Association>(_ annotation: Annotation<A>) -> QueryInterfaceRequest<T> 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<A: Association>(with annotation: Annotation<A>) -> QueryInterfaceRequest<Self> where A.OriginRowDecoder == Self {
return all().annotated(with: annotation)
}
/// TODO
public static func having<A: Association>(_ annotation: Annotation<A>) -> QueryInterfaceRequest<Self> where A.OriginRowDecoder == Self {
return all().having(annotation)
}
}
+113 -24
View File
@@ -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"))
""")
}
}
}
}