Skip to content

Commit 2c311c1

Browse files
committed
small test fixes
1 parent df25fcd commit 2c311c1

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
extension PostgreSQLConnection {
2-
/// Runs a parameterized `DataQuery`, returning the results as an array of rows.
2+
/// Runs a parameterized `Query`, returning the results as an array of rows.
33
///
44
/// let users = conn.query(.select(.all, from: "users"))
55
///
66
/// Any values bound to the `DataQuery` as placeholders will be sent as query parameters.
77
///
88
/// - parameters:
9-
/// - query: `DataQuery` to execute.
9+
/// - query: `Query` to execute.
1010
/// - returns: A future array of results.
11-
public func query(_ q: DataQuery) -> Future<[[PostgreSQLColumn: PostgreSQLData]]> {
11+
public func query(_ q: Query) -> Future<[[PostgreSQLColumn: PostgreSQLData]]> {
1212
var rows: [[PostgreSQLColumn: PostgreSQLData]] = []
1313
return query(q) { row in
1414
rows.append(row)
@@ -17,7 +17,7 @@ extension PostgreSQLConnection {
1717
}
1818
}
1919

20-
/// Runs a parameterized `DataQuery`, returning each row of the results to the supplied handler one at a time.
20+
/// Runs a parameterized `Query`, returning each row of the results to the supplied handler one at a time.
2121
///
2222
/// try conn.query(.select(.all, from: "users")) { row in
2323
/// print(row)
@@ -26,11 +26,11 @@ extension PostgreSQLConnection {
2626
/// Any values bound to the `DataQuery` as placeholders will be sent as query parameters.
2727
///
2828
/// - parameters:
29-
/// - query: `DataQuery` to execute.
29+
/// - query: `Query` to execute.
3030
/// - resultFormat: Desired `PostgreSQLResultFormat` to request from PostgreSQL. Defaults to `.binary`.
3131
/// - onRow: PostgreSQL row accepting closure to handle results, if any.
3232
/// - returns: A future that signals query completion.
33-
public func query(_ q: DataQuery, resultFormat: PostgreSQLResultFormat = .binary, onRow: @escaping ([PostgreSQLColumn: PostgreSQLData]) throws -> ()) -> Future<Void> {
33+
public func query(_ q: Query, resultFormat: PostgreSQLResultFormat = .binary, onRow: @escaping ([PostgreSQLColumn: PostgreSQLData]) throws -> ()) -> Future<Void> {
3434
var binds = Binds()
3535
let sql = PostgreSQLSerializer().serialize(query: q, binds: &binds)
3636
do {

Sources/PostgreSQL/PostgreSQLRowDecoder.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ public struct PostgreSQLRowDecoder {
88
/// - parameters:
99
/// - decodable: Type to decode.
1010
/// - row: PostgreSQL row to decode.
11-
/// - tableName: Optional table name to use when decoding. If supplied, columns with table names
12-
/// can be matched while decoding. Columns without table names will always match if the column name matches.
11+
/// - tableName: Optional table OID to use when decoding. If supplied, columns with table OIDs
12+
/// can be matched while decoding. Columns without table OIDs will always match if the column name matches.
1313
/// - returns: Instance of Decodable type.
14-
public func decode<D>(_ decodable: D.Type, from row: [DataManipulationQuery.Column: PostgreSQLData], tableName: String? = nil) throws -> D
14+
public func decode<D>(_ decodable: D.Type, from row: [PostgreSQLColumn: PostgreSQLData], tableOID: UInt32? = nil) throws -> D
1515
where D: Decodable
1616
{
17-
let decoder = _PostgreSQLRowDecoder(row: row, tableName: tableName)
17+
let decoder = _PostgreSQLRowDecoder(row: row, tableOID: tableOID)
1818
return try D.init(from: decoder)
1919
}
2020
}
@@ -24,11 +24,11 @@ public struct PostgreSQLRowDecoder {
2424
private final class _PostgreSQLRowDecoder: Decoder {
2525
let codingPath: [CodingKey] = []
2626
let userInfo: [CodingUserInfoKey: Any] = [:]
27-
let data: [DataManipulationQuery.Column: PostgreSQLData]
28-
let tableName: String?
29-
init(row: [DataManipulationQuery.Column: PostgreSQLData], tableName: String?) {
27+
let data: [PostgreSQLColumn: PostgreSQLData]
28+
let tableOID: UInt32?
29+
init(row: [PostgreSQLColumn: PostgreSQLData], tableOID: UInt32?) {
3030
self.data = row
31-
self.tableName = tableName
31+
self.tableOID = tableOID
3232
}
3333

3434
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
@@ -45,8 +45,8 @@ private final class _PostgreSQLRowDecoder: Decoder {
4545
}
4646

4747
func get(key: CodingKey) -> PostgreSQLData? {
48-
guard let value = data[.init(table: tableName, name: key.stringValue)] else {
49-
guard let value = data[.init(table: nil, name: key.stringValue)], tableName != nil else {
48+
guard let value = data[.init(tableOID: tableOID ?? 0, name: key.stringValue)] else {
49+
guard let value = data[.init(tableOID: 0, name: key.stringValue)], tableOID != 0 else {
5050
return nil
5151
}
5252
return value
@@ -73,7 +73,7 @@ private struct PostgreSQLRowKeyedDecodingContainer<K>: KeyedDecodingContainerPro
7373
init(decoder: _PostgreSQLRowDecoder) {
7474
self.decoder = decoder
7575
allKeys = self.decoder.data.keys.compactMap {
76-
if $0.table == decoder.tableName {
76+
if $0.tableOID == decoder.tableOID || $0.tableOID == 0 {
7777
return K(stringValue: $0.name)
7878
} else {
7979
return nil

Sources/PostgreSQL/PostgreSQLRowEncoder.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ public struct PostgreSQLRowEncoder {
33
/// Creates a new `PostgreSQLRowEncoder`.
44
public init() { }
55

6-
/// Encodes an `Encodable` object to `[DataColumn: PostgreSQLData]`.
6+
/// Encodes an `Encodable` object to `[PostgreSQLColumn: PostgreSQLData]`.
77
///
88
/// - parameters:
99
/// - encodable: Item to encode.
10-
/// - tableName: Optional table name to use when encoding.
11-
public func encode<E>(_ encodable: E, tableName: String? = nil) throws -> [DataManipulationQuery.Column: PostgreSQLData]
10+
/// - tableOID: Optional table OID to use when encoding.
11+
public func encode<E>(_ encodable: E, tableOID: UInt32? = nil) throws -> [PostgreSQLColumn: PostgreSQLData]
1212
where E: Encodable
1313
{
14-
let encoder = _PostgreSQLRowEncoder(tableName: tableName)
14+
let encoder = _PostgreSQLRowEncoder(tableOID: tableOID)
1515
try encodable.encode(to: encoder)
1616
return encoder.data
1717
}
@@ -23,11 +23,11 @@ public struct PostgreSQLRowEncoder {
2323
private final class _PostgreSQLRowEncoder: Encoder {
2424
let codingPath: [CodingKey] = []
2525
let userInfo: [CodingUserInfoKey: Any] = [:]
26-
var data: [DataManipulationQuery.Column: PostgreSQLData]
27-
let tableName: String?
28-
init(tableName: String?) {
26+
var data: [PostgreSQLColumn: PostgreSQLData]
27+
let tableOID: UInt32?
28+
init(tableOID: UInt32?) {
2929
self.data = [:]
30-
self.tableName = tableName
30+
self.tableOID = tableOID
3131
}
3232

3333
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
@@ -56,7 +56,7 @@ private struct _PostgreSQLRowKeyedEncodingContainer<K>: KeyedEncodingContainerPr
5656
}
5757

5858
func set(_ key: CodingKey, to value: PostgreSQLDataConvertible) throws {
59-
let col = DataManipulationQuery.Column(table: encoder.tableName, name: key.stringValue)
59+
let col = PostgreSQLColumn(tableOID: encoder.tableOID ?? 0, name: key.stringValue)
6060
self.encoder.data[col] = try value.convertToPostgreSQLData()
6161
}
6262

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ class PostgreSQLConnectionTests: XCTestCase {
478478
defer { _ = try? conn.simpleQuery("DROP TABLE users").wait() }
479479

480480
let save = try conn.query(.dml(
481-
statement: .insert(),
481+
statement: .insert,
482482
table: "users",
483483
columns: [
484484
"id": .bind(42),
@@ -488,15 +488,15 @@ class PostgreSQLConnectionTests: XCTestCase {
488488
XCTAssertEqual(save.count, 0)
489489

490490
let search = try conn.query(.dml(
491-
statement: .select(),
491+
statement: .select,
492492
table: "users",
493493
keys: [.all],
494-
predicates: [.predicate("name", .equal, .bind("vapor"))]
494+
predicate: .predicate("name", .equal, .bind("vapor"))
495495
)).wait()
496496
XCTAssertEqual(search.count, 1)
497497

498498

499-
try conn.query(.select("id", "name", from: "users")) { row in
499+
try conn.query(.select(["id", "name"], from: "users")) { row in
500500
print(row)
501501
}.wait()
502502
}
@@ -522,7 +522,7 @@ class PostgreSQLConnectionTests: XCTestCase {
522522
extension PostgreSQLConnection {
523523
/// Creates a test event loop and psql client over ssl.
524524
static func makeTest(transport: PostgreSQLTransportConfig) throws -> PostgreSQLConnection {
525-
#if os(macOS)
525+
#if Xcode
526526
return try _makeTest(serverAddress: .tcp(hostname: "localhost", port: transport.isTLS ? 5433 : 5432), password: "vapor_password", transport: transport)
527527
#else
528528
return try _makeTest(serverAddress: .tcp(hostname: transport.isTLS ? "tls" : "cleartext", port: 5432), password: "vapor_password", transport: transport)
@@ -531,10 +531,10 @@ extension PostgreSQLConnection {
531531

532532
/// Creates a test connection.
533533
private static func _makeTest(serverAddress: PostgreSQLDatabaseConfig.ServerAddress, password: String? = nil, transport: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection {
534-
let group = MultiThreadedEventLoopGroup(numThreads: 1)
534+
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
535535
let client = try PostgreSQLConnection.connect(to: serverAddress, transport: transport, on: group) { error in
536536
XCTFail("\(error)")
537-
}.wait()
537+
}.wait()
538538
_ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: password).wait()
539539
return client
540540
}

0 commit comments

Comments
 (0)