Skip to content

Commit 74833fd

Browse files
committed
Added new method getLastRow(in:valuesToGet:)
1 parent ce1188f commit 74833fd

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

Sources/SQLiteAdapter/SQLiteAdapter.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public protocol SQLiteType {
6464
func getRow(sql: String, valuesToBind: SQLValues?, valuesToGet: SQLValues) throws -> [SQLValues]
6565
func getAllRows(in tableName: String, valuesToGet: SQLValues) throws -> [SQLValues]
6666
func getByID(in tableName: String, id: Int, valuesToGet: SQLValues) throws -> SQLValues
67+
func getLastRow(in tableName: String, valuesToGet: SQLValues) throws -> SQLValues
6768
func getLastInsertID() throws -> Int
6869
func vacuum() throws
6970
func resetAutoincrement(in tableName: String) throws
@@ -418,6 +419,17 @@ open class SQLite: SQLiteType {
418419
}
419420
}
420421

422+
public func getLastRow(in tableName: String, valuesToGet: SQLValues) throws -> SQLValues {
423+
let sql = "SELECT * FROM \(tableName) WHERE id = (SELECT MAX(id) FROM \(tableName));"
424+
let result = try getRow(sql: sql, valuesToBind: nil, valuesToGet: valuesToGet)
425+
if result.count == 1 {
426+
log("successfully read last row in \(tableName)")
427+
return result[0]
428+
} else {
429+
throw SQLiteError.Column(getErrorMessage(dbPointer: dbPointer))
430+
}
431+
}
432+
421433
public func getLastInsertID() throws -> Int {
422434
let sqlStatement = try prepareStatement(sql: "SELECT last_insert_rowid();")
423435
defer {

0 commit comments

Comments
 (0)