Skip to content

Commit 4ff3fcf

Browse files
committed
add TIMESTAMPTZ support, fixes vapor#63
1 parent 82f1321 commit 4ff3fcf

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

Sources/PostgreSQL/Column/PostgreSQLDataType.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public struct PostgreSQLDataType: Codable, Equatable, ExpressibleByIntegerLitera
3838
public static let time = PostgreSQLDataType(1083)
3939
public static let timestamp = PostgreSQLDataType(1114)
4040
public static let _timestamp = PostgreSQLDataType(1115)
41+
public static let timestamptz = PostgreSQLDataType(1184)
42+
public static let timetz = PostgreSQLDataType(1266)
4143
public static let numeric = PostgreSQLDataType(1700)
4244
public static let void = PostgreSQLDataType(2278)
4345
public static let uuid = PostgreSQLDataType(2950)

Sources/PostgreSQL/Data/PostgreSQLData+Date.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ extension Date: PostgreSQLDataConvertible {
77
case .timestamp: return try value.parseDate(format: "yyyy-MM-dd HH:mm:ss")
88
case .date: return try value.parseDate(format: "yyyy-MM-dd")
99
case .time: return try value.parseDate(format: "HH:mm:ss")
10-
default: throw PostgreSQLError(identifier: "date", reason: "Could not parse Date from text data type: \(data.type).")
10+
default: throw PostgreSQLError.decode(Date.self, from: data)
1111
}
1212
case .binary(let value):
1313
switch data.type {
14-
case .timestamp, .time:
14+
case .timestamp, .timestamptz:
1515
let microseconds = value.as(Int64.self, default: 0).bigEndian
1616
let seconds = Double(microseconds) / Double(_microsecondsPerSecond)
1717
return Date(timeInterval: seconds, since: _psqlDateStart)
18+
case .time, .timetz: throw PostgreSQLError.decode(Date.self, from: data)
1819
case .date:
1920
let days = value.as(Int32.self, default: 0).bigEndian
2021
let seconds = days * _secondsInDay
2122
return Date(timeInterval: Double(seconds), since: _psqlDateStart)
22-
default: throw PostgreSQLError(identifier: "date", reason: "Could not parse Date from binary data type: \(data.type).")
23+
default: throw PostgreSQLError.decode(Date.self, from: data)
2324
}
24-
case .null: throw PostgreSQLError(identifier: "data", reason: "Could not decode String from null.")
25+
case .null: throw PostgreSQLError.decode(Date.self, from: data)
2526
}
2627
}
2728

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,21 +489,41 @@ class PostgreSQLConnectionTests: XCTestCase {
489489
)).wait()
490490
XCTAssertEqual(save.count, 0)
491491

492-
let search = try conn.query(.dml(
493-
statement: .select,
494-
table: "users",
495-
keys: [.all],
496-
predicates: [.predicate("name", .equal, .bind("vapor"))]
497-
)).wait()
492+
let search = try conn.query(.select([.all], from: "users", where: [.predicate("name", .equal, .bind("vapor"))]
493+
)).wait()
498494
XCTAssertEqual(search.count, 1)
499495

500496
try conn.query(.select(["id", "name", "pet"], from: "users")) { row in
501497
print(row)
502498
}.wait()
503499
}
500+
501+
// https://github.com/vapor/postgresql/issues/63
502+
func testTimeTz() throws {
503+
let conn = try PostgreSQLConnection.makeTest(transport: .cleartext)
504+
_ = try conn.simpleQuery(.create(table: "timetest", columns: [
505+
.column("timestamptz", .columnType("TIMESTAMPTZ"))
506+
])).wait()
507+
defer { _ = try? conn.simpleQuery(.drop(table: "timetest")).wait() }
508+
509+
struct Time: Codable, Equatable {
510+
var timestamptz: Date
511+
}
512+
513+
let time = Time(timestamptz: .init())
514+
_ = try conn.query(.insert(into: "timetest", values: SQLRowEncoder().encode(time))).wait()
515+
516+
let fetch = try conn.query(.select([.all], from: "timetest"), decoding: Time.self).wait()
517+
switch fetch.count {
518+
case 1:
519+
XCTAssertEqual(fetch[0], time)
520+
default: XCTFail("invalid row count")
521+
}
522+
}
504523

505524
static var allTests = [
506525
("testVersion", testVersion),
526+
("testTimeTz", testTimeTz),
507527
]
508528
}
509529

0 commit comments

Comments
 (0)