Skip to content

Commit 33df166

Browse files
ast: add PostgreSQL text search DDL statement nodes
Introduce AST structures for CREATE/ALTER TEXT SEARCH object types\n(dictionary, configuration, template, parser), including display\nimplementations, statement variants, From conversions, and span wiring.
1 parent bd7f70e commit 33df166

File tree

3 files changed

+183
-10
lines changed

3 files changed

+183
-10
lines changed

src/ast/ddl.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5121,6 +5121,151 @@ impl Spanned for AlterOperatorClass {
51215121
}
51225122
}
51235123

5124+
/// PostgreSQL text search object kind.
5125+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
5126+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5127+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5128+
pub enum TextSearchObjectType {
5129+
/// `DICTIONARY`
5130+
Dictionary,
5131+
/// `CONFIGURATION`
5132+
Configuration,
5133+
/// `TEMPLATE`
5134+
Template,
5135+
/// `PARSER`
5136+
Parser,
5137+
}
5138+
5139+
impl fmt::Display for TextSearchObjectType {
5140+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5141+
match self {
5142+
TextSearchObjectType::Dictionary => write!(f, "DICTIONARY"),
5143+
TextSearchObjectType::Configuration => write!(f, "CONFIGURATION"),
5144+
TextSearchObjectType::Template => write!(f, "TEMPLATE"),
5145+
TextSearchObjectType::Parser => write!(f, "PARSER"),
5146+
}
5147+
}
5148+
}
5149+
5150+
/// PostgreSQL `CREATE TEXT SEARCH ...` statement.
5151+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5152+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5153+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5154+
pub struct CreateTextSearch {
5155+
/// The specific text search object type.
5156+
pub object_type: TextSearchObjectType,
5157+
/// Object name.
5158+
pub name: ObjectName,
5159+
/// Parenthesized options.
5160+
pub options: Vec<SqlOption>,
5161+
}
5162+
5163+
impl fmt::Display for CreateTextSearch {
5164+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5165+
write!(
5166+
f,
5167+
"CREATE TEXT SEARCH {} {} ({})",
5168+
self.object_type,
5169+
self.name,
5170+
display_comma_separated(&self.options)
5171+
)
5172+
}
5173+
}
5174+
5175+
impl Spanned for CreateTextSearch {
5176+
fn span(&self) -> Span {
5177+
Span::empty()
5178+
}
5179+
}
5180+
5181+
/// Option assignment used by `ALTER TEXT SEARCH DICTIONARY ... ( ... )`.
5182+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5183+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5184+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5185+
pub struct AlterTextSearchDictionaryOption {
5186+
/// Option name.
5187+
pub key: Ident,
5188+
/// Optional value (`option [= value]`).
5189+
pub value: Option<Expr>,
5190+
}
5191+
5192+
impl fmt::Display for AlterTextSearchDictionaryOption {
5193+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5194+
match &self.value {
5195+
Some(value) => write!(f, "{} = {}", self.key, value),
5196+
None => write!(f, "{}", self.key),
5197+
}
5198+
}
5199+
}
5200+
5201+
/// Operation for PostgreSQL `ALTER TEXT SEARCH ...`.
5202+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5203+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5204+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5205+
pub enum AlterTextSearchOperation {
5206+
/// `RENAME TO new_name`
5207+
RenameTo {
5208+
/// New name.
5209+
new_name: Ident,
5210+
},
5211+
/// `OWNER TO ...`
5212+
OwnerTo(Owner),
5213+
/// `SET SCHEMA schema_name`
5214+
SetSchema {
5215+
/// Target schema.
5216+
schema_name: ObjectName,
5217+
},
5218+
/// `( option [= value] [, ...] )`
5219+
SetOptions {
5220+
/// Dictionary options to apply.
5221+
options: Vec<AlterTextSearchDictionaryOption>,
5222+
},
5223+
}
5224+
5225+
impl fmt::Display for AlterTextSearchOperation {
5226+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5227+
match self {
5228+
AlterTextSearchOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
5229+
AlterTextSearchOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
5230+
AlterTextSearchOperation::SetSchema { schema_name } => {
5231+
write!(f, "SET SCHEMA {schema_name}")
5232+
}
5233+
AlterTextSearchOperation::SetOptions { options } => {
5234+
write!(f, "({})", display_comma_separated(options))
5235+
}
5236+
}
5237+
}
5238+
}
5239+
5240+
/// PostgreSQL `ALTER TEXT SEARCH ...` statement.
5241+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5242+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5243+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5244+
pub struct AlterTextSearch {
5245+
/// The specific text search object type.
5246+
pub object_type: TextSearchObjectType,
5247+
/// Object name.
5248+
pub name: ObjectName,
5249+
/// Operation to apply.
5250+
pub operation: AlterTextSearchOperation,
5251+
}
5252+
5253+
impl fmt::Display for AlterTextSearch {
5254+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5255+
write!(
5256+
f,
5257+
"ALTER TEXT SEARCH {} {} {}",
5258+
self.object_type, self.name, self.operation
5259+
)
5260+
}
5261+
}
5262+
5263+
impl Spanned for AlterTextSearch {
5264+
fn span(&self) -> Span {
5265+
Span::empty()
5266+
}
5267+
}
5268+
51245269
/// CREATE POLICY statement.
51255270
///
51265271
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createpolicy.html)

src/ast/mod.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,22 @@ pub use self::ddl::{
6464
AlterOperatorClass, AlterOperatorClassOperation, AlterOperatorFamily,
6565
AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy, AlterPolicyOperation,
6666
AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm, AlterTableLock,
67-
AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
67+
AlterTableOperation, AlterTableType, AlterTextSearch, AlterTextSearchDictionaryOption,
68+
AlterTextSearchOperation, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
6869
AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue, ClusteredBy, ColumnDef,
6970
ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy, ColumnPolicyProperty,
7071
ConstraintCharacteristics, CreateConnector, CreateDomain, CreateExtension, CreateFunction,
7172
CreateIndex, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreatePolicy,
72-
CreatePolicyCommand, CreatePolicyType, CreateTable, CreateTrigger, CreateView, Deduplicate,
73-
DeferrableInitial, DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorClass,
74-
DropOperatorFamily, DropOperatorSignature, DropPolicy, DropTrigger, ForValues, GeneratedAs,
75-
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
76-
IdentityPropertyKind, IdentityPropertyOrder, IndexColumn, IndexOption, IndexType,
77-
KeyOrIndexDisplay, Msck, NullsDistinctOption, OperatorArgTypes, OperatorClassItem,
78-
OperatorFamilyDropItem, OperatorFamilyItem, OperatorOption, OperatorPurpose, Owner, Partition,
79-
PartitionBoundValue, ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity,
80-
TagsColumnOption, TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
73+
CreatePolicyCommand, CreatePolicyType, CreateTable, CreateTextSearch, CreateTrigger,
74+
CreateView, Deduplicate, DeferrableInitial, DropBehavior, DropExtension, DropFunction,
75+
DropOperator, DropOperatorClass, DropOperatorFamily, DropOperatorSignature, DropPolicy,
76+
DropTrigger, ForValues, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
77+
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
78+
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
79+
OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem, OperatorFamilyItem,
80+
OperatorOption, OperatorPurpose, Owner, Partition, PartitionBoundValue, ProcedureParam,
81+
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
82+
TextSearchObjectType, TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
8183
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
8284
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
8385
};
@@ -3707,6 +3709,11 @@ pub enum Statement {
37073709
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createopclass.html)
37083710
CreateOperatorClass(CreateOperatorClass),
37093711
/// ```sql
3712+
/// CREATE TEXT SEARCH { DICTIONARY | CONFIGURATION | TEMPLATE | PARSER }
3713+
/// ```
3714+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/textsearch-intro.html)
3715+
CreateTextSearch(CreateTextSearch),
3716+
/// ```sql
37103717
/// ALTER TABLE
37113718
/// ```
37123719
AlterTable(AlterTable),
@@ -3759,6 +3766,11 @@ pub enum Statement {
37593766
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteropclass.html)
37603767
AlterOperatorClass(AlterOperatorClass),
37613768
/// ```sql
3769+
/// ALTER TEXT SEARCH { DICTIONARY | CONFIGURATION | TEMPLATE | PARSER }
3770+
/// ```
3771+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/textsearch-configuration.html)
3772+
AlterTextSearch(AlterTextSearch),
3773+
/// ```sql
37623774
/// ALTER ROLE
37633775
/// ```
37643776
AlterRole {
@@ -5443,6 +5455,7 @@ impl fmt::Display for Statement {
54435455
create_operator_family.fmt(f)
54445456
}
54455457
Statement::CreateOperatorClass(create_operator_class) => create_operator_class.fmt(f),
5458+
Statement::CreateTextSearch(create_text_search) => create_text_search.fmt(f),
54465459
Statement::AlterTable(alter_table) => write!(f, "{alter_table}"),
54475460
Statement::AlterIndex { name, operation } => {
54485461
write!(f, "ALTER INDEX {name} {operation}")
@@ -5472,6 +5485,7 @@ impl fmt::Display for Statement {
54725485
Statement::AlterOperatorClass(alter_operator_class) => {
54735486
write!(f, "{alter_operator_class}")
54745487
}
5488+
Statement::AlterTextSearch(alter_text_search) => write!(f, "{alter_text_search}"),
54755489
Statement::AlterRole { name, operation } => {
54765490
write!(f, "ALTER ROLE {name} {operation}")
54775491
}
@@ -11912,6 +11926,12 @@ impl From<CreateOperatorClass> for Statement {
1191211926
}
1191311927
}
1191411928

11929+
impl From<CreateTextSearch> for Statement {
11930+
fn from(c: CreateTextSearch) -> Self {
11931+
Self::CreateTextSearch(c)
11932+
}
11933+
}
11934+
1191511935
impl From<AlterSchema> for Statement {
1191611936
fn from(a: AlterSchema) -> Self {
1191711937
Self::AlterSchema(a)
@@ -11942,6 +11962,12 @@ impl From<AlterOperatorClass> for Statement {
1194211962
}
1194311963
}
1194411964

11965+
impl From<AlterTextSearch> for Statement {
11966+
fn from(a: AlterTextSearch) -> Self {
11967+
Self::AlterTextSearch(a)
11968+
}
11969+
}
11970+
1194511971
impl From<Merge> for Statement {
1194611972
fn from(m: Merge) -> Self {
1194711973
Self::Merge(m)

src/ast/spans.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ impl Spanned for Statement {
388388
create_operator_family.span()
389389
}
390390
Statement::CreateOperatorClass(create_operator_class) => create_operator_class.span(),
391+
Statement::CreateTextSearch(create_text_search) => create_text_search.span(),
391392
Statement::AlterTable(alter_table) => alter_table.span(),
392393
Statement::AlterIndex { name, operation } => name.span().union(&operation.span()),
393394
Statement::AlterView {
@@ -406,6 +407,7 @@ impl Spanned for Statement {
406407
Statement::AlterOperator { .. } => Span::empty(),
407408
Statement::AlterOperatorFamily { .. } => Span::empty(),
408409
Statement::AlterOperatorClass { .. } => Span::empty(),
410+
Statement::AlterTextSearch { .. } => Span::empty(),
409411
Statement::AlterRole { .. } => Span::empty(),
410412
Statement::AlterSession { .. } => Span::empty(),
411413
Statement::AttachDatabase { .. } => Span::empty(),

0 commit comments

Comments
 (0)