From e6c96b9dc3e00f5aa636253042d5f8b17411364e Mon Sep 17 00:00:00 2001 From: wugeer <1284057728@qq.com> Date: Wed, 30 Oct 2024 14:05:44 +0800 Subject: [PATCH] allow `listen`/`notify` syntax for postgres, deny for others --- src/ast/mod.rs | 4 ++-- src/parser/mod.rs | 8 ++++++-- tests/sqlparser_common.rs | 31 ------------------------------- tests/sqlparser_postgres.rs | 31 +++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index ad9ce024f..d77d8d698 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -3283,14 +3283,14 @@ pub enum Statement { /// ``` /// listen for a notification channel /// - /// See Postgres + /// See Postgres LISTEN { channel: Ident }, /// ```sql /// NOTIFY channel [ , payload ] /// ``` /// send a notification event together with an optional “payload” string to channel /// - /// See Postgres + /// See Postgres NOTIFY { channel: Ident, payload: Option, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index f4b7503d2..3a3f57e83 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -534,8 +534,12 @@ impl<'a> Parser<'a> { Keyword::MERGE => self.parse_merge(), // `LISTEN` and `NOTIFY` are Postgres-specific // syntaxes. They are used for Postgres statement. - Keyword::LISTEN => self.parse_listen(), - Keyword::NOTIFY => self.parse_notify(), + Keyword::LISTEN if dialect_of!(self is PostgreSqlDialect | GenericDialect) => { + self.parse_listen() + } + Keyword::NOTIFY if dialect_of!(self is PostgreSqlDialect | GenericDialect) => { + self.parse_notify() + } // `PRAGMA` is sqlite specific https://www.sqlite.org/pragma.html Keyword::PRAGMA => self.parse_pragma(), Keyword::UNLOAD => self.parse_unload(), diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 443dfdaa0..5683bcf91 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -11460,34 +11460,3 @@ fn test_try_convert() { all_dialects_where(|d| d.supports_try_convert() && !d.convert_type_before_value()); dialects.verified_expr("TRY_CONVERT('foo', VARCHAR(MAX))"); } - -#[test] -fn test_listen() { - match verified_stmt("LISTEN test1") { - Statement::LISTEN { channel } => { - assert_eq!(Ident::new("test1"), channel); - } - _ => unreachable!(), - } -} - -#[test] -fn test_notify() { - match verified_stmt("NOTIFY test1") { - Statement::NOTIFY { channel, payload } => { - assert_eq!(Ident::new("test1"), channel); - assert_eq!(payload, None); - } - _ => unreachable!(), - } - match verified_stmt("NOTIFY test1, 'this is a test notification'") { - Statement::NOTIFY { - channel, - payload: Some(payload), - } => { - assert_eq!(Ident::new("test1"), channel); - assert_eq!("this is a test notification", payload); - } - _ => unreachable!(), - } -} diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index bd37214ce..2fe0789b5 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -5157,3 +5157,34 @@ fn parse_create_type_as_enum() { _ => unreachable!(), } } + +#[test] +fn parse_listen_channel() { + match pg_and_generic().verified_stmt("LISTEN test1") { + Statement::LISTEN { channel } => { + assert_eq!(Ident::new("test1"), channel); + } + _ => unreachable!(), + } +} + +#[test] +fn parse_notify_channel() { + match pg_and_generic().verified_stmt("NOTIFY test1") { + Statement::NOTIFY { channel, payload } => { + assert_eq!(Ident::new("test1"), channel); + assert_eq!(payload, None); + } + _ => unreachable!(), + } + match pg_and_generic().verified_stmt("NOTIFY test1, 'this is a test notification'") { + Statement::NOTIFY { + channel, + payload: Some(payload), + } => { + assert_eq!(Ident::new("test1"), channel); + assert_eq!("this is a test notification", payload); + } + _ => unreachable!(), + } +}