Skip to content
This repository has been archived by the owner on Mar 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from codynhat/master
Browse files Browse the repository at this point in the history
Uses Node instead of PostgreSQL.Value
  • Loading branch information
tanner0101 authored Aug 31, 2016
2 parents 63923de + 9d86ead commit 57088a6
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 104 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PackageDescription
let package = Package(
name: "PostgreSQL",
dependencies: [
.Package(url: "https://github.com/qutheory/cpostgresql.git", majorVersion: 0)
.Package(url: "https://github.com/qutheory/cpostgresql.git", majorVersion: 0),
.Package(url: "https://github.com/vapor/node.git", majorVersion: 0, minor: 4)
]
)
12 changes: 6 additions & 6 deletions Sources/PostgreSQL/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Database {
}

@discardableResult
public func execute(_ query: String, _ values: [Value]? = [], on connection: Connection? = nil) throws -> [[String: Value]] {
public func execute(_ query: String, _ values: [NodeRepresentable]? = [], on connection: Connection? = nil) throws -> [[String: Node]] {
let internalConnection: Connection

if let conn = connection {
Expand All @@ -44,7 +44,7 @@ public class Database {

let res: Result.ResultPointer
if let values = values, values.count > 0 {
let paramsValues = bind(values)
let paramsValues = bind(try values.map { try $0.makeNode() })
res = PQexecParams(internalConnection.connection, query, Int32(values.count), nil, paramsValues, nil, nil, Int32(0))

defer {
Expand All @@ -58,11 +58,11 @@ public class Database {
defer { PQclear(res) }
switch Status(result: res) {
case .nonFatalError:
throw DatabaseError.invalidSQL(message: String(cString: PQresultErrorMessage(res)) ?? "")
throw DatabaseError.invalidSQL(message: String(cString: PQresultErrorMessage(res)) )
case .fatalError:
throw DatabaseError.invalidSQL(message: String(cString: PQresultErrorMessage(res)) ?? "")
throw DatabaseError.invalidSQL(message: String(cString: PQresultErrorMessage(res)) )
case .unknown:
throw DatabaseError.invalidSQL(message: String(cString: PQresultErrorMessage(res)) ?? "An unknown error has occurred")
throw DatabaseError.invalidSQL(message: String(cString: PQresultErrorMessage(res)) )
case .tuplesOk:
return Result(resultPointer: res).dictionary
default:
Expand All @@ -71,7 +71,7 @@ public class Database {
return []
}

func bind(_ values: [Value]) -> UnsafeMutablePointer<UnsafePointer<Int8>?> {
func bind(_ values: [Node]) -> UnsafeMutablePointer<UnsafePointer<Int8>?> {


let paramsValues = UnsafeMutablePointer<UnsafePointer<Int8>?>.allocate(capacity: values.count)
Expand Down
41 changes: 41 additions & 0 deletions Sources/PostgreSQL/Node+Oid.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#if os(Linux)
import CPostgreSQLLinux
#else
import CPostgreSQLMac
#endif

public enum OID: Oid {
case text = 25
case varchar = 1043
case int2 = 21
case int4 = 23
case int8 = 20
case float4 = 700
case float8 = 701
case numeric = 1700
case bool = 16
case char = 18
case unknown = 705
}

extension Node {
init(oid: Oid, value: String) {
guard let type = OID(rawValue: oid) else {
self = .null
return
}

switch type {
case .text, .varchar, .char:
self = .string(value)
case .int2, .int4, .int8:
self = .number(.int(Int(value) ?? 0))
case .float4, .float8, .numeric:
self = .number(.double(Double(value) ?? 0))
case .bool:
self = .bool((value == "true") ? true : false)
case .unknown:
self = .null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
import Foundation


extension Value {
extension Node {
var utf8: String.UTF8View {
switch self {
case .string(let str):
return str.utf8
case .double(let double):
return String(double).utf8
case .int(let int):
return String(int).utf8
case .bool(let bool):
return bool == 1 ? "true".utf8 : "false".utf8
case .null:
return bool ? "true".utf8 : "false".utf8
case .number(.double(let double)):
return String(double).utf8
case .number(.int(let int)):
return String(int).utf8
default:
return "".utf8
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/PostgreSQL/PostgreSQL+Node.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@_exported import Node
10 changes: 5 additions & 5 deletions Sources/PostgreSQL/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ public class Result {
self.resultPointer = resultPointer
}

lazy var dictionary: [[String: Value]] = { [unowned self] in
lazy var dictionary: [[String: Node]] = { [unowned self] in
let rowCount = Int(PQntuples(self.resultPointer))
let columnCount = Int(PQnfields(self.resultPointer))

guard rowCount > 0 && columnCount > 0 else {
return []
}

var parsedData = [[String: Value]]()
var parsedData = [[String: Node]]()

for row in 0..<rowCount {
var item = [String: Value]()
var item = [String: Node]()
for column in 0..<columnCount {
let name = String(cString: PQfname(self.resultPointer, Int32(column)))

if PQgetisnull(self.resultPointer, Int32(row), Int32(column)) == 1 {
item[name] = .null
} else {
let value = String(cString: PQgetvalue(self.resultPointer, Int32(row), Int32(column))) ?? ""
let value = String(cString: PQgetvalue(self.resultPointer, Int32(row), Int32(column)))
let type = PQftype(self.resultPointer, Int32(column))
item[name] = Value(oid: type, value: value)
item[name] = Node(oid: type, value: value)

}
}
Expand Down
42 changes: 0 additions & 42 deletions Sources/PostgreSQL/Value+Oid.swift

This file was deleted.

12 changes: 0 additions & 12 deletions Sources/PostgreSQL/Value.swift

This file was deleted.

8 changes: 4 additions & 4 deletions Tests/PostgreSQL/PostgreSQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class PostgreSQLTests: XCTestCase {
try postgreSQL.execute("INSERT INTO parameterization VALUES (NULL, NULL, 'life', 42)")
try postgreSQL.execute("INSERT INTO parameterization VALUES (NULL, -1, 'test', NULL)")

if let result = try postgreSQL.execute("SELECT * FROM parameterization WHERE d = $1", [.string("3.14")]).first {
if let result = try postgreSQL.execute("SELECT * FROM parameterization WHERE d = $1", [3.14]).first {
XCTAssertEqual(result["d"]?.double, 3.14)
XCTAssertEqual(result["i"]?.int, nil)
XCTAssertEqual(result["s"]?.string, "pi")
Expand All @@ -87,7 +87,7 @@ class PostgreSQLTests: XCTestCase {
XCTFail("Could not get pi result")
}

if let result = try postgreSQL.execute("SELECT * FROM parameterization WHERE u = $1", [.string("42")]).first {
if let result = try postgreSQL.execute("SELECT * FROM parameterization WHERE u = $1", [42]).first {
XCTAssertEqual(result["d"]?.double, nil)
XCTAssertEqual(result["i"]?.int, nil)
XCTAssertEqual(result["s"]?.string, "life")
Expand All @@ -96,7 +96,7 @@ class PostgreSQLTests: XCTestCase {
XCTFail("Could not get life result")
}

if let result = try postgreSQL.execute("SELECT * FROM parameterization WHERE i = $1", [.string("-1")]).first {
if let result = try postgreSQL.execute("SELECT * FROM parameterization WHERE i = $1", [-1]).first {
XCTAssertEqual(result["d"]?.double, nil)
XCTAssertEqual(result["i"]?.int, -1)
XCTAssertEqual(result["s"]?.string, "test")
Expand All @@ -105,7 +105,7 @@ class PostgreSQLTests: XCTestCase {
XCTFail("Could not get test by int result")
}

if let result = try postgreSQL.execute("SELECT * FROM parameterization WHERE s = $1", [.string("test")]).first {
if let result = try postgreSQL.execute("SELECT * FROM parameterization WHERE s = $1", ["test"]).first {
XCTAssertEqual(result["d"]?.double, nil)
XCTAssertEqual(result["i"]?.int, -1)
XCTAssertEqual(result["s"]?.string, "test")
Expand Down
27 changes: 0 additions & 27 deletions Tests/PostgreSQL/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,3 @@ extension PostgreSQL.Database {
}
}

// Makes fetching values during tests easier
extension PostgreSQL.Value {
var string: String? {
guard case .string(let string) = self else {
return nil
}

return string
}

var int: Int? {
guard case .int(let int) = self else {
return nil
}

return int
}

var double: Double? {
guard case .double(let double) = self else {
return nil
}

return double
}

}

0 comments on commit 57088a6

Please sign in to comment.