Skip to content

feat: add DECLARE parsing for mssql #1235

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

Merged
merged 6 commits into from
May 1, 2024
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
11 changes: 11 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,14 @@ pub enum DeclareAssignment {
/// DECLARE c1 CURSOR FOR res
/// ```
For(Box<Expr>),

/// Expression via the `=` syntax.
///
/// Example:
/// ```sql
/// DECLARE @variable AS INT = 100
/// ```
MsSqlAssignment(Box<Expr>),
}

impl fmt::Display for DeclareAssignment {
Expand All @@ -1546,6 +1554,9 @@ impl fmt::Display for DeclareAssignment {
DeclareAssignment::DuckAssignment(expr) => {
write!(f, ":= {expr}")
}
DeclareAssignment::MsSqlAssignment(expr) => {
write!(f, "= {expr}")
}
DeclareAssignment::For(expr) => {
write!(f, "FOR {expr}")
}
Expand Down
86 changes: 86 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4244,6 +4244,9 @@ impl<'a> Parser<'a> {
if dialect_of!(self is SnowflakeDialect) {
return self.parse_snowflake_declare();
}
if dialect_of!(self is MsSqlDialect) {
return self.parse_mssql_declare();
}

let name = self.parse_identifier(false)?;

Expand Down Expand Up @@ -4457,6 +4460,69 @@ impl<'a> Parser<'a> {
Ok(Statement::Declare { stmts })
}

/// Parse a [MsSql] `DECLARE` statement.
///
/// Syntax:
/// ```text
/// DECLARE
// {
// { @local_variable [AS] data_type [ = value ] }
// | { @cursor_variable_name CURSOR }
// } [ ,...n ]
/// ```
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
pub fn parse_mssql_declare(&mut self) -> Result<Statement, ParserError> {
let mut stmts = vec![];

loop {
let name = {
let ident = self.parse_identifier(false)?;
if !ident.value.starts_with('@') {
Err(ParserError::TokenizerError(
"Invalid MsSql variable declaration.".to_string(),
))
} else {
Ok(ident)
}
}?;

let (declare_type, data_type) = match self.peek_token().token {
Token::Word(w) => match w.keyword {
Keyword::CURSOR => {
self.next_token();
(Some(DeclareType::Cursor), None)
}
Keyword::AS => {
self.next_token();
(None, Some(self.parse_data_type()?))
}
_ => (None, Some(self.parse_data_type()?)),
},
_ => (None, Some(self.parse_data_type()?)),
};

let assignment = self.parse_mssql_variable_declaration_expression()?;

stmts.push(Declare {
names: vec![name],
data_type,
assignment,
declare_type,
binary: None,
sensitive: None,
scroll: None,
hold: None,
for_query: None,
});

if self.next_token() != Token::Comma {
break;
}
}

Ok(Statement::Declare { stmts })
}

/// Parses the assigned expression in a variable declaration.
///
/// Syntax:
Expand All @@ -4482,6 +4548,26 @@ impl<'a> Parser<'a> {
})
}

/// Parses the assigned expression in a variable declaration.
///
/// Syntax:
/// ```text
/// [ = <expression>]
/// ```
pub fn parse_mssql_variable_declaration_expression(
&mut self,
) -> Result<Option<DeclareAssignment>, ParserError> {
Ok(match self.peek_token().token {
Token::Eq => {
self.next_token(); // Skip `=`
Some(DeclareAssignment::MsSqlAssignment(Box::new(
self.parse_expr()?,
)))
}
_ => None,
})
}

// FETCH [ direction { FROM | IN } ] cursor INTO target;
pub fn parse_fetch_statement(&mut self) -> Result<Statement, ParserError> {
let direction = if self.parse_keyword(Keyword::NEXT) {
Expand Down
64 changes: 63 additions & 1 deletion tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

#[macro_use]
mod test_utils;

use test_utils::*;

use sqlparser::ast::DataType::{Int, Text};
use sqlparser::ast::DeclareAssignment::MsSqlAssignment;
use sqlparser::ast::Value::SingleQuotedString;
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, MsSqlDialect};
use sqlparser::parser::ParserError;
use sqlparser::parser::{Parser, ParserError};

#[test]
fn parse_mssql_identifiers() {
Expand Down Expand Up @@ -539,6 +543,64 @@ fn parse_substring_in_select() {
}
}

#[test]
fn parse_mssql_declare() {
let sql = "DECLARE @foo CURSOR, @bar INT, @baz AS TEXT = 'foobar';";
let ast = Parser::parse_sql(&MsSqlDialect {}, sql).unwrap();

assert_eq!(
vec![Statement::Declare {
stmts: vec![
Declare {
names: vec![Ident {
value: "@foo".to_string(),
quote_style: None
}],
data_type: None,
assignment: None,
declare_type: Some(DeclareType::Cursor),
binary: None,
sensitive: None,
scroll: None,
hold: None,
for_query: None
},
Declare {
names: vec![Ident {
value: "@bar".to_string(),
quote_style: None
}],
data_type: Some(Int(None)),
assignment: None,
declare_type: None,
binary: None,
sensitive: None,
scroll: None,
hold: None,
for_query: None
},
Declare {
names: vec![Ident {
value: "@baz".to_string(),
quote_style: None
}],
data_type: Some(Text),
assignment: Some(MsSqlAssignment(Box::new(Expr::Value(SingleQuotedString(
"foobar".to_string()
))))),
declare_type: None,
binary: None,
sensitive: None,
scroll: None,
hold: None,
for_query: None
}
]
}],
ast
);
}

fn ms() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(MsSqlDialect {})],
Expand Down
Loading