Skip to content

Commit 67c2243

Browse files
committed
Added new method checkIfTableExists(_:)
1 parent ba929b2 commit 67c2243

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

Sources/SQLiteAdapter/SQLiteAdapter.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ open class SQLite {
191191
log("successfully created table, sql: \(sql)")
192192
}
193193

194+
public func checkIfTableExists(_ tableName: String) throws -> Bool {
195+
if let count = try? getRowCountWithCondition(sql: "SELECT count(*) FROM sqlite_schema WHERE type='table' AND name='\(tableName)';", valuesToBind: nil) {
196+
let result = count == 1 ? true : false
197+
log("successfully checked if \(tableName) exists: \(result)")
198+
return result
199+
}
200+
throw SQLiteError.Other(getErrorMessage(dbPointer: dbPointer))
201+
}
202+
194203
public func dropTable(_ tableName: String, vacuum: Bool = true) throws {
195204
let sql = "DROP TABLE IF EXISTS \(tableName);"
196205
try operation(sql: sql)

0 commit comments

Comments
 (0)