|
19 | 19 | //! (commonly referred to as Data Definition Language, or DDL) |
20 | 20 |
|
21 | 21 | #[cfg(not(feature = "std"))] |
22 | | -use alloc::{boxed::Box, format, string::String, vec, vec::Vec}; |
| 22 | +use alloc::{ |
| 23 | + boxed::Box, |
| 24 | + format, |
| 25 | + string::{String, ToString}, |
| 26 | + vec, |
| 27 | + vec::Vec, |
| 28 | +}; |
23 | 29 | use core::fmt::{self, Display, Write}; |
24 | 30 |
|
25 | 31 | #[cfg(feature = "serde")] |
@@ -3960,3 +3966,233 @@ impl Spanned for DropFunction { |
3960 | 3966 | Span::empty() |
3961 | 3967 | } |
3962 | 3968 | } |
| 3969 | + |
| 3970 | +/// CREATE OPERATOR statement |
| 3971 | +/// See <https://www.postgresql.org/docs/current/sql-createoperator.html> |
| 3972 | +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] |
| 3973 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 3974 | +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] |
| 3975 | +pub struct CreateOperator { |
| 3976 | + /// Operator name (can be schema-qualified) |
| 3977 | + pub name: ObjectName, |
| 3978 | + /// FUNCTION or PROCEDURE parameter (function name) |
| 3979 | + pub function: ObjectName, |
| 3980 | + /// Whether PROCEDURE keyword was used (vs FUNCTION) |
| 3981 | + pub is_procedure: bool, |
| 3982 | + /// LEFTARG parameter (left operand type) |
| 3983 | + pub left_arg: Option<DataType>, |
| 3984 | + /// RIGHTARG parameter (right operand type) |
| 3985 | + pub right_arg: Option<DataType>, |
| 3986 | + /// COMMUTATOR parameter (commutator operator) |
| 3987 | + pub commutator: Option<ObjectName>, |
| 3988 | + /// NEGATOR parameter (negator operator) |
| 3989 | + pub negator: Option<ObjectName>, |
| 3990 | + /// RESTRICT parameter (restriction selectivity function) |
| 3991 | + pub restrict: Option<ObjectName>, |
| 3992 | + /// JOIN parameter (join selectivity function) |
| 3993 | + pub join: Option<ObjectName>, |
| 3994 | + /// HASHES flag |
| 3995 | + pub hashes: bool, |
| 3996 | + /// MERGES flag |
| 3997 | + pub merges: bool, |
| 3998 | +} |
| 3999 | + |
| 4000 | +/// CREATE OPERATOR FAMILY statement |
| 4001 | +/// See <https://www.postgresql.org/docs/current/sql-createopfamily.html> |
| 4002 | +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] |
| 4003 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 4004 | +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] |
| 4005 | +pub struct CreateOperatorFamily { |
| 4006 | + /// Operator family name (can be schema-qualified) |
| 4007 | + pub name: ObjectName, |
| 4008 | + /// Index method (btree, hash, gist, gin, etc.) |
| 4009 | + pub using: Ident, |
| 4010 | +} |
| 4011 | + |
| 4012 | +/// CREATE OPERATOR CLASS statement |
| 4013 | +/// See <https://www.postgresql.org/docs/current/sql-createopclass.html> |
| 4014 | +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] |
| 4015 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 4016 | +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] |
| 4017 | +pub struct CreateOperatorClass { |
| 4018 | + /// Operator class name (can be schema-qualified) |
| 4019 | + pub name: ObjectName, |
| 4020 | + /// Whether this is the default operator class for the type |
| 4021 | + pub default: bool, |
| 4022 | + /// The data type |
| 4023 | + pub for_type: DataType, |
| 4024 | + /// Index method (btree, hash, gist, gin, etc.) |
| 4025 | + pub using: Ident, |
| 4026 | + /// Optional operator family name |
| 4027 | + pub family: Option<ObjectName>, |
| 4028 | + /// List of operator class items (operators, functions, storage) |
| 4029 | + pub items: Vec<OperatorClassItem>, |
| 4030 | +} |
| 4031 | + |
| 4032 | +impl fmt::Display for CreateOperator { |
| 4033 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 4034 | + write!(f, "CREATE OPERATOR {} (", self.name)?; |
| 4035 | + |
| 4036 | + let function_keyword = if self.is_procedure { |
| 4037 | + "PROCEDURE" |
| 4038 | + } else { |
| 4039 | + "FUNCTION" |
| 4040 | + }; |
| 4041 | + let mut params = vec![format!("{} = {}", function_keyword, self.function)]; |
| 4042 | + |
| 4043 | + if let Some(left_arg) = &self.left_arg { |
| 4044 | + params.push(format!("LEFTARG = {}", left_arg)); |
| 4045 | + } |
| 4046 | + if let Some(right_arg) = &self.right_arg { |
| 4047 | + params.push(format!("RIGHTARG = {}", right_arg)); |
| 4048 | + } |
| 4049 | + if let Some(commutator) = &self.commutator { |
| 4050 | + params.push(format!("COMMUTATOR = {}", commutator)); |
| 4051 | + } |
| 4052 | + if let Some(negator) = &self.negator { |
| 4053 | + params.push(format!("NEGATOR = {}", negator)); |
| 4054 | + } |
| 4055 | + if let Some(restrict) = &self.restrict { |
| 4056 | + params.push(format!("RESTRICT = {}", restrict)); |
| 4057 | + } |
| 4058 | + if let Some(join) = &self.join { |
| 4059 | + params.push(format!("JOIN = {}", join)); |
| 4060 | + } |
| 4061 | + if self.hashes { |
| 4062 | + params.push("HASHES".to_string()); |
| 4063 | + } |
| 4064 | + if self.merges { |
| 4065 | + params.push("MERGES".to_string()); |
| 4066 | + } |
| 4067 | + |
| 4068 | + write!(f, "{}", params.join(", "))?; |
| 4069 | + write!(f, ")") |
| 4070 | + } |
| 4071 | +} |
| 4072 | + |
| 4073 | +impl fmt::Display for CreateOperatorFamily { |
| 4074 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 4075 | + write!( |
| 4076 | + f, |
| 4077 | + "CREATE OPERATOR FAMILY {} USING {}", |
| 4078 | + self.name, self.using |
| 4079 | + ) |
| 4080 | + } |
| 4081 | +} |
| 4082 | + |
| 4083 | +impl fmt::Display for CreateOperatorClass { |
| 4084 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 4085 | + write!(f, "CREATE OPERATOR CLASS {}", self.name)?; |
| 4086 | + if self.default { |
| 4087 | + write!(f, " DEFAULT")?; |
| 4088 | + } |
| 4089 | + write!(f, " FOR TYPE {} USING {}", self.for_type, self.using)?; |
| 4090 | + if let Some(family) = &self.family { |
| 4091 | + write!(f, " FAMILY {}", family)?; |
| 4092 | + } |
| 4093 | + write!(f, " AS {}", display_comma_separated(&self.items)) |
| 4094 | + } |
| 4095 | +} |
| 4096 | + |
| 4097 | +/// Operator argument types for CREATE OPERATOR CLASS |
| 4098 | +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] |
| 4099 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 4100 | +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] |
| 4101 | +pub struct OperatorArgTypes { |
| 4102 | + pub left: DataType, |
| 4103 | + pub right: DataType, |
| 4104 | +} |
| 4105 | + |
| 4106 | +impl fmt::Display for OperatorArgTypes { |
| 4107 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 4108 | + write!(f, "{}, {}", self.left, self.right) |
| 4109 | + } |
| 4110 | +} |
| 4111 | + |
| 4112 | +/// An item in a CREATE OPERATOR CLASS statement |
| 4113 | +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] |
| 4114 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 4115 | +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] |
| 4116 | +pub enum OperatorClassItem { |
| 4117 | + /// OPERATOR clause |
| 4118 | + Operator { |
| 4119 | + strategy_number: u32, |
| 4120 | + operator_name: ObjectName, |
| 4121 | + /// Optional operator argument types |
| 4122 | + op_types: Option<OperatorArgTypes>, |
| 4123 | + /// FOR SEARCH or FOR ORDER BY |
| 4124 | + purpose: Option<OperatorPurpose>, |
| 4125 | + }, |
| 4126 | + /// FUNCTION clause |
| 4127 | + Function { |
| 4128 | + support_number: u32, |
| 4129 | + /// Optional function argument types for the operator class |
| 4130 | + op_types: Option<Vec<DataType>>, |
| 4131 | + function_name: ObjectName, |
| 4132 | + /// Function argument types |
| 4133 | + argument_types: Vec<DataType>, |
| 4134 | + }, |
| 4135 | + /// STORAGE clause |
| 4136 | + Storage { storage_type: DataType }, |
| 4137 | +} |
| 4138 | + |
| 4139 | +/// Purpose of an operator in an operator class |
| 4140 | +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] |
| 4141 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 4142 | +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] |
| 4143 | +pub enum OperatorPurpose { |
| 4144 | + ForSearch, |
| 4145 | + ForOrderBy { sort_family: ObjectName }, |
| 4146 | +} |
| 4147 | + |
| 4148 | +impl fmt::Display for OperatorClassItem { |
| 4149 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 4150 | + match self { |
| 4151 | + OperatorClassItem::Operator { |
| 4152 | + strategy_number, |
| 4153 | + operator_name, |
| 4154 | + op_types, |
| 4155 | + purpose, |
| 4156 | + } => { |
| 4157 | + write!(f, "OPERATOR {strategy_number} {operator_name}")?; |
| 4158 | + if let Some(types) = op_types { |
| 4159 | + write!(f, " ({types})")?; |
| 4160 | + } |
| 4161 | + if let Some(purpose) = purpose { |
| 4162 | + write!(f, " {purpose}")?; |
| 4163 | + } |
| 4164 | + Ok(()) |
| 4165 | + } |
| 4166 | + OperatorClassItem::Function { |
| 4167 | + support_number, |
| 4168 | + op_types, |
| 4169 | + function_name, |
| 4170 | + argument_types, |
| 4171 | + } => { |
| 4172 | + write!(f, "FUNCTION {support_number}")?; |
| 4173 | + if let Some(types) = op_types { |
| 4174 | + write!(f, " ({})", display_comma_separated(types))?; |
| 4175 | + } |
| 4176 | + write!(f, " {function_name}")?; |
| 4177 | + if !argument_types.is_empty() { |
| 4178 | + write!(f, "({})", display_comma_separated(argument_types))?; |
| 4179 | + } |
| 4180 | + Ok(()) |
| 4181 | + } |
| 4182 | + OperatorClassItem::Storage { storage_type } => { |
| 4183 | + write!(f, "STORAGE {storage_type}") |
| 4184 | + } |
| 4185 | + } |
| 4186 | + } |
| 4187 | +} |
| 4188 | + |
| 4189 | +impl fmt::Display for OperatorPurpose { |
| 4190 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 4191 | + match self { |
| 4192 | + OperatorPurpose::ForSearch => write!(f, "FOR SEARCH"), |
| 4193 | + OperatorPurpose::ForOrderBy { sort_family } => { |
| 4194 | + write!(f, "FOR ORDER BY {sort_family}") |
| 4195 | + } |
| 4196 | + } |
| 4197 | + } |
| 4198 | +} |
0 commit comments