Skip to content

Commit 3b0e126

Browse files
committed
feat: Initial support for DECLARE (cursors)
1 parent 0fa812b commit 3b0e126

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

src/ast/mod.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,29 @@ pub enum Statement {
903903
/// deleted along with the dropped table
904904
purge: bool,
905905
},
906+
/// DECLARE - Declaring Cursor Variables
907+
///
908+
/// Note: this is a PostgreSQL-specific statement,
909+
/// but may also compatible with other SQL.
910+
Declare {
911+
/// Cursor name
912+
name: ObjectName,
913+
/// Causes the cursor to return data in binary rather than in text format.
914+
binary: bool,
915+
/// None = Not specified
916+
/// Some(true) = INSENSITIVE
917+
/// Some(false) = ASENSITIVE
918+
sensitive: Option<bool>,
919+
/// None = Not specified
920+
/// Some(true) = SCROLL
921+
/// Some(false) = NO SCROLL
922+
scroll: Option<bool>,
923+
/// None = Not specified
924+
/// Some(true) = WITH HOLD, specifies that the cursor can continue to be used after the transaction that created it successfully commits
925+
/// Some(false) = WITHOUT HOLD, specifies that the cursor cannot be used outside of the transaction that created it
926+
hold: Option<bool>,
927+
query: Box<Query>,
928+
},
906929
/// DISCARD [ ALL | PLANS | SEQUENCES | TEMPORARY | TEMP ]
907930
///
908931
/// Note: this is a PostgreSQL-specific statement,
@@ -1113,6 +1136,48 @@ impl fmt::Display for Statement {
11131136
write!(f, "{}", statement)
11141137
}
11151138
Statement::Query(s) => write!(f, "{}", s),
1139+
Statement::Declare {
1140+
name,
1141+
binary,
1142+
sensitive,
1143+
scroll,
1144+
hold,
1145+
query,
1146+
} => {
1147+
write!(f, "DECLARE {} ", name)?;
1148+
1149+
if *binary {
1150+
write!(f, "BINARY ")?;
1151+
}
1152+
1153+
if let Some(sensitive) = sensitive {
1154+
if *sensitive {
1155+
write!(f, "INSENSITIVE ")?;
1156+
} else {
1157+
write!(f, "ASENSITIVE ")?;
1158+
}
1159+
}
1160+
1161+
if let Some(scroll) = scroll {
1162+
if *scroll {
1163+
write!(f, "SCROLL ")?;
1164+
} else {
1165+
write!(f, "NO SCROLL ")?;
1166+
}
1167+
}
1168+
1169+
write!(f, "CURSOR ")?;
1170+
1171+
if let Some(hold) = hold {
1172+
if *hold {
1173+
write!(f, "WITH HOLD ")?;
1174+
} else {
1175+
write!(f, "WITHOUT HOLD ")?;
1176+
}
1177+
}
1178+
1179+
write!(f, "FOR {}", query)
1180+
}
11161181
Statement::Directory {
11171182
overwrite,
11181183
local,

src/parser.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ impl<'a> Parser<'a> {
167167
Keyword::CREATE => Ok(self.parse_create()?),
168168
Keyword::DROP => Ok(self.parse_drop()?),
169169
Keyword::DISCARD => Ok(self.parse_discard()?),
170+
Keyword::DECLARE => Ok(self.parse_declare()?),
170171
Keyword::DELETE => Ok(self.parse_delete()?),
171172
Keyword::INSERT => Ok(self.parse_insert()?),
172173
Keyword::UPDATE => Ok(self.parse_update()?),
@@ -1786,6 +1787,56 @@ impl<'a> Parser<'a> {
17861787
})
17871788
}
17881789

1790+
/// DECLARE name [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ]
1791+
// CURSOR [ { WITH | WITHOUT } HOLD ] FOR query
1792+
pub fn parse_declare(&mut self) -> Result<Statement, ParserError> {
1793+
let name = self.parse_object_name()?;
1794+
1795+
let binary = self.parse_keyword(Keyword::BINARY);
1796+
let sensitive = if self.parse_keyword(Keyword::INSENSITIVE) {
1797+
Some(true)
1798+
} else if self.parse_keyword(Keyword::ASENSITIVE) {
1799+
Some(false)
1800+
} else {
1801+
None
1802+
};
1803+
let scroll = if self.parse_keyword(Keyword::SCROLL) {
1804+
Some(true)
1805+
} else if self.parse_keywords(&[Keyword::NO, Keyword::SCROLL]) {
1806+
Some(false)
1807+
} else {
1808+
None
1809+
};
1810+
1811+
self.expect_keyword(Keyword::CURSOR)?;
1812+
1813+
let hold = match self.parse_one_of_keywords(&[Keyword::WITH, Keyword::WITHOUT]) {
1814+
Some(keyword) => {
1815+
self.expect_keyword(Keyword::HOLD)?;
1816+
1817+
match keyword {
1818+
Keyword::WITH => Some(true),
1819+
Keyword::WITHOUT => Some(false),
1820+
_ => unreachable!(),
1821+
}
1822+
}
1823+
None => None,
1824+
};
1825+
1826+
self.expect_keyword(Keyword::FOR)?;
1827+
1828+
let query = self.parse_query()?;
1829+
1830+
Ok(Statement::Declare {
1831+
name,
1832+
binary,
1833+
sensitive,
1834+
scroll,
1835+
hold,
1836+
query: Box::new(query),
1837+
})
1838+
}
1839+
17891840
pub fn parse_discard(&mut self) -> Result<Statement, ParserError> {
17901841
let object_type = if self.parse_keyword(Keyword::ALL) {
17911842
DiscardObject::ALL

tests/sqlparser_postgres.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,3 +1508,20 @@ fn parse_escaped_literal_string() {
15081508
"sql parser error: Unterminated encoded string literal at Line: 1, Column 8"
15091509
);
15101510
}
1511+
1512+
#[test]
1513+
fn parse_declare() {
1514+
pg_and_generic()
1515+
.verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" CURSOR WITH HOLD FOR SELECT 1");
1516+
pg_and_generic()
1517+
.verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" CURSOR WITHOUT HOLD FOR SELECT 1");
1518+
pg_and_generic().verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" BINARY CURSOR FOR SELECT 1");
1519+
pg_and_generic()
1520+
.verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" ASENSITIVE CURSOR FOR SELECT 1");
1521+
pg_and_generic()
1522+
.verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" INSENSITIVE CURSOR FOR SELECT 1");
1523+
pg_and_generic().verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" SCROLL CURSOR FOR SELECT 1");
1524+
pg_and_generic()
1525+
.verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" NO SCROLL CURSOR FOR SELECT 1");
1526+
pg_and_generic().verified_stmt("DECLARE \"SQL_CUR0x7fa44801bc00\" BINARY INSENSITIVE SCROLL CURSOR WITH HOLD FOR SELECT * FROM table_name LIMIT 2222");
1527+
}

0 commit comments

Comments
 (0)