-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStoredProcedureDDL.cs
253 lines (208 loc) · 8.89 KB
/
StoredProcedureDDL.cs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace SQLServer_Stored_Procedure_Converter
{
internal class StoredProcedureDDL
{
// Ignore Spelling: auth, citext, plpgsql, Postgres, udf
private readonly StoredProcedureConverter mProcedureConverter;
public string FunctionReturnType { get; set; }
/// <summary>
/// True if a function, false if a stored procedure
/// </summary>
public bool IsFunction { get; private set; }
/// <summary>
/// Local variables defined in the procedure
/// </summary>
public List<string> LocalVariablesToDeclare { get; }
/// <summary>
/// Processing options
/// </summary>
public StoredProcedureConverterOptions Options { get; }
/// <summary>
/// List of arguments for the procedure
/// </summary>
public List<string> ProcedureArguments { get; }
/// <summary>
/// Comments associated with the procedure arguments
/// Keys are the argument name, values are the comment (without --)
/// </summary>
/// <remarks>
/// This comments will be added to the procedure comment block
/// </remarks>
public List<KeyValuePair<string, string>> ProcedureArgumentComments { get; }
/// <summary>
/// Main body of the procedure
/// </summary>
public List<string> ProcedureBody { get; }
/// <summary>
/// Comment block
/// </summary>
/// <remarks>
/// Inserted between the AS and DECLARE keywords
/// </remarks>
public List<string> ProcedureCommentBlock { get; }
/// <summary>
/// Procedure name
/// </summary>
public string ProcedureName { get; private set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="options"></param>
/// <param name="procedureConverter"></param>
/// <param name="procedureName"></param>
public StoredProcedureDDL(StoredProcedureConverterOptions options, StoredProcedureConverter procedureConverter, string procedureName)
{
Options = options;
LocalVariablesToDeclare = new List<string>();
ProcedureArguments = new List<string>();
ProcedureArgumentComments = new List<KeyValuePair<string, string>>();
ProcedureBody = new List<string>();
ProcedureCommentBlock = new List<string>();
mProcedureConverter = procedureConverter;
Reset(procedureName);
}
/// <summary>
/// Get the object name, without the schema
/// </summary>
/// <param name="objectName"></param>
public static string GetNameWithoutSchema(string objectName)
{
if (string.IsNullOrWhiteSpace(objectName))
return string.Empty;
var periodIndex = objectName.IndexOf('.');
if (periodIndex > 0 && periodIndex < objectName.Length - 1)
return objectName.Substring(periodIndex + 1);
return objectName;
}
/// <summary>
/// Clear all cached data
/// </summary>
/// <param name="procedureName">Procedure or function name</param>
/// <param name="isFunction">True if this DDL is for a function; false for a stored procedure</param>
public void Reset(string procedureName, bool isFunction = false)
{
ProcedureName = procedureName;
IsFunction = isFunction;
FunctionReturnType = string.Empty;
LocalVariablesToDeclare.Clear();
ProcedureArguments.Clear();
ProcedureArgumentComments.Clear();
ProcedureBody.Clear();
ProcedureCommentBlock.Clear();
}
/// <summary>
/// Write the DDL for creating this stored procedure in PostgreSQL
/// </summary>
/// <param name="writer"></param>
public void ToWriterForPostgres(StreamWriter writer)
{
if (string.IsNullOrWhiteSpace(ProcedureName) || ProcedureBody.Count == 0)
return;
writer.WriteLine();
var snakeCaseName = StoredProcedureConverter.ConvertNameToSnakeCase(ProcedureName);
var snakeCaseNameToUse = snakeCaseName.Contains("udf_") ? snakeCaseName.Replace("udf_", string.Empty) : snakeCaseName;
var newProcedureName = Options.ConvertNamesToSnakeCase
? snakeCaseNameToUse
: ProcedureName;
var createMethod = IsFunction
? "CREATE OR REPLACE FUNCTION " + newProcedureName
: "CREATE OR REPLACE PROCEDURE " + newProcedureName;
if (ProcedureArguments.Count == 0)
{
writer.WriteLine(createMethod + "()");
}
else
{
writer.WriteLine(createMethod);
writer.WriteLine("(");
foreach (var item in ProcedureArguments)
{
writer.WriteLine(" " + item.TrimEnd());
}
writer.WriteLine(")");
}
if (IsFunction)
{
var returnType = string.IsNullOrWhiteSpace(FunctionReturnType) ? "citext" : FunctionReturnType;
writer.WriteLine("RETURNS " + mProcedureConverter.VarcharToText(returnType));
}
writer.WriteLine("LANGUAGE plpgsql");
writer.WriteLine("AS $$");
// The procedure comment block must appear after $$, otherwise it will be discarded (and not associated with the procedure)
var argumentCommentsAdded = false;
foreach (var item in ProcedureCommentBlock)
{
if (ProcedureArgumentComments.Count > 0)
{
if (item.StartsWith("** Auth:"))
{
WriteArgumentComments(writer);
argumentCommentsAdded = true;
}
else if (!argumentCommentsAdded && item.EndsWith("********/"))
{
WriteArgumentComments(writer);
argumentCommentsAdded = true;
}
}
writer.WriteLine(item.TrimEnd());
}
if (ProcedureArgumentComments.Count > 0 && !argumentCommentsAdded)
{
writer.WriteLine("/******************");
WriteArgumentComments(writer);
writer.WriteLine("******************/");
}
if (LocalVariablesToDeclare.Count > 0)
{
writer.WriteLine("DECLARE");
foreach (var item in LocalVariablesToDeclare)
{
if (item.StartsWith("_myRowCount", StringComparison.OrdinalIgnoreCase))
{
writer.WriteLine(" _myRowCount int := 0;");
continue;
}
writer.WriteLine(" " + item + ";");
}
}
writer.WriteLine("BEGIN");
foreach (var item in ProcedureBody)
{
writer.WriteLine(item.TrimEnd());
}
writer.WriteLine("END");
writer.WriteLine("$$;");
writer.WriteLine();
var nameWithoutSchema = GetNameWithoutSchema(ProcedureName);
var nameForComment = nameWithoutSchema.StartsWith("udf")
? nameWithoutSchema.Substring(3)
: nameWithoutSchema;
writer.WriteLine(
"COMMENT ON {0} {1} IS '{2}';",
IsFunction ? "FUNCTION" : "PROCEDURE",
newProcedureName,
nameForComment);
}
private void WriteArgumentComments(TextWriter writer)
{
if (ProcedureArgumentComments.Count == 0)
return;
// Find the longest argument name by examining the Keys in ProcedureArgumentComments
var maxArgNameLength = ProcedureArgumentComments.Max(argumentComment => argumentComment.Key.Length);
writer.WriteLine("** Arguments:");
// Format string will be of the form
// "** {0,-12} {1}"
var formatString = string.Format("** {{0,-{0}}} {{1}}", maxArgNameLength + 2);
foreach (var argumentComment in ProcedureArgumentComments)
{
writer.WriteLine(formatString, argumentComment.Key, argumentComment.Value.TrimEnd());
}
writer.WriteLine("**");
}
}
}