diff --git a/GRDB/QueryInterface/SQL/SQLCollection.swift b/GRDB/QueryInterface/SQL/SQLCollection.swift index 506bface1..71da31939 100644 --- a/GRDB/QueryInterface/SQL/SQLCollection.swift +++ b/GRDB/QueryInterface/SQL/SQLCollection.swift @@ -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: Request) throws { } +} diff --git a/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift b/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift index 965a45bd0..5e70a9829 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift @@ -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: diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 6d4ab3a47..70c1e87a6 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -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 {