From df2cc0af36ce7a17d9baccf2916361dec923965b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 30 Apr 2020 19:01:14 +0200 Subject: [PATCH] Test for SQLCollection qualification --- GRDB/QueryInterface/SQL/SQLOperators.swift | 20 +++++++++++++++++++ ...sociationBelongsToSQLDerivationTests.swift | 12 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/GRDB/QueryInterface/SQL/SQLOperators.swift b/GRDB/QueryInterface/SQL/SQLOperators.swift index 58e42fd93..f60fad939 100644 --- a/GRDB/QueryInterface/SQL/SQLOperators.swift +++ b/GRDB/QueryInterface/SQL/SQLOperators.swift @@ -552,6 +552,26 @@ extension Sequence where Self.Iterator.Element: SQLExpressible { } } +extension Sequence where Self.Iterator.Element == SQLExpressible { + /// An SQL expression that checks the inclusion of an expression in + /// a sequence. + /// + /// // id IN (1,2,3) + /// [1, 2, 3].contains(Column("id")) + public func contains(_ element: SQLSpecificExpressible) -> SQLExpression { + SQLExpressionsArray(expressions: map(\.sqlExpression)).contains(element.sqlExpression) + } + + /// An SQL expression that checks the inclusion of an expression in + /// a sequence. + /// + /// // name IN ('A', 'B') COLLATE NOCASE + /// ["A", "B"].contains(Column("name").collating(.nocase)) + public func contains(_ element: SQLCollatedExpression) -> SQLExpression { + SQLExpressionCollate(contains(element.expression), collationName: element.collationName) + } +} + // MARK: - Arithmetic Operators (+, -, *, /) diff --git a/Tests/GRDBTests/AssociationBelongsToSQLDerivationTests.swift b/Tests/GRDBTests/AssociationBelongsToSQLDerivationTests.swift index 33b411cb4..05f2a71cf 100644 --- a/Tests/GRDBTests/AssociationBelongsToSQLDerivationTests.swift +++ b/Tests/GRDBTests/AssociationBelongsToSQLDerivationTests.swift @@ -128,6 +128,18 @@ class AssociationBelongsToSQLDerivationTests: GRDBTestCase { JOIN "b" ON ("b"."id" = "a"."bid") AND ("b"."id" IN (1, 2, 3)) """) } + do { + let alias = TableAlias() + let request = A + .aliased(alias) + .including(required: A.b + .filter([alias[Column("id")], Column("id"), 42].contains(Column("id")))) + try assertEqualSQL(db, request, """ + SELECT "a".*, "b".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bid") AND ("b"."id" IN ("a"."id", "b"."id", 42)) + """) + } do { let request = A.including(required: A.b.filter(key: ["id": 1])) try assertEqualSQL(db, request, """