forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLDataType.swift
120 lines (113 loc) · 4.75 KB
/
PostgreSQLDataType.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
import Foundation
/// The data type's raw object ID.
/// Use `select * from pg_type where oid = <idhere>;` to lookup more information.
public struct PostgreSQLDataType: Codable, Equatable, ExpressibleByIntegerLiteral {
/// Recognized types
public static let bool = PostgreSQLDataType(16)
public static let bytea = PostgreSQLDataType(17)
public static let char = PostgreSQLDataType(18)
public static let name = PostgreSQLDataType(19)
public static let int8 = PostgreSQLDataType(20)
public static let int2 = PostgreSQLDataType(21)
public static let int4 = PostgreSQLDataType(23)
public static let regproc = PostgreSQLDataType(24)
public static let text = PostgreSQLDataType(25)
public static let oid = PostgreSQLDataType(26)
public static let json = PostgreSQLDataType(114)
public static let pg_node_tree = PostgreSQLDataType(194)
public static let point = PostgreSQLDataType(600)
public static let float4 = PostgreSQLDataType(700)
public static let float8 = PostgreSQLDataType(701)
public static let _bool = PostgreSQLDataType(1000)
public static let _bytea = PostgreSQLDataType(1001)
public static let _char = PostgreSQLDataType(1002)
public static let _name = PostgreSQLDataType(1003)
public static let _int2 = PostgreSQLDataType(1005)
public static let _int4 = PostgreSQLDataType(1007)
public static let _text = PostgreSQLDataType(1009)
public static let _int8 = PostgreSQLDataType(1016)
public static let _point = PostgreSQLDataType(1017)
public static let _float4 = PostgreSQLDataType(1021)
public static let _float8 = PostgreSQLDataType(1022)
public static let _aclitem = PostgreSQLDataType(1034)
public static let bpchar = PostgreSQLDataType(1042)
public static let varchar = PostgreSQLDataType(1043)
public static let date = PostgreSQLDataType(1082)
public static let time = PostgreSQLDataType(1083)
public static let timestamp = PostgreSQLDataType(1114)
public static let _timestamp = PostgreSQLDataType(1115)
public static let numeric = PostgreSQLDataType(1700)
public static let void = PostgreSQLDataType(2278)
public static let uuid = PostgreSQLDataType(2950)
public static let _uuid = PostgreSQLDataType(2951)
public static let jsonb = PostgreSQLDataType(3802)
public static let _jsonb = PostgreSQLDataType(3807)
/// See `Equatable.==`
public static func ==(lhs: PostgreSQLDataType, rhs: PostgreSQLDataType) -> Bool {
return lhs.raw == rhs.raw
}
/// The raw data type code recognized by PostgreSQL.
public var raw: Int32
/// See `ExpressibleByIntegerLiteral.init(integerLiteral:)`
public init(integerLiteral value: Int32) {
self.init(value)
}
/// Creates a new `PostgreSQLDataType`
public init(_ raw: Int32) {
self.raw = raw
}
}
extension PostgreSQLDataType {
/// 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 .pg_node_tree: return "PGNODETREE"
case .point: return "POINT"
case .float4: return "REAL"
case .float8: return "DOUBLE PRECISION"
case ._bool: return "BOOLEAN[]"
case ._bytea: return "BYTEA[]"
case ._char: return "CHAR[]"
case ._name: return "NAME[]"
case ._int2: return "SMALLINT[]"
case ._int4: return "INTEGER[]"
case ._text: return "TEXT[]"
case ._int8: return "BIGINT[]"
case ._point: return "POINT[]"
case ._float4: return "REAL[]"
case ._float8: return "DOUBLE PRECISION[]"
case ._aclitem: return "ACLITEM[]"
case .bpchar: return "BPCHAR"
case .varchar: return "VARCHAR"
case .date: return "DATE"
case .time: return "TIME"
case .timestamp: return "TIMESTAMP"
case ._timestamp: return "TIMESTAMP[]"
case .numeric: return "NUMERIC"
case .void: return "VOID"
case .uuid: return "UUID"
case ._uuid: return "UUID[]"
case .jsonb: return "JSONB"
case ._jsonb: return "JSONB[]"
default: return nil
}
}
}
extension PostgreSQLDataType: CustomStringConvertible {
/// See `CustomStringConvertible.description`
public var description: String {
return knownSQLName ?? "UNKNOWN \(raw)"
}
}