mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
SQLRequestExpressible is a sub protocol of SQLSpecificExpressible
This commit is contained in:
1 parent
69641ff7d6
commit
be249bc697
5 files changed
+23
-103
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -154,20 +154,7 @@ extension SQLInterpolation {
|
||||
/// let request: SQLRequest<Player> = """
|
||||
/// SELECT * FROM player WHERE score = (\(subQuery))
|
||||
/// """
|
||||
public mutating func appendInterpolation<RowDecoder>(_ request: SQLRequest<RowDecoder>) {
|
||||
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<Int> = "SELECT MAX(score) FROM player"
|
||||
/// let request: SQLRequest<Player> = """
|
||||
/// SELECT * FROM player WHERE score = (\(subQuery))
|
||||
/// """
|
||||
public mutating func appendInterpolation<RowDecoder>(_ request: QueryInterfaceRequest<RowDecoder>) {
|
||||
elements.append(.custom { context in
|
||||
try SQLQueryGenerator(query: request.query).sql(context)
|
||||
})
|
||||
public mutating func appendInterpolation(_ request: SQLRequestExpressible) {
|
||||
elements.append(.custom(request.requestSQL))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user