Skip to content

Commit

Permalink
T-SQL support default params and no RETURN value (#2434)
Browse files Browse the repository at this point in the history
* T-SQL ddetails params and no RETURN value

* Review feedback

Co-authored-by: Barry Hart <barrywhart@yahoo.com>
  • Loading branch information
tunetheweb and barrywhart authored Jan 23, 2022
1 parent 172a8f7 commit 6ccaaa9
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/sqlfluff/dialects/dialect_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@
ParameterNameSegment=RegexParser(
r"[@][A-Za-z0-9_]+", CodeSegment, name="parameter", type="parameter"
),
FunctionParameterGrammar=Sequence(
Ref("ParameterNameSegment", optional=True),
Ref("DatatypeSegment"),
Sequence(Ref("EqualsSegment"), Ref("ExpressionSegment"), optional=True),
),
FunctionNameIdentifierSegment=RegexParser(
r"[A-Z][A-Z0-9_]*|\[[A-Z][A-Z0-9_]*\]",
CodeSegment,
Expand Down Expand Up @@ -1461,7 +1466,7 @@ class ReturnStatementSegment(BaseSegment):
type = "return_segment"
match_grammar = Sequence(
"RETURN",
Ref("ExpressionSegment"),
Ref("ExpressionSegment", optional=True),
Ref("DelimiterSegment", optional=True),
)

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/dialects/tsql/function_default_params.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create or alter procedure name
@param1 nvarchar(10) = 'test',
@param2 int = 21
as begin
return 1;
end
48 changes: 48 additions & 0 deletions test/fixtures/dialects/tsql/function_default_params.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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: 2501cb05d43246a910e1715a625e9329547f0246615cce94560e3f5bf18bc535
file:
batch:
create_procedure_statement:
- keyword: create
- binary_operator: or
- keyword: alter
- keyword: procedure
- object_reference:
identifier: name
- function_parameter_list:
- parameter: '@param1'
- data_type:
identifier: nvarchar
bracketed:
start_bracket: (
expression:
literal: '10'
end_bracket: )
- comparison_operator:
raw_comparison_operator: '='
- expression:
literal: "'test'"
- comma: ','
- parameter: '@param2'
- data_type:
identifier: int
- comparison_operator:
raw_comparison_operator: '='
- expression:
literal: '21'
- keyword: as
- procedure_statement:
statement:
begin_end_block:
- keyword: begin
- statement:
return_segment:
keyword: return
expression:
literal: '1'
statement_terminator: ;
- keyword: end
14 changes: 14 additions & 0 deletions test/fixtures/dialects/tsql/function_no_return.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE PROCEDURE findjobs @nm sysname = NULL
AS
IF @nm IS NULL
BEGIN
PRINT 'You must give a user name'
RETURN
END
ELSE
BEGIN
SELECT o.name, o.id, o.uid
FROM sysobjects o INNER JOIN master..syslogins l
ON o.uid = l.sid
WHERE l.name = @nm
END;
116 changes: 116 additions & 0 deletions test/fixtures/dialects/tsql/function_no_return.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# 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: 421175885d34b10910511e8d2a7a277da2928fd4fa4ae698210aac7159928a59
file:
batch:
create_procedure_statement:
- keyword: CREATE
- keyword: PROCEDURE
- object_reference:
identifier: findjobs
- function_parameter_list:
parameter: '@nm'
data_type:
identifier: sysname
comparison_operator:
raw_comparison_operator: '='
expression:
literal: 'NULL'
- keyword: AS
- procedure_statement:
statement:
if_then_statement:
- if_clause:
keyword: IF
expression:
- column_reference:
parameter: '@nm'
- keyword: IS
- keyword: 'NULL'
- statement:
begin_end_block:
- keyword: BEGIN
- statement:
print_statement:
keyword: PRINT
expression:
literal: "'You must give a user name'"
- statement:
return_segment:
keyword: RETURN
- keyword: END
- keyword: ELSE
- statement:
begin_end_block:
- keyword: BEGIN
- statement:
select_statement:
select_clause:
- keyword: SELECT
- select_clause_element:
column_reference:
- identifier: o
- dot: .
- identifier: name
- comma: ','
- select_clause_element:
column_reference:
- identifier: o
- dot: .
- identifier: id
- comma: ','
- select_clause_element:
column_reference:
- identifier: o
- dot: .
- identifier: uid
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
identifier: sysobjects
alias_expression:
identifier: o
join_clause:
- keyword: INNER
- keyword: JOIN
- from_expression_element:
table_expression:
table_reference:
- identifier: master
- dot: .
- dot: .
- identifier: syslogins
alias_expression:
identifier: l
- join_on_condition:
keyword: 'ON'
expression:
- column_reference:
- identifier: o
- dot: .
- identifier: uid
- comparison_operator:
raw_comparison_operator: '='
- column_reference:
- identifier: l
- dot: .
- identifier: sid
where_clause:
keyword: WHERE
expression:
- column_reference:
- identifier: l
- dot: .
- identifier: name
- comparison_operator:
raw_comparison_operator: '='
- column_reference:
parameter: '@nm'
- keyword: END
- statement_terminator: ;

0 comments on commit 6ccaaa9

Please sign in to comment.