Skip to content

Commit bce88aa

Browse files
committed
Updated getLastInsertID()
1 parent 8760ca3 commit bce88aa

1 file changed

Lines changed: 4 additions & 13 deletions

File tree

Sources/SQLiteAdapter/SQLiteAdapter.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public protocol SQLiteType {
6565
func getAllRows(in tableName: String, valuesToGet: SQLValues) throws -> [SQLValues]
6666
func getByID(in tableName: String, id: Int, valuesToGet: SQLValues) throws -> SQLValues
6767
func getLastRow(in tableName: String, valuesToGet: SQLValues) throws -> SQLValues
68-
func getLastInsertID() throws -> Int
68+
func getLastInsertID() -> Int
6969
func vacuum() throws
7070
func resetAutoincrement(in tableName: String) throws
7171
func query(sql: String, valuesToBind: SQLValues?) throws
@@ -297,7 +297,7 @@ open class SQLite: SQLiteType {
297297
public func insertRow(sql: String, valuesToBind: SQLValues? = nil) throws -> Int {
298298
try operation(sql: sql, valuesToBind: valuesToBind)
299299
log("successfully inserted row(s), sql: \(sql)")
300-
return Int(sqlite3_last_insert_rowid(dbPointer))
300+
return getLastInsertID()
301301
}
302302

303303
/// Can be used to update one or several rows depending on the SQL statement
@@ -437,17 +437,8 @@ open class SQLite: SQLiteType {
437437
}
438438
}
439439

440-
public func getLastInsertID() throws -> Int {
441-
let sqlStatement = try prepareStatement(sql: "SELECT last_insert_rowid();")
442-
defer {
443-
sqlite3_finalize(sqlStatement)
444-
}
445-
guard sqlite3_step(sqlStatement) == SQLITE_ROW else {
446-
throw SQLiteError.Step(getErrorMessage(dbPointer: dbPointer))
447-
}
448-
let id = sqlite3_column_int(sqlStatement, 0)
449-
log("successfully got last_insert_id: \(id)")
450-
return Int(id)
440+
public func getLastInsertID() -> Int {
441+
return Int(sqlite3_last_insert_rowid(dbPointer))
451442
}
452443

453444
/// Repack the DB to take advantage of deleted data

0 commit comments

Comments
 (0)