Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for ORDER and LIMIT on UPDATE and DELETE #722

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,9 @@ extension QueryType {
tableName(),
Expression<Void>(literal: "SET"),
", ".join(values.map { " = ".join([$0.column, $0.value]) }),
whereClause
whereClause,
orderClause,
limitOffsetClause
]

return Update(" ".join(clauses.flatMap { $0 }).expression)
Expand All @@ -660,7 +662,9 @@ extension QueryType {
let clauses: [Expressible?] = [
Expression<Void>(literal: "DELETE FROM"),
tableName(),
whereClause
whereClause,
orderClause,
limitOffsetClause
]

return Delete(" ".join(clauses.flatMap { $0 }).expression)
Expand Down
14 changes: 14 additions & 0 deletions Tests/SQLiteTests/QueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,27 @@ class QueryTests : XCTestCase {
)
}

func test_update_compilesUpdateLimitOrderExpression() {
AssertSQL(
"UPDATE \"users\" SET \"age\" = 30 ORDER BY \"id\" LIMIT 1",
users.order(id).limit(1).update(age <- 30)
)
}

func test_delete_compilesDeleteExpression() {
AssertSQL(
"DELETE FROM \"users\" WHERE (\"id\" = 1)",
users.filter(id == 1).delete()
)
}

func test_delete_compilesDeleteLimitOrderExpression() {
AssertSQL(
"DELETE FROM \"users\" ORDER BY \"id\" LIMIT 1",
users.order(id).limit(1).delete()
)
}

func test_delete_compilesExistsExpression() {
AssertSQL(
"SELECT EXISTS (SELECT * FROM \"users\")",
Expand Down