Skip to content

Commit 973545b

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 6f8e7b8 commit 973545b

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
@@ -5222,6 +5222,151 @@ impl Spanned for AlterOperatorClass {
52225222
}
52235223
}
52245224

5225+
/// PostgreSQL text search object kind.
5226+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
5227+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5228+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5229+
pub enum TextSearchObjectType {
5230+
/// `DICTIONARY`
5231+
Dictionary,
5232+
/// `CONFIGURATION`
5233+
Configuration,
5234+
/// `TEMPLATE`
5235+
Template,
5236+
/// `PARSER`
5237+
Parser,
5238+
}
5239+
5240+
impl fmt::Display for TextSearchObjectType {
5241+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5242+
match self {
5243+
TextSearchObjectType::Dictionary => write!(f, "DICTIONARY"),
5244+
TextSearchObjectType::Configuration => write!(f, "CONFIGURATION"),
5245+
TextSearchObjectType::Template => write!(f, "TEMPLATE"),
5246+
TextSearchObjectType::Parser => write!(f, "PARSER"),
5247+
}
5248+
}
5249+
}
5250+
5251+
/// PostgreSQL `CREATE TEXT SEARCH ...` statement.
5252+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5253+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5254+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5255+
pub struct CreateTextSearch {
5256+
/// The specific text search object type.
5257+
pub object_type: TextSearchObjectType,
5258+
/// Object name.
5259+
pub name: ObjectName,
5260+
/// Parenthesized options.
5261+
pub options: Vec<SqlOption>,
5262+
}
5263+
5264+
impl fmt::Display for CreateTextSearch {
5265+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5266+
write!(
5267+
f,
5268+
"CREATE TEXT SEARCH {} {} ({})",
5269+
self.object_type,
5270+
self.name,
5271+
display_comma_separated(&self.options)
5272+
)
5273+
}
5274+
}
5275+
5276+
impl Spanned for CreateTextSearch {
5277+
fn span(&self) -> Span {
5278+
Span::empty()
5279+
}
5280+
}
5281+
5282+
/// Option assignment used by `ALTER TEXT SEARCH DICTIONARY ... ( ... )`.
5283+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5284+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5285+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5286+
pub struct AlterTextSearchDictionaryOption {
5287+
/// Option name.
5288+
pub key: Ident,
5289+
/// Optional value (`option [= value]`).
5290+
pub value: Option<Expr>,
5291+
}
5292+
5293+
impl fmt::Display for AlterTextSearchDictionaryOption {
5294+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5295+
match &self.value {
5296+
Some(value) => write!(f, "{} = {}", self.key, value),
5297+
None => write!(f, "{}", self.key),
5298+
}
5299+
}
5300+
}
5301+
5302+
/// Operation for PostgreSQL `ALTER TEXT SEARCH ...`.
5303+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5304+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5305+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5306+
pub enum AlterTextSearchOperation {
5307+
/// `RENAME TO new_name`
5308+
RenameTo {
5309+
/// New name.
5310+
new_name: Ident,
5311+
},
5312+
/// `OWNER TO ...`
5313+
OwnerTo(Owner),
5314+
/// `SET SCHEMA schema_name`
5315+
SetSchema {
5316+
/// Target schema.
5317+
schema_name: ObjectName,
5318+
},
5319+
/// `( option [= value] [, ...] )`
5320+
SetOptions {
5321+
/// Dictionary options to apply.
5322+
options: Vec<AlterTextSearchDictionaryOption>,
5323+
},
5324+
}
5325+
5326+
impl fmt::Display for AlterTextSearchOperation {
5327+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5328+
match self {
5329+
AlterTextSearchOperation::RenameTo { new_name } => write!(f, "RENAME TO {new_name}"),
5330+
AlterTextSearchOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
5331+
AlterTextSearchOperation::SetSchema { schema_name } => {
5332+
write!(f, "SET SCHEMA {schema_name}")
5333+
}
5334+
AlterTextSearchOperation::SetOptions { options } => {
5335+
write!(f, "({})", display_comma_separated(options))
5336+
}
5337+
}
5338+
}
5339+
}
5340+
5341+
/// PostgreSQL `ALTER TEXT SEARCH ...` statement.
5342+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5343+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5344+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5345+
pub struct AlterTextSearch {
5346+
/// The specific text search object type.
5347+
pub object_type: TextSearchObjectType,
5348+
/// Object name.
5349+
pub name: ObjectName,
5350+
/// Operation to apply.
5351+
pub operation: AlterTextSearchOperation,
5352+
}
5353+
5354+
impl fmt::Display for AlterTextSearch {
5355+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5356+
write!(
5357+
f,
5358+
"ALTER TEXT SEARCH {} {} {}",
5359+
self.object_type, self.name, self.operation
5360+
)
5361+
}
5362+
}
5363+
5364+
impl Spanned for AlterTextSearch {
5365+
fn span(&self) -> Span {
5366+
Span::empty()
5367+
}
5368+
}
5369+
52255370
/// CREATE POLICY statement.
52265371
///
52275372
/// 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
@@ -64,23 +64,25 @@ 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, DistStyle, DropBehavior, DropExtension, DropFunction, DropOperator,
74-
DropOperatorClass, DropOperatorFamily, DropOperatorSignature, DropPolicy, DropTrigger,
75-
ForValues, FunctionReturnType, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
73+
CreatePolicyCommand, CreatePolicyType, CreateTable, CreateTextSearch, CreateTrigger,
74+
CreateView, Deduplicate, DeferrableInitial, DistStyle, DropBehavior, DropExtension,
75+
DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily, DropOperatorSignature,
76+
DropPolicy, DropTrigger, ForValues, FunctionReturnType, GeneratedAs,
77+
GeneratedExpressionMode, IdentityParameters,
7678
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
7779
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
7880
OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem, OperatorFamilyItem,
7981
OperatorOption, OperatorPurpose, Owner, Partition, PartitionBoundValue, ProcedureParam,
80-
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption, TriggerObjectKind,
81-
Truncate, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
82-
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
83-
UserDefinedTypeStorage, ViewColumnDef,
82+
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
83+
TextSearchObjectType, TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
84+
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
85+
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
8486
};
8587
pub use self::dml::{
8688
Delete, Insert, Merge, MergeAction, MergeClause, MergeClauseKind, MergeInsertExpr,
@@ -3719,6 +3721,11 @@ pub enum Statement {
37193721
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createopclass.html)
37203722
CreateOperatorClass(CreateOperatorClass),
37213723
/// ```sql
3724+
/// CREATE TEXT SEARCH { DICTIONARY | CONFIGURATION | TEMPLATE | PARSER }
3725+
/// ```
3726+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/textsearch-intro.html)
3727+
CreateTextSearch(CreateTextSearch),
3728+
/// ```sql
37223729
/// ALTER TABLE
37233730
/// ```
37243731
AlterTable(AlterTable),
@@ -3771,6 +3778,11 @@ pub enum Statement {
37713778
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteropclass.html)
37723779
AlterOperatorClass(AlterOperatorClass),
37733780
/// ```sql
3781+
/// ALTER TEXT SEARCH { DICTIONARY | CONFIGURATION | TEMPLATE | PARSER }
3782+
/// ```
3783+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/textsearch-configuration.html)
3784+
AlterTextSearch(AlterTextSearch),
3785+
/// ```sql
37743786
/// ALTER ROLE
37753787
/// ```
37763788
AlterRole {
@@ -5467,6 +5479,7 @@ impl fmt::Display for Statement {
54675479
create_operator_family.fmt(f)
54685480
}
54695481
Statement::CreateOperatorClass(create_operator_class) => create_operator_class.fmt(f),
5482+
Statement::CreateTextSearch(create_text_search) => create_text_search.fmt(f),
54705483
Statement::AlterTable(alter_table) => write!(f, "{alter_table}"),
54715484
Statement::AlterIndex { name, operation } => {
54725485
write!(f, "ALTER INDEX {name} {operation}")
@@ -5496,6 +5509,7 @@ impl fmt::Display for Statement {
54965509
Statement::AlterOperatorClass(alter_operator_class) => {
54975510
write!(f, "{alter_operator_class}")
54985511
}
5512+
Statement::AlterTextSearch(alter_text_search) => write!(f, "{alter_text_search}"),
54995513
Statement::AlterRole { name, operation } => {
55005514
write!(f, "ALTER ROLE {name} {operation}")
55015515
}
@@ -12074,6 +12088,12 @@ impl From<CreateOperatorClass> for Statement {
1207412088
}
1207512089
}
1207612090

12091+
impl From<CreateTextSearch> for Statement {
12092+
fn from(c: CreateTextSearch) -> Self {
12093+
Self::CreateTextSearch(c)
12094+
}
12095+
}
12096+
1207712097
impl From<AlterSchema> for Statement {
1207812098
fn from(a: AlterSchema) -> Self {
1207912099
Self::AlterSchema(a)
@@ -12104,6 +12124,12 @@ impl From<AlterOperatorClass> for Statement {
1210412124
}
1210512125
}
1210612126

12127+
impl From<AlterTextSearch> for Statement {
12128+
fn from(a: AlterTextSearch) -> Self {
12129+
Self::AlterTextSearch(a)
12130+
}
12131+
}
12132+
1210712133
impl From<Merge> for Statement {
1210812134
fn from(m: Merge) -> Self {
1210912135
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 {
@@ -407,6 +408,7 @@ impl Spanned for Statement {
407408
Statement::AlterOperator { .. } => Span::empty(),
408409
Statement::AlterOperatorFamily { .. } => Span::empty(),
409410
Statement::AlterOperatorClass { .. } => Span::empty(),
411+
Statement::AlterTextSearch { .. } => Span::empty(),
410412
Statement::AlterRole { .. } => Span::empty(),
411413
Statement::AlterSession { .. } => Span::empty(),
412414
Statement::AttachDatabase { .. } => Span::empty(),

0 commit comments

Comments
 (0)