Skip to content

Commit cb2aeb8

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 7c78d13 commit cb2aeb8

3 files changed

Lines changed: 183 additions & 10 deletions

File tree

src/ast/ddl.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5627,6 +5627,151 @@ impl Spanned for AlterFunction {
56275627
}
56285628
}
56295629

5630+
/// PostgreSQL text search object kind.
5631+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
5632+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5633+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5634+
pub enum TextSearchObjectType {
5635+
/// `DICTIONARY`
5636+
Dictionary,
5637+
/// `CONFIGURATION`
5638+
Configuration,
5639+
/// `TEMPLATE`
5640+
Template,
5641+
/// `PARSER`
5642+
Parser,
5643+
}
5644+
5645+
impl fmt::Display for TextSearchObjectType {
5646+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5647+
match self {
5648+
TextSearchObjectType::Dictionary => write!(f, "DICTIONARY"),
5649+
TextSearchObjectType::Configuration => write!(f, "CONFIGURATION"),
5650+
TextSearchObjectType::Template => write!(f, "TEMPLATE"),
5651+
TextSearchObjectType::Parser => write!(f, "PARSER"),
5652+
}
5653+
}
5654+
}
5655+
5656+
/// PostgreSQL `CREATE TEXT SEARCH ...` statement.
5657+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5658+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5659+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5660+
pub struct CreateTextSearch {
5661+
/// The specific text search object type.
5662+
pub object_type: TextSearchObjectType,
5663+
/// Object name.
5664+
pub name: ObjectName,
5665+
/// Parenthesized options.
5666+
pub options: Vec<SqlOption>,
5667+
}
5668+
5669+
impl fmt::Display for CreateTextSearch {
5670+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5671+
write!(
5672+
f,
5673+
"CREATE TEXT SEARCH {} {} ({})",
5674+
self.object_type,
5675+
self.name,
5676+
display_comma_separated(&self.options)
5677+
)
5678+
}
5679+
}
5680+
5681+
impl Spanned for CreateTextSearch {
5682+
fn span(&self) -> Span {
5683+
Span::empty()
5684+
}
5685+
}
5686+
5687+
/// Option assignment used by `ALTER TEXT SEARCH DICTIONARY ... ( ... )`.
5688+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5689+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5690+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5691+
pub struct AlterTextSearchDictionaryOption {
5692+
/// Option name.
5693+
pub key: Ident,
5694+
/// Optional value (`option [= value]`).
5695+
pub value: Option<Expr>,
5696+
}
5697+
5698+
impl fmt::Display for AlterTextSearchDictionaryOption {
5699+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5700+
match &self.value {
5701+
Some(value) => write!(f, "{} = {}", self.key, value),
5702+
None => write!(f, "{}", self.key),
5703+
}
5704+
}
5705+
}
5706+
5707+
/// Operation for PostgreSQL `ALTER TEXT SEARCH ...`.
5708+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5709+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5710+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5711+
pub enum AlterTextSearchOperation {
5712+
/// `RENAME TO new_name`
5713+
RenameTo {
5714+
/// New name.
5715+
new_name: Ident,
5716+
},
5717+
/// `OWNER TO ...`
5718+
OwnerTo(Owner),
5719+
/// `SET SCHEMA schema_name`
5720+
SetSchema {
5721+
/// Target schema.
5722+
schema_name: ObjectName,
5723+
},
5724+
/// `( option [= value] [, ...] )`
5725+
SetOptions {
5726+
/// Dictionary options to apply.
5727+
options: Vec<AlterTextSearchDictionaryOption>,
5728+
},
5729+
}
5730+
5731+
impl fmt::Display for AlterTextSearchOperation {
5732+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5733+
match self {
5734+
AlterTextSearchOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
5735+
AlterTextSearchOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
5736+
AlterTextSearchOperation::SetSchema { schema_name } => {
5737+
write!(f, "SET SCHEMA {schema_name}")
5738+
}
5739+
AlterTextSearchOperation::SetOptions { options } => {
5740+
write!(f, "({})", display_comma_separated(options))
5741+
}
5742+
}
5743+
}
5744+
}
5745+
5746+
/// PostgreSQL `ALTER TEXT SEARCH ...` statement.
5747+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5748+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5749+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5750+
pub struct AlterTextSearch {
5751+
/// The specific text search object type.
5752+
pub object_type: TextSearchObjectType,
5753+
/// Object name.
5754+
pub name: ObjectName,
5755+
/// Operation to apply.
5756+
pub operation: AlterTextSearchOperation,
5757+
}
5758+
5759+
impl fmt::Display for AlterTextSearch {
5760+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5761+
write!(
5762+
f,
5763+
"ALTER TEXT SEARCH {} {} {}",
5764+
self.object_type, self.name, self.operation
5765+
)
5766+
}
5767+
}
5768+
5769+
impl Spanned for AlterTextSearch {
5770+
fn span(&self) -> Span {
5771+
Span::empty()
5772+
}
5773+
}
5774+
56305775
/// CREATE POLICY statement.
56315776
///
56325777
/// 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
@@ -65,21 +65,23 @@ pub use self::ddl::{
6565
AlterIndexOperation, AlterOperator, AlterOperatorClass, AlterOperatorClassOperation,
6666
AlterOperatorFamily, AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy,
6767
AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm,
68-
AlterTableLock, AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue,
68+
AlterTableLock, AlterTableOperation, AlterTableType, AlterTextSearch,
69+
AlterTextSearchDictionaryOption, AlterTextSearchOperation, AlterType, AlterTypeAddValue,
6970
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
7071
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
7172
ColumnPolicyProperty, ConstraintCharacteristics, CreateCollation, CreateCollationDefinition,
7273
CreateConnector, CreateDomain, CreateExtension, CreateFunction, CreateIndex, CreateOperator,
7374
CreateOperatorClass, CreateOperatorFamily, CreatePolicy, CreatePolicyCommand, CreatePolicyType,
74-
CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial, DistStyle,
75-
DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily,
76-
DropOperatorSignature, DropPolicy, DropTrigger, ForValues, FunctionReturnType, GeneratedAs,
77-
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
78-
IdentityPropertyKind, IdentityPropertyOrder, IndexColumn, IndexOption, IndexType,
79-
KeyOrIndexDisplay, Msck, NullsDistinctOption, OperatorArgTypes, OperatorClassItem,
80-
OperatorFamilyDropItem, OperatorFamilyItem, OperatorOption, OperatorPurpose, Owner, Partition,
81-
PartitionBoundValue, ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity,
82-
TagsColumnOption, TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
75+
CreateTable, CreateTextSearch, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
76+
DistStyle, DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorClass,
77+
DropOperatorFamily, DropOperatorSignature, DropPolicy, DropTrigger, ForValues,
78+
FunctionReturnType, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
79+
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
80+
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
81+
OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem, OperatorFamilyItem,
82+
OperatorOption, OperatorPurpose, Owner, Partition, PartitionBoundValue, ProcedureParam,
83+
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
84+
TextSearchObjectType, TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
8385
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
8486
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef, WithData,
8587
};
@@ -3756,6 +3758,11 @@ pub enum Statement {
37563758
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createopclass.html)
37573759
CreateOperatorClass(CreateOperatorClass),
37583760
/// ```sql
3761+
/// CREATE TEXT SEARCH { DICTIONARY | CONFIGURATION | TEMPLATE | PARSER }
3762+
/// ```
3763+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/textsearch-intro.html)
3764+
CreateTextSearch(CreateTextSearch),
3765+
/// ```sql
37593766
/// ALTER TABLE
37603767
/// ```
37613768
AlterTable(AlterTable),
@@ -3820,6 +3827,11 @@ pub enum Statement {
38203827
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteropclass.html)
38213828
AlterOperatorClass(AlterOperatorClass),
38223829
/// ```sql
3830+
/// ALTER TEXT SEARCH { DICTIONARY | CONFIGURATION | TEMPLATE | PARSER }
3831+
/// ```
3832+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/textsearch-configuration.html)
3833+
AlterTextSearch(AlterTextSearch),
3834+
/// ```sql
38233835
/// ALTER ROLE
38243836
/// ```
38253837
AlterRole {
@@ -5543,6 +5555,7 @@ impl fmt::Display for Statement {
55435555
create_operator_family.fmt(f)
55445556
}
55455557
Statement::CreateOperatorClass(create_operator_class) => create_operator_class.fmt(f),
5558+
Statement::CreateTextSearch(create_text_search) => create_text_search.fmt(f),
55465559
Statement::AlterTable(alter_table) => write!(f, "{alter_table}"),
55475560
Statement::AlterIndex { name, operation } => {
55485561
write!(f, "ALTER INDEX {name} {operation}")
@@ -5574,6 +5587,7 @@ impl fmt::Display for Statement {
55745587
Statement::AlterOperatorClass(alter_operator_class) => {
55755588
write!(f, "{alter_operator_class}")
55765589
}
5590+
Statement::AlterTextSearch(alter_text_search) => write!(f, "{alter_text_search}"),
55775591
Statement::AlterRole { name, operation } => {
55785592
write!(f, "ALTER ROLE {name} {operation}")
55795593
}
@@ -12197,6 +12211,12 @@ impl From<CreateOperatorClass> for Statement {
1219712211
}
1219812212
}
1219912213

12214+
impl From<CreateTextSearch> for Statement {
12215+
fn from(c: CreateTextSearch) -> Self {
12216+
Self::CreateTextSearch(c)
12217+
}
12218+
}
12219+
1220012220
impl From<AlterSchema> for Statement {
1220112221
fn from(a: AlterSchema) -> Self {
1220212222
Self::AlterSchema(a)
@@ -12239,6 +12259,12 @@ impl From<AlterOperatorClass> for Statement {
1223912259
}
1224012260
}
1224112261

12262+
impl From<AlterTextSearch> for Statement {
12263+
fn from(a: AlterTextSearch) -> Self {
12264+
Self::AlterTextSearch(a)
12265+
}
12266+
}
12267+
1224212268
impl From<Merge> for Statement {
1224312269
fn from(m: Merge) -> Self {
1224412270
Self::Merge(m)

src/ast/spans.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ impl Spanned for Statement {
399399
create_operator_family.span()
400400
}
401401
Statement::CreateOperatorClass(create_operator_class) => create_operator_class.span(),
402+
Statement::CreateTextSearch(create_text_search) => create_text_search.span(),
402403
Statement::AlterTable(alter_table) => alter_table.span(),
403404
Statement::AlterIndex { name, operation } => name.span().union(&operation.span()),
404405
Statement::AlterView {
@@ -419,6 +420,7 @@ impl Spanned for Statement {
419420
Statement::AlterOperator { .. } => Span::empty(),
420421
Statement::AlterOperatorFamily { .. } => Span::empty(),
421422
Statement::AlterOperatorClass { .. } => Span::empty(),
423+
Statement::AlterTextSearch { .. } => Span::empty(),
422424
Statement::AlterRole { .. } => Span::empty(),
423425
Statement::AlterSession { .. } => Span::empty(),
424426
Statement::AttachDatabase { .. } => Span::empty(),

0 commit comments

Comments
 (0)