Skip to content

Commit

Permalink
TSQL PRINT statement parsing (#1648)
Browse files Browse the repository at this point in the history
Co-authored-by: jpersons <jpersons@iuhealth.org>
  • Loading branch information
jpers36 and jpersons committed Oct 14, 2021
1 parent 15cba85 commit 52b85c5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/sqlfluff/dialects/dialect_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class StatementSegment(ansi_dialect.get_segment("StatementSegment")): # type: i
Ref("DeclareStatementSegment"),
Ref("SetStatementSegment"),
Ref("AlterTableSwitchStatementSegment"),
Ref("PrintStatementSegment"),
Ref(
"CreateTableAsSelectStatementSegment"
), # Azure Synapse Analytics specific
Expand Down Expand Up @@ -1476,3 +1477,15 @@ class DatePartFunctionNameSegment(BaseSegment):

type = "function_name"
match_grammar = OneOf("DATEADD", "DATEDIFF", "DATEDIFF_BIG", "DATENAME")


@tsql_dialect.segment()
class PrintStatementSegment(BaseSegment):
"""PRINT statement segment."""

type = "print_statement"
match_grammar = Sequence(
"PRINT",
Ref("ExpressionSegment"),
Ref("DelimiterSegment", optional=True),
)
8 changes: 8 additions & 0 deletions test/fixtures/dialects/tsql/print.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DECLARE @TestVal VARCHAR(20) = 'Test Print'


PRINT '#Dates'

PRINT CAST(GETDATE() AS VARCHAR(50));

PRINT @TestVal
58 changes: 58 additions & 0 deletions test/fixtures/dialects/tsql/print.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# YML test files are auto-generated from SQL files and should not be edited by
# hand. To help enforce this, the "hash" field in the file must match a hash
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: ae5fdbfdc047812dbf5359045d2a417e4e7c09cbe83683737b5e96d1900da119
file:
batch:
- statement:
declare_segment:
keyword: DECLARE
parameter: '@TestVal'
data_type:
identifier: VARCHAR
bracketed:
start_bracket: (
expression:
literal: '20'
end_bracket: )
comparison_operator: '='
literal: "'Test Print'"
- statement:
print_statement:
keyword: PRINT
expression:
literal: "'#Dates'"
- statement:
print_statement:
keyword: PRINT
expression:
function:
function_name:
keyword: CAST
bracketed:
start_bracket: (
expression:
function:
function_name:
function_name_identifier: GETDATE
bracketed:
start_bracket: (
end_bracket: )
keyword: AS
data_type:
identifier: VARCHAR
bracketed:
start_bracket: (
expression:
literal: '50'
end_bracket: )
end_bracket: )
statement_terminator: ;
- statement:
print_statement:
keyword: PRINT
expression:
column_reference:
parameter: '@TestVal'

0 comments on commit 52b85c5

Please sign in to comment.