-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathSqlScriptGeneratorVisitor.FunctionStatementBody.cs
More file actions
116 lines (105 loc) · 4.68 KB
/
Copy pathSqlScriptGeneratorVisitor.FunctionStatementBody.cs
File metadata and controls
116 lines (105 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//------------------------------------------------------------------------------
// <copyright file="SqlScriptGeneratorVisitor.FunctionStatementBody.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using Microsoft.SqlServer.TransactSql.ScriptDom;
namespace Microsoft.SqlServer.TransactSql.ScriptDom.ScriptGenerator
{
partial class SqlScriptGeneratorVisitor
{
protected static Dictionary<FunctionOptionKind, List<TokenGenerator>> _functionOptionsGenerators =
new Dictionary<FunctionOptionKind, List<TokenGenerator>>()
{
{ FunctionOptionKind.CalledOnNullInput, new List<TokenGenerator>() {
new IdentifierGenerator(CodeGenerationSupporter.Called, true),
new KeywordGenerator(TSqlTokenType.On, true),
new KeywordGenerator(TSqlTokenType.Null, true),
new IdentifierGenerator(CodeGenerationSupporter.Input),
}},
{ FunctionOptionKind.Encryption, new List<TokenGenerator>() {
new IdentifierGenerator(CodeGenerationSupporter.Encryption),
}},
// handled specially
//{ FunctionOptions.None, new List<TokenGenerator>() {
// }},
{ FunctionOptionKind.ReturnsNullOnNullInput, new List<TokenGenerator>() {
new IdentifierGenerator(CodeGenerationSupporter.Returns, true),
new KeywordGenerator(TSqlTokenType.Null, true),
new KeywordGenerator(TSqlTokenType.On, true),
new KeywordGenerator(TSqlTokenType.Null, true),
new IdentifierGenerator(CodeGenerationSupporter.Input),
}},
{ FunctionOptionKind.SchemaBinding, new List<TokenGenerator>() {
new IdentifierGenerator(CodeGenerationSupporter.SchemaBinding),
}},
{ FunctionOptionKind.NativeCompilation, new List<TokenGenerator>() {
new IdentifierGenerator(CodeGenerationSupporter.NativeCompilation),
}},
{ FunctionOptionKind.Inline, new List<TokenGenerator>() {
new IdentifierGenerator(CodeGenerationSupporter.Inline),
}},
};
protected void GenerateFunctionStatementBody(FunctionStatementBody node)
{
GenerateKeyword(TSqlTokenType.Function);
// name
GenerateSpaceAndFragmentIfNotNull(node.Name);
// parameters
GenerateProcedureOrFunctionParameters(node.Parameters, parenthesized: true);
// RETURNS
NewLine();
GenerateIdentifier(CodeGenerationSupporter.Returns);
GenerateSpace();
// SelectFunctionReturn also holds function body (which is single select expression)
SelectFunctionReturnType selectReturn = node.ReturnType as SelectFunctionReturnType;
if (selectReturn != null)
{
GenerateKeywordAndSpace(TSqlTokenType.Table);
}
else
{
// could be
// ScalarFunctionReturnType
// SelectFunctionReturnType
// TableValuedFunctionReturnType
GenerateFragmentIfNotNull(node.ReturnType);
}
GenerateCommaSeparatedWithClause(node.Options, false, false);
if (node.OrderHint != null)
{
NewLine();
GenerateFragmentIfNotNull(node.OrderHint);
}
NewLine();
GenerateKeyword(TSqlTokenType.As);
NewLine();
if (selectReturn != null)
{
GenerateKeywordAndSpace(TSqlTokenType.Return);
GenerateFragmentIfNotNull(selectReturn);
}
else
{
if (node.MethodSpecifier != null)
GenerateSpaceAndFragmentIfNotNull(node.MethodSpecifier);
else if (node.StatementList != null)
GenerateFragmentIfNotNull(node.StatementList);
}
}
public override void ExplicitVisit(FunctionOption node)
{
List<TokenGenerator> generators = GetValueForEnumKey(_functionOptionsGenerators, node.OptionKind);
if (generators != null)
{
GenerateTokenList(generators);
}
}
public override void ExplicitVisit(ExecuteAsFunctionOption node)
{
GenerateFragmentIfNotNull(node.ExecuteAs);
}
}
}