Skip to content

Commit 849d4ef

Browse files
committed
Support IN condition against subquery
Closes stephencelis#94 Signed-off-by: Stephen Celis <stephen@stephencelis.com>
1 parent fc086d0 commit 849d4ef

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

SQLite Tests/ExpressionTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ class ExpressionTests: SQLiteTestCase {
536536
AssertSQLContains("(\"age\" IN (20))", contains(Set([20]), age))
537537
}
538538

539+
func test_containsFunction_withValueExpressionAndQuery_buildsInExpression() {
540+
let query = users.select(max(age)).group(id)
541+
AssertSQLContains("(\"id\" IN (SELECT max(\"age\") FROM \"users\" GROUP BY \"id\"))", contains(query, id))
542+
}
543+
539544
func test_plusEquals_withStringExpression_buildsSetter() {
540545
users.update(email += email)!
541546
users.update(email += email2)!

SQLite/Expression.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,14 +769,16 @@ public typealias Star = (Expression<Binding>?, Expression<Binding>?) -> Expressi
769769
public func * (Expression<Binding>?, Expression<Binding>?) -> Expression<()> {
770770
return Expression(literal: "*")
771771
}
772-
773772
public func contains<V: Value, C: CollectionType where C.Generator.Element == V, C.Index.Distance == Int>(values: C, column: Expression<V>) -> Expression<Bool> {
774773
let templates = join(", ", [String](count: count(values), repeatedValue: "?"))
775774
return infix("IN", column, Expression<V>(literal: "(\(templates))", map(values) { $0.datatypeValue }))
776775
}
777776
public func contains<V: Value, C: CollectionType where C.Generator.Element == V, C.Index.Distance == Int>(values: C, column: Expression<V?>) -> Expression<Bool> {
778777
return contains(values, Expression<V>(column))
779778
}
779+
public func contains<V: Value>(values: Query, column: Expression<V>) -> Expression<Bool> {
780+
return infix("IN", column, wrap("", values.selectExpression) as Expression<()>)
781+
}
780782

781783
// MARK: - Modifying
782784

0 commit comments

Comments
 (0)