Skip to content

Commit 2c9333d

Browse files
authored
Merge pull request #17 from vapor/null-encode
null data encode
2 parents 82c7e14 + b3ef0f6 commit 2c9333d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Sources/PostgreSQL/Message+Serialize/PostgreSQLMessageEncoder.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ fileprivate final class _PostgreSQLMessageKeyedEncoder<K>: KeyedEncodingContaine
188188
func nestedUnkeyedContainer(forKey key: K) -> UnkeyedEncodingContainer { return encoder.unkeyedContainer() }
189189
func superEncoder() -> Encoder { return encoder }
190190
func superEncoder(forKey key: K) -> Encoder { return encoder }
191+
192+
func encodeIfPresent<T>(_ value: T?, forKey key: K) throws where T : Encodable {
193+
if T.self == Data.self {
194+
if let data = value {
195+
try encoder.encode(data)
196+
} else {
197+
try encoder.encode(Int32(-1)) // indicate nil data
198+
}
199+
} else {
200+
if let value = value {
201+
try encoder.encode(value)
202+
} else {
203+
try encoder.encodeNil()
204+
}
205+
}
206+
}
191207
}
192208

193209
/// MARK: Unkeyed

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class PostgreSQLConnectionTests: XCTestCase {
88
func testVersion() throws {
99
let (client, eventLoop) = try PostgreSQLConnection.makeTest()
1010
let results = try client.simpleQuery("SELECT version();").await(on: eventLoop)
11-
try XCTAssert(results[0]["version"]?.decode(String.self).contains("10.1") == true)
11+
try XCTAssert(results[0]["version"]?.decode(String.self).contains("10.") == true)
1212
}
1313

1414
func testSelectTypes() throws {
@@ -270,13 +270,28 @@ class PostgreSQLConnectionTests: XCTestCase {
270270
}
271271
}
272272

273+
func testNull() throws {
274+
let (client, eventLoop) = try PostgreSQLConnection.makeTest()
275+
_ = try client.query("drop table if exists nulltest;").await(on: eventLoop)
276+
let createResult = try client.query("create table nulltest (i integer not null, d timestamp);").await(on: eventLoop)
277+
XCTAssertEqual(createResult.count, 0)
278+
let insertResult = try! client.query("insert into nulltest (i, d) VALUES ($1, $2)", [
279+
PostgreSQLData(type: .int2, format: .binary, data: Data([0x00, 0x01])),
280+
PostgreSQLData(type: .timestamp, format: .binary, data: nil),
281+
]).await(on: eventLoop)
282+
XCTAssertEqual(insertResult.count, 0)
283+
let parameterizedResult = try! client.query("select * from nulltest").await(on: eventLoop)
284+
XCTAssertEqual(parameterizedResult.count, 1)
285+
}
286+
273287
static var allTests = [
274288
("testVersion", testVersion),
275289
("testSelectTypes", testSelectTypes),
276290
("testParse", testParse),
277291
("testTypes", testTypes),
278292
("testParameterizedTypes", testParameterizedTypes),
279293
("testStruct", testStruct),
294+
("testNull", testNull),
280295
]
281296
}
282297

0 commit comments

Comments
 (0)