SQLRequestExpressible

This protocol makes it possible to write request.filter(column == subRequest)
This commit is contained in:
Gwendal Roué committed 2020-04-29 20:18:00 +02:00
1 parent 473bc3a88e
commit 3d86f45aa2
4 files changed
+125 -11

No files matched your search

+12 -5
View File
@@ -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<RowDecoder>: SQLExpression {
var request: SQLRequest<RowDecoder>
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
}
}
@@ -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<RowDecoder>: SQLExpression {
var request: QueryInterfaceRequest<RowDecoder>
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
}
}
@@ -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 {
@@ -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)