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