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 @@ -3109,6 +3109,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 @@ -3151,6 +3174,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 @@ -3176,6 +3201,7 @@ impl fmt::Display for Declare {
data_type,
assignment,
declare_type,
cursor_options,
binary,
sensitive,
scroll,
Expand Down Expand Up @@ -3208,6 +3234,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 @@ -421,6 +421,7 @@ define_keywords!(
FALLBACK,
FALSE,
FAMILY,
FAST_FORWARD,
FETCH,
FIELDS,
FILE,
Expand Down
25 changes: 25 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7938,6 +7938,7 @@ impl<'a> Parser<'a> {
data_type: None,
assignment: None,
declare_type,
cursor_options: vec![],
binary,
sensitive,
scroll,
Expand Down Expand Up @@ -7981,6 +7982,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 @@ -8075,6 +8077,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 @@ -8150,10 +8153,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 @@ -8179,6 +8184,7 @@ impl<'a> Parser<'a> {
data_type,
assignment,
declare_type,
cursor_options,
binary: None,
sensitive: None,
scroll: None,
Expand All @@ -8187,6 +8193,25 @@ impl<'a> Parser<'a> {
})
}

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

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

/// Parses the assigned expression in a variable declaration.
///
/// Syntax:
Expand Down
38 changes: 38 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 @@ -2958,3 +2962,37 @@ fn parse_create_proc() {
.expect_err("PROC should remain MSSQL-specific");
ms_and_generic().verified_stmt("SELECT proc FROM jobs");
}

#[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],
),

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.

Red test for the aforementioned case

Suggested change
),
),
(
"DECLARE local_cursor CURSOR FAST_FORWARD LOCAL FOR SELECT name FROM dbo.reports",
vec![MsSqlCursorOption::FastForward, MsSqlCursorOption::Local],
),

@t8y2 t8y2 Sep 26, 2026 •

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.

Added this regression case in 619da03, asserting the preserved option order as FastForward followed by Local.

(
"DECLARE local_cursor CURSOR FAST_FORWARD LOCAL FOR SELECT name FROM dbo.reports",
vec![MsSqlCursorOption::FastForward, MsSqlCursorOption::Local],
),
] {
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");
}
Loading