-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathSqlScriptGeneratorVisitor.FunctionCall.cs
More file actions
174 lines (162 loc) · 8.46 KB
/
SqlScriptGeneratorVisitor.FunctionCall.cs
File metadata and controls
174 lines (162 loc) · 8.46 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//------------------------------------------------------------------------------
// <copyright file="SqlScriptGeneratorVisitor.FunctionCall.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Globalization;
namespace Microsoft.SqlServer.TransactSql.ScriptDom.ScriptGenerator
{
partial class SqlScriptGeneratorVisitor
{
public override void ExplicitVisit(LeftFunctionCall node)
{
GenerateKeyword(TSqlTokenType.Left);
GenerateParenthesisedCommaSeparatedList(node.Parameters, true);
GenerateSpaceAndCollation(node.Collation);
}
public override void ExplicitVisit(RightFunctionCall node)
{
GenerateKeyword(TSqlTokenType.Right);
GenerateParenthesisedCommaSeparatedList(node.Parameters, true);
GenerateSpaceAndCollation(node.Collation);
}
public override void ExplicitVisit(FunctionCall node)
{
GenerateFragmentIfNotNull(node.CallTarget);
GenerateFragmentIfNotNull(node.FunctionName);
GenerateSymbol(TSqlTokenType.LeftParenthesis);
if (node.FunctionName.Value.ToUpper(CultureInfo.InvariantCulture) == CodeGenerationSupporter.Trim &&
2 == node.Parameters.Count)
{
// If Trimoptions has a saved value. The Syntax is modified to TRIM (Identifier ARG2 FROM ARG3)
// Trimoptions can only be LEADING/TRAILING/BOTH.
//
if (node.TrimOptions != null)
{
GenerateSpace();
GenerateFragmentIfNotNull(node.TrimOptions);
GenerateSpace();
}
GenerateFragmentIfNotNull(node.Parameters[0]);
GenerateSpace();
GenerateKeyword(TSqlTokenType.From);
GenerateSpace();
GenerateFragmentIfNotNull(node.Parameters[1]);
GenerateSymbol(TSqlTokenType.RightParenthesis);
}
else if (node.FunctionName.Value.ToUpper(CultureInfo.InvariantCulture) == CodeGenerationSupporter.JsonObject)
{
GenerateCommaSeparatedList(node.JsonParameters);
if (node.JsonParameters?.Count > 0 && node.AbsentOrNullOnNull?.Count > 0) //If there are values and null on null or absent on null present then generate space in between them
GenerateSpace();
GenerateNullOnNullOrAbsentOnNull(node?.AbsentOrNullOnNull);
if (node.JsonParameters?.Count > 0 && node.ReturnType?.Count > 0) //If there are values and null on null or absent on null present then generate space in between them
GenerateSpace();
GenerateReturnType(node?.ReturnType);
GenerateSymbol(TSqlTokenType.RightParenthesis);
}
else if (node.FunctionName.Value.ToUpper(CultureInfo.InvariantCulture) == CodeGenerationSupporter.JsonObjectAgg)
{
GenerateCommaSeparatedList(node.JsonParameters);
if (node.JsonParameters?.Count > 0 && node.AbsentOrNullOnNull?.Count > 0) //If there are values and null on null or absent on null present then generate space in between them
GenerateSpace();
GenerateNullOnNullOrAbsentOnNull(node?.AbsentOrNullOnNull);
if (node.JsonParameters?.Count > 0 && node.ReturnType?.Count > 0) //If there are values and null on null or absent on null present then generate space in between them
GenerateSpace();
GenerateReturnType(node?.ReturnType);
GenerateSymbol(TSqlTokenType.RightParenthesis);
}
else if (node.FunctionName.Value.ToUpper(CultureInfo.InvariantCulture) == CodeGenerationSupporter.JsonArray)
{
GenerateCommaSeparatedList(node.Parameters);
if (node.Parameters?.Count > 0 && node?.AbsentOrNullOnNull?.Count > 0) //If there are values and null on null or absent on null present then generate space in between them
GenerateSpace();
GenerateNullOnNullOrAbsentOnNull(node?.AbsentOrNullOnNull);
if (node.ReturnType?.Count > 0) //If there are values and null on null or absent on null present then generate space in between them
GenerateSpace();
GenerateReturnType(node?.ReturnType);
GenerateSymbol(TSqlTokenType.RightParenthesis);
}
else if (node.FunctionName.Value.ToUpper(CultureInfo.InvariantCulture) == CodeGenerationSupporter.JsonArrayAgg)
{
GenerateCommaSeparatedList(node.Parameters);
if (node.Parameters?.Count > 0 && node?.AbsentOrNullOnNull?.Count > 0) //If there are values and null on null or absent on null present then generate space in between them
GenerateSpace();
GenerateNullOnNullOrAbsentOnNull(node?.AbsentOrNullOnNull);
if (node.ReturnType?.Count > 0) //If there are values and null on null or absent on null present then generate space in between them
GenerateSpace();
GenerateReturnType(node?.ReturnType);
GenerateSymbol(TSqlTokenType.RightParenthesis);
}
else if (node.FunctionName.Value.ToUpper(CultureInfo.InvariantCulture) == CodeGenerationSupporter.JsonQuery)
{
GenerateCommaSeparatedList(node.Parameters);
GenerateSymbol(TSqlTokenType.RightParenthesis);
// Handle WITH ARRAY WRAPPER clause
if (node.WithArrayWrapper)
{
GenerateSpace();
GenerateKeyword(TSqlTokenType.With);
GenerateSpace();
GenerateIdentifier(CodeGenerationSupporter.Array);
GenerateSpace();
GenerateIdentifier(CodeGenerationSupporter.Wrapper);
}
}
else
{
GenerateUniqueRowFilter(node.UniqueRowFilter, false);
if (node.UniqueRowFilter != UniqueRowFilter.NotSpecified && node.Parameters.Count > 0)
GenerateSpace();
GenerateCommaSeparatedList(node.Parameters);
GenerateSymbol(TSqlTokenType.RightParenthesis);
if (node.IgnoreRespectNulls?.Count > 0)
{
GenerateSpace();
GenerateSpaceSeparatedList(node.IgnoreRespectNulls);
}
GenerateSpaceAndFragmentIfNotNull(node.WithinGroupClause);
GenerateSpaceAndFragmentIfNotNull(node.OverClause);
}
GenerateSpaceAndCollation(node.Collation);
}
public override void ExplicitVisit(JsonKeyValue pair)
{
GenerateFragmentIfNotNull(pair.JsonKeyName);
//if key is not null, then add colon
if (pair.JsonKeyName != null)
GenerateSymbol(TSqlTokenType.Colon);
GenerateFragmentIfNotNull(pair.JsonValue);
}
//Generate Absent on Null or Null on Null
private void GenerateNullOnNullOrAbsentOnNull(IList<Identifier> list)
{
if (list?.Count > 0 && list[0].Value?.ToUpper(CultureInfo.InvariantCulture) == CodeGenerationSupporter.Absent)
{
GenerateSpaceSeparatedList(list);
GenerateSpace();
GenerateKeyword(TSqlTokenType.On);
GenerateSpace();
GenerateKeyword(TSqlTokenType.Null);
}
else if (list?.Count > 0 && list[0].Value?.ToUpper(CultureInfo.InvariantCulture) == CodeGenerationSupporter.Null)
{
GenerateKeyword(TSqlTokenType.Null);
GenerateSpace();
GenerateKeyword(TSqlTokenType.On);
GenerateSpace();
GenerateKeyword(TSqlTokenType.Null);
}
}
private void GenerateReturnType(IList<Identifier> list)
{
if (list?.Count > 0 && list[0].Value?.ToUpper(CultureInfo.InvariantCulture) == CodeGenerationSupporter.Json)
{
GenerateIdentifier("RETURNING");
GenerateSpace();
GenerateSpaceSeparatedList(list);
}
}
}
}