1#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum TypeKind {
6 Bool,
7 Int,
8 BigInt,
9 Float,
10 Numeric,
11 Json,
12 Bytea,
13 Text,
15 Array {
16 elem: Box<TypeInfo>,
17 delim: char,
18 },
19 Range {
20 sub: Box<TypeInfo>,
21 },
22 Multirange {
23 sub: Box<TypeInfo>,
24 },
25}
26
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct TypeInfo {
29 pub oid: u32,
30 pub name: String,
31 pub kind: TypeKind,
32}
33
34#[derive(Clone, Copy)]
35enum Leaf {
36 Bool,
37 Int,
38 BigInt,
39 Float,
40 Numeric,
41 Json,
42 Bytea,
43 Text,
44}
45
46const LEAVES: &[(u32, &str, Leaf)] = &[
47 (16, "bool", Leaf::Bool),
48 (21, "int2", Leaf::Int),
49 (23, "int4", Leaf::Int),
50 (26, "oid", Leaf::Int),
51 (28, "xid", Leaf::Int),
52 (29, "cid", Leaf::Int),
53 (20, "int8", Leaf::BigInt),
54 (5069, "xid8", Leaf::BigInt),
55 (6437, "oid8", Leaf::BigInt),
56 (700, "float4", Leaf::Float),
57 (701, "float8", Leaf::Float),
58 (1700, "numeric", Leaf::Numeric),
59 (114, "json", Leaf::Json),
60 (3802, "jsonb", Leaf::Json),
61 (17, "bytea", Leaf::Bytea),
62 (18, "char", Leaf::Text),
63 (19, "name", Leaf::Text),
64 (25, "text", Leaf::Text),
65 (1042, "bpchar", Leaf::Text),
66 (1043, "varchar", Leaf::Text),
67 (2950, "uuid", Leaf::Text),
68 (142, "xml", Leaf::Text),
69 (1082, "date", Leaf::Text),
70 (1083, "time", Leaf::Text),
71 (1114, "timestamp", Leaf::Text),
72 (1184, "timestamptz", Leaf::Text),
73 (1266, "timetz", Leaf::Text),
74 (1186, "interval", Leaf::Text),
75 (790, "money", Leaf::Text),
76 (1560, "bit", Leaf::Text),
77 (1562, "varbit", Leaf::Text),
78 (869, "inet", Leaf::Text),
79 (650, "cidr", Leaf::Text),
80 (829, "macaddr", Leaf::Text),
81 (774, "macaddr8", Leaf::Text),
82 (600, "point", Leaf::Text),
83 (601, "lseg", Leaf::Text),
84 (602, "path", Leaf::Text),
85 (603, "box", Leaf::Text),
86 (604, "polygon", Leaf::Text),
87 (628, "line", Leaf::Text),
88 (718, "circle", Leaf::Text),
89 (3614, "tsvector", Leaf::Text),
90 (3615, "tsquery", Leaf::Text),
91 (4072, "jsonpath", Leaf::Text),
92 (3220, "pg_lsn", Leaf::Text),
93 (5038, "pg_snapshot", Leaf::Text),
94 (2970, "txid_snapshot", Leaf::Text),
95 (2205, "regclass", Leaf::Text),
96 (2206, "regtype", Leaf::Text),
97 (24, "regproc", Leaf::Text),
98 (2202, "regprocedure", Leaf::Text),
99 (2203, "regoper", Leaf::Text),
100 (2204, "regoperator", Leaf::Text),
101 (3734, "regconfig", Leaf::Text),
102 (3769, "regdictionary", Leaf::Text),
103 (4089, "regnamespace", Leaf::Text),
104 (4096, "regrole", Leaf::Text),
105 (4191, "regcollation", Leaf::Text),
106 (22, "int2vector", Leaf::Text),
107 (30, "oidvector", Leaf::Text),
108 (2249, "record", Leaf::Text),
109 (705, "unknown", Leaf::Text),
110 (2278, "void", Leaf::Text),
111];
112
113const ARRAYS: &[(u32, &str, u32, char)] = &[
115 (1000, "_bool", 16, ','),
116 (1005, "_int2", 21, ','),
117 (1007, "_int4", 23, ','),
118 (1016, "_int8", 20, ','),
119 (1021, "_float4", 700, ','),
120 (1022, "_float8", 701, ','),
121 (1231, "_numeric", 1700, ','),
122 (1009, "_text", 25, ','),
123 (1015, "_varchar", 1043, ','),
124 (1014, "_bpchar", 1042, ','),
125 (1003, "_name", 19, ','),
126 (2951, "_uuid", 2950, ','),
127 (199, "_json", 114, ','),
128 (3807, "_jsonb", 3802, ','),
129 (1001, "_bytea", 17, ','),
130 (1182, "_date", 1082, ','),
131 (1183, "_time", 1083, ','),
132 (1115, "_timestamp", 1114, ','),
133 (1185, "_timestamptz", 1184, ','),
134 (1270, "_timetz", 1266, ','),
135 (1187, "_interval", 1186, ','),
136 (1041, "_inet", 869, ','),
137 (651, "_cidr", 650, ','),
138 (1040, "_macaddr", 829, ','),
139 (1028, "_oid", 26, ','),
140 (791, "_money", 790, ','),
141 (1561, "_bit", 1560, ','),
142 (1563, "_varbit", 1562, ','),
143 (1017, "_point", 600, ','),
144 (1020, "_box", 603, ';'),
145 (3643, "_tsvector", 3614, ','),
146 (3645, "_tsquery", 3615, ','),
147 (143, "_xml", 142, ','),
148 (3905, "_int4range", 3904, ','),
149 (3927, "_int8range", 3926, ','),
150 (3907, "_numrange", 3906, ','),
151 (3909, "_tsrange", 3908, ','),
152 (3911, "_tstzrange", 3910, ','),
153 (3913, "_daterange", 3912, ','),
154];
155
156const RANGES: &[(u32, &str, u32)] = &[
158 (3904, "int4range", 23),
159 (3926, "int8range", 20),
160 (3906, "numrange", 1700),
161 (3908, "tsrange", 1114),
162 (3910, "tstzrange", 1184),
163 (3912, "daterange", 1082),
164];
165
166const MULTIRANGES: &[(u32, &str, u32)] = &[
168 (4451, "int4multirange", 23),
169 (4536, "int8multirange", 20),
170 (4532, "nummultirange", 1700),
171 (4533, "tsmultirange", 1114),
172 (4534, "tstzmultirange", 1184),
173 (4535, "datemultirange", 1082),
174];
175
176pub fn builtin_type(oid: u32) -> Option<TypeInfo> {
178 if let Some(&(_, name, leaf)) = LEAVES.iter().find(|r| r.0 == oid) {
179 return Some(TypeInfo {
180 oid,
181 name: name.to_string(),
182 kind: leaf_kind(leaf),
183 });
184 }
185 if let Some(&(_, name, elem, delim)) = ARRAYS.iter().find(|r| r.0 == oid) {
186 let elem = builtin_type(elem)?;
187 return Some(TypeInfo {
188 oid,
189 name: format!("{}[]", name.trim_start_matches('_')),
190 kind: TypeKind::Array {
191 elem: Box::new(elem),
192 delim,
193 },
194 });
195 }
196 if let Some(&(_, name, sub)) = RANGES.iter().find(|r| r.0 == oid) {
197 let sub = builtin_type(sub)?;
198 return Some(TypeInfo {
199 oid,
200 name: name.to_string(),
201 kind: TypeKind::Range { sub: Box::new(sub) },
202 });
203 }
204 if let Some(&(_, name, sub)) = MULTIRANGES.iter().find(|r| r.0 == oid) {
205 let sub = builtin_type(sub)?;
206 return Some(TypeInfo {
207 oid,
208 name: name.to_string(),
209 kind: TypeKind::Multirange { sub: Box::new(sub) },
210 });
211 }
212 None
213}
214
215fn leaf_kind(leaf: Leaf) -> TypeKind {
216 match leaf {
217 Leaf::Bool => TypeKind::Bool,
218 Leaf::Int => TypeKind::Int,
219 Leaf::BigInt => TypeKind::BigInt,
220 Leaf::Float => TypeKind::Float,
221 Leaf::Numeric => TypeKind::Numeric,
222 Leaf::Json => TypeKind::Json,
223 Leaf::Bytea => TypeKind::Bytea,
224 Leaf::Text => TypeKind::Text,
225 }
226}