Skip to content

Commit

Permalink
Support binding of Dates
Browse files Browse the repository at this point in the history
...
  • Loading branch information
helje5 committed Nov 25, 2024
1 parent 37746ca commit 9c0f562
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Sources/PostgreSQLAdaptor/PostgreSQLAdaptorChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ open class PostgreSQLAdaptorChannel : AdaptorChannel, SmartDescription {
// - Max resolution 1 microsecond
if value.count == 8 {
// 1_000_000
let msecs = Double(UInt64(bigEndian: cast(value.baseAddress!)))
let msecs = Double(Int64(bigEndian: cast(value.baseAddress!)))
let date = Date(timeInterval: TimeInterval(msecs) / 1000000.0,
since: Date.pgReferenceDate)
return date
Expand Down Expand Up @@ -541,7 +541,10 @@ fileprivate extension Date {

// MARK: - Binding

fileprivate struct Bind { // move out
fileprivate struct Bind {
// So this always has the value *malloc*'ed in rawValue, which is not
// particularily great :-)

var type : Oid = 0
var length : Int32 = 0
var isBinary : Int32 = BinaryFlag
Expand Down Expand Up @@ -592,6 +595,18 @@ extension UInt : PGBindableValue {}
extension Int32 : PGBindableValue {}
extension Int64 : PGBindableValue {}

extension Date: PGBindableValue {

fileprivate func bind(index idx: Int, log: Bool) throws -> Bind {
let diff = self.timeIntervalSince(Date.pgReferenceDate)
let msecs = Int64(diff * 1000000.0) // seconds to milliseconds
let bp = tdup(msecs.bigEndian)
return Bind(type: OIDs.TIMESTAMPTZ, // 1184
length: 8,
rawValue: bp.baseAddress!)
}
}

extension UUID: PGBindableValue {
fileprivate func bind(index idx: Int, log: Bool) throws -> Bind {
return try uuidString.bind(index: idx, log: log)
Expand Down

0 comments on commit 9c0f562

Please sign in to comment.