mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Cleanup _SQLExpressionContains
This commit is contained in:
1 parent
d05f11293e
commit
fc3e64989d
3 files changed
+66
-13
No files matched your search
@@ -18,7 +18,6 @@ public protocol SQLCollection: _SQLCollection {
|
||||
func contains(_ value: SQLExpressible) -> SQLExpression
|
||||
}
|
||||
|
||||
|
||||
// MARK: - _SQLExpressionsArray
|
||||
|
||||
/// _SQLExpressionsArray wraps an array of expressions
|
||||
@@ -53,3 +52,24 @@ public struct _SQLExpressionsArray: SQLCollection {
|
||||
try visitor.visit(self)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLCollectionExpressions
|
||||
|
||||
extension SQLCollection {
|
||||
func expressions() -> [SQLExpression]? {
|
||||
var visitor = SQLCollectionExpressions()
|
||||
try! _accept(&visitor)
|
||||
return visitor.expressions
|
||||
}
|
||||
}
|
||||
|
||||
/// Support for SQLCollection.expressions
|
||||
private struct SQLCollectionExpressions: _SQLCollectionVisitor {
|
||||
var expressions: [SQLExpression]?
|
||||
|
||||
mutating func visit(_ collection: _SQLExpressionsArray) throws {
|
||||
expressions = collection.expressions
|
||||
}
|
||||
|
||||
mutating func visit<Request: SQLRequestProtocol>(_ request: Request) throws { }
|
||||
}
|
||||
@@ -376,23 +376,42 @@ public struct _SQLExpressionContains: SQLExpression {
|
||||
let collection: SQLCollection
|
||||
let isNegated: Bool
|
||||
|
||||
init(_ value: SQLExpressible, _ collection: SQLCollection, negated: Bool = false) {
|
||||
self.expression = value.sqlExpression
|
||||
/// - precondition: collection.expressions.count > 1
|
||||
init(_ value: SQLExpressible, _ collection: _SQLExpressionsArray, negated: Bool = false) {
|
||||
assert(collection.expressions.count > 1)
|
||||
self.init(
|
||||
expression: value.sqlExpression,
|
||||
collection: collection,
|
||||
isNegated: negated)
|
||||
}
|
||||
|
||||
init(_ value: SQLExpressible, _ collection: SQLRequestProtocol, negated: Bool = false) {
|
||||
self.init(
|
||||
expression: value.sqlExpression,
|
||||
collection: collection,
|
||||
isNegated: negated)
|
||||
}
|
||||
|
||||
private init(expression: SQLExpression, collection: SQLCollection, isNegated: Bool) {
|
||||
self.expression = expression
|
||||
self.collection = collection
|
||||
self.isNegated = negated
|
||||
self.isNegated = isNegated
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public var _negated: SQLExpression {
|
||||
_SQLExpressionContains(expression, collection, negated: !isNegated)
|
||||
_SQLExpressionContains(
|
||||
expression: expression,
|
||||
collection: collection,
|
||||
isNegated: !isNegated)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func _qualifiedExpression(with alias: TableAlias) -> SQLExpression {
|
||||
return _SQLExpressionContains(
|
||||
expression._qualifiedExpression(with: alias),
|
||||
collection._qualifiedCollection(with: alias),
|
||||
negated: isNegated)
|
||||
_SQLExpressionContains(
|
||||
expression: expression._qualifiedExpression(with: alias),
|
||||
collection: collection._qualifiedCollection(with: alias),
|
||||
isNegated: isNegated)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
|
||||
@@ -873,8 +873,13 @@ private struct SQLExpressionIsConstantInRequest: _SQLExpressionVisitor {
|
||||
}
|
||||
|
||||
mutating func visit(_ expr: _SQLExpressionContains) throws {
|
||||
// TODO: visit the collection
|
||||
try setNotConstant() // Don't know - assume not constant
|
||||
guard let expressions = expr.collection.expressions() else {
|
||||
try setNotConstant() // Don't know - assume not constant
|
||||
}
|
||||
try expr.expression._accept(&self)
|
||||
for expression in expressions {
|
||||
try expression._accept(&self)
|
||||
}
|
||||
}
|
||||
|
||||
mutating func visit(_ expr: _SQLExpressionCount) throws {
|
||||
@@ -1225,11 +1230,11 @@ private struct SQLIdentifyingRowIDs: _SQLExpressionVisitor {
|
||||
|
||||
mutating func visit(_ expr: _SQLExpressionContains) throws {
|
||||
if
|
||||
let array = expr.collection as? _SQLExpressionsArray, // TODO: implement as a visitor
|
||||
let expressions = expr.collection.expressions(),
|
||||
let column = try expr.expression.column(db, for: alias),
|
||||
try db.columnIsRowID(column, of: alias.tableName)
|
||||
{
|
||||
rowIDs = Set(array.expressions.compactMap {
|
||||
rowIDs = Set(expressions.compactMap {
|
||||
($0 as? DatabaseValue).flatMap { Int64.fromDatabaseValue($0) }
|
||||
})
|
||||
}
|
||||
@@ -1364,7 +1369,16 @@ private struct SQLSelectableIsAggregate: _SQLSelectableVisitor {
|
||||
}
|
||||
|
||||
mutating func visit(_ expr: _SQLExpressionContains) throws {
|
||||
// SELECT aggregate IN (...)
|
||||
try expr.expression._accept(&self)
|
||||
|
||||
// SELECT expr IN (aggregate, ...)
|
||||
if
|
||||
let expressions = expr.collection.expressions(),
|
||||
expressions.contains(where: { $0.isAggregate() })
|
||||
{
|
||||
try setAggregate()
|
||||
}
|
||||
}
|
||||
|
||||
mutating func visit(_ expr: _SQLExpressionCount) throws {
|
||||
|
||||
Reference in new issue
Block a user