Skip to content

Commit

Permalink
Added drop table support
Browse files Browse the repository at this point in the history
  • Loading branch information
noursandid committed May 20, 2024
1 parent 518ac4e commit cc1a445
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Library/ClassHandler/SundeedQLite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ extension SundeedQLite {
.deleteFromDB(tableName: object.getTableName(),
withFilters: filters)
}
func dropTable<T: SundeedQLiter>(forClass sundeedClass: T.Type) async {
let object = sundeedClass.init()
let map = SundeedQLiteMap(fetchingColumns: true)
object.sundeedQLiterMapping(map: map)
await Processor()
.dropTableProcessor
.dropTable(tableName: object.getTableName())
}
public static func deleteDatabase() {
Sundeed.shared.tables.removeAll()
SundeedQLiteConnection.pool.deleteDatabase()
Expand Down
7 changes: 6 additions & 1 deletion Library/ClassHandler/SundeedQLiter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ extension SundeedQLiter {
return objects
}

/** deletes all the objects of this type locally */
/** delete all the objects of this type saved locally */
public static func delete(withFilter filters: SundeedExpression<Bool>...) async {
await SundeedQLite.instance.deleteAllFromDB(forClass: self,
withFilters: filters)
}

/** drop the scheme that holds all the objects of this type */
public static func drop() async {
await SundeedQLite.instance.dropTable(forClass: self)
}

/** updates specific columns of all objects of this class, or objects with a specific criteria */
public static func update(changes: SundeedUpdateSetStatement...,
withFilter filter: SundeedExpression<Bool>? = nil) async throws {
Expand Down
3 changes: 3 additions & 0 deletions Library/Main/Processor/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Processor {
var createTableProcessor: CreateTableProcessor {
CreateTableProcessor()
}
var dropTableProcessor: DropTableProcessor {
DropTableProcessor()
}
var saveProcessor: SaveProcessor {
SaveProcessor()
}
Expand Down
18 changes: 18 additions & 0 deletions Library/Main/Processor/Processors/DropTableProcessor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// DropTableProcessor.swift
// SundeedQLiteLibrary
//
// Created by Nour Sandid on 20/05/2024.
// Copyright © 2024 LUMBERCODE. All rights reserved.
//

import Foundation

class DropTableProcessor {
func dropTable(tableName: String) async {
let dropTableStatement = DropTableStatement(with: tableName).build()
await SundeedQLiteConnection.pool.execute(query: dropTableStatement,
parameters: nil)
Sundeed.shared.tables.removeAll(where: {$0 == tableName})
}
}
20 changes: 20 additions & 0 deletions Library/Main/Statements/DropTableStatement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// DropTableStatement.swift
// SundeedQLiteLibrary
//
// Created by Nour Sandid on 20/05/2024.
// Copyright © 2024 LUMBERCODE. All rights reserved.
//

import Foundation

class DropTableStatement {
private var tableName: String
init(with tableName: String) {
self.tableName = tableName
}
func build() -> String? {
var statement = "DROP TABLE IF EXISTS \(tableName);"
return statement
}
}
8 changes: 8 additions & 0 deletions SundeedQLiteLibrary.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
06BB09FA24746D4400BB668B /* ArrayMandatoryTestWithEmpty.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06BB09F924746D4400BB668B /* ArrayMandatoryTestWithEmpty.swift */; };
06BB09FD2474723900BB668B /* NoPrimariesClasses.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06BB09FC2474723900BB668B /* NoPrimariesClasses.swift */; };
06BB0A052474742E00BB668B /* Listeners.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06BB0A042474742E00BB668B /* Listeners.swift */; };
06D8C7AC2BFBA08F00F2ED75 /* DropTableProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D8C7AB2BFBA08F00F2ED75 /* DropTableProcessor.swift */; };
06D8C7AE2BFBA0EB00F2ED75 /* DropTableStatement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06D8C7AD2BFBA0EB00F2ED75 /* DropTableStatement.swift */; };
06EF758A2467266E00BDA118 /* Processor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06EF75892467266E00BDA118 /* Processor.swift */; };
06EF758D246727CB00BDA118 /* StatementBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06EF758C246727CB00BDA118 /* StatementBuilder.swift */; };
06EF758F2467291500BDA118 /* CreateTableStatement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06EF758E2467291500BDA118 /* CreateTableStatement.swift */; };
Expand Down Expand Up @@ -82,6 +84,8 @@
06BB09F924746D4400BB668B /* ArrayMandatoryTestWithEmpty.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayMandatoryTestWithEmpty.swift; sourceTree = "<group>"; };
06BB09FC2474723900BB668B /* NoPrimariesClasses.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoPrimariesClasses.swift; sourceTree = "<group>"; };
06BB0A042474742E00BB668B /* Listeners.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Listeners.swift; sourceTree = "<group>"; };
06D8C7AB2BFBA08F00F2ED75 /* DropTableProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropTableProcessor.swift; sourceTree = "<group>"; };
06D8C7AD2BFBA0EB00F2ED75 /* DropTableStatement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropTableStatement.swift; sourceTree = "<group>"; };
06EF75892467266E00BDA118 /* Processor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Processor.swift; sourceTree = "<group>"; };
06EF758C246727CB00BDA118 /* StatementBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatementBuilder.swift; sourceTree = "<group>"; };
06EF758E2467291500BDA118 /* CreateTableStatement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateTableStatement.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -243,6 +247,7 @@
062EE090246F2E410095C956 /* Statement.swift */,
06EF758C246727CB00BDA118 /* StatementBuilder.swift */,
06EF758E2467291500BDA118 /* CreateTableStatement.swift */,
06D8C7AD2BFBA0EB00F2ED75 /* DropTableStatement.swift */,
06EF759524677A0000BDA118 /* DeleteStatement.swift */,
06EF75972467848600BDA118 /* InsertStatement.swift */,
06EF75992468123200BDA118 /* UpdateStatement.swift */,
Expand All @@ -266,6 +271,7 @@
children = (
06EF75A72469740500BDA118 /* SaveProcessor.swift */,
06EF75A92469741500BDA118 /* CreateTableProcessor.swift */,
06D8C7AB2BFBA08F00F2ED75 /* DropTableProcessor.swift */,
06EF75AB2469742000BDA118 /* RetrieveProcessor.swift */,
06EF75AD24697F2100BDA118 /* UpdateProcessor.swift */,
);
Expand Down Expand Up @@ -438,6 +444,8 @@
06EF75A5246973C600BDA118 /* ObjectWrapper.swift in Sources */,
06EF7594246771FC00BDA118 /* Sundeed.swift in Sources */,
0611465D24707A1000E522A6 /* SundeedQLiteMap+ArrayMandatory.swift in Sources */,
06D8C7AE2BFBA0EB00F2ED75 /* DropTableStatement.swift in Sources */,
06D8C7AC2BFBA08F00F2ED75 /* DropTableProcessor.swift in Sources */,
06F8A19F234908C6001D1381 /* SundeedQLite.swift in Sources */,
061146592470794000E522A6 /* SundeedQLiteMap+Normal.swift in Sources */,
06EF758A2467266E00BDA118 /* Processor.swift in Sources */,
Expand Down
12 changes: 8 additions & 4 deletions SundeedQLiteLibraryTests/ErrorCodes/SundeedQLiteErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ import XCTest
class SundeedQLiteErrorTests: XCTestCase {
var error: SundeedQLiteError?

override func tearDown() {
SundeedQLite.deleteDatabase()
error = nil
override func tearDown(completion: @escaping ((any Error)?) -> Void) {
Task {
await EmployerForTesting.delete()
await EmployeeForTesting.delete()
error = nil
completion(nil)
}
}

func testPrimaryKeyError() {
let employer = EmployerForTesting()
employer.fillData()
Expand Down
14 changes: 13 additions & 1 deletion SundeedQLiteLibraryTests/Operations/OperationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,23 @@ class OperationTests: XCTestCase {
await employer?.save()
await EmployerForTesting.delete()
let allEmployers = await EmployerForTesting.retrieve()
XCTAssert(allEmployers.isEmpty)
XCTAssertTrue(allEmployers.isEmpty)
let allEmployees = await EmployeeForTesting.retrieve()
XCTAssertEqual(allEmployees.count, 6)
}

func testDrop() async {
await employer?.save()
await EmployerForTesting.drop()
let allEmployers = await EmployerForTesting.retrieve()
XCTAssertTrue(allEmployers.isEmpty)
let allEmployees = await EmployeeForTesting.retrieve()
XCTAssertEqual(allEmployees.count, 6)
await EmployeeForTesting.drop()
let employees = await EmployeeForTesting.retrieve()
XCTAssertEqual(employees.count, 0)
}

func testRetrieve() async {

await employer?.save()
Expand Down
8 changes: 6 additions & 2 deletions SundeedQLiteLibraryTests/Updates/UpdateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ class UpdateTests: XCTestCase {
override func setUp() {
employer?.fillData()
}
override func tearDown() {
SundeedQLite.deleteDatabase()
override func tearDown(completion: @escaping ((any Error)?) -> Void) {
Task {
await EmployerForTesting.delete()
await EmployeeForTesting.delete()
completion(nil)
}
}

func testGlobalUpdate() async {
Expand Down

0 comments on commit cc1a445

Please sign in to comment.