Skip to content

Commit 4ee575e

Browse files
committed
Added new method checkIfIndexExists(in:indexName:)
1 parent 422978c commit 4ee575e

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

Sources/SQLiteAdapter/SQLiteAdapter.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public protocol SQLiteType {
5151
func dropTable(_ tableName: String, vacuum: Bool) throws
5252
func deleteAllRows(in tableName: String, vacuum: Bool, resetAutoincrement: Bool) throws
5353
func addIndex(to tableName: String, forColumn columnName: String, unique: Bool, order: SQLOrder) throws
54+
func checkIfIndexExists(in tableName: String, indexName: String) throws -> Bool
5455
func dropIndex(in tableName: String, forColumn columnName: String) throws
5556
func beginTransaction() throws
5657
func endTransaction() throws
@@ -268,6 +269,15 @@ open class SQLite: SQLiteType {
268269
log("successfully added index \(indexName)")
269270
}
270271

272+
public func checkIfIndexExists(in tableName: String, indexName: String) throws -> Bool {
273+
if let count = try? getRowCountWithCondition(sql: "SELECT count(*) FROM sqlite_schema WHERE type='index' AND tbl_name='\(tableName)' AND name='\(indexName)';", valuesToBind: nil) {
274+
let result = count == 1 ? true : false
275+
log("successfully checked if index \(indexName) exists: \(result)")
276+
return result
277+
}
278+
throw SQLiteError.Other(getErrorMessage(dbPointer: dbPointer))
279+
}
280+
271281
public func dropIndex(in tableName: String, forColumn columnName: String) throws {
272282
let indexName = "\(tableName)_\(columnName)_idx"
273283
let sql = "DROP INDEX IF EXISTS \"\(indexName)\";"

0 commit comments

Comments
 (0)