diff --git a/GRDB/Core/SQLRequest.swift b/GRDB/Core/SQLRequest.swift index 646775d60..0b9815001 100644 --- a/GRDB/Core/SQLRequest.swift +++ b/GRDB/Core/SQLRequest.swift @@ -131,14 +131,21 @@ extension SQLRequest: SQLCollection { } } -extension SQLRequest: SQLExpression { +extension SQLRequest: SQLRequestExpressible { /// :nodoc - public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { - try "(" + sqlLiteral.sql(&context) + ")" + public var sqlExpression: SQLExpression { + SQLRequestExpression(request: self) + } +} + +private struct SQLRequestExpression: SQLExpression { + var request: SQLRequest + + func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + try "(" + request.sqlLiteral.sql(&context) + ")" } - /// :nodoc - public func qualifiedExpression(with alias: TableAlias) -> SQLExpression { + func qualifiedExpression(with alias: TableAlias) -> SQLExpression { return self } } diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index e77c37073..0b1463382 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -406,15 +406,28 @@ extension QueryInterfaceRequest: SQLCollection { } } -extension QueryInterfaceRequest: SQLExpression { +extension QueryInterfaceRequest: SQLRequestExpressible { /// :nodoc - public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { - let generator = SQLQueryGenerator(query, forSingleResult: true, requiresSingleColumn: true) - return try "(" + generator.sql(&context) + ")" + public var sqlExpression: SQLExpression { + QueryInterfaceRequestExpression(request: self) + } +} + +private struct QueryInterfaceRequestExpression: SQLExpression { + var request: QueryInterfaceRequest + + func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + let generator = SQLQueryGenerator(request.query, forSingleResult: true, requiresSingleColumn: true) + let (sql, arguments) = try generator.buildSelectSQL(context.db) + if context.append(arguments: arguments) == false { + // Crap, a raw SQL context which does not accept arguments. + // Can this really happen? + fatalError("Not implemented") + } + return "(\(sql))" } - /// :nodoc - public func qualifiedExpression(with alias: TableAlias) -> SQLExpression { + func qualifiedExpression(with alias: TableAlias) -> SQLExpression { return self } } diff --git a/GRDB/QueryInterface/SQL/SQLExpressible.swift b/GRDB/QueryInterface/SQL/SQLExpressible.swift index 83e206d5c..ca95b8f1a 100644 --- a/GRDB/QueryInterface/SQL/SQLExpressible.swift +++ b/GRDB/QueryInterface/SQL/SQLExpressible.swift @@ -52,6 +52,20 @@ public protocol SQLSpecificExpressible: SQLExpressible { // - ==(SQLSpecificExpressible, SQLSpecificExpressible) } +// MARK: - SQLRequestExpressible + +/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) +/// +/// A protocol for request types that are used as an SQL expression. +/// +/// :nodoc: +public protocol SQLRequestExpressible { + /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) + /// + /// Returns an SQLExpression + var sqlExpression: SQLExpression { get } +} + // MARK: - SQLExpressible & SQLOrderingTerm extension SQLExpressible where Self: SQLOrderingTerm { diff --git a/GRDB/QueryInterface/SQL/SQLOperators.swift b/GRDB/QueryInterface/SQL/SQLOperators.swift index c0ba964d2..d65d9c9b9 100644 --- a/GRDB/QueryInterface/SQL/SQLOperators.swift +++ b/GRDB/QueryInterface/SQL/SQLOperators.swift @@ -116,6 +116,22 @@ 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' @@ -210,6 +226,22 @@ 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' @@ -254,6 +286,14 @@ 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' @@ -298,6 +338,14 @@ 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 (<, >, <=, >=) @@ -355,6 +403,14 @@ 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 @@ -395,6 +451,14 @@ 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 @@ -435,6 +499,14 @@ 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 @@ -475,6 +547,14 @@ 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)