Skip to content

Commit 171658c

Browse files
committed
psql data updates
1 parent a2fd67a commit 171658c

27 files changed

+1202
-818
lines changed

Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift

+30-30
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ extension PostgreSQLConnection {
2525
let parse = PostgreSQLParseRequest(
2626
statementName: "",
2727
query: string,
28-
parameterTypes: parameters.map { .type(forData: $0) }
28+
parameterTypes: parameters.map { $0.type }
2929
)
3030
let describe = PostgreSQLDescribeRequest(type: .statement, name: "")
3131
var currentRow: PostgreSQLRowDescription?
3232
var currentParameters: PostgreSQLParameterDescription?
33+
3334
return send([
3435
.parse(parse), .describe(describe), .sync
3536
]) { message in
@@ -41,18 +42,16 @@ extension PostgreSQLConnection {
4142
default: fatalError("Unexpected message during PostgreSQLParseRequest: \(message)")
4243
}
4344
}.flatMap(to: Void.self) {
44-
let parameterDataTypes = currentParameters?.dataTypes ?? [] // no parameters
45-
let resultDataTypes = currentRow?.fields.map { $0.dataType } ?? [] // nil currentRow means no resutls
45+
// let parameterDataTypes = currentParameters?.dataTypes ?? [] // no parameters
46+
// let resultDataTypes = currentRow?.fields.map { $0.dataType } ?? [] // nil currentRow means no resutls
4647

4748
// cache so we don't compute twice
48-
let _parameterFormats = parameterDataTypes.map { $0.preferredFormat }
49-
let _resultFormats = resultDataTypes.map { $0.preferredFormat }
50-
let bind = try PostgreSQLBindRequest(
49+
let bind = PostgreSQLBindRequest(
5150
portalName: "",
5251
statementName: "",
53-
parameterFormatCodes: _parameterFormats,
54-
parameters: parameters.enumerated().map { try .make(data: $0.1, format: _parameterFormats[$0.0]) },
55-
resultFormatCodes: _resultFormats
52+
parameterFormatCodes: parameters.map { $0.format },
53+
parameters: parameters.map { .init(data: $0.data) },
54+
resultFormatCodes: [.text]
5655
)
5756
let execute = PostgreSQLExecuteRequest(
5857
portalName: "",
@@ -65,7 +64,7 @@ extension PostgreSQLConnection {
6564
case .bindComplete: break
6665
case .dataRow(let data):
6766
let row = currentRow !! "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription."
68-
let parsed = try row.parse(data: data, formats: _resultFormats)
67+
let parsed = try row.parse(data: data)
6968
onRow(parsed)
7069
case .close: break
7170
case .noData: break
@@ -78,24 +77,25 @@ extension PostgreSQLConnection {
7877

7978
/// MARK: Codable
8079

81-
extension PostgreSQLConnection {
82-
/// Sends a parameterized PostgreSQL query command, collecting the parsed results.
83-
public func query(
84-
_ string: String,
85-
encoding parameters: [Encodable]
86-
) throws -> Future<[[String: PostgreSQLData]]> {
87-
let parameters = try parameters.map { try PostgreSQLDataEncoder().encode($0) }
88-
return try query(string, parameters)
89-
}
80+
//extension PostgreSQLConnection {
81+
// /// Sends a parameterized PostgreSQL query command, collecting the parsed results.
82+
// public func query(
83+
// _ string: String,
84+
// encoding parameters: [Encodable]
85+
// ) throws -> Future<[[String: PostgreSQLData]]> {
86+
// let parameters = try parameters.map { try PostgreSQLDataEncoder().encode($0) }
87+
// return try query(string, parameters)
88+
// }
89+
//
90+
// /// Sends a parameterized PostgreSQL query command, returning the parsed results to
91+
// /// the supplied closure.
92+
// public func query(
93+
// _ string: String,
94+
// encoding parameters: [Encodable],
95+
// onRow: @escaping ([String: PostgreSQLData]) -> ()
96+
// ) throws -> Future<Void> {
97+
// let parameters = try parameters.map { try PostgreSQLDataEncoder().encode($0) }
98+
// return try query(string, parameters, onRow: onRow)
99+
// }
100+
//}
90101

91-
/// Sends a parameterized PostgreSQL query command, returning the parsed results to
92-
/// the supplied closure.
93-
public func query(
94-
_ string: String,
95-
encoding parameters: [Encodable],
96-
onRow: @escaping ([String: PostgreSQLData]) -> ()
97-
) throws -> Future<Void> {
98-
let parameters = try parameters.map { try PostgreSQLDataEncoder().encode($0) }
99-
return try query(string, parameters, onRow: onRow)
100-
}
101-
}

Sources/PostgreSQL/Connection/PostgreSQLConnection+SimpleQuery.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension PostgreSQLConnection {
2323
currentRow = row
2424
case .dataRow(let data):
2525
let row = currentRow !! "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription."
26-
let parsed = try row.parse(data: data, formats: row.fields.map { $0.formatCode })
26+
let parsed = try row.parse(data: data)
2727
onRow(parsed)
2828
case .close: break // query over, waiting for `readyForQuery`
2929
default: fatalError("Unexpected message during PostgreSQLQuery: \(message)")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// Encodes `Decodable` items to `CodableData`.
2+
public final class CodableDataDecoder {
3+
/// Creates a new `CodableDataDecoder`.
4+
public init() {}
5+
6+
/// Decodes the supplied `Decodable` to `CodableData`
7+
public func decode<D>(_ type: D.Type = D.self, from data: CodableData) throws -> D
8+
where D: Decodable
9+
{
10+
let decoder = _CodableDataDecoder(partialData: .init(data: .null), at: [])
11+
return try D(from: decoder)
12+
}
13+
}
14+
15+
/// Internal `Decoder` implementation for `CodableDataDecoder`.
16+
internal final class _CodableDataDecoder: Decoder {
17+
/// See `Decoder.codingPath`
18+
var codingPath: [CodingKey]
19+
20+
/// See `Decoder.codingPath`
21+
var userInfo: [CodingUserInfoKey: Any]
22+
23+
/// Data being encoded.
24+
let partialData: PartialCodableData
25+
26+
/// Creates a new internal `_CodableDataDecoder`.
27+
init(partialData: PartialCodableData, at path: [CodingKey]) {
28+
self.codingPath = path
29+
self.userInfo = [:]
30+
self.partialData = partialData
31+
}
32+
33+
/// See `Decoder.container`
34+
func container<Key>(keyedBy type: Key.Type) -> KeyedDecodingContainer<Key>
35+
where Key: CodingKey
36+
{
37+
let container = CodableDataKeyedDecodingContainer<Key>(partialData: partialData, at: codingPath)
38+
return .init(container)
39+
}
40+
41+
/// See `Decoder.unkeyedContainer`
42+
func unkeyedContainer() -> UnkeyedDecodingContainer {
43+
return CodableDataUnkeyedDecodingContainer(partialData: partialData, at: codingPath)
44+
}
45+
46+
/// See `Decoder.singleValueContainer`
47+
func singleValueContainer() -> SingleValueDecodingContainer {
48+
return CodableDataSingleValueDecodingContainer(partialData: partialData, at: codingPath)
49+
}
50+
}
51+

Sources/PostgreSQL/Data+Decoder/PostgreSQLDataKeyedDecodingContainer.swift renamed to Sources/PostgreSQL/Data+Decoder/CodableDataKeyedDecodingContainer.swift

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
/// Internal `KeyedDecodingContainerProtocol` for `PostgreSQLDataDecoder`
2-
final class PostgreSQLDataKeyedDecodingContainer<K>: KeyedDecodingContainerProtocol
3-
where K: CodingKey
1+
/// Internal `KeyedDecodingContainerProtocol` for `CodableDataDecoder`
2+
final class CodableDataKeyedDecodingContainer<K>: KeyedDecodingContainerProtocol
3+
where K: CodingKey
44
{
55
/// See `KeyedDecodingContainerProtocol.allKeys`
6-
var allKeys: [K] {
7-
return partialData.data.keys.flatMap { Key(stringValue: $0) }
8-
}
6+
var allKeys: [K]
97

108
/// See `KeyedDecodingContainerProtocol.codingPath`
119
var codingPath: [CodingKey]
1210

1311
/// Data being encoded.
14-
let partialData: PartialPostgreSQLData
12+
let partialData: PartialCodableData
1513

16-
/// Creates a new internal `PostgreSQLDataKeyedDecodingContainer`.
17-
init(partialData: PartialPostgreSQLData, at path: [CodingKey]) {
14+
/// Creates a new internal `CodableDataKeyedDecodingContainer`.
15+
init(partialData: PartialCodableData, at path: [CodingKey]) {
1816
self.codingPath = path
1917
self.partialData = partialData
18+
switch partialData.get(at: path) {
19+
case .some(let data):
20+
switch data {
21+
case .dictionary(let value): allKeys = value.keys.flatMap { Key(stringValue: $0) }
22+
default: allKeys = []
23+
}
24+
default: allKeys = []
25+
}
2026
}
2127

2228
/// See `KeyedDecodingContainerProtocol.contains`
@@ -106,22 +112,23 @@ final class PostgreSQLDataKeyedDecodingContainer<K>: KeyedDecodingContainerProto
106112

107113
/// See `KeyedDecodingContainerProtocol.nestedContainer`
108114
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
109-
let container = PostgreSQLDataKeyedDecodingContainer<NestedKey>(partialData: partialData, at: codingPath + [key])
115+
let container = CodableDataKeyedDecodingContainer<NestedKey>(partialData: partialData, at: codingPath + [key])
110116
return .init(container)
111117
}
112118

113119
/// See `KeyedDecodingContainerProtocol.nestedUnkeyedContainer`
114120
func nestedUnkeyedContainer(forKey key: K) throws -> UnkeyedDecodingContainer {
115-
fatalError("Not yet supported")
121+
return CodableDataUnkeyedDecodingContainer(partialData: partialData, at: codingPath + [key])
116122
}
117123

118124
/// See `KeyedDecodingContainerProtocol.superDecoder`
119125
func superDecoder() throws -> Decoder {
120-
return _PostgreSQLDataDecoder(partialData: partialData, at: codingPath)
126+
return _CodableDataDecoder(partialData: partialData, at: codingPath)
121127
}
122128

123129
/// See `KeyedDecodingContainerProtocol.superDecoder`
124130
func superDecoder(forKey key: K) throws -> Decoder {
125-
return _PostgreSQLDataDecoder(partialData: partialData, at: codingPath + [key])
131+
return _CodableDataDecoder(partialData: partialData, at: codingPath + [key])
126132
}
127133
}
134+

Sources/PostgreSQL/Data+Decoder/PostgreSQLDataSingleValueDecodingContainer.swift renamed to Sources/PostgreSQL/Data+Decoder/CodableDataSingleValueDecodingContainer.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
/// Internal `SingleValueDecodingContainer` for `PostgreSQLDataDecoder`
2-
final class PostgreSQLDataSingleValueDecodingContainer: SingleValueDecodingContainer {
1+
/// Internal `SingleValueDecodingContainer` for `CodableDataDecoder`
2+
final class CodableDataSingleValueDecodingContainer: SingleValueDecodingContainer {
33
/// See `SingleValueDecodingContainer.codingPath`
44
var codingPath: [CodingKey]
55

66
/// Data being encoded.
7-
let partialData: PartialPostgreSQLData
7+
let partialData: PartialCodableData
88

9-
/// Creates a new internal `PostgreSQLDataSingleValueDecodingContainer`.
10-
init(partialData: PartialPostgreSQLData, at path: [CodingKey]) {
9+
/// Creates a new internal `CodableDataSingleValueDecodingContainer`.
10+
init(partialData: PartialCodableData, at path: [CodingKey]) {
1111
self.codingPath = path
1212
self.partialData = partialData
1313
}
@@ -96,18 +96,18 @@ final class PostgreSQLDataSingleValueDecodingContainer: SingleValueDecodingConta
9696
func nestedContainer<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key>
9797
where Key: CodingKey
9898
{
99-
let container = PostgreSQLDataKeyedDecodingContainer<Key>(partialData: partialData, at: codingPath)
99+
let container = CodableDataKeyedDecodingContainer<Key>(partialData: partialData, at: codingPath)
100100
return .init(container)
101101
}
102102

103103
/// See `SingleValueDecodingContainer.nestedSingleValueContainer`
104104
func nestedSingleValueContainer() throws -> SingleValueDecodingContainer {
105-
return PostgreSQLDataSingleValueDecodingContainer(partialData: partialData, at: codingPath)
105+
return CodableDataSingleValueDecodingContainer(partialData: partialData, at: codingPath)
106106
}
107107

108108
/// See `SingleValueDecodingContainer.superDecoder`
109109
func superDecoder() throws -> Decoder {
110-
return _PostgreSQLDataDecoder(partialData: partialData, at: codingPath)
110+
return _CodableDataDecoder(partialData: partialData, at: codingPath)
111111
}
112112
}
113113

0 commit comments

Comments
 (0)