UpdateStatement.Changes store the changes performed by an update statement.

This commit is contained in:
Gwendal Roué committed 2015-07-07 08:33:46 +02:00
1 parent c77f28a571
commit ebf39fdd70
4 files changed
+24 -27

No files matched your search

+3 -19
View File
@@ -85,10 +85,11 @@ public final class Database {
This method may throw a SQLiteError.
- parameter sql: An SQL query.
- parameter sql: An SQL query.
- parameter bindings: Optional bindings for query parameters.
- returns: A UpdateStatement.Changes.
*/
public func execute(sql: String, bindings: Bindings? = nil) throws {
public func execute(sql: String, bindings: Bindings? = nil) throws -> UpdateStatement.Changes {
return try updateStatement(sql, bindings: bindings).execute()
}
@@ -111,23 +112,6 @@ public final class Database {
// MARK: - Miscellaneous
/// The last inserted Row ID
public var lastInsertedRowID: Int64? {
let rowid = sqlite3_last_insert_rowid(sqliteConnection)
return rowid == 0 ? nil : rowid
}
/**
The number of rows changed by the most recent INSERT, UPDATE or DELETE
statement.
See [sqlite3_changes()](http://sqlite.org/c3ref/changes.html).
*/
public var changes: Int {
return Int(sqlite3_changes(sqliteConnection))
}
/**
Returns whether a table exists.
+4 -5
View File
@@ -166,13 +166,13 @@ public class RowModel {
verb = "INSERT"
}
let sql = "\(verb) INTO \(tableName.sqliteQuotedIdentifier) (\(columnSQL)) VALUES (\(valuesSQL))"
try db.execute(sql, bindings: Bindings(insertedDic.values))
let changes = try db.execute(sql, bindings: Bindings(insertedDic.values))
// Update RowID column
if let rowIDColumn = rowIDColumn, let lastInsertedRowID = db.lastInsertedRowID {
let row = Row(sqliteDictionary: [rowIDColumn: SQLiteValue.Integer(lastInsertedRowID)])
if let rowIDColumn = rowIDColumn, let rowID = changes.insertedRowID {
let row = Row(sqliteDictionary: [rowIDColumn: SQLiteValue.Integer(rowID)])
updateFromDatabaseRow(row)
}
}
@@ -238,8 +238,7 @@ public class RowModel {
verb = "UPDATE"
}
let sql = "\(verb) \(tableName.sqliteQuotedIdentifier) SET \(updateSQL) WHERE \(whereSQL)"
try db.execute(sql, bindings: bindings)
return db.changes > 0
return try db.execute(sql, bindings: bindings).changedRowCount > 0
}
+17 -2
View File
@@ -24,7 +24,17 @@
public final class UpdateStatement : Statement {
public func execute(bindings bindings: Bindings? = nil) throws {
/// The changes performed by an UpdateStatement.
public struct Changes {
/// The number of rows changed by the statement.
let changedRowCount: Int
/// The last inserted Row ID. Relevant if and only if the statement is
/// an INSERT statement.
let insertedRowID: Int64?
}
public func execute(bindings bindings: Bindings? = nil) throws -> Changes {
if let bindings = bindings {
self.bindings = bindings
}
@@ -36,7 +46,12 @@ public final class UpdateStatement : Statement {
}
let code = sqlite3_step(sqliteStatement)
if code != SQLITE_DONE {
if code == SQLITE_DONE {
let changedRowCount = Int(sqlite3_changes(database.sqliteConnection))
let lastInsertedRowID = sqlite3_last_insert_rowid(database.sqliteConnection)
let insertedRowID: Int64? = (lastInsertedRowID == 0) ? nil : lastInsertedRowID
return Changes(changedRowCount: changedRowCount, insertedRowID: insertedRowID)
} else {
throw SQLiteError(code: code, message: database.lastErrorMessage, sql: sql, bindings: self.bindings)
}
}
-1
View File
@@ -2,4 +2,3 @@
- [ ] db.fetch(RowModel.type, primaryKeys:[...]) // several primary keys
- [ ] Let Bindings eat arrays, or make it easy to generate series of ?,?,?,?
- [ ] Pierlo complains that databaseDictionary returns a dictionary of values when updateFromDatabaseRow eats a Row. See if we can do something about that.
- [ ] Have update statements return a result that holds (changes, lastInsertedRowID).