forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresData.swift
58 lines (50 loc) · 1.96 KB
/
PostgresData.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import NIO
public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertible {
public static var null: PostgresData {
return .init(type: .null)
}
/// The object ID of the field's data type.
public var type: PostgresDataType
/// The type modifier (see pg_attribute.atttypmod). The meaning of the modifier is type-specific.
public var typeModifier: Int32?
/// The format code being used for the field.
/// Currently will be zero (text) or one (binary).
/// In a RowDescription returned from the statement variant of Describe,
/// the format code is not yet known and will always be zero.
public var formatCode: PostgresFormatCode
public var value: ByteBuffer?
public init(type: PostgresDataType, typeModifier: Int32? = nil, formatCode: PostgresFormatCode = .binary, value: ByteBuffer? = nil) {
self.type = type
self.typeModifier = typeModifier
self.formatCode = formatCode
self.value = value
}
public var description: String {
if let string = self.string {
return string
} else {
let string: String
if var value = self.value {
switch self.formatCode {
case .text:
let raw = value.readString(length: value.readableBytes) ?? ""
string = "\"\(raw)\""
case .binary:
string = "0x" + value.readableBytesView.hexdigest()
}
} else {
string = "<null>"
}
return string + " (\(self.type))"
}
}
public var debugDescription: String {
let valueDescription: String
if let value = self.value {
valueDescription = "\(value.readableBytes.description) bytes"
} else {
valueDescription = "nil"
}
return "PostgresData(type: \(self.type), value: \(valueDescription))"
}
}