Skip to content

Commit

Permalink
Kitura/Swift-Kuery#59 Allow nil parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
irar2 committed Mar 2, 2017
1 parent 11cdb54 commit bea4182
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let package = Package(
name: "SwiftKueryPostgreSQL",
dependencies: [
.Package(url: "https://github.com/IBM-Swift/CLibpq.git", majorVersion: 0, minor: 1),
.Package(url: "https://github.com/IBM-Swift/Swift-Kuery.git", majorVersion: 0, minor: 6),
.Package(url: "https://github.com/IBM-Swift/Swift-Kuery.git", majorVersion: 0, minor: 7),
],
exclude: ["Configuration", "Scripts"]
)
21 changes: 12 additions & 9 deletions Sources/SwiftKueryPostgreSQL/PostgreSQLConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class PostgreSQLConnection: Connection {
/// - Parameter query: The query to execute.
/// - Parameter parameters: An array of the parameters.
/// - Parameter onCompletion: The function to be called when the execution of the query has completed.
public func execute(query: Query, parameters: [Any], onCompletion: @escaping ((QueryResult) -> ())) {
public func execute(query: Query, parameters: [Any?], onCompletion: @escaping ((QueryResult) -> ())) {
do {
let postgresQuery = try query.build(queryBuilder: queryBuilder)
executeQueryWithParameters(query: postgresQuery, parameters: parameters, onCompletion: onCompletion)
Expand Down Expand Up @@ -169,7 +169,7 @@ public class PostgreSQLConnection: Connection {
/// - Parameter query: A String with the query to execute.
/// - Parameter parameters: An array of the parameters.
/// - Parameter onCompletion: The function to be called when the execution of the query has completed.
public func execute(_ raw: String, parameters: [Any], onCompletion: @escaping ((QueryResult) -> ())) {
public func execute(_ raw: String, parameters: [Any?], onCompletion: @escaping ((QueryResult) -> ())) {
executeQueryWithParameters(query: raw, parameters: parameters, onCompletion: onCompletion)
}

Expand All @@ -179,14 +179,17 @@ public class PostgreSQLConnection: Connection {
processQueryResult(query: query, onCompletion: onCompletion)
}

private func executeQueryWithParameters(query: String, parameters: [Any], onCompletion: @escaping ((QueryResult) -> ())) {
private func executeQueryWithParameters(query: String, parameters: [Any?], onCompletion: @escaping ((QueryResult) -> ())) {
var parameterData = [UnsafePointer<Int8>?]()
// At the moment we only create string parameters. Binary parameters should be added.
for parameter in parameters {
let value = AnyCollection("\(parameter)".utf8CString)
let pointer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(value.count))
for (index, byte) in value.enumerated() {
pointer[index] = byte
var pointer: UnsafeMutablePointer<Int8>?
if parameter != nil {
let value = AnyCollection("\(parameter!)".utf8CString)
pointer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(value.count))
for (index, byte) in value.enumerated() {
pointer![index] = byte
}
}
parameterData.append(pointer)
}
Expand Down Expand Up @@ -226,7 +229,7 @@ public class PostgreSQLConnection: Connection {
/// - Parameter query: The query to execute.
/// - Parameter parameters: A dictionary of the parameters with parameter names as the keys.
/// - Parameter onCompletion: The function to be called when the execution of the query has completed.
public func execute(query: Query, parameters: [String:Any], onCompletion: @escaping ((QueryResult) -> ())) {
public func execute(query: Query, parameters: [String:Any?], onCompletion: @escaping ((QueryResult) -> ())) {
onCompletion(.error(QueryError.unsupported("Named parameters are not supported in PostgreSQL")))
}

Expand All @@ -235,7 +238,7 @@ public class PostgreSQLConnection: Connection {
/// - Parameter query: A String with the query to execute.
/// - Parameter parameters: A dictionary of the parameters with parameter names as the keys.
/// - Parameter onCompletion: The function to be called when the execution of the query has completed.
public func execute(_ raw: String, parameters: [String:Any], onCompletion: @escaping ((QueryResult) -> ())) {
public func execute(_ raw: String, parameters: [String:Any?], onCompletion: @escaping ((QueryResult) -> ())) {
onCompletion(.error(QueryError.unsupported("Named parameters are not supported in PostgreSQL")))
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftKueryPostgreSQLTests/CommonUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func executeQuery(query: Query, connection: Connection, callback: @escaping (Que
}
}

func executeQueryWithParameters(query: Query, connection: Connection, parameters: Any..., callback: @escaping (QueryResult, [[Any?]]?)->()) {
func executeQueryWithParameters(query: Query, connection: Connection, parameters: Any?..., callback: @escaping (QueryResult, [[Any?]]?)->()) {
do {
try print("=======\(connection.descriptionOf(query: query))=======")
}
Expand All @@ -73,7 +73,7 @@ func executeQueryWithParameters(query: Query, connection: Connection, parameters
}
}

func executeRawQueryWithParameters(_ raw: String, connection: Connection, parameters: Any..., callback: @escaping (QueryResult, [[Any?]]?)->()) {
func executeRawQueryWithParameters(_ raw: String, connection: Connection, parameters: Any?..., callback: @escaping (QueryResult, [[Any?]]?)->()) {
print("=======\(raw)=======")
connection.execute(raw, parameters: parameters) { result in
let rows = printResultAndGetRowsAsArray(result)
Expand Down
5 changes: 5 additions & 0 deletions Tests/SwiftKueryPostgreSQLTests/TestParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class TestParameters: XCTestCase {
XCTAssertEqual(rows!.count, 3, "SELECT returned wrong number of rows: \(rows!.count) instead of 3")
XCTAssertEqual(rows![2][0]! as! String, "banana", "Wrong value in row 0 column 0: \(rows![2][0]) instead of 'peach'")
XCTAssertEqual(rows![2][1]! as! String, "4", "Wrong value in row 0 column 0: \(rows![2][1]) instead of 4")

let s2 = Select(from: t).where(t.a != Parameter())
executeQueryWithParameters(query: s2, connection: connection, parameters: nil) { result, rows in
XCTAssertEqual(result.success, true, "SELECT failed")
}
}
}
}
Expand Down

0 comments on commit bea4182

Please sign in to comment.