QueryInterfaceRequest.deleteAll() now honors the order and limit clauses. New Set(cursor) initialiser.

This commit is contained in:
Gwendal Roué committed 2017-09-07 19:22:21 +02:00
1 parent e68e5f6265
commit a7ecf41e3b
5 files changed
+84 -27

No files matched your search

@@ -59,26 +59,7 @@ struct QueryInterfaceSelectQueryDefinition {
sql += " HAVING " + havingExpression.expressionSQL(&arguments)
}
var orderings = self.orderings
if isReversed {
if orderings.isEmpty {
// https://www.sqlite.org/lang_createtable.html#rowid
//
// > The rowid value can be accessed using one of the special
// > case-independent names "rowid", "oid", or "_rowid_" in
// > place of a column name. If a table contains a user defined
// > column named "rowid", "oid" or "_rowid_", then that name
// > always refers the explicitly declared column and cannot be
// > used to retrieve the integer rowid value.
//
// Here we assume that rowid is not a custom column.
// TODO: support for user-defined rowid column.
// TODO: support for WITHOUT ROWID tables.
orderings = [Column.rowID.desc]
} else {
orderings = orderings.map { $0.reversed }
}
}
let orderings = self.queryOrderings
if !orderings.isEmpty {
sql += " ORDER BY " + orderings.map { $0.orderingTermSQL(&arguments) }.joined(separator: ", ")
}
@@ -101,11 +82,6 @@ struct QueryInterfaceSelectQueryDefinition {
fatalError("Can't delete query with GROUP BY expression")
}
guard limit == nil else {
// Programmer error
fatalError("Can't delete query with limit")
}
var sql = "DELETE"
var arguments: StatementArguments? = StatementArguments()
@@ -117,11 +93,48 @@ struct QueryInterfaceSelectQueryDefinition {
sql += " WHERE " + whereExpression.expressionSQL(&arguments)
}
if let limit = limit {
let orderings = self.queryOrderings
if !orderings.isEmpty {
sql += " ORDER BY " + orderings.map { $0.orderingTermSQL(&arguments) }.joined(separator: ", ")
}
if Database.sqliteCompileOptions.contains("ENABLE_UPDATE_DELETE_LIMIT") {
sql += " LIMIT " + limit.sql
} else {
fatalError("Can't delete query with limit")
}
}
let statement = try db.makeUpdateStatement(sql)
statement.arguments = arguments!
return statement
}
private var queryOrderings: [SQLOrderingTerm] {
if isReversed {
if orderings.isEmpty {
// https://www.sqlite.org/lang_createtable.html#rowid
//
// > The rowid value can be accessed using one of the special
// > case-independent names "rowid", "oid", or "_rowid_" in
// > place of a column name. If a table contains a user defined
// > column named "rowid", "oid" or "_rowid_", then that name
// > always refers the explicitly declared column and cannot be
// > used to retrieve the integer rowid value.
//
// Here we assume that rowid is not a custom column.
// TODO: support for user-defined rowid column.
// TODO: support for WITHOUT ROWID tables.
return [Column.rowID.desc]
} else {
return orderings.map { $0.reversed }
}
} else {
return orderings
}
}
/// Remove ordering
var unorderedQuery: QueryInterfaceSelectQueryDefinition {
var query = self