Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse UInt8/Int to Bool if expected type is Bool, Convenience Query function #111

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Sources/SwiftKueryORM/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
}

25 changes: 19 additions & 6 deletions Sources/SwiftKueryORM/DatabaseDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,26 @@ open class DatabaseDecoder {

/// Cast value from database to expect type in the model
private func castedValue<T : Any>(_ 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
Expand Down