Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PostgreSQL LISTEN/NOTIFY syntax #1485

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3278,6 +3278,23 @@ pub enum Statement {
include_final: bool,
deduplicate: Option<Deduplicate>,
},
/// ```sql
/// LISTEN
/// ```
/// listen for a notification channel
///
/// See Postgres <https://www.postgresql.org/docs/current/sql-notify.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>
NOTIFY {
channel: Ident,
payload: Option<String>,
},
}

impl fmt::Display for Statement {
Expand Down Expand Up @@ -4782,6 +4799,17 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::LISTEN { channel } => {
write!(f, "LISTEN {channel}")?;
Ok(())
}
Statement::NOTIFY { channel, payload } => {
write!(f, "NOTIFY {channel}")?;
if let Some(payload) = payload {
write!(f, ", '{payload}'")?;
}
Ok(())
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ define_keywords!(
LIKE_REGEX,
LIMIT,
LINES,
LISTEN,
LN,
LOAD,
LOCAL,
Expand Down Expand Up @@ -512,6 +513,7 @@ define_keywords!(
NOSUPERUSER,
NOT,
NOTHING,
NOTIFY,
NOWAIT,
NO_WRITE_TO_BINLOG,
NTH_VALUE,
Expand Down
19 changes: 19 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ impl<'a> Parser<'a> {
Keyword::EXECUTE => self.parse_execute(),
Keyword::PREPARE => self.parse_prepare(),
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(),
// `PRAGMA` is sqlite specific https://www.sqlite.org/pragma.html
Keyword::PRAGMA => self.parse_pragma(),
Keyword::UNLOAD => self.parse_unload(),
Expand Down Expand Up @@ -946,6 +950,21 @@ impl<'a> Parser<'a> {
Ok(Statement::ReleaseSavepoint { name })
}

pub fn parse_listen(&mut self) -> Result<Statement, ParserError> {
let channel = self.parse_identifier(false)?;
Ok(Statement::LISTEN { channel })
}

pub fn parse_notify(&mut self) -> Result<Statement, ParserError> {
let channel = self.parse_identifier(false)?;
let payload = if self.consume_token(&Token::Comma) {
Some(self.parse_literal_string()?)
} else {
None
};
Ok(Statement::NOTIFY { channel, payload })
}

/// Parse an expression prefix.
pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
// allow the dialect to override prefix parsing
Expand Down
31 changes: 31 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11460,3 +11460,34 @@ 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!(),
}
}