Skip to content

Commit e7b0545

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 2b0dc2d commit e7b0545

File tree

3 files changed

+182
-9
lines changed

3 files changed

+182
-9
lines changed

src/ast/ddl.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5430,6 +5430,151 @@ impl Spanned for AlterFunction {
54305430
}
54315431
}
54325432

5433+
/// PostgreSQL text search object kind.
5434+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
5435+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5436+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5437+
pub enum TextSearchObjectType {
5438+
/// `DICTIONARY`
5439+
Dictionary,
5440+
/// `CONFIGURATION`
5441+
Configuration,
5442+
/// `TEMPLATE`
5443+
Template,
5444+
/// `PARSER`
5445+
Parser,
5446+
}
5447+
5448+
impl fmt::Display for TextSearchObjectType {
5449+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5450+
match self {
5451+
TextSearchObjectType::Dictionary => write!(f, "DICTIONARY"),
5452+
TextSearchObjectType::Configuration => write!(f, "CONFIGURATION"),
5453+
TextSearchObjectType::Template => write!(f, "TEMPLATE"),
5454+
TextSearchObjectType::Parser => write!(f, "PARSER"),
5455+
}
5456+
}
5457+
}
5458+
5459+
/// PostgreSQL `CREATE TEXT SEARCH ...` statement.
5460+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5461+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5462+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5463+
pub struct CreateTextSearch {
5464+
/// The specific text search object type.
5465+
pub object_type: TextSearchObjectType,
5466+
/// Object name.
5467+
pub name: ObjectName,
5468+
/// Parenthesized options.
5469+
pub options: Vec<SqlOption>,
5470+
}
5471+
5472+
impl fmt::Display for CreateTextSearch {
5473+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5474+
write!(
5475+
f,
5476+
"CREATE TEXT SEARCH {} {} ({})",
5477+
self.object_type,
5478+
self.name,
5479+
display_comma_separated(&self.options)
5480+
)
5481+
}
5482+
}
5483+
5484+
impl Spanned for CreateTextSearch {
5485+
fn span(&self) -> Span {
5486+
Span::empty()
5487+
}
5488+
}
5489+
5490+
/// Option assignment used by `ALTER TEXT SEARCH DICTIONARY ... ( ... )`.
5491+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5492+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5493+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5494+
pub struct AlterTextSearchDictionaryOption {
5495+
/// Option name.
5496+
pub key: Ident,
5497+
/// Optional value (`option [= value]`).
5498+
pub value: Option<Expr>,
5499+
}
5500+
5501+
impl fmt::Display for AlterTextSearchDictionaryOption {
5502+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5503+
match &self.value {
5504+
Some(value) => write!(f, "{} = {}", self.key, value),
5505+
None => write!(f, "{}", self.key),
5506+
}
5507+
}
5508+
}
5509+
5510+
/// Operation for PostgreSQL `ALTER TEXT SEARCH ...`.
5511+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5512+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5513+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5514+
pub enum AlterTextSearchOperation {
5515+
/// `RENAME TO new_name`
5516+
RenameTo {
5517+
/// New name.
5518+
new_name: Ident,
5519+
},
5520+
/// `OWNER TO ...`
5521+
OwnerTo(Owner),
5522+
/// `SET SCHEMA schema_name`
5523+
SetSchema {
5524+
/// Target schema.
5525+
schema_name: ObjectName,
5526+
},
5527+
/// `( option [= value] [, ...] )`
5528+
SetOptions {
5529+
/// Dictionary options to apply.
5530+
options: Vec<AlterTextSearchDictionaryOption>,
5531+
},
5532+
}
5533+
5534+
impl fmt::Display for AlterTextSearchOperation {
5535+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5536+
match self {
5537+
AlterTextSearchOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
5538+
AlterTextSearchOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
5539+
AlterTextSearchOperation::SetSchema { schema_name } => {
5540+
write!(f, "SET SCHEMA {schema_name}")
5541+
}
5542+
AlterTextSearchOperation::SetOptions { options } => {
5543+
write!(f, "({})", display_comma_separated(options))
5544+
}
5545+
}
5546+
}
5547+
}
5548+
5549+
/// PostgreSQL `ALTER TEXT SEARCH ...` statement.
5550+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5551+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5552+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5553+
pub struct AlterTextSearch {
5554+
/// The specific text search object type.
5555+
pub object_type: TextSearchObjectType,
5556+
/// Object name.
5557+
pub name: ObjectName,
5558+
/// Operation to apply.
5559+
pub operation: AlterTextSearchOperation,
5560+
}
5561+
5562+
impl fmt::Display for AlterTextSearch {
5563+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5564+
write!(
5565+
f,
5566+
"ALTER TEXT SEARCH {} {} {}",
5567+
self.object_type, self.name, self.operation
5568+
)
5569+
}
5570+
}
5571+
5572+
impl Spanned for AlterTextSearch {
5573+
fn span(&self) -> Span {
5574+
Span::empty()
5575+
}
5576+
}
5577+
54335578
/// CREATE POLICY statement.
54345579
///
54355580
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createpolicy.html)

src/ast/mod.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,25 @@ pub use self::ddl::{
6565
AlterOperatorClass, AlterOperatorClassOperation, AlterOperatorFamily,
6666
AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy, AlterPolicyOperation,
6767
AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm, AlterTableLock,
68-
AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
68+
AlterTableOperation, AlterTableType, AlterTextSearch, AlterTextSearchDictionaryOption,
69+
AlterTextSearchOperation, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
6970
AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue, ClusteredBy, ColumnDef,
7071
ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy, ColumnPolicyProperty,
7172
ConstraintCharacteristics, CreateConnector, CreateDomain, CreateExtension, CreateFunction,
7273
CreateIndex, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreatePolicy,
73-
CreatePolicyCommand, CreatePolicyType, CreateTable, CreateTrigger, CreateView, Deduplicate,
74-
DeferrableInitial, DistStyle, DropBehavior, DropExtension, DropFunction, DropOperator,
75-
DropOperatorClass, DropOperatorFamily, DropOperatorSignature, DropPolicy, DropTrigger,
76-
ForValues, FunctionReturnType, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
74+
CreatePolicyCommand, CreatePolicyType, CreateTable, CreateTextSearch, CreateTrigger,
75+
CreateView, Deduplicate, DeferrableInitial, DistStyle, DropBehavior, DropExtension,
76+
DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily, DropOperatorSignature,
77+
DropPolicy, DropTrigger, ForValues, FunctionReturnType, GeneratedAs,
78+
GeneratedExpressionMode, IdentityParameters,
7779
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
7880
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
7981
OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem, OperatorFamilyItem,
8082
OperatorOption, OperatorPurpose, Owner, Partition, PartitionBoundValue, ProcedureParam,
81-
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption, TriggerObjectKind,
82-
Truncate, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
83-
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
84-
UserDefinedTypeStorage, ViewColumnDef,
83+
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
84+
TextSearchObjectType, TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
85+
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
86+
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
8587
};
8688
pub use self::dml::{
8789
Delete, Insert, Merge, MergeAction, MergeClause, MergeClauseKind, MergeInsertExpr,
@@ -3720,6 +3722,11 @@ pub enum Statement {
37203722
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createopclass.html)
37213723
CreateOperatorClass(CreateOperatorClass),
37223724
/// ```sql
3725+
/// CREATE TEXT SEARCH { DICTIONARY | CONFIGURATION | TEMPLATE | PARSER }
3726+
/// ```
3727+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/textsearch-intro.html)
3728+
CreateTextSearch(CreateTextSearch),
3729+
/// ```sql
37233730
/// ALTER TABLE
37243731
/// ```
37253732
AlterTable(AlterTable),
@@ -3779,6 +3786,11 @@ pub enum Statement {
37793786
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteropclass.html)
37803787
AlterOperatorClass(AlterOperatorClass),
37813788
/// ```sql
3789+
/// ALTER TEXT SEARCH { DICTIONARY | CONFIGURATION | TEMPLATE | PARSER }
3790+
/// ```
3791+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/textsearch-configuration.html)
3792+
AlterTextSearch(AlterTextSearch),
3793+
/// ```sql
37823794
/// ALTER ROLE
37833795
/// ```
37843796
AlterRole {
@@ -5484,6 +5496,7 @@ impl fmt::Display for Statement {
54845496
create_operator_family.fmt(f)
54855497
}
54865498
Statement::CreateOperatorClass(create_operator_class) => create_operator_class.fmt(f),
5499+
Statement::CreateTextSearch(create_text_search) => create_text_search.fmt(f),
54875500
Statement::AlterTable(alter_table) => write!(f, "{alter_table}"),
54885501
Statement::AlterIndex { name, operation } => {
54895502
write!(f, "ALTER INDEX {name} {operation}")
@@ -5514,6 +5527,7 @@ impl fmt::Display for Statement {
55145527
Statement::AlterOperatorClass(alter_operator_class) => {
55155528
write!(f, "{alter_operator_class}")
55165529
}
5530+
Statement::AlterTextSearch(alter_text_search) => write!(f, "{alter_text_search}"),
55175531
Statement::AlterRole { name, operation } => {
55185532
write!(f, "ALTER ROLE {name} {operation}")
55195533
}
@@ -12107,6 +12121,12 @@ impl From<CreateOperatorClass> for Statement {
1210712121
}
1210812122
}
1210912123

12124+
impl From<CreateTextSearch> for Statement {
12125+
fn from(c: CreateTextSearch) -> Self {
12126+
Self::CreateTextSearch(c)
12127+
}
12128+
}
12129+
1211012130
impl From<AlterSchema> for Statement {
1211112131
fn from(a: AlterSchema) -> Self {
1211212132
Self::AlterSchema(a)
@@ -12143,6 +12163,12 @@ impl From<AlterOperatorClass> for Statement {
1214312163
}
1214412164
}
1214512165

12166+
impl From<AlterTextSearch> for Statement {
12167+
fn from(a: AlterTextSearch) -> Self {
12168+
Self::AlterTextSearch(a)
12169+
}
12170+
}
12171+
1214612172
impl From<Merge> for Statement {
1214712173
fn from(m: Merge) -> Self {
1214812174
Self::Merge(m)

src/ast/spans.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ impl Spanned for Statement {
389389
create_operator_family.span()
390390
}
391391
Statement::CreateOperatorClass(create_operator_class) => create_operator_class.span(),
392+
Statement::CreateTextSearch(create_text_search) => create_text_search.span(),
392393
Statement::AlterTable(alter_table) => alter_table.span(),
393394
Statement::AlterIndex { name, operation } => name.span().union(&operation.span()),
394395
Statement::AlterView {
@@ -408,6 +409,7 @@ impl Spanned for Statement {
408409
Statement::AlterOperator { .. } => Span::empty(),
409410
Statement::AlterOperatorFamily { .. } => Span::empty(),
410411
Statement::AlterOperatorClass { .. } => Span::empty(),
412+
Statement::AlterTextSearch { .. } => Span::empty(),
411413
Statement::AlterRole { .. } => Span::empty(),
412414
Statement::AlterSession { .. } => Span::empty(),
413415
Statement::AttachDatabase { .. } => Span::empty(),

0 commit comments

Comments
 (0)