From eb0b5a6911396c6a66279689bb82c8b9f787f75c Mon Sep 17 00:00:00 2001 From: Florian Kostenzer Date: Sun, 9 Jun 2019 09:57:01 +0200 Subject: [PATCH] Parse UInt8/Int to Bool if expected type is Bool, Convenience Query call using Database class --- Sources/SwiftKueryORM/Database.swift | 44 +++++++++++++++++++++ Sources/SwiftKueryORM/DatabaseDecoder.swift | 25 +++++++++--- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/Sources/SwiftKueryORM/Database.swift b/Sources/SwiftKueryORM/Database.swift index 33653f9..adbe724 100644 --- a/Sources/SwiftKueryORM/Database.swift +++ b/Sources/SwiftKueryORM/Database.swift @@ -126,3 +126,47 @@ public class Database { } } } + +extension Query { + + /// Execute the query. + /// + /// - Parameter using: Optional Database to use, if none specified the default database will be used + /// - Parameter onCompletion: The function to be called when the execution of the query has completed. + public func execute(using db: Database?, onCompletion: @escaping ((SwiftKuery.QueryResult) -> ())) { + guard let db = db ?? Database.default else { + return onCompletion(.error(QueryError.databaseError("ORM database not initialised"))) + } + db.executeTask { (connection, error) in + guard let connection = connection else { + onCompletion(.error(error ?? QueryError.connection("Failed to get database connection"))) + return + } + self.execute(connection, onCompletion: { (result) in + onCompletion(result) + }) + } + } + + + /// Execute the query with parameters. + /// + /// - Parameter using: Optional Database to use, if none specified the default database will be used + /// - Parameter parameters: An array of the query parameters. + /// - Parameter onCompletion: The function to be called when the execution of the query has completed. + public func execute(using db: Database?, parameters: [Any], onCompletion: @escaping ((SwiftKuery.QueryResult) -> ())) { + guard let db = db ?? Database.default else { + return onCompletion(.error(QueryError.databaseError("ORM database not initialised"))) + } + db.executeTask { (connection, error) in + guard let connection = connection else { + onCompletion(.error(error ?? QueryError.connection("Failed to get database connection"))) + return + } + self.execute(connection, parameters: parameters, onCompletion: { (result) in + onCompletion(result) + }) + } + } +} + diff --git a/Sources/SwiftKueryORM/DatabaseDecoder.swift b/Sources/SwiftKueryORM/DatabaseDecoder.swift index 0835065..6278289 100644 --- a/Sources/SwiftKueryORM/DatabaseDecoder.swift +++ b/Sources/SwiftKueryORM/DatabaseDecoder.swift @@ -98,13 +98,26 @@ open class DatabaseDecoder { /// Cast value from database to expect type in the model private func castedValue(_ value: Any?, _ type: T.Type, _ key: Key) throws -> T { - guard let castedValue = value as? T else { - throw DecodingError.typeMismatch(type, DecodingError.Context( - codingPath: [key], - debugDescription: "Could not cast " + String(describing: value) - )) + if T.self is Bool.Type { + if let intValue = value as? UInt8, (intValue == 0 || intValue == 1) { + return (intValue == 1) as! T + } else if let intValue = value as? Int8, (intValue == 0 || intValue == 1) { + return (intValue == 1) as! T + } else { + throw DecodingError.typeMismatch(type, DecodingError.Context( + codingPath: [key], + debugDescription: "Could not cast " + String(describing: value) + )) + } + } else { + guard let castedValue = value as? T else { + throw DecodingError.typeMismatch(type, DecodingError.Context( + codingPath: [key], + debugDescription: "Could not cast " + String(describing: value) + )) + } + return castedValue } - return castedValue } /// Special case for integer, no integer type in database