Skip to content

Commit

Permalink
allow listen/notify syntax for postgres, deny for others
Browse files Browse the repository at this point in the history
  • Loading branch information
wugeer committed Oct 30, 2024
1 parent d526a64 commit e6c96b9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3283,14 +3283,14 @@ pub enum Statement {
/// ```
/// listen for a notification channel
///
/// See Postgres <https://www.postgresql.org/docs/current/sql-notify.html>
/// See Postgres <https://www.postgresql.org/docs/current/sql-listen.html>
LISTEN { channel: Ident },
/// ```sql
/// NOTIFY channel [ , payload ]
/// ```
/// send a notification event together with an optional “payload” string to channel
///
/// See Postgres <https://www.postgresql.org/docs/current/sql-listen.html>
/// See Postgres <https://www.postgresql.org/docs/current/sql-notify.html>
NOTIFY {
channel: Ident,
payload: Option<String>,
Expand Down
8 changes: 6 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
31 changes: 0 additions & 31 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
}
}
31 changes: 31 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
}
}

0 comments on commit e6c96b9

Please sign in to comment.