Skip to content
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
30 changes: 30 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3081,6 +3081,29 @@ pub enum DeclareType {
Exception,
}

/// SQL Server cursor options that appear after the `CURSOR` keyword.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum MsSqlCursorOption {
/// `LOCAL` cursor scope.
Local,
/// `GLOBAL` cursor scope.
Global,
/// `FAST_FORWARD` forward-only, read-only cursor.
FastForward,
}

impl fmt::Display for MsSqlCursorOption {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
MsSqlCursorOption::Local => f.write_str("LOCAL"),
MsSqlCursorOption::Global => f.write_str("GLOBAL"),
MsSqlCursorOption::FastForward => f.write_str("FAST_FORWARD"),
}
}
}

impl fmt::Display for DeclareType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down Expand Up @@ -3123,6 +3146,8 @@ pub struct Declare {
pub assignment: Option<DeclareAssignment>,
/// Represents the type of the declared variable.
pub declare_type: Option<DeclareType>,
/// SQL Server cursor options following the `CURSOR` keyword.
pub cursor_options: Vec<MsSqlCursorOption>,
/// Causes the cursor to return data in binary rather than in text format.
pub binary: Option<bool>,
/// None = Not specified
Expand All @@ -3148,6 +3173,7 @@ impl fmt::Display for Declare {
data_type,
assignment,
declare_type,
cursor_options,
binary,
sensitive,
scroll,
Expand Down Expand Up @@ -3180,6 +3206,10 @@ impl fmt::Display for Declare {
write!(f, " {declare_type}")?;
}

if !cursor_options.is_empty() {
write!(f, " {}", display_separated(cursor_options, " "))?;
}

if let Some(hold) = hold {
if *hold {
write!(f, " WITH HOLD")?;
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ define_keywords!(
FALLBACK,
FALSE,
FAMILY,
FAST_FORWARD,
FETCH,
FIELDS,
FILE,
Expand Down
21 changes: 21 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7885,6 +7885,7 @@ impl<'a> Parser<'a> {
data_type: None,
assignment: None,
declare_type,
cursor_options: vec![],
binary,
sensitive,
scroll,
Expand Down Expand Up @@ -7928,6 +7929,7 @@ impl<'a> Parser<'a> {
data_type,
assignment: expr.map(|expr| DeclareAssignment::Default(Box::new(expr))),
declare_type: None,
cursor_options: vec![],
binary: None,
sensitive: None,
scroll: None,
Expand Down Expand Up @@ -8022,6 +8024,7 @@ impl<'a> Parser<'a> {
data_type,
assignment: assigned_expr,
declare_type,
cursor_options: vec![],
binary: None,
sensitive: None,
scroll: None,
Expand Down Expand Up @@ -8097,10 +8100,12 @@ impl<'a> Parser<'a> {
}
}?;

let mut cursor_options = vec![];
let (declare_type, data_type) = match &self.peek_token_ref().token {
Token::Word(w) => match w.keyword {
Keyword::CURSOR => {
self.next_token();
cursor_options = self.parse_mssql_cursor_options();
(Some(DeclareType::Cursor), None)
}
Keyword::AS => {
Expand All @@ -8126,6 +8131,7 @@ impl<'a> Parser<'a> {
data_type,
assignment,
declare_type,
cursor_options,
binary: None,
sensitive: None,
scroll: None,
Expand All @@ -8134,6 +8140,21 @@ impl<'a> Parser<'a> {
})
}

fn parse_mssql_cursor_options(&mut self) -> Vec<MsSqlCursorOption> {
let mut options = vec![];

match self.parse_one_of_keywords(&[Keyword::LOCAL, Keyword::GLOBAL]) {
Some(Keyword::LOCAL) => options.push(MsSqlCursorOption::Local),
Some(Keyword::GLOBAL) => options.push(MsSqlCursorOption::Global),
_ => {}
}
if self.parse_keyword(Keyword::FAST_FORWARD) {
options.push(MsSqlCursorOption::FastForward);
}

options
}

/// Parses the assigned expression in a variable declaration.
///
/// Syntax:
Expand Down
34 changes: 34 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,7 @@ fn parse_mssql_declare() {
data_type: None,
assignment: None,
declare_type: Some(DeclareType::Cursor),
cursor_options: vec![],
binary: None,
sensitive: None,
scroll: None,
Expand All @@ -1444,6 +1445,7 @@ fn parse_mssql_declare() {
data_type: Some(Int(None)),
assignment: None,
declare_type: None,
cursor_options: vec![],
binary: None,
sensitive: None,
scroll: None,
Expand All @@ -1461,6 +1463,7 @@ fn parse_mssql_declare() {
(SingleQuotedString("foobar".to_string())).with_empty_span()
)))),
declare_type: None,
cursor_options: vec![],
binary: None,
sensitive: None,
scroll: None,
Expand All @@ -1482,6 +1485,7 @@ fn parse_mssql_declare() {
data_type: Some(Int(None)),
assignment: None,
declare_type: None,
cursor_options: vec![],
binary: None,
sensitive: None,
scroll: None,
Expand Down Expand Up @@ -2925,3 +2929,33 @@ fn parse_mssql_money_constants() {
expr_from_projection(only(&select.projection)),
);
}

#[test]
fn parse_mssql_cursor_options() {
for (sql, expected_options) in [
(
"DECLARE local_cursor CURSOR LOCAL FOR SELECT name FROM dbo.reports",
vec![MsSqlCursorOption::Local],
),
(
"DECLARE fast_cursor CURSOR FAST_FORWARD FOR SELECT name FROM dbo.reports",
vec![MsSqlCursorOption::FastForward],
),
(
"DECLARE global_cursor CURSOR GLOBAL FAST_FORWARD FOR SELECT name FROM dbo.reports",
vec![MsSqlCursorOption::Global, MsSqlCursorOption::FastForward],
),
] {
let Statement::Declare { stmts } = ms().verified_stmt(sql) else {
panic!("expected DECLARE statement");
};
assert_eq!(only(&stmts).cursor_options, expected_options);
}

TestedDialects::new(vec![Box::new(GenericDialect {})])
.parse_sql_statements(
"DECLARE report_cursor CURSOR LOCAL FAST_FORWARD FOR SELECT name FROM reports",
)
.expect_err("MSSQL cursor options should remain dialect-specific");
ms_and_generic().verified_stmt("SELECT fast_forward FROM jobs");
}