Skip to content

Commit 4a48729

Browse files
authored
Add support for PRINT statement for SQL Server (apache#1811)
1 parent 81d8909 commit 4a48729

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

src/ast/mod.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -4054,6 +4054,12 @@ pub enum Statement {
40544054
arguments: Vec<Expr>,
40554055
options: Vec<RaisErrorOption>,
40564056
},
4057+
/// ```sql
4058+
/// PRINT msg_str | @local_variable | string_expr
4059+
/// ```
4060+
///
4061+
/// See: <https://learn.microsoft.com/en-us/sql/t-sql/statements/print-transact-sql>
4062+
Print(PrintStatement),
40574063
}
40584064

40594065
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -5745,7 +5751,7 @@ impl fmt::Display for Statement {
57455751
}
57465752
Ok(())
57475753
}
5748-
5754+
Statement::Print(s) => write!(f, "{s}"),
57495755
Statement::List(command) => write!(f, "LIST {command}"),
57505756
Statement::Remove(command) => write!(f, "REMOVE {command}"),
57515757
}
@@ -9211,6 +9217,19 @@ pub enum CopyIntoSnowflakeKind {
92119217
Location,
92129218
}
92139219

9220+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9221+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9222+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9223+
pub struct PrintStatement {
9224+
pub message: Box<Expr>,
9225+
}
9226+
9227+
impl fmt::Display for PrintStatement {
9228+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9229+
write!(f, "PRINT {}", self.message)
9230+
}
9231+
}
9232+
92149233
#[cfg(test)]
92159234
mod tests {
92169235
use super::*;

src/ast/spans.rs

+1
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ impl Spanned for Statement {
519519
Statement::UNLISTEN { .. } => Span::empty(),
520520
Statement::RenameTable { .. } => Span::empty(),
521521
Statement::RaisError { .. } => Span::empty(),
522+
Statement::Print { .. } => Span::empty(),
522523
Statement::List(..) | Statement::Remove(..) => Span::empty(),
523524
}
524525
}

src/keywords.rs

+1
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ define_keywords!(
686686
PRESERVE,
687687
PREWHERE,
688688
PRIMARY,
689+
PRINT,
689690
PRIOR,
690691
PRIVILEGES,
691692
PROCEDURE,

src/parser/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ impl<'a> Parser<'a> {
617617
}
618618
// `COMMENT` is snowflake specific https://docs.snowflake.com/en/sql-reference/sql/comment
619619
Keyword::COMMENT if self.dialect.supports_comment_on() => self.parse_comment(),
620+
Keyword::PRINT => self.parse_print(),
620621
_ => self.expected("an SQL statement", next_token),
621622
},
622623
Token::LParen => {
@@ -15056,6 +15057,13 @@ impl<'a> Parser<'a> {
1505615057
}
1505715058
}
1505815059

15060+
/// Parse [Statement::Print]
15061+
fn parse_print(&mut self) -> Result<Statement, ParserError> {
15062+
Ok(Statement::Print(PrintStatement {
15063+
message: Box::new(self.parse_expr()?),
15064+
}))
15065+
}
15066+
1505915067
/// Consume the parser and return its underlying token buffer
1506015068
pub fn into_tokens(self) -> Vec<TokenWithSpan> {
1506115069
self.tokens

tests/sqlparser_mssql.rs

+17
Original file line numberDiff line numberDiff line change
@@ -2053,3 +2053,20 @@ fn parse_drop_trigger() {
20532053
}
20542054
);
20552055
}
2056+
2057+
#[test]
2058+
fn parse_print() {
2059+
let print_string_literal = "PRINT 'Hello, world!'";
2060+
let print_stmt = ms().verified_stmt(print_string_literal);
2061+
assert_eq!(
2062+
print_stmt,
2063+
Statement::Print(PrintStatement {
2064+
message: Box::new(Expr::Value(
2065+
(Value::SingleQuotedString("Hello, world!".to_string())).with_empty_span()
2066+
)),
2067+
})
2068+
);
2069+
2070+
let _ = ms().verified_stmt("PRINT N'Hello, ⛄️!'");
2071+
let _ = ms().verified_stmt("PRINT @my_variable");
2072+
}

0 commit comments

Comments
 (0)