Skip to content
Merged
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
27 changes: 24 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3142,6 +3142,22 @@ impl fmt::Display for DeclareType {
}
}

/// Separator written between the declarations collected by one `DECLARE` keyword.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum DeclareSeparator {
/// Each declaration ends with `;`, as in a Snowflake Scripting `DECLARE` section.
///
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/snowflake-scripting/declare)
#[default]
Semicolon,
/// One comma-separated list, as in `DECLARE @a INT, @b INT`.
///
/// [MsSql](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql)
Comma,
}

/// A `DECLARE` statement.
/// [PostgreSQL] [Snowflake] [BigQuery]
///
Expand Down Expand Up @@ -4086,6 +4102,8 @@ pub enum Statement {
Declare {
/// Cursor declaration statements collected by `DECLARE`.
stmts: Vec<Declare>,
/// Separator between `stmts` in the rendered SQL.
separator: DeclareSeparator,
},
/// ```sql
/// CREATE EXTENSION [ IF NOT EXISTS ] extension_name
Expand Down Expand Up @@ -5264,9 +5282,12 @@ impl fmt::Display for Statement {
write!(f, "{statement}")
}
Statement::Query(s) => s.fmt(f),
Statement::Declare { stmts } => {
write!(f, "DECLARE ")?;
write!(f, "{}", display_separated(stmts, "; "))
Statement::Declare { stmts, separator } => {
let sep = match separator {
DeclareSeparator::Comma => ", ",
DeclareSeparator::Semicolon => "; ",
};
write!(f, "DECLARE {}", display_separated(stmts, sep))
}
Statement::Fetch {
name,
Expand Down
12 changes: 10 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8009,6 +8009,7 @@ impl<'a> Parser<'a> {
hold,
for_query: query,
}],
separator: DeclareSeparator::Semicolon,
})
}

Expand Down Expand Up @@ -8052,6 +8053,7 @@ impl<'a> Parser<'a> {
hold: None,
for_query: None,
}],
separator: DeclareSeparator::Semicolon,
})
}

Expand Down Expand Up @@ -8168,7 +8170,10 @@ impl<'a> Parser<'a> {
break;
}

Ok(Statement::Declare { stmts })
Ok(Statement::Declare {
stmts,
separator: DeclareSeparator::Semicolon,
})
}

/// Parse a [MsSql] `DECLARE` statement.
Expand All @@ -8185,7 +8190,10 @@ impl<'a> Parser<'a> {
pub fn parse_mssql_declare(&mut self) -> Result<Statement, ParserError> {
let stmts = self.parse_comma_separated(Parser::parse_mssql_declare_stmt)?;

Ok(Statement::Declare { stmts })
Ok(Statement::Declare {
stmts,
separator: DeclareSeparator::Comma,
})
}

/// Parse the body of a [MsSql] `DECLARE`statement.
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,7 @@ fn parse_big_query_declare() {
),
] {
match bigquery().verified_stmt(sql) {
Statement::Declare { mut stmts } => {
Statement::Declare { mut stmts, .. } => {
assert_eq!(1, stmts.len());
let Declare {
names,
Expand Down
13 changes: 11 additions & 2 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,8 @@ fn parse_mssql_declare() {
hold: None,
for_query: None
}
]
],
separator: DeclareSeparator::Comma,
}],
ast
);
Expand All @@ -1495,7 +1496,8 @@ fn parse_mssql_declare() {
scroll: None,
hold: None,
for_query: None
}]
}],
separator: DeclareSeparator::Comma,
},
Statement::Set(Set::SingleAssignment {
scope: None,
Expand Down Expand Up @@ -3004,3 +3006,10 @@ fn parse_create_proc() {
.expect_err("PROC should remain MSSQL-specific");
ms_and_generic().verified_stmt("SELECT proc FROM jobs");
}

#[test]
fn parse_mssql_declare_comma_list_display() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any tests for ; as a separator 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not add one because it is already present in several other tests as part of the existing syntax. It does not have a dedicated test.

ms().verified_stmt("DECLARE @a INT, @b INT");
ms().verified_stmt("DECLARE @a INT = 1, @b TEXT = 'x'");
ms().verified_stmt("DECLARE @foo CURSOR, @bar INT");
}
10 changes: 5 additions & 5 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ fn parse_snowflake_declare_cursor() {
),
] {
match snowflake().verified_stmt(sql) {
Statement::Declare { mut stmts } => {
Statement::Declare { mut stmts, .. } => {
assert_eq!(1, stmts.len());
let Declare {
names,
Expand Down Expand Up @@ -1807,7 +1807,7 @@ fn parse_snowflake_declare_result_set() {
("DECLARE res RESULTSET", "res", None),
] {
match snowflake().verified_stmt(sql) {
Statement::Declare { mut stmts } => {
Statement::Declare { mut stmts, .. } => {
assert_eq!(1, stmts.len());
let Declare {
names,
Expand Down Expand Up @@ -1860,7 +1860,7 @@ fn parse_snowflake_declare_exception() {
("DECLARE ex EXCEPTION", "ex", None),
] {
match snowflake().verified_stmt(sql) {
Statement::Declare { mut stmts } => {
Statement::Declare { mut stmts, .. } => {
assert_eq!(1, stmts.len());
let Declare {
names,
Expand Down Expand Up @@ -1900,7 +1900,7 @@ fn parse_snowflake_declare_variable() {
("DECLARE profit", "profit", None, None),
] {
match snowflake().verified_stmt(sql) {
Statement::Declare { mut stmts } => {
Statement::Declare { mut stmts, .. } => {
assert_eq!(1, stmts.len());
let Declare {
names,
Expand Down Expand Up @@ -1950,7 +1950,7 @@ fn parse_snowflake_declare_multi_statements() {
"ex EXCEPTION (-20003, 'ERROR: Could not create table.')"
);
match snowflake().verified_stmt(sql) {
Statement::Declare { stmts } => {
Statement::Declare { stmts, .. } => {
let actual = stmts
.iter()
.map(|stmt| (stmt.names[0].value.as_str(), stmt.declare_type.clone()))
Expand Down
Loading