Skip to content

Commit 82c1ffa

Browse files
authored
add isUserDefined + fix optional (vapor#75)
1 parent 3adaee5 commit 82c1ffa

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

Sources/PostgresNIO/Data/PostgresData+Optional.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Optional: PostgresDataConvertible where Wrapped: PostgresDataConvertib
1212
case .some(let wrapped):
1313
return wrapped.postgresData
1414
case .none:
15-
return PostgresData.null
15+
return .init(type: Wrapped.postgresDataType, value: nil)
1616
}
1717
}
1818
}

Sources/PostgresNIO/Data/PostgresData+String.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ extension PostgresData {
4949
case .bpchar:
5050
return self.character?.description
5151
default:
52-
return nil
52+
if self.type.isUserDefined {
53+
// custom type
54+
return value.readString(length: value.readableBytes)
55+
} else {
56+
return nil
57+
}
5358
}
5459
case .text:
5560
guard let string = value.readString(length: value.readableBytes) else {

Sources/PostgresNIO/Data/PostgresData.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertibl
7171
case .jsonbArray:
7272
description = self.array?.description
7373
default:
74-
description = nil
74+
if self.type.isUserDefined {
75+
// custom type
76+
description = value.readString(length: value.readableBytes)
77+
} else {
78+
description = nil
79+
}
7580
}
7681

7782
if let description = description {

Sources/PostgresNIO/Data/PostgresDataType.swift

+7
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ public struct PostgresDataType: Codable, Equatable, ExpressibleByIntegerLiteral,
106106

107107
/// The raw data type code recognized by PostgreSQL.
108108
public var rawValue: UInt32
109+
110+
/// Returns `true` if the type's raw value is greater than `2^14`.
111+
/// This _appears_ to be true for all user-defined types, but I don't
112+
/// have any documentation to back this up.
113+
public var isUserDefined: Bool {
114+
self.rawValue >= 1 << 14
115+
}
109116

110117
/// See `ExpressibleByIntegerLiteral.init(integerLiteral:)`
111118
public init(integerLiteral value: UInt32) {

Tests/PostgresNIOTests/PostgresNIOTests.swift

+21
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,27 @@ final class PostgresNIOTests: XCTestCase {
10281028
XCTFail("\(error)")
10291029
}
10301030
}
1031+
1032+
func testUserDefinedType() throws {
1033+
let conn = try PostgresConnection.test(on: eventLoop).wait()
1034+
defer { try! conn.close().wait() }
1035+
1036+
_ = try conn.query("DROP TYPE IF EXISTS foo").wait()
1037+
_ = try conn.query("CREATE TYPE foo AS ENUM ('bar', 'qux')").wait()
1038+
defer {
1039+
_ = try! conn.query("DROP TYPE foo").wait()
1040+
}
1041+
let res = try conn.query("SELECT 'qux'::foo as foo").wait()
1042+
XCTAssertEqual(res[0].column("foo")?.string, "qux")
1043+
}
1044+
1045+
func testNullBind() throws {
1046+
let conn = try PostgresConnection.test(on: eventLoop).wait()
1047+
defer { try! conn.close().wait() }
1048+
1049+
let res = try conn.query("SELECT $1::text as foo", [String?.none.postgresData!]).wait()
1050+
XCTAssertEqual(res[0].column("foo")?.string, nil)
1051+
}
10311052
}
10321053

10331054
private func performance(function: String = #function) -> Bool {

0 commit comments

Comments
 (0)