From be249bc697c6662b37892ea36b065f9767e2020b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 29 Apr 2020 23:40:37 +0200 Subject: [PATCH] SQLRequestExpressible is a sub protocol of SQLSpecificExpressible --- GRDB/Core/SQLRequest.swift | 10 ++- .../Request/QueryInterfaceRequest.swift | 10 ++- GRDB/QueryInterface/SQL/SQLExpressible.swift | 7 +- GRDB/QueryInterface/SQL/SQLOperators.swift | 82 ------------------- .../SQLInterpolation+QueryInterface.swift | 17 +--- 5 files changed, 23 insertions(+), 103 deletions(-) diff --git a/GRDB/Core/SQLRequest.swift b/GRDB/Core/SQLRequest.swift index 9569d864c..5b5b6cc8a 100644 --- a/GRDB/Core/SQLRequest.swift +++ b/GRDB/Core/SQLRequest.swift @@ -133,13 +133,21 @@ extension SQLRequest: SQLCollection { } // Support for `request == expression` -extension SQLRequest: SQLRequestExpressible { +extension SQLRequest: SQLSpecificExpressible { /// :nodoc public var sqlExpression: SQLExpression { sqlLiteral.sqlExpression } } +// Support for `SQLLiteral("SELECT ... IN (\(request))")` +extension SQLRequest: SQLRequestExpressible { + /// :nodoc + public func requestSQL(_ context: SQLGenerationContext) throws -> String { + try sqlLiteral.sql(context) + } +} + extension SQLRequest: ExpressibleByStringInterpolation { /// :nodoc public init(unicodeScalarLiteral: String) { diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 6115534f8..8a4b788f9 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -415,7 +415,7 @@ extension QueryInterfaceRequest: SQLCollection { } // Support for `request == expression` -extension QueryInterfaceRequest: SQLRequestExpressible { +extension QueryInterfaceRequest: SQLSpecificExpressible { private struct Expression: SQLExpression { let generator: SQLQueryGenerator @@ -437,6 +437,14 @@ extension QueryInterfaceRequest: SQLRequestExpressible { } } +// Support for `SQLLiteral("SELECT ... IN (\(request))")` +extension QueryInterfaceRequest: SQLRequestExpressible { + /// :nodoc + public func requestSQL(_ context: SQLGenerationContext) throws -> String { + try SQLQueryGenerator(query: query).sql(context) + } +} + extension QueryInterfaceRequest: TableRequest { /// :nodoc: public var databaseTableName: String { diff --git a/GRDB/QueryInterface/SQL/SQLExpressible.swift b/GRDB/QueryInterface/SQL/SQLExpressible.swift index 914ce66da..786d257b5 100644 --- a/GRDB/QueryInterface/SQL/SQLExpressible.swift +++ b/GRDB/QueryInterface/SQL/SQLExpressible.swift @@ -59,11 +59,10 @@ public protocol SQLSpecificExpressible: SQLExpressible { /// A protocol for request types that are used as an SQL expression. /// /// :nodoc: -public protocol SQLRequestExpressible { +public protocol SQLRequestExpressible: SQLSpecificExpressible { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) - /// - /// Returns an SQLExpression - var sqlExpression: SQLExpression { get } + /// :nodoc: + func requestSQL(_ context: SQLGenerationContext) throws -> String } // MARK: - SQLExpressible & SQLOrderingTerm diff --git a/GRDB/QueryInterface/SQL/SQLOperators.swift b/GRDB/QueryInterface/SQL/SQLOperators.swift index d65d9c9b9..f931f2e24 100644 --- a/GRDB/QueryInterface/SQL/SQLOperators.swift +++ b/GRDB/QueryInterface/SQL/SQLOperators.swift @@ -116,22 +116,6 @@ public func == (lhs: SQLSpecificExpressible, rhs: SQLSpecificExpressible) -> SQL isEqual(lhs.sqlExpression, rhs.sqlExpression) } -public func == (lhs: SQLExpressible?, rhs: SQLRequestExpressible) -> SQLExpression { - lhs == rhs.sqlExpression -} - -public func == (lhs: SQLRequestExpressible, rhs: SQLExpressible?) -> SQLExpression { - lhs.sqlExpression == rhs -} - -public func == (lhs: Bool, rhs: SQLRequestExpressible) -> SQLExpression { - lhs == rhs.sqlExpression -} - -public func == (lhs: SQLRequestExpressible, rhs: Bool) -> SQLExpression { - lhs.sqlExpression == rhs -} - /// An SQL expression that compares two expressions with the `<>` SQL operator. /// /// // name <> 'Arthur' @@ -226,22 +210,6 @@ public func != (lhs: SQLSpecificExpressible, rhs: SQLSpecificExpressible) -> SQL isEqual(lhs.sqlExpression, rhs.sqlExpression).negated } -public func != (lhs: SQLExpressible?, rhs: SQLRequestExpressible) -> SQLExpression { - lhs != rhs.sqlExpression -} - -public func != (lhs: SQLRequestExpressible, rhs: SQLExpressible?) -> SQLExpression { - lhs.sqlExpression != rhs -} - -public func != (lhs: Bool, rhs: SQLRequestExpressible) -> SQLExpression { - lhs != rhs.sqlExpression -} - -public func != (lhs: SQLRequestExpressible, rhs: Bool) -> SQLExpression { - lhs.sqlExpression == rhs -} - /// An SQL expression that compares two expressions with the `IS` SQL operator. /// /// // name IS 'Arthur' @@ -286,14 +254,6 @@ public func === (lhs: SQLSpecificExpressible, rhs: SQLSpecificExpressible) -> SQ SQLExpressionEqual(.is, lhs.sqlExpression, rhs.sqlExpression) } -public func === (lhs: SQLExpressible?, rhs: SQLRequestExpressible) -> SQLExpression { - lhs === rhs.sqlExpression -} - -public func === (lhs: SQLRequestExpressible, rhs: SQLExpressible?) -> SQLExpression { - lhs.sqlExpression === rhs -} - /// An SQL expression that compares two expressions with the `IS NOT` SQL operator. /// /// // name IS NOT 'Arthur' @@ -338,15 +298,6 @@ public func !== (lhs: SQLSpecificExpressible, rhs: SQLSpecificExpressible) -> SQ SQLExpressionEqual(.isNot, lhs.sqlExpression, rhs.sqlExpression) } -public func !== (lhs: SQLExpressible, rhs: SQLRequestExpressible) -> SQLExpression { - lhs !== rhs.sqlExpression -} - -public func !== (lhs: SQLRequestExpressible, rhs: SQLExpressible) -> SQLExpression { - lhs.sqlExpression !== rhs -} - - // MARK: - Comparison Operators (<, >, <=, >=) extension SQLBinaryOperator { @@ -403,14 +354,6 @@ public func < (lhs: SQLSpecificExpressible, rhs: SQLSpecificExpressible) -> SQLE SQLExpressionBinary(.lessThan, lhs.sqlExpression, rhs.sqlExpression) } -public func < (lhs: SQLExpressible, rhs: SQLRequestExpressible) -> SQLExpression { - lhs < rhs.sqlExpression -} - -public func < (lhs: SQLRequestExpressible, rhs: SQLExpressible) -> SQLExpression { - lhs.sqlExpression < rhs -} - /// An SQL expression that compares two expressions with the `<=` SQL operator. /// /// // score <= 18 @@ -451,14 +394,6 @@ public func <= (lhs: SQLSpecificExpressible, rhs: SQLSpecificExpressible) -> SQL SQLExpressionBinary(.lessThanOrEqual, lhs.sqlExpression, rhs.sqlExpression) } -public func <= (lhs: SQLExpressible, rhs: SQLRequestExpressible) -> SQLExpression { - lhs <= rhs.sqlExpression -} - -public func <= (lhs: SQLRequestExpressible, rhs: SQLExpressible) -> SQLExpression { - lhs.sqlExpression <= rhs -} - /// An SQL expression that compares two expressions with the `>` SQL operator. /// /// // score > 18 @@ -499,14 +434,6 @@ public func > (lhs: SQLSpecificExpressible, rhs: SQLSpecificExpressible) -> SQLE SQLExpressionBinary(.greaterThan, lhs.sqlExpression, rhs.sqlExpression) } -public func > (lhs: SQLExpressible, rhs: SQLRequestExpressible) -> SQLExpression { - lhs > rhs.sqlExpression -} - -public func > (lhs: SQLRequestExpressible, rhs: SQLExpressible) -> SQLExpression { - lhs.sqlExpression > rhs -} - /// An SQL expression that compares two expressions with the `>=` SQL operator. /// /// // score >= 18 @@ -547,15 +474,6 @@ public func >= (lhs: SQLSpecificExpressible, rhs: SQLSpecificExpressible) -> SQL SQLExpressionBinary(.greaterThanOrEqual, lhs.sqlExpression, rhs.sqlExpression) } -public func >= (lhs: SQLExpressible, rhs: SQLRequestExpressible) -> SQLExpression { - lhs >= rhs.sqlExpression -} - -public func >= (lhs: SQLRequestExpressible, rhs: SQLExpressible) -> SQLExpression { - lhs.sqlExpression >= rhs -} - - // MARK: - Inclusion Operators (BETWEEN, IN) extension Range where Bound: SQLExpressible { diff --git a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift index 391f5e499..385e1824e 100644 --- a/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift +++ b/GRDB/QueryInterface/SQLInterpolation+QueryInterface.swift @@ -154,20 +154,7 @@ extension SQLInterpolation { /// let request: SQLRequest = """ /// SELECT * FROM player WHERE score = (\(subQuery)) /// """ - public mutating func appendInterpolation(_ request: SQLRequest) { - elements.append(.custom(request.sqlLiteral.sql)) - } - - /// Appends the request SQL (not wrapped inside parentheses). - /// - /// // SELECT name FROM player WHERE score = (SELECT MAX(score) FROM player) - /// let subQuery: SQLRequest = "SELECT MAX(score) FROM player" - /// let request: SQLRequest = """ - /// SELECT * FROM player WHERE score = (\(subQuery)) - /// """ - public mutating func appendInterpolation(_ request: QueryInterfaceRequest) { - elements.append(.custom { context in - try SQLQueryGenerator(query: request.query).sql(context) - }) + public mutating func appendInterpolation(_ request: SQLRequestExpressible) { + elements.append(.custom(request.requestSQL)) } }