diff --git a/GRDB/Database.swift b/GRDB/Database.swift index c498a0b2a..536f112df 100644 --- a/GRDB/Database.swift +++ b/GRDB/Database.swift @@ -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. diff --git a/GRDB/RowModel.swift b/GRDB/RowModel.swift index 2127d39e6..918d444d4 100644 --- a/GRDB/RowModel.swift +++ b/GRDB/RowModel.swift @@ -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 } diff --git a/GRDB/UpdateStatement.swift b/GRDB/UpdateStatement.swift index d6a705104..d8568addc 100644 --- a/GRDB/UpdateStatement.swift +++ b/GRDB/UpdateStatement.swift @@ -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) } } diff --git a/TODO.md b/TODO.md index 6281395e8..f8d45c0eb 100644 --- a/TODO.md +++ b/TODO.md @@ -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).