Skip to content

Commit 9cacf35

Browse files
MsSql: Keep the comma separator when displaying DECLARE lists (#2671)
1 parent eda940d commit 9cacf35

5 files changed

Lines changed: 51 additions & 13 deletions

File tree

‎src/ast/mod.rs‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,6 +3124,22 @@ impl fmt::Display for DeclareType {
31243124
}
31253125
}
31263126

3127+
/// Separator written between the declarations collected by one `DECLARE` keyword.
3128+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Default)]
3129+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3130+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3131+
pub enum DeclareSeparator {
3132+
/// Each declaration ends with `;`, as in a Snowflake Scripting `DECLARE` section.
3133+
///
3134+
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/snowflake-scripting/declare)
3135+
#[default]
3136+
Semicolon,
3137+
/// One comma-separated list, as in `DECLARE @a INT, @b INT`.
3138+
///
3139+
/// [MsSql](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql)
3140+
Comma,
3141+
}
3142+
31273143
/// A `DECLARE` statement.
31283144
/// [PostgreSQL] [Snowflake] [BigQuery]
31293145
///
@@ -4068,6 +4084,8 @@ pub enum Statement {
40684084
Declare {
40694085
/// Cursor declaration statements collected by `DECLARE`.
40704086
stmts: Vec<Declare>,
4087+
/// Separator between `stmts` in the rendered SQL.
4088+
separator: DeclareSeparator,
40714089
},
40724090
/// ```sql
40734091
/// CREATE EXTENSION [ IF NOT EXISTS ] extension_name
@@ -5246,9 +5264,12 @@ impl fmt::Display for Statement {
52465264
write!(f, "{statement}")
52475265
}
52485266
Statement::Query(s) => s.fmt(f),
5249-
Statement::Declare { stmts } => {
5250-
write!(f, "DECLARE ")?;
5251-
write!(f, "{}", display_separated(stmts, "; "))
5267+
Statement::Declare { stmts, separator } => {
5268+
let sep = match separator {
5269+
DeclareSeparator::Comma => ", ",
5270+
DeclareSeparator::Semicolon => "; ",
5271+
};
5272+
write!(f, "DECLARE {}", display_separated(stmts, sep))
52525273
}
52535274
Statement::Fetch {
52545275
name,

‎src/parser/mod.rs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8030,6 +8030,7 @@ impl<'a> Parser<'a> {
80308030
hold,
80318031
for_query: query,
80328032
}],
8033+
separator: DeclareSeparator::Semicolon,
80338034
})
80348035
}
80358036

@@ -8073,6 +8074,7 @@ impl<'a> Parser<'a> {
80738074
hold: None,
80748075
for_query: None,
80758076
}],
8077+
separator: DeclareSeparator::Semicolon,
80768078
})
80778079
}
80788080

@@ -8189,7 +8191,10 @@ impl<'a> Parser<'a> {
81898191
break;
81908192
}
81918193

8192-
Ok(Statement::Declare { stmts })
8194+
Ok(Statement::Declare {
8195+
stmts,
8196+
separator: DeclareSeparator::Semicolon,
8197+
})
81938198
}
81948199

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

8209-
Ok(Statement::Declare { stmts })
8214+
Ok(Statement::Declare {
8215+
stmts,
8216+
separator: DeclareSeparator::Comma,
8217+
})
82108218
}
82118219

82128220
/// Parse the body of a [MsSql] `DECLARE`statement.

‎tests/sqlparser_bigquery.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ fn parse_big_query_declare() {
21912191
),
21922192
] {
21932193
match bigquery().verified_stmt(sql) {
2194-
Statement::Declare { mut stmts } => {
2194+
Statement::Declare { mut stmts, .. } => {
21952195
assert_eq!(1, stmts.len());
21962196
let Declare {
21972197
names,

‎tests/sqlparser_mssql.rs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,8 @@ fn parse_mssql_declare() {
14751475
hold: None,
14761476
for_query: None
14771477
}
1478-
]
1478+
],
1479+
separator: DeclareSeparator::Comma,
14791480
}],
14801481
ast
14811482
);
@@ -1495,7 +1496,8 @@ fn parse_mssql_declare() {
14951496
scroll: None,
14961497
hold: None,
14971498
for_query: None
1498-
}]
1499+
}],
1500+
separator: DeclareSeparator::Comma,
14991501
},
15001502
Statement::Set(Set::SingleAssignment {
15011503
scope: None,
@@ -3004,3 +3006,10 @@ fn parse_create_proc() {
30043006
.expect_err("PROC should remain MSSQL-specific");
30053007
ms_and_generic().verified_stmt("SELECT proc FROM jobs");
30063008
}
3009+
3010+
#[test]
3011+
fn parse_mssql_declare_comma_list_display() {
3012+
ms().verified_stmt("DECLARE @a INT, @b INT");
3013+
ms().verified_stmt("DECLARE @a INT = 1, @b TEXT = 'x'");
3014+
ms().verified_stmt("DECLARE @foo CURSOR, @bar INT");
3015+
}

‎tests/sqlparser_snowflake.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,7 @@ fn parse_snowflake_declare_cursor() {
17391739
),
17401740
] {
17411741
match snowflake().verified_stmt(sql) {
1742-
Statement::Declare { mut stmts } => {
1742+
Statement::Declare { mut stmts, .. } => {
17431743
assert_eq!(1, stmts.len());
17441744
let Declare {
17451745
names,
@@ -1807,7 +1807,7 @@ fn parse_snowflake_declare_result_set() {
18071807
("DECLARE res RESULTSET", "res", None),
18081808
] {
18091809
match snowflake().verified_stmt(sql) {
1810-
Statement::Declare { mut stmts } => {
1810+
Statement::Declare { mut stmts, .. } => {
18111811
assert_eq!(1, stmts.len());
18121812
let Declare {
18131813
names,
@@ -1860,7 +1860,7 @@ fn parse_snowflake_declare_exception() {
18601860
("DECLARE ex EXCEPTION", "ex", None),
18611861
] {
18621862
match snowflake().verified_stmt(sql) {
1863-
Statement::Declare { mut stmts } => {
1863+
Statement::Declare { mut stmts, .. } => {
18641864
assert_eq!(1, stmts.len());
18651865
let Declare {
18661866
names,
@@ -1900,7 +1900,7 @@ fn parse_snowflake_declare_variable() {
19001900
("DECLARE profit", "profit", None, None),
19011901
] {
19021902
match snowflake().verified_stmt(sql) {
1903-
Statement::Declare { mut stmts } => {
1903+
Statement::Declare { mut stmts, .. } => {
19041904
assert_eq!(1, stmts.len());
19051905
let Declare {
19061906
names,
@@ -1950,7 +1950,7 @@ fn parse_snowflake_declare_multi_statements() {
19501950
"ex EXCEPTION (-20003, 'ERROR: Could not create table.')"
19511951
);
19521952
match snowflake().verified_stmt(sql) {
1953-
Statement::Declare { stmts } => {
1953+
Statement::Declare { stmts, .. } => {
19541954
let actual = stmts
19551955
.iter()
19561956
.map(|stmt| (stmt.names[0].value.as_str(), stmt.declare_type.clone()))

0 commit comments

Comments
 (0)