Nicer WHERE, HAVING and ON output

This commit is contained in:
Gwendal Roué committed 2019-05-26 15:32:00 +02:00
1 parent b6b1f9b3e8
commit 680f3ad20c
19 files changed
+152 -158

No files matched your search

@@ -373,7 +373,7 @@ extension QueryInterfaceRequest {
// Prevent information loss
GRDBPrecondition(!query.isDistinct, "Not implemented: join distinct queries")
GRDBPrecondition(query.groupPromise == nil, "Can't join aggregated queries")
GRDBPrecondition(query.havingExpression == nil, "Can't join aggregated queries")
GRDBPrecondition(query.havingExpressions.isEmpty, "Can't join aggregated queries")
GRDBPrecondition(query.limit == nil, "Can't join limited queries")
return query.relation
@@ -275,6 +275,10 @@ struct SQLExpressionAnd : SQLExpression {
}
func expressionSQL(_ context: inout SQLGenerationContext) -> String {
return expressionSQL(&context, wrappedInParenthesis: true)
}
func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) -> String {
guard let first = expressions.first else {
// Ruby [].all? # => true
return true.sqlExpression.expressionSQL(&context)
@@ -283,7 +287,11 @@ struct SQLExpressionAnd : SQLExpression {
return first.expressionSQL(&context)
}
let expressionSQLs = expressions.map { $0.expressionSQL(&context) }
return "(" + expressionSQLs.joined(separator: " AND ") + ")"
if wrappedInParenthesis {
return "(" + expressionSQLs.joined(separator: " AND ") + ")"
} else {
return expressionSQLs.joined(separator: " AND ")
}
}
func qualifiedExpression(with alias: TableAlias) -> SQLExpression {
+7 -8
View File
@@ -6,7 +6,10 @@ struct SQLQuery {
var isDistinct: Bool
var expectsSingleResult: Bool
var groupPromise: DatabasePromise<[SQLExpression]>?
var havingExpression: SQLExpression?
// Having clause is an array of expressions that we'll join with
// SQLExpressionAnd. This gives nicer output in generated SQL:
// `(a AND b AND c)` instead of `((a AND b) AND c)`.
var havingExpressions: [SQLExpression]
var limit: SQLLimit?
init(
@@ -14,14 +17,14 @@ struct SQLQuery {
isDistinct: Bool = false,
expectsSingleResult: Bool = false,
groupPromise: DatabasePromise<[SQLExpression]>? = nil,
havingExpression: SQLExpression? = nil,
havingExpressions: [SQLExpression] = [],
limit: SQLLimit? = nil)
{
self.relation = relation
self.isDistinct = isDistinct
self.expectsSingleResult = expectsSingleResult
self.groupPromise = groupPromise
self.havingExpression = havingExpression
self.havingExpressions = havingExpressions
self.limit = limit
}
}
@@ -59,11 +62,7 @@ extension SQLQuery: SelectionRequest, FilteredRequest, OrderedRequest {
func having(_ predicate: SQLExpressible) -> SQLQuery {
var query = self
if let havingExpression = query.havingExpression {
query.havingExpression = (havingExpression && predicate).sqlExpression
} else {
query.havingExpression = predicate.sqlExpression
}
query.havingExpressions.append(predicate.sqlExpression)
return query
}
+11 -20
View File
@@ -153,7 +153,10 @@ struct SQLRelation {
var source: SQLSource
var selection: [SQLSelectable]
var filterPromise: DatabasePromise<SQLExpression?>
// Filter is an array of expressions that we'll join with SQLExpressionAnd.
// This gives nicer output in generated SQL: `(a AND b AND c)` instead of
// `((a AND b) AND c)`.
var filtersPromise: DatabasePromise<[SQLExpression]>
var ordering: SQLRelation.Ordering
var children: OrderedDictionary<String, Child>
@@ -173,13 +176,13 @@ struct SQLRelation {
init(
source: SQLSource,
selection: [SQLSelectable] = [],
filterPromise: DatabasePromise<SQLExpression?> = DatabasePromise(value: nil),
filtersPromise: DatabasePromise<[SQLExpression]> = DatabasePromise(value: []),
ordering: SQLRelation.Ordering = SQLRelation.Ordering(),
children: OrderedDictionary<String, Child> = [:])
{
self.source = source
self.selection = selection
self.filterPromise = filterPromise
self.filtersPromise = filtersPromise
self.ordering = ordering
self.children = children
}
@@ -200,12 +203,8 @@ extension SQLRelation {
func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> SQLRelation {
var relation = self
relation.filterPromise = relation.filterPromise.flatMap { filter in
if let filter = filter {
return DatabasePromise { try filter && predicate($0) }
} else {
return DatabasePromise { try predicate($0).sqlExpression }
}
relation.filtersPromise = relation.filtersPromise.flatMap { filters in
DatabasePromise { try filters + [predicate($0).sqlExpression] }
}
return relation
}
@@ -661,16 +660,8 @@ extension SQLRelation {
return nil
}
let mergedFilterPromise: DatabasePromise<SQLExpression?> = filterPromise.flatMap { expression in
return DatabasePromise { db in
let otherExpression = try other.filterPromise.resolve(db)
let expressions = [expression, otherExpression].compactMap { $0 }
if expressions.isEmpty {
return nil
} else {
return expressions.joined(operator: .and)
}
}
let mergedFiltersPromise: DatabasePromise<[SQLExpression]> = filtersPromise.flatMap { filters in
DatabasePromise { try filters + other.filtersPromise.resolve($0) }
}
var mergedChildren: OrderedDictionary<String, SQLRelation.Child> = [:]
@@ -698,7 +689,7 @@ extension SQLRelation {
return SQLRelation(
source: mergedSource,
selection: mergedSelection,
filterPromise: mergedFilterPromise,
filtersPromise: mergedFiltersPromise,
ordering: mergedOrdering,
children: mergedChildren)
}
@@ -3,7 +3,7 @@ struct SQLQueryGenerator {
fileprivate let relation: SQLQualifiedRelation
private let isDistinct: Bool
private let groupPromise: DatabasePromise<[SQLExpression]>?
private let havingExpression: SQLExpression?
private let havingExpressions: [SQLExpression]
private let limit: SQLLimit?
init(_ query: SQLQuery) {
@@ -36,7 +36,7 @@ struct SQLQueryGenerator {
// `HAVING MAX(year) < 2000` INTO `HAVING MAX(book.year) < 2000`.
let alias = relation.alias
groupPromise = query.groupPromise?.map { [alias] in $0.map { $0.qualifiedExpression(with: alias) } }
havingExpression = query.havingExpression?.qualifiedExpression(with: alias)
havingExpressions = query.havingExpressions.map { $0.qualifiedExpression(with: alias) }
// Preserve other flags
isDistinct = query.isDistinct
@@ -60,16 +60,17 @@ struct SQLQueryGenerator {
sql += try " " + join.sql(db, &context, leftAlias: relation.alias)
}
if let filter = try relation.filterPromise.resolve(db) {
sql += " WHERE " + filter.expressionSQL(&context)
let filters = try relation.filtersPromise.resolve(db)
if filters.isEmpty == false {
sql += " WHERE " + SQLExpressionAnd(filters).expressionSQL(&context, wrappedInParenthesis: false)
}
if let groupExpressions = try groupPromise?.resolve(db), !groupExpressions.isEmpty {
sql += " GROUP BY " + groupExpressions.map { $0.expressionSQL(&context) }.joined(separator: ", ")
}
if let havingExpression = havingExpression {
sql += " HAVING " + havingExpression.expressionSQL(&context)
if havingExpressions.isEmpty == false {
sql += " HAVING " + SQLExpressionAnd(havingExpressions).expressionSQL(&context, wrappedInParenthesis: false)
}
let orderings = try relation.ordering.resolve(db)
@@ -106,13 +107,9 @@ struct SQLQueryGenerator {
return databaseRegion
}
// Give up unless there is a where clause
guard let filter = try relation.filterPromise.resolve(db) else {
return databaseRegion
}
// The filter knows better
guard let rowIds = filter.matchedRowIds(rowIdName: primaryKeyInfo.rowIDColumn) else {
// The filters knows better
let filters = try relation.filtersPromise.resolve(db)
guard let rowIds = SQLExpressionAnd(filters).matchedRowIds(rowIdName: primaryKeyInfo.rowIDColumn) else {
return databaseRegion
}
@@ -127,7 +124,7 @@ struct SQLQueryGenerator {
fatalError("Can't delete query with GROUP BY clause")
}
guard havingExpression == nil else {
guard havingExpressions.isEmpty else {
// Programmer error
fatalError("Can't delete query with HAVING clause")
}
@@ -146,8 +143,9 @@ struct SQLQueryGenerator {
var sql = try "DELETE FROM " + relation.source.sql(db, &context)
if let filter = try relation.filterPromise.resolve(db) {
sql += " WHERE " + filter.expressionSQL(&context)
let filters = try relation.filtersPromise.resolve(db)
if filters.isEmpty == false {
sql += " WHERE " + SQLExpressionAnd(filters).expressionSQL(&context, wrappedInParenthesis: false)
}
if let limit = limit {
@@ -267,8 +265,8 @@ private struct SQLQualifiedRelation {
///
/// SELECT ... FROM ... AS ... JOIN ... WHERE ... ORDER BY ...
/// |
/// • filterPromise
let filterPromise: DatabasePromise<SQLExpression?>
/// • filtersPromise
let filtersPromise: DatabasePromise<[SQLExpression]>
/// The ordering, not including ordering of joined relations
private let ownOrdering: SQLRelation.Ordering
@@ -321,7 +319,7 @@ private struct SQLQualifiedRelation {
relation: SQLQualifiedRelation(child.relation))
}
ownSelection = relation.selection.map { $0.qualifiedSelectable(with: alias) }
filterPromise = relation.filterPromise.map { [alias] in $0?.qualifiedExpression(with: alias) }
filtersPromise = relation.filtersPromise.map { [alias] in $0.map { $0.qualifiedExpression(with: alias) } }
ownOrdering = relation.ordering.qualified(with: alias)
}
@@ -464,12 +462,10 @@ private struct SQLQualifiedJoin {
sql += try " " + relation.source.sql(db, &context)
let rightAlias = relation.alias
let filters = try [
condition.joinExpression(db, leftAlias: leftAlias, rightAlias: rightAlias),
relation.filterPromise.resolve(db)
].compactMap { $0 }
if !filters.isEmpty {
sql += " ON " + filters.joined(operator: .and).expressionSQL(&context)
var filters = try [condition.joinExpression(db, leftAlias: leftAlias, rightAlias: rightAlias)]
try filters.append(contentsOf: relation.filtersPromise.resolve(db))
if filters.isEmpty == false {
sql += " ON " + SQLExpressionAnd(filters).expressionSQL(&context, wrappedInParenthesis: false)
}
for (_, join) in relation.joins {
@@ -94,7 +94,7 @@ class AssociationAggregateTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "team".*, COUNT(DISTINCT "custom"."rowid") AS "playerCount" \
FROM "team" \
JOIN "player" "custom" ON (("custom"."teamId" = "team"."id") AND (custom.score < 500)) \
JOIN "player" "custom" ON ("custom"."teamId" = "team"."id") AND (custom.score < 500) \
GROUP BY "team"."id"
""")
}
@@ -717,8 +717,8 @@ class AssociationAggregateTests: GRDBTestCase {
COUNT(DISTINCT "player1"."rowid") AS "lowPlayerCount", \
COUNT(DISTINCT "player2"."rowid") AS "highPlayerCount" \
FROM "team" \
LEFT JOIN "player" "player1" ON (("player1"."teamId" = "team"."id") AND ("player1"."score" < 500)) \
LEFT JOIN "player" "player2" ON (("player2"."teamId" = "team"."id") AND ("player2"."score" >= 500)) \
LEFT JOIN "player" "player1" ON ("player1"."teamId" = "team"."id") AND ("player1"."score" < 500) \
LEFT JOIN "player" "player2" ON ("player2"."teamId" = "team"."id") AND ("player2"."score" >= 500) \
GROUP BY "team"."id" \
ORDER BY "team"."id"
""")
@@ -130,7 +130,7 @@ class AssociationBelongsToDecodableRecordTests: GRDBTestCase {
XCTAssertEqual(lastSQLQuery, """
SELECT "players".*, "teams"."name", "teams"."id" \
FROM "players" \
JOIN "teams" ON (("teams"."id" = "players"."teamId") AND ("teams"."name" = 'Reds')) \
JOIN "teams" ON ("teams"."id" = "players"."teamId") AND ("teams"."name" = 'Reds') \
ORDER BY "teams"."name", "players"."name"
""")
XCTAssertEqual(records.count, 1)
@@ -113,7 +113,7 @@ class AssociationBelongsToSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."id" = "a"."bid") AND ("b"."name" IS NOT NULL))
JOIN "b" ON ("b"."id" = "a"."bid") AND ("b"."name" IS NOT NULL)
""")
}
do {
@@ -121,7 +121,7 @@ class AssociationBelongsToSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."id" = "a"."bid") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "a"."bid") AND ("b"."id" = 1)
""")
}
do {
@@ -129,7 +129,7 @@ class AssociationBelongsToSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."id" = "a"."bid") AND ("b"."id" IN (1, 2, 3)))
JOIN "b" ON ("b"."id" = "a"."bid") AND ("b"."id" IN (1, 2, 3))
""")
}
do {
@@ -137,7 +137,7 @@ class AssociationBelongsToSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."id" = "a"."bid") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "a"."bid") AND ("b"."id" = 1)
""")
}
do {
@@ -145,7 +145,7 @@ class AssociationBelongsToSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."id" = "a"."bid") AND (("b"."id" = 1) OR ("b"."id" = 2)))
JOIN "b" ON ("b"."id" = "a"."bid") AND (("b"."id" = 1) OR ("b"."id" = 2))
""")
}
do {
@@ -156,7 +156,7 @@ class AssociationBelongsToSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "customB".* \
FROM "a" \
JOIN "b" "customB" ON (("customB"."id" = "a"."bid") AND (customB.name = 'foo'))
JOIN "b" "customB" ON ("customB"."id" = "a"."bid") AND (customB.name = 'foo')
""")
}
}
@@ -92,13 +92,13 @@ class AssociationChainSQLTests: GRDBTestCase {
SELECT "c".*, "d".* \
FROM "c" \
JOIN "d" ON ("d"."id" = "c"."did") \
JOIN "b" ON (("b"."id" = "c"."bid") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bid") AND ("b"."id" = 1)
""")
try assertEqualSQL(db, A().request(for: A.c.including(optional: C.d)), """
SELECT "c".*, "d".* \
FROM "c" \
LEFT JOIN "d" ON ("d"."id" = "c"."did") \
JOIN "b" ON (("b"."id" = "c"."bid") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bid") AND ("b"."id" = 1)
""")
}
}
@@ -335,13 +335,13 @@ class AssociationChainSQLTests: GRDBTestCase {
SELECT "c".* \
FROM "c" \
JOIN "d" ON ("d"."id" = "c"."did") \
JOIN "b" ON (("b"."id" = "c"."bid") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bid") AND ("b"."id" = 1)
""")
try assertEqualSQL(db, A().request(for: A.c.joining(optional: C.d)), """
SELECT "c".* \
FROM "c" \
LEFT JOIN "d" ON ("d"."id" = "c"."did") \
JOIN "b" ON (("b"."id" = "c"."bid") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bid") AND ("b"."id" = 1)
""")
}
}
@@ -44,7 +44,7 @@ class AssociationHasManyThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.c), """
SELECT "c".* FROM "c" \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."id" = 1)
""")
}
}
@@ -84,7 +84,7 @@ class AssociationHasManyThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.c), """
SELECT "c".* FROM "c" \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."aId" = 1)
""")
}
}
@@ -124,7 +124,7 @@ class AssociationHasManyThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.c), """
SELECT "c".* FROM "c" \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."aId" = 1)
""")
}
}
@@ -164,7 +164,7 @@ class AssociationHasManyThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.c), """
SELECT "c".* FROM "c" \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."aId" = 1)
""")
}
}
@@ -113,7 +113,7 @@ class AssociationHasOneSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."aid" = "a"."id") AND ("b"."name" IS NOT NULL))
JOIN "b" ON ("b"."aid" = "a"."id") AND ("b"."name" IS NOT NULL)
""")
}
do {
@@ -121,7 +121,7 @@ class AssociationHasOneSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."aid" = "a"."id") AND ("b"."id" = 1))
JOIN "b" ON ("b"."aid" = "a"."id") AND ("b"."id" = 1)
""")
}
do {
@@ -129,7 +129,7 @@ class AssociationHasOneSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."aid" = "a"."id") AND ("b"."id" IN (1, 2, 3)))
JOIN "b" ON ("b"."aid" = "a"."id") AND ("b"."id" IN (1, 2, 3))
""")
}
do {
@@ -137,7 +137,7 @@ class AssociationHasOneSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."aid" = "a"."id") AND ("b"."id" = 1))
JOIN "b" ON ("b"."aid" = "a"."id") AND ("b"."id" = 1)
""")
}
do {
@@ -145,7 +145,7 @@ class AssociationHasOneSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON (("b"."aid" = "a"."id") AND (("b"."id" = 1) OR ("b"."id" = 2)))
JOIN "b" ON ("b"."aid" = "a"."id") AND (("b"."id" = 1) OR ("b"."id" = 2))
""")
}
do {
@@ -156,7 +156,7 @@ class AssociationHasOneSQLDerivationTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".*, "customB".* \
FROM "a" \
JOIN "b" "customB" ON (("customB"."aid" = "a"."id") AND (customB.name = 'foo'))
JOIN "b" "customB" ON ("customB"."aid" = "a"."id") AND (customB.name = 'foo')
""")
}
}
@@ -127,7 +127,7 @@ class AssociationHasOneThroughSQLDerivationTests: GRDBTestCase {
SELECT "a".*, "c".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bId") \
JOIN "c" ON (("c"."id" = "b"."cId") AND ("c"."name" IS NOT NULL))
JOIN "c" ON ("c"."id" = "b"."cId") AND ("c"."name" IS NOT NULL)
""")
}
do {
@@ -136,7 +136,7 @@ class AssociationHasOneThroughSQLDerivationTests: GRDBTestCase {
SELECT "a".*, "c".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bId") \
JOIN "c" ON (("c"."id" = "b"."cId") AND ("c"."id" = 1))
JOIN "c" ON ("c"."id" = "b"."cId") AND ("c"."id" = 1)
""")
}
do {
@@ -145,7 +145,7 @@ class AssociationHasOneThroughSQLDerivationTests: GRDBTestCase {
SELECT "a".*, "c".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bId") \
JOIN "c" ON (("c"."id" = "b"."cId") AND ("c"."id" IN (1, 2, 3)))
JOIN "c" ON ("c"."id" = "b"."cId") AND ("c"."id" IN (1, 2, 3))
""")
}
do {
@@ -154,7 +154,7 @@ class AssociationHasOneThroughSQLDerivationTests: GRDBTestCase {
SELECT "a".*, "c".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bId") \
JOIN "c" ON (("c"."id" = "b"."cId") AND ("c"."id" = 1))
JOIN "c" ON ("c"."id" = "b"."cId") AND ("c"."id" = 1)
""")
}
do {
@@ -163,7 +163,7 @@ class AssociationHasOneThroughSQLDerivationTests: GRDBTestCase {
SELECT "a".*, "c".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bId") \
JOIN "c" ON (("c"."id" = "b"."cId") AND (("c"."id" = 1) OR ("c"."id" = 2)))
JOIN "c" ON ("c"."id" = "b"."cId") AND (("c"."id" = 1) OR ("c"."id" = 2))
""")
}
do {
@@ -175,7 +175,7 @@ class AssociationHasOneThroughSQLDerivationTests: GRDBTestCase {
SELECT "a".*, "customC".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bId") \
JOIN "c" "customC" ON (("customC"."id" = "b"."cId") AND (customC.name = 'foo'))
JOIN "c" "customC" ON ("customC"."id" = "b"."cId") AND (customC.name = 'foo')
""")
}
}
@@ -46,7 +46,7 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.c), """
SELECT "c".* FROM "c" \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."id" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."id" = 1)
""")
}
}
@@ -86,7 +86,7 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.c), """
SELECT "c".* FROM "c" \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."id" = 1)
""")
}
}
@@ -126,7 +126,7 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.c), """
SELECT "c".* FROM "c" \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."aId" = 1)
""")
}
}
@@ -166,7 +166,7 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.c), """
SELECT "c".* FROM "c" \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."aId" = 1)
""")
}
}
@@ -365,12 +365,12 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.dThroughC), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."dId" = "d"."id") \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."id" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."id" = 1)
""")
try assertEqualSQL(db, A().request(for: A.dThroughB), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."dId" = "d"."id") \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."id" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."id" = 1)
""")
}
}
@@ -422,12 +422,12 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.dThroughC), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."id" = "d"."cId") \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."id" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."id" = 1)
""")
try assertEqualSQL(db, A().request(for: A.dThroughB), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."id" = "d"."cId") \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."id" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."id" = 1)
""")
}
}
@@ -479,12 +479,12 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.dThroughC), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."dId" = "d"."id") \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."id" = 1)
""")
try assertEqualSQL(db, A().request(for: A.dThroughB), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."dId" = "d"."id") \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."id" = 1)
""")
}
}
@@ -536,12 +536,12 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.dThroughC), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."id" = "d"."cId") \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."id" = 1)
""")
try assertEqualSQL(db, A().request(for: A.dThroughB), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."id" = "d"."cId") \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."id" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."id" = 1)
""")
}
}
@@ -593,12 +593,12 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.dThroughC), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."dId" = "d"."id") \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."aId" = 1)
""")
try assertEqualSQL(db, A().request(for: A.dThroughB), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."dId" = "d"."id") \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."aId" = 1)
""")
}
}
@@ -650,12 +650,12 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.dThroughC), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."id" = "d"."cId") \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."aId" = 1)
""")
try assertEqualSQL(db, A().request(for: A.dThroughB), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."id" = "d"."cId") \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."aId" = 1)
""")
}
}
@@ -707,12 +707,12 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.dThroughC), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."dId" = "d"."id") \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."aId" = 1)
""")
try assertEqualSQL(db, A().request(for: A.dThroughB), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."dId" = "d"."id") \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."aId" = 1)
""")
}
}
@@ -764,12 +764,12 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
try assertEqualSQL(db, A().request(for: A.dThroughC), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."id" = "d"."cId") \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."aId" = 1)
""")
try assertEqualSQL(db, A().request(for: A.dThroughB), """
SELECT "d".* FROM "d" \
JOIN "c" ON ("c"."id" = "d"."cId") \
JOIN "b" ON (("b"."id" = "c"."bId") AND ("b"."aId" = 1))
JOIN "b" ON ("b"."id" = "c"."bId") AND ("b"."aId" = 1)
""")
}
}
@@ -1358,7 +1358,7 @@ class AssociationHasOneThroughSQLTests: GRDBTestCase {
FROM "e" \
JOIN "d" ON ("d"."eId" = "e"."id") \
JOIN "c" ON ("c"."dId" = "d"."id") \
JOIN "b" ON (("b"."cId" = "c"."id") AND ("b"."id" = 1))
JOIN "b" ON ("b"."cId" = "c"."id") AND ("b"."id" = 1)
""")
}
}
@@ -516,7 +516,7 @@ class AssociationParallelSQLTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".* \
FROM "a" \
JOIN "b" ON (("b"."id" = "a"."bid") AND ("b"."id" > 1))
JOIN "b" ON ("b"."id" = "a"."bid") AND ("b"."id" > 1)
""")
}
do {
@@ -526,7 +526,7 @@ class AssociationParallelSQLTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".* \
FROM "a" \
JOIN "b" ON (("b"."id" = "a"."bid") AND ("b"."id" < 3))
JOIN "b" ON ("b"."id" = "a"."bid") AND ("b"."id" < 3)
""")
}
do {
@@ -536,7 +536,7 @@ class AssociationParallelSQLTests: GRDBTestCase {
try assertEqualSQL(db, request, """
SELECT "a".* \
FROM "a" \
JOIN "b" ON (("b"."id" = "a"."bid") AND (("b"."id" > 1) AND ("b"."id" < 3)))
JOIN "b" ON ("b"."id" = "a"."bid") AND ("b"."id" > 1) AND ("b"."id" < 3)
""")
}
}
@@ -142,13 +142,13 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT *, "colb2" AS "grdb_colb2" \
FROM "b" \
WHERE (("colb1" = 4) AND ("colb2" IN (1, 2))) \
WHERE ("colb1" = 4) AND ("colb2" IN (1, 2)) \
ORDER BY "colb1"
""",
"""
SELECT *, "colb2" AS "grdb_colb2" \
FROM "b" \
WHERE (("colb1" <> 4) AND ("colb2" IN (1, 2))) \
WHERE ("colb1" <> 4) AND ("colb2" IN (1, 2)) \
ORDER BY "colb1"
"""])
}
@@ -281,7 +281,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT *, "colc2" AS "grdb_colc2" \
FROM "c" \
WHERE (0 AND ("colc2" IN (1, 2, 3))) \
WHERE 0 AND ("colc2" IN (1, 2, 3)) \
ORDER BY "colc1"
"""])
}
@@ -336,37 +336,37 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT *, "colc2" AS "grdb_colc2" \
FROM "c" \
WHERE (("colc1" > 7) AND ("colc2" IN (1, 2))) \
WHERE ("colc1" > 7) AND ("colc2" IN (1, 2)) \
ORDER BY "colc1"
""",
"""
SELECT *, "cold2" AS "grdb_cold2" \
FROM "d" \
WHERE (("cold1" = 11) AND ("cold2" IN (8, 9))) \
WHERE ("cold1" = 11) AND ("cold2" IN (8, 9)) \
ORDER BY "cold1"
""",
"""
SELECT *, "cold2" AS "grdb_cold2" \
FROM "d" \
WHERE (("cold1" <> 11) AND ("cold2" IN (8, 9))) \
WHERE ("cold1" <> 11) AND ("cold2" IN (8, 9)) \
ORDER BY "cold1"
""",
"""
SELECT *, "colc2" AS "grdb_colc2" \
FROM "c" \
WHERE (("colc1" < 9) AND ("colc2" IN (1, 2))) \
WHERE ("colc1" < 9) AND ("colc2" IN (1, 2)) \
ORDER BY "colc1"
""",
"""
SELECT *, "cold2" AS "grdb_cold2" \
FROM "d" \
WHERE (("cold1" = 11) AND ("cold2" IN (7, 8))) \
WHERE ("cold1" = 11) AND ("cold2" IN (7, 8)) \
ORDER BY "cold1"
""",
"""
SELECT *, "cold2" AS "grdb_cold2" \
FROM "d" \
WHERE (("cold1" <> 11) AND ("cold2" IN (7, 8))) \
WHERE ("cold1" <> 11) AND ("cold2" IN (7, 8)) \
ORDER BY "cold1"
"""])
}
@@ -428,7 +428,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
SELECT "c".*, "c"."colc2" AS "grdb_colc2", "d".* \
FROM "c" \
JOIN "d" ON ("d"."cold2" = "c"."colc1") \
WHERE (0 AND ("c"."colc2" IN (1, 2, 3))) \
WHERE 0 AND ("c"."colc2" IN (1, 2, 3)) \
ORDER BY "c"."colc1", "d"."cold1"
"""])
}
@@ -483,17 +483,17 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "c".*, "c"."colc2" AS "grdb_colc2", "d1".*, "d2".* \
FROM "c" \
LEFT JOIN "d" "d1" ON (("d1"."cold2" = "c"."colc1") AND ("d1"."cold1" = 11)) \
JOIN "d" "d2" ON (("d2"."cold2" = "c"."colc1") AND ("d2"."cold1" <> 11)) \
WHERE (("c"."colc1" > 7) AND ("c"."colc2" IN (1, 2))) \
LEFT JOIN "d" "d1" ON ("d1"."cold2" = "c"."colc1") AND ("d1"."cold1" = 11) \
JOIN "d" "d2" ON ("d2"."cold2" = "c"."colc1") AND ("d2"."cold1" <> 11) \
WHERE ("c"."colc1" > 7) AND ("c"."colc2" IN (1, 2)) \
ORDER BY "c"."colc1", "d1"."cold1", "d2"."cold1"
""",
"""
SELECT "c".*, "c"."colc2" AS "grdb_colc2", "d1".*, "d2".* \
FROM "c" \
LEFT JOIN "d" "d1" ON (("d1"."cold2" = "c"."colc1") AND ("d1"."cold1" = 11)) \
JOIN "d" "d2" ON (("d2"."cold2" = "c"."colc1") AND ("d2"."cold1" <> 11)) \
WHERE (("c"."colc1" < 9) AND ("c"."colc2" IN (1, 2))) \
LEFT JOIN "d" "d1" ON ("d1"."cold2" = "c"."colc1") AND ("d1"."cold1" = 11) \
JOIN "d" "d2" ON ("d2"."cold2" = "c"."colc1") AND ("d2"."cold1" <> 11) \
WHERE ("c"."colc1" < 9) AND ("c"."colc2" IN (1, 2)) \
ORDER BY "c"."colc1", "d1"."cold1", "d2"."cold1"
"""])
}
@@ -522,7 +522,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2, 3))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2, 3)) \
ORDER BY "d"."cold1"
"""])
}
@@ -560,20 +560,20 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND (("c"."colc1" = 8) AND ("c"."colc2" IN (1, 2)))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc1" = 8) AND ("c"."colc2" IN (1, 2)) \
ORDER BY "d"."cold1"
""",
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2)) \
WHERE ("d"."cold1" <> 11) \
ORDER BY "d"."cold1"
""",
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2)) \
WHERE ("d"."cold1" = 11) \
ORDER BY "d"."cold1"
"""])
@@ -601,7 +601,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" JOIN "a" ON (("a"."cola1" = "c"."colc2") AND ("a"."cola1" IN (1, 2))) \
FROM "c" JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola1" IN (1, 2)) \
ORDER BY "c"."colc1"
"""])
}
@@ -637,7 +637,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND ((("a"."cola2" = 'a1') AND ("a"."cola2" <> 'a1')) AND ("a"."cola1" IN (1, 2)))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola2" = 'a1') AND ("a"."cola2" <> 'a1') AND ("a"."cola1" IN (1, 2)) \
ORDER BY "c"."colc1"
"""])
}
@@ -675,13 +675,13 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND ((("a"."cola2" = 'a1') AND ("a"."cola2" <> 'a1')) AND ("a"."cola1" IN (1, 2)))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola2" = 'a1') AND ("a"."cola2" <> 'a1') AND ("a"."cola1" IN (1, 2)) \
ORDER BY "c"."colc1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND ((("a"."cola2" = 'a1') AND ("a"."cola2" <> 'a1')) AND ("a"."cola1" IN (1, 2)))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola2" = 'a1') AND ("a"."cola2" <> 'a1') AND ("a"."cola1" IN (1, 2)) \
ORDER BY "c"."colc1"
"""])
}
@@ -717,13 +717,13 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola2" = 'a1') AND ("a"."cola1" IN (1, 2)))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola2" = 'a1') AND ("a"."cola1" IN (1, 2)) \
ORDER BY "c"."colc1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola2" <> 'a1') AND ("a"."cola1" IN (1, 2)))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola2" <> 'a1') AND ("a"."cola1" IN (1, 2)) \
ORDER BY "c"."colc1"
"""])
}
@@ -751,7 +751,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2, 3))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc2" IN (1, 2, 3)) \
ORDER BY "d"."cold1"
"""])
}
@@ -791,7 +791,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ((("c"."colc1" = 7) AND ("c"."colc1" <> 7)) AND ("c"."colc2" IN (1, 2, 3)))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc1" = 7) AND ("c"."colc1" <> 7) AND ("c"."colc2" IN (1, 2, 3)) \
ORDER BY "d"."cold1"
""",
"""
@@ -833,13 +833,13 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ((("c"."colc1" = 7) AND ("c"."colc1" <> 7)) AND ("c"."colc2" IN (1, 2, 3)))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc1" = 7) AND ("c"."colc1" <> 7) AND ("c"."colc2" IN (1, 2, 3)) \
ORDER BY "d"."cold1"
""",
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND ((("c"."colc1" = 7) AND ("c"."colc1" <> 7)) AND ("c"."colc2" IN (1, 2, 3)))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc1" = 7) AND ("c"."colc1" <> 7) AND ("c"."colc2" IN (1, 2, 3)) \
ORDER BY "d"."cold1"
""",
"""
@@ -879,13 +879,13 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND (("c"."colc1" = 7) AND ("c"."colc2" IN (1, 2, 3)))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc1" = 7) AND ("c"."colc2" IN (1, 2, 3)) \
ORDER BY "d"."cold1"
""",
"""
SELECT "d".*, "c"."colc2" AS "grdb_colc2" \
FROM "d" \
JOIN "c" ON (("c"."colc1" = "d"."cold2") AND (("c"."colc1" <> 7) AND ("c"."colc2" IN (1, 2, 3)))) \
JOIN "c" ON ("c"."colc1" = "d"."cold2") AND ("c"."colc1" <> 7) AND ("c"."colc2" IN (1, 2, 3)) \
ORDER BY "d"."cold1"
""",
"""
@@ -924,7 +924,7 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND ("a"."cola1" IN (1, 2))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola1" IN (1, 2)) \
ORDER BY "c"."colc1"
"""])
}
@@ -970,35 +970,35 @@ class AssociationPrefetchingSQLTests: GRDBTestCase {
"""
SELECT "b".*, "a1".*, "a2".* \
FROM "b" \
LEFT JOIN "a" "a1" ON (("a1"."cola1" = "b"."colb2") AND ("a1"."cola2" = 'a1')) \
LEFT JOIN "a" "a2" ON (("a2"."cola1" = "b"."colb2") AND ("a2"."cola2" = 'a2')) \
LEFT JOIN "a" "a1" ON ("a1"."cola1" = "b"."colb2") AND ("a1"."cola2" = 'a1') \
LEFT JOIN "a" "a2" ON ("a2"."cola1" = "b"."colb2") AND ("a2"."cola2" = 'a2') \
ORDER BY "b"."colb1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola2" = 'a1') AND ("a"."cola1" IN (1, 2)))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola2" = 'a1') AND ("a"."cola1" IN (1, 2)) \
WHERE ("c"."colc1" = 9) \
ORDER BY "c"."colc1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola2" = 'a1') AND ("a"."cola1" IN (1, 2)))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola2" = 'a1') AND ("a"."cola1" IN (1, 2)) \
WHERE ("c"."colc1" <> 9) \
ORDER BY "c"."colc1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola2" = 'a2') AND ("a"."cola1" IN (1, 2)))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola2" = 'a2') AND ("a"."cola1" IN (1, 2)) \
WHERE ("c"."colc1" = 9) \
ORDER BY "c"."colc1"
""",
"""
SELECT "c".*, "a"."cola1" AS "grdb_cola1" \
FROM "c" \
JOIN "a" ON (("a"."cola1" = "c"."colc2") AND (("a"."cola2" = 'a2') AND ("a"."cola1" IN (1, 2)))) \
JOIN "a" ON ("a"."cola1" = "c"."colc2") AND ("a"."cola2" = 'a2') AND ("a"."cola1" IN (1, 2)) \
WHERE ("c"."colc1" <> 9) \
ORDER BY "c"."colc1"
"""])
@@ -53,7 +53,7 @@ class AssociationTableAliasTestsSQLTests : GRDBTestCase {
let expectedSQL = """
SELECT "name" \
FROM "a" \
WHERE (("id" = 1) AND (("name" IS NOT NULL) AND ("name" = 'foo'))) \
WHERE ("id" = 1) AND (("name" IS NOT NULL) AND ("name" = 'foo')) \
GROUP BY "name" \
HAVING "name" \
ORDER BY "name"
@@ -90,7 +90,7 @@ class AssociationTableAliasTestsSQLTests : GRDBTestCase {
let expectedSQL = """
SELECT "customA"."name" \
FROM "a" "customA" \
WHERE (("customA"."id" = 1) AND (("customA"."name" IS NOT NULL) AND ("customA"."name" = 'foo'))) \
WHERE ("customA"."id" = 1) AND (("customA"."name" IS NOT NULL) AND ("customA"."name" = 'foo')) \
GROUP BY "customA"."name" \
HAVING "customA"."name" \
ORDER BY "customA"."name"
@@ -42,7 +42,7 @@ class QueryInterfacePromiseTests: GRDBTestCase {
let sql = """
SELECT "node1".* \
FROM "node" "node1" \
LEFT JOIN "node" "node2" ON (("node2"."id" = "node1"."parentId") AND ("node2"."id" = 2)) \
LEFT JOIN "node" "node2" ON ("node2"."id" = "node1"."parentId") AND ("node2"."id" = 2) \
WHERE ("node1"."id" = 1)
"""
try assertEqualSQL(db, request, sql)
@@ -55,7 +55,7 @@ class QueryInterfacePromiseTests: GRDBTestCase {
let sql = """
SELECT "node1".* \
FROM "node" "node1" \
LEFT JOIN "node" "node2" ON (("node2"."id" = "node1"."parentId") AND ("node2"."id" = 2)) \
LEFT JOIN "node" "node2" ON ("node2"."id" = "node1"."parentId") AND ("node2"."id" = 2) \
WHERE ("node1"."id" = 1)
"""
try assertEqualSQL(db, request, sql)
@@ -122,7 +122,7 @@ class QueryInterfacePromiseTests: GRDBTestCase {
let sql = """
SELECT "node1".* \
FROM "node" "node1" \
LEFT JOIN "node" "node2" ON (("node2"."id" = "node1"."parentId") AND ("node2"."id" = 2)) \
LEFT JOIN "node" "node2" ON ("node2"."id" = "node1"."parentId") AND ("node2"."id" = 2) \
WHERE ("node1"."id" = 1)
"""
try assertEqualSQL(db, request, sql)
@@ -510,12 +510,12 @@ class QueryInterfaceRequestTests: GRDBTestCase {
sql(dbQueue, tableRequest
.filter(sql: "age > :age", arguments: ["age": 20])
.filter(sql: "name = ?", arguments: ["arthur"])),
"SELECT * FROM \"readers\" WHERE ((age > 20) AND (name = 'arthur'))")
"SELECT * FROM \"readers\" WHERE (age > 20) AND (name = 'arthur')")
XCTAssertEqual(
sql(dbQueue, tableRequest
.filter(sql: "age > ?", arguments: [20])
.filter(sql: "name = :name", arguments: ["name": "arthur"])),
"SELECT * FROM \"readers\" WHERE ((age > 20) AND (name = 'arthur'))")
"SELECT * FROM \"readers\" WHERE (age > 20) AND (name = 'arthur')")
}
func testFilter() throws {
@@ -529,7 +529,7 @@ class QueryInterfaceRequestTests: GRDBTestCase {
let dbQueue = try makeDatabaseQueue()
XCTAssertEqual(
sql(dbQueue, tableRequest.filter(true).filter(false)),
"SELECT * FROM \"readers\" WHERE (1 AND 0)")
"SELECT * FROM \"readers\" WHERE 1 AND 0")
}
@@ -608,7 +608,7 @@ class QueryInterfaceRequestTests: GRDBTestCase {
let dbQueue = try makeDatabaseQueue()
XCTAssertEqual(
sql(dbQueue, tableRequest.group(Col.name).having(min(Col.age) > 18).having(max(Col.age) < 50)),
"SELECT * FROM \"readers\" GROUP BY \"name\" HAVING ((MIN(\"age\") > 18) AND (MAX(\"age\") < 50))")
"SELECT * FROM \"readers\" GROUP BY \"name\" HAVING (MIN(\"age\") > 18) AND (MAX(\"age\") < 50)")
}
@@ -214,7 +214,7 @@ class TableRecordQueryInterfaceRequestTests: GRDBTestCase {
let dbQueue = try makeDatabaseQueue()
XCTAssertEqual(
sql(dbQueue, Reader.filter(true).filter(false)),
"SELECT * FROM \"readers\" WHERE (1 AND 0)")
"SELECT * FROM \"readers\" WHERE 1 AND 0")
}