From 849d4efe8632eee1bbee036453a52dc8a7e99426 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Sat, 11 Apr 2015 10:27:24 -0400 Subject: [PATCH] Support IN condition against subquery Closes #94 Signed-off-by: Stephen Celis --- SQLite Tests/ExpressionTests.swift | 5 +++++ SQLite/Expression.swift | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/SQLite Tests/ExpressionTests.swift b/SQLite Tests/ExpressionTests.swift index 31e94b5..c69c52c 100644 --- a/SQLite Tests/ExpressionTests.swift +++ b/SQLite Tests/ExpressionTests.swift @@ -536,6 +536,11 @@ class ExpressionTests: SQLiteTestCase { AssertSQLContains("(\"age\" IN (20))", contains(Set([20]), age)) } + func test_containsFunction_withValueExpressionAndQuery_buildsInExpression() { + let query = users.select(max(age)).group(id) + AssertSQLContains("(\"id\" IN (SELECT max(\"age\") FROM \"users\" GROUP BY \"id\"))", contains(query, id)) + } + func test_plusEquals_withStringExpression_buildsSetter() { users.update(email += email)! users.update(email += email2)! diff --git a/SQLite/Expression.swift b/SQLite/Expression.swift index 20eaff5..e076b9a 100644 --- a/SQLite/Expression.swift +++ b/SQLite/Expression.swift @@ -769,7 +769,6 @@ public typealias Star = (Expression?, Expression?) -> Expressi public func * (Expression?, Expression?) -> Expression<()> { return Expression(literal: "*") } - public func contains(values: C, column: Expression) -> Expression { let templates = join(", ", [String](count: count(values), repeatedValue: "?")) return infix("IN", column, Expression(literal: "(\(templates))", map(values) { $0.datatypeValue })) @@ -777,6 +776,9 @@ public func contains(values: C, column: Expression) -> Expression { return contains(values, Expression(column)) } +public func contains(values: Query, column: Expression) -> Expression { + return infix("IN", column, wrap("", values.selectExpression) as Expression<()>) +} // MARK: - Modifying