1use serde::{Deserialize, Serialize};
8
9use super::{IntervalFields, RangeSubtype};
10
11mod catalog_names;
12mod display;
13mod identity;
14mod modifiers;
15mod names;
16mod parsing;
17mod production;
18mod references;
19
20pub use display::TypeDisplayScope;
21pub use identity::{UserTypeIdentity, UserTypeKind};
22
23pub(crate) use modifiers::split_type_modifier_with_control;
24
25#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
26pub enum ColumnType {
27 Named(String),
29 SmallInteger,
30 Integer,
31 BigInteger,
32 Oid,
34 Xid,
36 Boolean,
37 Void,
39 Text,
40 RefCursor,
42 Name,
43 Uuid,
44 Varchar(Option<u32>),
45 Bpchar,
47 Character(u32),
51 Real,
52 DoublePrecision,
53 Numeric {
58 precision: Option<u32>,
59 scale: Option<i32>,
60 },
61 Json,
63 JsonB,
65 Bytea,
67 Bit(Option<u32>),
69 VarBit(Option<u32>),
71 InternalChar,
73 Regproc,
74 Regprocedure,
76 Regclass,
78 Regnamespace,
80 Regcollation,
82 Regrole,
84 Regtype,
85 PgNodeTree,
86 AclItem,
87 Int2Vector,
88 OidVector,
89 AnyArray,
90 Record,
92 Array(Box<ColumnType>),
95 Date,
97 Time,
99 TimePrecision(u32),
101 TimeTz,
103 TimeTzPrecision(u32),
105 Timestamp,
108 TimestampPrecision(u32),
110 TimestampTz,
113 TimestampTzPrecision(u32),
115 Interval,
116 IntervalWithFields {
118 fields: IntervalFields,
119 precision: Option<u32>,
120 },
121 Range(RangeSubtype),
125 Multirange(RangeSubtype),
127 DeclaredRange(super::RangeTypeReference),
129 DeclaredMultirange(super::RangeTypeReference),
131 Vector(u32),
133 Tensor(u32),
137 Domain {
140 schema: String,
141 name: String,
142 oid: u32,
143 #[serde(default, skip_serializing_if = "Option::is_none")]
145 array_oid: Option<u32>,
146 #[serde(default, skip_serializing_if = "Option::is_none")]
148 collation_oid: Option<u32>,
149 base: Box<ColumnType>,
150 },
151 Enum(super::EnumTypeReference),
153 Composite(super::CompositeTypeReference),
155}
156
157pub(crate) fn builtin_array_element_name(type_name: &str) -> Option<&'static str> {
158 Some(match type_name {
159 "_bool" => "bool",
160 "_bytea" => "bytea",
161 "_char" => "\"char\"",
162 "_name" => "name",
163 "_int8" => "int8",
164 "_int2" => "int2",
165 "_int2vector" => "int2vector",
166 "_int4" => "int4",
167 "_regproc" => "regproc",
168 "_regprocedure" => "regprocedure",
169 "_regclass" => "regclass",
170 "_regrole" => "regrole",
171 "_text" => "text",
172 "_refcursor" => "refcursor",
173 "_oid" => "oid",
174 "_oidvector" => "oidvector",
175 "_bpchar" => "bpchar",
176 "_varchar" => "varchar",
177 "_bit" => "\"bit\"",
178 "_varbit" => "varbit",
179 "_float4" => "float4",
180 "_float8" => "float8",
181 "_aclitem" => "aclitem",
182 "_date" => "date",
183 "_time" => "time",
184 "_timestamp" => "timestamp",
185 "_timestamptz" => "timestamptz",
186 "_interval" => "interval",
187 "_numeric" => "numeric",
188 "_timetz" => "timetz",
189 "_record" => "record",
190 "_uuid" => "uuid",
191 "_json" => "json",
192 "_jsonb" => "jsonb",
193 "_regtype" => "regtype",
194 "_xid" => "xid",
195 "_int4range" => "int4range",
196 "_int8range" => "int8range",
197 "_numrange" => "numrange",
198 "_daterange" => "daterange",
199 "_tsrange" => "tsrange",
200 "_tstzrange" => "tstzrange",
201 "_int4multirange" => "int4multirange",
202 "_int8multirange" => "int8multirange",
203 "_nummultirange" => "nummultirange",
204 "_datemultirange" => "datemultirange",
205 "_tsmultirange" => "tsmultirange",
206 "_tstzmultirange" => "tstzmultirange",
207 _ => return None,
208 })
209}
210
211impl ColumnType {
212 pub(crate) fn has_type_modifiers(&self) -> bool {
214 match self {
215 Self::Varchar(length) | Self::Bit(length) | Self::VarBit(length) => length.is_some(),
216 Self::Numeric { precision, scale } => precision.is_some() || scale.is_some(),
217 Self::Array(element) => element.has_type_modifiers(),
218 Self::Character(_)
219 | Self::TimePrecision(_)
220 | Self::TimeTzPrecision(_)
221 | Self::TimestampPrecision(_)
222 | Self::TimestampTzPrecision(_)
223 | Self::IntervalWithFields { .. } => true,
224 _ => false,
225 }
226 }
227
228 #[must_use]
230 pub fn without_type_modifiers(&self) -> Self {
231 self.without_type_modifiers_with_control(
232 &uqa_core::memory::ProductionControl::uncontrolled(),
233 )
234 .expect("ordinary type modifier removal cannot be limited or cancelled")
235 .into_uncontrolled()
236 .expect("ordinary type modifier removal has no reservation")
237 }
238
239 #[must_use]
240 pub const fn temporal_precision(&self) -> Option<u32> {
241 match self {
242 Self::IntervalWithFields { precision, .. } => *precision,
243 Self::TimePrecision(p)
244 | Self::TimeTzPrecision(p)
245 | Self::TimestampPrecision(p)
246 | Self::TimestampTzPrecision(p) => Some(*p),
247 _ => None,
248 }
249 }
250
251 #[must_use]
252 pub const fn without_temporal_modifiers(&self) -> &Self {
253 match self {
254 Self::IntervalWithFields { .. } => &Self::Interval,
255 Self::TimePrecision(_) => &Self::Time,
256 Self::TimeTzPrecision(_) => &Self::TimeTz,
257 Self::TimestampPrecision(_) => &Self::Timestamp,
258 Self::TimestampTzPrecision(_) => &Self::TimestampTz,
259 other => other,
260 }
261 }
262
263 pub(crate) fn with_temporal_precision(
264 self,
265 precision: Option<i64>,
266 ) -> Result<Self, crate::SQLError> {
267 let Some(precision) = precision else {
268 return Ok(self);
269 };
270 if precision < 0 {
271 return Err(crate::SQLError::Routine {
272 sqlstate: "22023".into(),
273 message: format!(
274 "{} precision must not be negative",
275 self.regtype_name().to_uppercase()
276 ),
277 });
278 }
279 let precision = u32::try_from(precision.min(6)).expect("bounded temporal precision");
280 Ok(match self {
281 Self::Time => Self::TimePrecision(precision),
282 Self::TimeTz => Self::TimeTzPrecision(precision),
283 Self::Timestamp => Self::TimestampPrecision(precision),
284 Self::TimestampTz => Self::TimestampTzPrecision(precision),
285 other => other,
286 })
287 }
288
289 pub(crate) fn with_interval_modifiers(
290 fields: IntervalFields,
291 precision: Option<i64>,
292 ) -> Result<Self, crate::SQLError> {
293 let precision = precision
294 .map(|precision| {
295 if precision < 0 {
296 return Err(crate::SQLError::Routine {
297 sqlstate: "22023".into(),
298 message: "INTERVAL precision must not be negative".into(),
299 });
300 }
301 Ok(u32::try_from(precision.min(6)).expect("bounded interval precision"))
302 })
303 .transpose()?;
304 if fields == IntervalFields::All && precision.is_none() {
305 return Ok(Self::Interval);
306 }
307 Ok(Self::IntervalWithFields { fields, precision })
308 }
309
310 #[must_use]
311 pub fn is_integer(&self) -> bool {
312 match self {
313 Self::SmallInteger | Self::Integer | Self::BigInteger | Self::Oid | Self::Xid => true,
314 Self::Domain { base, .. } => base.is_integer(),
315 _ => false,
316 }
317 }
318
319 #[must_use]
320 pub fn is_character_string(&self) -> bool {
321 match self {
322 Self::Text
323 | Self::Name
324 | Self::Varchar(_)
325 | Self::Bpchar
326 | Self::Character(_)
327 | Self::InternalChar
328 | Self::PgNodeTree
329 | Self::AclItem => true,
330 Self::Domain { base, .. } => base.is_character_string(),
331 _ => false,
332 }
333 }
334}