forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresDataType.swift
217 lines (207 loc) · 7.51 KB
/
PostgresDataType.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/// 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 enum PostgresFormatCode: Int16, Codable, CustomStringConvertible {
case text = 0
case binary = 1
public var description: String {
switch self {
case .text: return "text"
case .binary: return "binary"
}
}
}
/// The data type's raw object ID.
/// Use `select * from pg_type where oid = <idhere>;` to lookup more information.
public struct PostgresDataType: Codable, Equatable, ExpressibleByIntegerLiteral, CustomStringConvertible, RawRepresentable {
/// `0`
public static let null = PostgresDataType(0)
/// `16`
public static let bool = PostgresDataType(16)
/// `17`
public static let bytea = PostgresDataType(17)
/// `18`
public static let char = PostgresDataType(18)
/// `19`
public static let name = PostgresDataType(19)
/// `20`
public static let int8 = PostgresDataType(20)
/// `21`
public static let int2 = PostgresDataType(21)
/// `23`
public static let int4 = PostgresDataType(23)
/// `24`
public static let regproc = PostgresDataType(24)
/// `25`
public static let text = PostgresDataType(25)
/// `26`
public static let oid = PostgresDataType(26)
/// `114`
public static let json = PostgresDataType(114)
/// `194` pg_node_tree
public static let pgNodeTree = PostgresDataType(194)
/// `600`
public static let point = PostgresDataType(600)
/// `700`
public static let float4 = PostgresDataType(700)
/// `701`
public static let float8 = PostgresDataType(701)
/// `790`
public static let money = PostgresDataType(790)
/// `1000` _bool
public static let boolArray = PostgresDataType(1000)
/// `1001` _bytea
public static let byteaArray = PostgresDataType(1001)
/// `1002` _char
public static let charArray = PostgresDataType(1002)
/// `1003` _name
public static let nameArray = PostgresDataType(1003)
/// `1005` _int2
public static let int2Array = PostgresDataType(1005)
/// `1007` _int4
public static let int4Array = PostgresDataType(1007)
/// `1009` _text
public static let textArray = PostgresDataType(1009)
/// `1016` _int8
public static let int8Array = PostgresDataType(1016)
/// `1017` _point
public static let pointArray = PostgresDataType(1017)
/// `1021` _float4
public static let float4Array = PostgresDataType(1021)
/// `1022` _float8
public static let float8Array = PostgresDataType(1022)
/// `1034` _aclitem
public static let aclitemArray = PostgresDataType(1034)
/// `1042`
public static let bpchar = PostgresDataType(1042)
/// `1043`
public static let varchar = PostgresDataType(1043)
/// `1082`
public static let date = PostgresDataType(1082)
/// `1083`
public static let time = PostgresDataType(1083)
/// `1114`
public static let timestamp = PostgresDataType(1114)
/// `1115` _timestamp
public static let timestampArray = PostgresDataType(1115)
/// `1184`
public static let timestamptz = PostgresDataType(1184)
/// `1266`
public static let timetz = PostgresDataType(1266)
/// `1700`
public static let numeric = PostgresDataType(1700)
/// `2278`
public static let void = PostgresDataType(2278)
/// `2950`
public static let uuid = PostgresDataType(2950)
/// `2951` _uuid
public static let uuidArray = PostgresDataType(2951)
/// `3802`
public static let jsonb = PostgresDataType(3802)
/// `3807` _jsonb
public static let jsonbArray = PostgresDataType(3807)
/// The raw data type code recognized by PostgreSQL.
public var rawValue: UInt32
/// See `ExpressibleByIntegerLiteral.init(integerLiteral:)`
public init(integerLiteral value: UInt32) {
self.init(value)
}
public init(_ rawValue: UInt32) {
self.rawValue = rawValue
}
public init?(rawValue: UInt32) {
self.init(rawValue)
}
/// Returns the known SQL name, if one exists.
/// Note: This only supports a limited subset of all PSQL types and is meant for convenience only.
public var knownSQLName: String? {
switch self {
case .bool: return "BOOLEAN"
case .bytea: return "BYTEA"
case .char: return "CHAR"
case .name: return "NAME"
case .int8: return "BIGINT"
case .int2: return "SMALLINT"
case .int4: return "INTEGER"
case .regproc: return "REGPROC"
case .text: return "TEXT"
case .oid: return "OID"
case .json: return "JSON"
case .pgNodeTree: return "PGNODETREE"
case .point: return "POINT"
case .float4: return "REAL"
case .float8: return "DOUBLE PRECISION"
case .money: return "MONEY"
case .boolArray: return "BOOLEAN[]"
case .byteaArray: return "BYTEA[]"
case .charArray: return "CHAR[]"
case .nameArray: return "NAME[]"
case .int2Array: return "SMALLINT[]"
case .int4Array: return "INTEGER[]"
case .textArray: return "TEXT[]"
case .int8Array: return "BIGINT[]"
case .pointArray: return "POINT[]"
case .float4Array: return "REAL[]"
case .float8Array: return "DOUBLE PRECISION[]"
case .aclitemArray: return "ACLITEM[]"
case .bpchar: return "BPCHAR"
case .varchar: return "VARCHAR"
case .date: return "DATE"
case .time: return "TIME"
case .timestamp: return "TIMESTAMP"
case .timestamptz: return "TIMESTAMPTZ"
case .timestampArray: return "TIMESTAMP[]"
case .numeric: return "NUMERIC"
case .void: return "VOID"
case .uuid: return "UUID"
case .uuidArray: return "UUID[]"
case .jsonb: return "JSONB"
case .jsonbArray: return "JSONB[]"
default: return nil
}
}
/// Returns the array type for this type if one is known.
internal var arrayType: PostgresDataType? {
switch self {
case .bool: return .boolArray
case .bytea: return .byteaArray
case .char: return .charArray
case .name: return .nameArray
case .int2: return .int2Array
case .int4: return .int4Array
case .int8: return .int8Array
case .point: return .pointArray
case .float4: return .float4Array
case .float8: return .float8Array
case .uuid: return .uuidArray
case .jsonb: return .jsonbArray
case .text: return .textArray
default: return nil
}
}
/// Returns the element type for this type if one is known.
/// Returns nil if this is not an array type.
internal var elementType: PostgresDataType? {
switch self {
case .boolArray: return .bool
case .byteaArray: return .bytea
case .charArray: return .char
case .nameArray: return .name
case .int2Array: return .int2
case .int4Array: return .int4
case .int8Array: return .int8
case .pointArray: return .point
case .float4Array: return .float4
case .float8Array: return .float8
case .uuidArray: return .uuid
case .jsonbArray: return .jsonb
case .textArray: return .text
default: return nil
}
}
/// See `CustomStringConvertible`.
public var description: String {
return self.knownSQLName ?? "UNKNOWN \(self.rawValue)"
}
}