forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresData.swift
114 lines (101 loc) · 3.82 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import NIOCore
import Foundation
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 {
guard var value = self.value else {
return "<null>"
}
let description: String?
switch self.type {
case .bool:
description = self.bool?.description
case .float4:
description = self.float?.description
case .float8, .numeric:
description = self.double?.description
case .int2:
description = self.int16?.description
case .int4, .regproc, .oid:
description = self.int32?.description
case .int8:
description = self.int64?.description
case .timestamp, .timestamptz, .date, .time, .timetz:
description = self.date?.description
case .text:
description = self.string?.debugDescription
case .uuid:
description = self.uuid?.description
case .json:
description = String(decoding: value.readableBytesView, as: UTF8.self)
case .jsonb:
var value = value
value.moveReaderIndex(forwardBy: 1)
description = String(decoding: value.readableBytesView, as: UTF8.self)
case .uuidArray:
description = self.array(of: UUID.self)?.description
case .int8Array:
description = self.array(of: Int.self)?.description
case .float8Array:
description = self.array(of: Double.self)?.description
case .float4Array:
description = self.array(of: Float.self)?.description
case .textArray:
description = self.array(of: String.self)?.description
case .jsonbArray:
description = self.array?.description
default:
if self.type.isUserDefined {
// custom type
description = value.readString(length: value.readableBytes)
} else {
description = nil
}
}
if let description = description {
return description
} else {
let raw: String
switch self.formatCode {
case .text:
raw = (value.readString(length: value.readableBytes) ?? "")
.debugDescription
case .binary:
raw = "0x" + value.readableBytesView.hexdigest()
}
return "\(raw) (\(self.type))"
}
}
public var debugDescription: String {
return self.description
}
}
extension PostgresData: PostgresDataConvertible {
public static var postgresDataType: PostgresDataType {
fatalError("PostgresData cannot be statically represented as a single data type")
}
public init?(postgresData: PostgresData) {
self = postgresData
}
public var postgresData: PostgresData? {
return self
}
}