Skip to content

Commit 648c024

Browse files
committed
Introduce a OpenStatement struct for the OPEN statement
1 parent 9965777 commit 648c024

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

src/ast/mod.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -3071,10 +3071,7 @@ pub enum Statement {
30713071
/// OPEN cursor_name
30723072
/// ```
30733073
/// Opens a cursor.
3074-
Open {
3075-
/// Cursor name
3076-
cursor_name: Ident,
3077-
},
3074+
Open(OpenStatement),
30783075
/// ```sql
30793076
/// CLOSE
30803077
/// ```
@@ -4535,11 +4532,7 @@ impl fmt::Display for Statement {
45354532
Ok(())
45364533
}
45374534
Statement::Delete(delete) => write!(f, "{delete}"),
4538-
Statement::Open { cursor_name } => {
4539-
write!(f, "OPEN {cursor_name}")?;
4540-
4541-
Ok(())
4542-
}
4535+
Statement::Open(open) => write!(f, "{open}"),
45434536
Statement::Close { cursor } => {
45444537
write!(f, "CLOSE {cursor}")?;
45454538

@@ -9390,6 +9383,21 @@ pub enum ReturnStatementValue {
93909383
Expr(Expr),
93919384
}
93929385

9386+
/// Represents an `OPEN` statement.
9387+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9388+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9389+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9390+
pub struct OpenStatement {
9391+
/// Cursor name
9392+
pub cursor_name: Ident,
9393+
}
9394+
9395+
impl fmt::Display for OpenStatement {
9396+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9397+
write!(f, "OPEN {}", self.cursor_name)
9398+
}
9399+
}
9400+
93939401
#[cfg(test)]
93949402
mod tests {
93959403
use super::*;

src/ast/spans.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ use super::{
3131
FunctionArguments, GroupByExpr, HavingBound, IfStatement, IlikeSelectItem, Insert, Interpolate,
3232
InterpolateExpr, Join, JoinConstraint, JoinOperator, JsonPath, JsonPathElem, LateralView,
3333
LimitClause, MatchRecognizePattern, Measure, NamedWindowDefinition, ObjectName, ObjectNamePart,
34-
Offset, OnConflict, OnConflictAction, OnInsert, OrderBy, OrderByExpr, OrderByKind, Partition,
35-
PivotValueSource, ProjectionSelect, Query, RaiseStatement, RaiseStatementValue,
36-
ReferentialAction, RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, Select,
37-
SelectInto, SelectItem, SetExpr, SqlOption, Statement, Subscript, SymbolDefinition, TableAlias,
38-
TableAliasColumnDef, TableConstraint, TableFactor, TableObject, TableOptionsClustered,
39-
TableWithJoins, UpdateTableFromKind, Use, Value, Values, ViewColumnDef, WhileStatement,
40-
WildcardAdditionalOptions, With, WithFill,
34+
Offset, OnConflict, OnConflictAction, OnInsert, OpenStatement, OrderBy, OrderByExpr,
35+
OrderByKind, Partition, PivotValueSource, ProjectionSelect, Query, RaiseStatement,
36+
RaiseStatementValue, ReferentialAction, RenameSelectItem, ReplaceSelectElement,
37+
ReplaceSelectItem, Select, SelectInto, SelectItem, SetExpr, SqlOption, Statement, Subscript,
38+
SymbolDefinition, TableAlias, TableAliasColumnDef, TableConstraint, TableFactor, TableObject,
39+
TableOptionsClustered, TableWithJoins, UpdateTableFromKind, Use, Value, Values, ViewColumnDef,
40+
WhileStatement, WildcardAdditionalOptions, With, WithFill,
4141
};
4242

4343
/// Given an iterator of spans, return the [Span::union] of all spans.
@@ -365,7 +365,7 @@ impl Spanned for Statement {
365365
from_query: _,
366366
partition: _,
367367
} => Span::empty(),
368-
Statement::Open { cursor_name } => cursor_name.span,
368+
Statement::Open(open) => open.span(),
369369
Statement::Close { cursor } => match cursor {
370370
CloseCursor::All => Span::empty(),
371371
CloseCursor::Specific { name } => name.span,
@@ -2305,6 +2305,13 @@ impl Spanned for BeginEndStatements {
23052305
}
23062306
}
23072307

2308+
impl Spanned for OpenStatement {
2309+
fn span(&self) -> Span {
2310+
let OpenStatement { cursor_name } = self;
2311+
cursor_name.span
2312+
}
2313+
}
2314+
23082315
#[cfg(test)]
23092316
pub mod tests {
23102317
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};

src/parser/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8784,9 +8784,9 @@ impl<'a> Parser<'a> {
87848784
/// Parse [Statement::Open]
87858785
fn parse_open(&mut self) -> Result<Statement, ParserError> {
87868786
self.expect_keyword(Keyword::OPEN)?;
8787-
Ok(Statement::Open {
8787+
Ok(Statement::Open(OpenStatement {
87888788
cursor_name: self.parse_identifier()?,
8789-
})
8789+
}))
87908790
}
87918791

87928792
pub fn parse_close(&mut self) -> Result<Statement, ParserError> {

0 commit comments

Comments
 (0)