Skip to content

Commit abfc55e

Browse files
committed
Add support for ENABLE on ALTER TABLE for pg
Signed-off-by: Toby Hede <[email protected]>
1 parent 9298cc4 commit abfc55e

File tree

4 files changed

+118
-10
lines changed

4 files changed

+118
-10
lines changed

src/ast/ddl.rs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,47 @@ pub enum AlterTableOperation {
7373
///
7474
/// Note: this is a MySQL-specific operation.
7575
DropPrimaryKey,
76+
77+
/// `ENABLE TRIGGER [ trigger_name | ALL | USER ]`
78+
///
79+
/// Note: this is a PostgreSQL-specific operation.
80+
EnableTrigger {
81+
name: Ident,
82+
},
83+
/// `ENABLE RULE rewrite_rule_name`
84+
///
85+
/// Note: this is a PostgreSQL-specific operation.
86+
EnableRule {
87+
name: Ident,
88+
},
89+
/// `ENABLE ROW LEVEL SECURITY`
90+
///
91+
/// Note: this is a PostgreSQL-specific operation.
92+
EnableRowLevelSecurity,
93+
/// `ENABLE REPLICA TRIGGER trigger_name`
94+
///
95+
/// Note: this is a PostgreSQL-specific operation.
96+
EnableReplicaTrigger {
97+
name: Ident,
98+
},
99+
/// `ENABLE ALWAYS TRIGGER trigger_name`
100+
///
101+
/// Note: this is a PostgreSQL-specific operation.
102+
EnableAlwaysTrigger {
103+
name: Ident,
104+
},
105+
/// `ENABLE REPLICA RULE rewrite_rule_name`
106+
///
107+
/// Note: this is a PostgreSQL-specific operation.
108+
EnableReplicaRule {
109+
name: Ident,
110+
},
111+
/// `ENABLE ALWAYS RULE rewrite_rule_name`
112+
///
113+
/// Note: this is a PostgreSQL-specific operation.
114+
EnableAlwaysRule {
115+
name: Ident,
116+
},
76117
/// `RENAME TO PARTITION (partition=val)`
77118
RenamePartitions {
78119
old_partitions: Vec<Expr>,
@@ -93,7 +134,9 @@ pub enum AlterTableOperation {
93134
new_column_name: Ident,
94135
},
95136
/// `RENAME TO <table_name>`
96-
RenameTable { table_name: ObjectName },
137+
RenameTable {
138+
table_name: ObjectName,
139+
},
97140
// CHANGE [ COLUMN ] <old_name> <new_name> <data_type> [ <options> ]
98141
ChangeColumn {
99142
old_name: Ident,
@@ -104,7 +147,10 @@ pub enum AlterTableOperation {
104147
/// `RENAME CONSTRAINT <old_constraint_name> TO <new_constraint_name>`
105148
///
106149
/// Note: this is a PostgreSQL-specific operation.
107-
RenameConstraint { old_name: Ident, new_name: Ident },
150+
RenameConstraint {
151+
old_name: Ident,
152+
new_name: Ident,
153+
},
108154
/// `ALTER [ COLUMN ]`
109155
AlterColumn {
110156
column_name: Ident,
@@ -113,7 +159,9 @@ pub enum AlterTableOperation {
113159
/// 'SWAP WITH <table_name>'
114160
///
115161
/// Note: this is Snowflake specific <https://docs.snowflake.com/en/sql-reference/sql/alter-table>
116-
SwapWith { table_name: ObjectName },
162+
SwapWith {
163+
table_name: ObjectName,
164+
},
117165
}
118166

119167
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -198,6 +246,27 @@ impl fmt::Display for AlterTableOperation {
198246
column_name,
199247
if *cascade { " CASCADE" } else { "" }
200248
),
249+
AlterTableOperation::EnableTrigger { name } => {
250+
write!(f, "ENABLE TRIGGER {name}")
251+
}
252+
AlterTableOperation::EnableRule { name } => {
253+
write!(f, "ENABLE RULE {name}")
254+
}
255+
AlterTableOperation::EnableRowLevelSecurity => {
256+
write!(f, "ENABLE ROW LEVEL SECURITY")
257+
}
258+
AlterTableOperation::EnableReplicaTrigger { name } => {
259+
write!(f, "ENABLE REPLICA TRIGGER {name}")
260+
}
261+
AlterTableOperation::EnableAlwaysTrigger { name } => {
262+
write!(f, "ENABLE ALWAYS TRIGGER {name}")
263+
}
264+
AlterTableOperation::EnableReplicaRule { name } => {
265+
write!(f, "ENABLE REPLICA RULE {name}")
266+
}
267+
AlterTableOperation::EnableAlwaysRule { name } => {
268+
write!(f, "ENABLE ALWAYS RULE {name}")
269+
}
201270
AlterTableOperation::RenamePartitions {
202271
old_partitions,
203272
new_partitions,

src/keywords.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ define_keywords!(
241241
ELEMENTS,
242242
ELSE,
243243
EMPTY,
244+
ENABLE,
244245
ENCODING,
245246
ENCRYPTION,
246247
END,
@@ -538,6 +539,7 @@ define_keywords!(
538539
REPAIR,
539540
REPEATABLE,
540541
REPLACE,
542+
REPLICA,
541543
REPLICATION,
542544
RESET,
543545
RESPECT,
@@ -567,6 +569,7 @@ define_keywords!(
567569
SCROLL,
568570
SEARCH,
569571
SECOND,
572+
SECURITY,
570573
SELECT,
571574
SEMI,
572575
SENSITIVE,

src/parser/mod.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4638,24 +4638,46 @@ impl<'a> Parser<'a> {
46384638
}
46394639
} else if self.parse_keyword(Keyword::DISABLE) {
46404640
if self.parse_keyword(Keyword::TRIGGER) {
4641-
// let all = self.parse_keyword(Keyword::ALL);
4642-
// let user = self.parse_keyword(Keyword::ALL);
46434641
let name = self.parse_identifier()?;
4644-
4645-
// println!("parse all: {}", all);
4646-
46474642
AlterTableOperation::DisableTrigger { name }
46484643
} else if self.parse_keyword(Keyword::RULE) {
46494644
let name = self.parse_identifier()?;
46504645
AlterTableOperation::DisableRule { name }
4651-
} else if self.parse_keyword(Keyword::ROW) {
4646+
} else if self.parse_keywords(&[Keyword::ROW, Keyword::LEVEL, Keyword::SECURITY]) {
46524647
AlterTableOperation::DisableRowLevelSecurity {}
46534648
} else {
46544649
return self.expected(
46554650
"TRIGGER, RULE or ROW LEVEL SECURITY after DISABLE",
46564651
self.peek_token(),
46574652
);
46584653
}
4654+
} else if self.parse_keyword(Keyword::ENABLE) {
4655+
if self.parse_keyword(Keyword::TRIGGER) {
4656+
let name = self.parse_identifier()?;
4657+
AlterTableOperation::EnableTrigger { name }
4658+
} else if self.parse_keyword(Keyword::RULE) {
4659+
let name = self.parse_identifier()?;
4660+
AlterTableOperation::EnableRule { name }
4661+
} else if self.parse_keywords(&[Keyword::ROW, Keyword::LEVEL, Keyword::SECURITY]) {
4662+
AlterTableOperation::EnableRowLevelSecurity {}
4663+
} else if self.parse_keywords(&[Keyword::REPLICA, Keyword::RULE]) {
4664+
let name = self.parse_identifier()?;
4665+
AlterTableOperation::EnableReplicaRule { name }
4666+
} else if self.parse_keywords(&[Keyword::ALWAYS, Keyword::RULE]) {
4667+
let name = self.parse_identifier()?;
4668+
AlterTableOperation::EnableAlwaysRule { name }
4669+
} else if self.parse_keywords(&[Keyword::REPLICA, Keyword::TRIGGER]) {
4670+
let name = self.parse_identifier()?;
4671+
AlterTableOperation::EnableReplicaTrigger { name }
4672+
} else if self.parse_keywords(&[Keyword::ALWAYS, Keyword::TRIGGER]) {
4673+
let name = self.parse_identifier()?;
4674+
AlterTableOperation::EnableAlwaysTrigger { name }
4675+
} else {
4676+
return self.expected(
4677+
"TRIGGER, RULE or ROW LEVEL SECURITY after ENABLE",
4678+
self.peek_token(),
4679+
);
4680+
}
46594681
} else if self.parse_keyword(Keyword::DROP) {
46604682
if self.parse_keywords(&[Keyword::IF, Keyword::EXISTS, Keyword::PARTITION]) {
46614683
self.expect_token(&Token::LParen)?;

tests/sqlparser_postgres.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,12 +564,26 @@ fn parse_alter_table_constraints_rename() {
564564
}
565565

566566
#[test]
567-
fn parse_alter_table_disable_trigger() {
567+
fn parse_alter_table_disable() {
568568
pg_and_generic().verified_stmt("ALTER TABLE tab DISABLE TRIGGER ALL");
569569
pg_and_generic().verified_stmt("ALTER TABLE tab DISABLE TRIGGER USER");
570570
pg_and_generic().verified_stmt("ALTER TABLE tab DISABLE TRIGGER trigger_name");
571+
pg_and_generic().verified_stmt("ALTER TABLE tab DISABLE RULE rule_name");
572+
pg_and_generic().verified_stmt("ALTER TABLE tab DISABLE ROW LEVEL SECURITY");
571573
}
572574

575+
#[test]
576+
fn parse_alter_table_enable() {
577+
pg_and_generic().verified_stmt("ALTER TABLE tab ENABLE TRIGGER ALL");
578+
pg_and_generic().verified_stmt("ALTER TABLE tab ENABLE TRIGGER USER");
579+
pg_and_generic().verified_stmt("ALTER TABLE tab ENABLE TRIGGER trigger_name");
580+
pg_and_generic().verified_stmt("ALTER TABLE tab ENABLE RULE rule_name");
581+
pg_and_generic().verified_stmt("ALTER TABLE tab ENABLE ROW LEVEL SECURITY");
582+
pg_and_generic().verified_stmt("ALTER TABLE tab ENABLE REPLICA TRIGGER trigger_name");
583+
pg_and_generic().verified_stmt("ALTER TABLE tab ENABLE ALWAYS TRIGGER trigger_name");
584+
pg_and_generic().verified_stmt("ALTER TABLE tab ENABLE REPLICA RULE rule_name");
585+
pg_and_generic().verified_stmt("ALTER TABLE tab ENABLE ALWAYS rule_name");
586+
}
573587
#[test]
574588
fn parse_alter_table_alter_column() {
575589
pg().one_statement_parses_to(

0 commit comments

Comments
 (0)