Skip to content

Commit

Permalink
feat: Chris's Requested Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
EnriqueL8 committed Apr 11, 2018
1 parent 1290c9b commit 02ac0d1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
51 changes: 51 additions & 0 deletions Sources/SwiftKueryORM/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public protocol Model: Codable {

static func delete(id: Identifier, using db: Database?, _ onCompletion: @escaping (RequestError?) -> Void)
static func deleteAll(using db: Database?, _ onCompletion: @escaping (RequestError?) -> Void)
static func deleteAll<Q: QueryParams>(using db: Database?, matching queryParams:Q, _ onCompletion: @escaping (RequestError?) -> Void)

static func getTable() throws -> Table
}
Expand Down Expand Up @@ -1046,6 +1047,56 @@ public extension Model {
}
}

/// Delete all the models matching the QueryParams
/// - Parameter using: Optional Database to use
/// - Returns: An optional RequestError
static func deleteAll<Q: QueryParams>(using db: Database? = nil, matching queryParams: Q, _ onCompletion: @escaping (RequestError?) -> Void) {
guard let database = db ?? Database.default else {
onCompletion(.ormDatabaseNotInitialized)
return
}
guard let connection = database.getConnection() else {
onCompletion(.ormConnectionFailed)
return
}

var table: Table
do {
table = try Self.getTable()
} catch {
onCompletion(Self.convertError(error))
return
}

var filter: Filter
do {
filter = try Self.getFilter(queryParams: queryParams, table: table)
} catch {
onCompletion(Self.convertError(error))
return
}

let query = Delete(from: table).where(filter)

connection.connect {error in
if let error = error {
onCompletion(Self.convertError(error))
return
} else {
connection.execute(query: query) { result in
guard result.success else {
guard let error = result.asError else {
onCompletion(Self.convertError(QueryError.databaseError("Query failed to execute but error was nil")))
return
}
onCompletion(Self.convertError(error))
return
}
onCompletion(nil)
}
}
}
}
static func getTable() throws -> Table {
return try Database.tableInfo.getTable((Self.idColumnName, Self.idColumnType), Self.tableName, for: Self.self)
}
Expand Down
33 changes: 33 additions & 0 deletions Tests/SwiftKueryORMTests/TestDelete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TestDelete: XCTestCase {
return [
("testDeleteWithId", testDeleteWithId),
("testDeleteAll", testDeleteAll),
("testDeleteAllMatching", testDeleteAllMatching),
]
}

Expand Down Expand Up @@ -57,4 +58,36 @@ class TestDelete: XCTestCase {
}
})
}

struct Filter: QueryParams {
let name: String
let age: Int
}

/**
Testing that the correct SQL Query is created to delete all model matching the QueryParams
*/
func testDeleteAllMatching() {
let connection: TestConnection = createConnection()
Database.default = Database(single: connection)
let filter = Filter(name: "Joe", age: 38)
performTest(asyncTasks: { expectation in
Person.deleteAll(matching: filter) { error in
XCTAssertNil(error, "Delete Failed: \(String(describing: error))")
XCTAssertNotNil(connection.query, "Delete Failed: Query is nil")
if let query = connection.query {
let expectedPrefix = "DELETE FROM People WHERE"
let expectedClauses = ["People.name = 'Joe'", "People.age = '38'"]
let expectedOperator = "AND"
let resultQuery = connection.descriptionOf(query: query)
XCTAssertTrue(resultQuery.hasPrefix(expectedPrefix))
for whereClause in expectedClauses {
XCTAssertTrue(resultQuery.contains(whereClause))
}
XCTAssertTrue(resultQuery.contains(expectedOperator))
}
expectation.fulfill()
}
})
}
}
6 changes: 3 additions & 3 deletions Tests/SwiftKueryORMTests/TestFind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TestFind: XCTestCase {
return [
("testFind", testFind),
("testFindAll", testFindAll),
("testFindAllWhere", testFindAllWhere),
("testFindAllMatching", testFindAllMatching),
]
}

Expand Down Expand Up @@ -79,12 +79,12 @@ class TestFind: XCTestCase {
Testing that the correct SQL Query is created to retrieve all the models.
Testing that correct amount of models are retrieved
*/
func testFindAllWhere() {
func testFindAllMatching() {
let connection: TestConnection = createConnection(.returnOneRow)
Database.default = Database(single: connection)
let filter = Filter(name: "Joe", age: 38)
performTest(asyncTasks: { expectation in
Person.findAll(filterOn: filter) { array, error in
Person.findAll(matching: filter) { array, error in
XCTAssertNil(error, "Find Failed: \(String(describing: error))")
XCTAssertNotNil(connection.query, "Find Failed: Query is nil")
if let query = connection.query {
Expand Down

0 comments on commit 02ac0d1

Please sign in to comment.