-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcedureListUpdater.cs
1099 lines (875 loc) · 44 KB
/
ProcedureListUpdater.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using PRISM;
namespace PgSqlProcedureListUpdater
{
public class ProcedureListUpdater : EventNotifier
{
// Ignore Spelling: Postgres, Sql
private const string COMMA_PLACEHOLDER = "_@_LITERAL_COMMA_@_";
private readonly ProcedureListUpdaterOptions mOptions;
/// <summary>
/// This RegEx matches the argument direction, name, type, and default value (if defined)
/// </summary>
private readonly Regex mArgumentMatcher = new("((?<Direction>INOUT|OUT|IN) +)?(?<Name>[^ ]+) +(?<Type>[^ ]+)(?<Default>.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
/// <summary>
/// This RegEx looks for lines with $$ or $_$ or $body$, optionally preceded by "AS "
/// </summary>
private readonly Regex mDollarMatcher = new(@"(AS +|^ *)(?<Delimiter>\$[^$]*\$)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
/// <summary>
/// This RegEx looks for the column names for functions that return a table
/// </summary>
private readonly Regex mReturnedColumnListMatcher = new(@"\bTABLE *\((?<TableColumns>[^)]+)\) *(?<LanguageAndOptions>.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
/// <summary>
/// This RegEx looks for the return type of a function
/// </summary>
/// <remarks>
/// Example matches:
/// RETURNS anyelement
/// RETURNS boolean
/// RETURNS event_trigger
/// RETURNS integer
/// RETURNS numeric
/// RETURNS public.citext
/// RETURNS record
/// RETURNS SETOF public.pg_stat_statements
/// RETURNS SETOF record
/// RETURNS smallint
/// RETURNS TABLE(category text, value text)
/// RETURNS TABLE(dy timestamp with time zone)
/// RETURNS text
/// RETURNS timestamp without time zone
/// RETURNS trigger
/// RETURNS xml
/// </remarks>
private readonly Regex mReturnsMatcher = new(@"\bRETURNS +(?<ReturnType>timestamp with(out)? time zone|(SETOF )?[^ (]+) *(?<LanguageAndOptions>.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
/// <summary>
/// Constructor
/// </summary>
/// <param name="options"></param>
public ProcedureListUpdater(ProcedureListUpdaterOptions options)
{
mOptions = options;
}
private bool ExtractArguments(
string objectType,
string sourceDescription,
ICollection<string> headerLines,
StringBuilder objectHeader,
ICollection<ArgumentInfo> argumentList)
{
argumentList.Clear();
// Determine the procedure or function argument names and types
var success = ExtractArgumentsFromHeader(objectType, sourceDescription, objectHeader, out var argumentListMatch);
if (!success)
return false;
if (argumentListMatch.Trim().Length == 0)
return true;
// Replace any quoted commas with a placeholder
var argumentListToParse = ReplaceQuotedCommas(argumentListMatch);
// Split the argument list on commas
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var argument in argumentListToParse.Split(','))
{
// Convert any comma placeholders back to actual commas
var argumentInfo = new ArgumentInfo(argument.Trim().Replace(COMMA_PLACEHOLDER, ","));
var nameWithSpace = string.Format("{0} ", argumentInfo.Definition.Replace("public.citext", "citext"));
// Look for the argument in headerLines
// If the argument has a comment, include the comment when adding to argumentList
foreach (var headerLine in headerLines)
{
var argumentIndex = headerLine.IndexOf(nameWithSpace, StringComparison.OrdinalIgnoreCase);
if (argumentIndex < 0)
continue;
var commentIndex = headerLine.IndexOf("--", StringComparison.Ordinal);
if (commentIndex < 0)
break;
argumentInfo.Comment = headerLine.Substring(commentIndex);
break;
}
argumentList.Add(argumentInfo);
}
return true;
}
private bool ExtractArgumentsFromHeader(
string objectType,
string sourceDescription,
StringBuilder objectHeader,
out string argumentListMatch)
{
var sourceHeaderText = objectHeader.ToString();
string headerText;
if (objectType.Equals("FUNCTION", StringComparison.OrdinalIgnoreCase))
{
var returnsIndex = sourceHeaderText.IndexOf("RETURNS ", StringComparison.OrdinalIgnoreCase);
if (returnsIndex < 0)
{
OnWarningEvent("Header found, but 'RETURNS' not found and thus unable to parse the arguments for {0}: {1}", sourceDescription, objectHeader);
argumentListMatch = string.Empty;
return false;
}
headerText = sourceHeaderText.Substring(0, returnsIndex);
}
else
{
headerText = sourceHeaderText;
}
var openingParentheses = headerText.IndexOf("(", StringComparison.Ordinal);
var closingParentheses = headerText.LastIndexOf(")", StringComparison.Ordinal);
if (openingParentheses < 0)
{
OnWarningEvent("Header found, but '(' not found and thus cannot parse the arguments for {0}: {1}", sourceDescription, objectHeader);
argumentListMatch = string.Empty;
return false;
}
if (closingParentheses < 0)
{
OnWarningEvent("Header found, but ')' not found and thus cannot parse the arguments for {0}: {1}", sourceDescription, objectHeader);
argumentListMatch = string.Empty;
return false;
}
if (closingParentheses == openingParentheses + 1)
{
argumentListMatch = string.Empty;
}
else
{
argumentListMatch = headerText.Substring(openingParentheses + 1, closingParentheses - openingParentheses - 1);
}
return true;
}
private bool GetObjectNameAndType(string dataLine, Regex objectNameMatcher, out string objectName, out string objectType)
{
var nameMatch = objectNameMatcher.Match(dataLine);
if (!nameMatch.Success)
{
objectName = string.Empty;
objectType = string.Empty;
return false;
}
objectName = nameMatch.Groups["ObjectName"].Value.Trim();
objectType = nameMatch.Groups["ObjectType"].Value;
return true;
}
private void ParseHeaderLinesAfterArguments(
string headerTextAfterArguments,
string objectBodyDelimiter,
string objectType,
FileSystemInfo inputFile,
ICollection<string> headerLinesAfterArguments)
{
var languageIndex = headerTextAfterArguments.IndexOf("LANGUAGE", StringComparison.OrdinalIgnoreCase);
var asBodyDelimiterIndex = headerTextAfterArguments.IndexOf(
string.Format(" AS {0}", objectBodyDelimiter), StringComparison.OrdinalIgnoreCase);
if (languageIndex >= 0 && asBodyDelimiterIndex >= 0)
{
if (languageIndex > 0)
{
headerLinesAfterArguments.Add(headerTextAfterArguments.Substring(0, languageIndex));
}
headerLinesAfterArguments.Add(headerTextAfterArguments.Substring(languageIndex, asBodyDelimiterIndex - languageIndex).Trim());
headerLinesAfterArguments.Add(headerTextAfterArguments.Substring(asBodyDelimiterIndex).Trim());
return;
}
if (asBodyDelimiterIndex > 0)
{
headerLinesAfterArguments.Add(headerTextAfterArguments.Substring(0, asBodyDelimiterIndex));
headerLinesAfterArguments.Add(headerTextAfterArguments.Substring(asBodyDelimiterIndex));
return;
}
OnWarningEvent("Did not find \"LANGUAGE\" and \"{0}\" in headerLinesAfterArguments for the {0}, file: {1}", objectType, inputFile.FullName);
headerLinesAfterArguments.Add(headerTextAfterArguments);
}
/// <summary>
/// Process the input file
/// </summary>
/// <returns>True if successful, false if an error</returns>
public bool ProcessInputFile()
{
const string PUBLIC_SCHEMA_PREFIX = "public.";
try
{
var inputFile = new FileInfo(mOptions.InputFilePath);
if (!inputFile.Exists)
{
OnErrorEvent("Input file not found: " + inputFile.FullName);
return false;
}
if (inputFile.Directory == null || inputFile.DirectoryName == null)
{
OnErrorEvent("Unable to determine the parent directory of the input file: " + inputFile.FullName);
return false;
}
var outputFilePath = Path.Combine(
inputFile.DirectoryName,
Path.GetFileNameWithoutExtension(inputFile.Name) + "_updated" + inputFile.Extension);
var sqlFilesDirectory = new DirectoryInfo(mOptions.ScriptFileDirectory);
Console.WriteLine();
OnStatusEvent("Reading " + PathUtils.CompactPathString(inputFile.FullName, 100));
OnStatusEvent("Writing " + PathUtils.CompactPathString(outputFilePath, 100));
using var reader = new StreamReader(new FileStream(inputFile.FullName, FileMode.Open, FileAccess.Read, FileShare.Read));
using var writer = new StreamWriter(new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.Read));
var objectNameMatcher = new Regex("CREATE OR REPLACE (?<ObjectType>PROCEDURE|FUNCTION) *(?<ObjectName>[^(]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
var currentLineNumber = 0;
var lastStatusTime = DateTime.UtcNow;
var objectsProcessed = 0;
var objectsUpdated = 0;
// Keys in this dictionary are procedure or function names, values are the number of instances of the object (used for handling overloaded procedures)
var objectNames = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
while (!reader.EndOfStream)
{
var dataLine = reader.ReadLine();
currentLineNumber++;
if (string.IsNullOrWhiteSpace(dataLine))
{
writer.WriteLine();
continue;
}
if (!dataLine.Trim().StartsWith("CREATE OR REPLACE", StringComparison.OrdinalIgnoreCase))
{
writer.WriteLine(dataLine);
continue;
}
if (!GetObjectNameAndType(dataLine, objectNameMatcher, out var objectNameWithSchema, out var objectType))
{
OnWarningEvent("Unable to parse out the procedure or function name from {0}", dataLine);
writer.WriteLine(dataLine);
continue;
}
int overloadNumber;
if (objectNames.TryGetValue(objectNameWithSchema, out var instanceCount))
{
instanceCount++;
objectNames[objectNameWithSchema] = instanceCount;
overloadNumber = instanceCount;
}
else
{
instanceCount = 1;
objectNames.Add(objectNameWithSchema, instanceCount);
overloadNumber = instanceCount;
}
if (objectNameWithSchema.Equals("public.retry_myemsl_upload"))
{
Console.WriteLine("Check this code");
}
string fileToFind;
if (objectNameWithSchema.StartsWith(PUBLIC_SCHEMA_PREFIX, StringComparison.OrdinalIgnoreCase) &&
objectNameWithSchema.Length > PUBLIC_SCHEMA_PREFIX.Length)
{
fileToFind = objectNameWithSchema.Substring(PUBLIC_SCHEMA_PREFIX.Length) + ".sql";
}
else
{
fileToFind = objectNameWithSchema + ".sql";
}
var foundFiles = sqlFilesDirectory.GetFiles(fileToFind, mOptions.Recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).ToList();
if (foundFiles.Count == 0)
{
OnWarningEvent("DDL file not found in the SQL files directory: {0}", fileToFind);
writer.WriteLine(dataLine);
continue;
}
var sourceHeaderLines = new List<string> { dataLine };
// This is the object header, without any line feeds
var sourceObjectHeader = new StringBuilder();
sourceObjectHeader.Append(dataLine);
// Typically $$, but is sometimes $_$
var objectBodyDelimiter = string.Empty;
var sourceDescription = string.Format("{0} {1}", objectType.ToLower(), objectNameWithSchema);
var readSuccess = ReadHeaderAndBody(
reader,
objectNameMatcher,
objectType,
sourceDescription,
sourceHeaderLines,
sourceObjectHeader,
out var sourceObjectBody,
ref objectBodyDelimiter);
if (!readSuccess)
{
OnWarningEvent("Aborting since ReadHeaderAndBody returned false");
return false;
}
var sourceArgumentList = new List<ArgumentInfo>();
var argumentsFound = ExtractArguments(objectType, sourceDescription, sourceHeaderLines, sourceObjectHeader, sourceArgumentList);
if (!argumentsFound)
{
OnWarningEvent("Aborting since ExtractArguments returned false");
return false;
}
var argumentNameMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var argumentNumber = 0;
foreach (var argument in sourceArgumentList)
{
argumentNumber++;
if (string.IsNullOrWhiteSpace(argument.Definition))
{
OnWarningEvent("Argument {0} is empty for {1} {2}{3}",
argumentNumber + 1, objectType.ToLower(), objectNameWithSchema,
argumentNumber == sourceArgumentList.Count ? "; likely the final argument has a trailing comma" : string.Empty);
continue;
}
var match = mArgumentMatcher.Match(argument.Definition);
if (match.Success)
{
var argumentName = match.Groups["Name"].Value;
if (argumentNameMap.ContainsKey(argumentName))
{
OnWarningEvent("Argument name map for {0} {1} already has argument {2}; skipping",
objectType.ToLower(), objectNameWithSchema, argument);
}
else
{
argumentNameMap.Add(argumentName, argumentName);
}
}
else
{
OnWarningEvent("Argument for {0} {1} did not match the expected format: {2}",
objectType.ToLower(), objectNameWithSchema, argument);
}
}
if (mOptions.VerboseOutput)
{
OnStatusEvent("Reading file {0}", PathUtils.CompactPathString(foundFiles[0].Name, 120));
}
if (DateTime.UtcNow.Subtract(lastStatusTime).TotalSeconds >= 0.2)
{
ShowUpdateStatus(objectsUpdated, objectsProcessed);
lastStatusTime = DateTime.UtcNow;
}
// Load the object info from the DDL file with the updated version of the procedure or function
var sqlFileRead = ReadSqlFile(
foundFiles[0],
objectType.ToLower(),
objectNameWithSchema,
objectNameMatcher,
overloadNumber,
out var objectArgumentList,
out var headerLinesAfterArguments,
out var objectBody);
if (!sqlFileRead)
{
// Write out the source file lines
foreach (var headerLine in sourceHeaderLines)
{
writer.WriteLine(headerLine);
}
foreach (var bodyLine in sourceObjectBody)
{
writer.WriteLine(bodyLine);
}
objectsProcessed++;
continue;
}
var writeSuccess = WriteHeaderAndBody(
writer,
objectType,
objectNameWithSchema,
objectArgumentList,
headerLinesAfterArguments,
objectBody,
argumentNameMap);
if (writeSuccess)
{
objectsProcessed++;
objectsUpdated++;
continue;
}
OnWarningEvent("Aborting since WriteHeaderAndBody returned false");
return false;
}
Console.WriteLine();
OnStatusEvent("Processed {0} lines in the input file", currentLineNumber);
ShowUpdateStatus(objectsUpdated, objectsProcessed);
Console.WriteLine();
OnStatusEvent("Output file: " + outputFilePath);
return true;
}
catch (Exception ex)
{
OnErrorEvent("Error in ProcessInputFile", ex);
return false;
}
}
/// <summary>
/// Read the object body text
/// </summary>
/// <param name="reader"></param>
/// <param name="objectBody"></param>
/// <param name="objectBodyDelimiter">Typically $$, but is sometimes $_$</param>
/// <param name="objectNameMatcher"></param>
/// <param name="sourceDescription"></param>
/// <returns>True if successful, false if an error</returns>
private bool ReadBody(
StreamReader reader,
out List<string> objectBody,
string objectBodyDelimiter,
Regex objectNameMatcher,
string sourceDescription)
{
objectBody = new List<string>();
try
{
// Cache the object body text in objectBody
while (!reader.EndOfStream)
{
var dataLine = reader.ReadLine();
if (string.IsNullOrWhiteSpace(dataLine))
{
objectBody.Add(string.Empty);
continue;
}
if (dataLine.Contains(objectBodyDelimiter))
{
objectBody.Add(dataLine);
return true;
}
var nameMatch = objectNameMatcher.Match(dataLine);
if (nameMatch.Success)
{
OnWarningEvent("Found the next object before finding closing body delimiter {0} for {1}: {2}",
objectBodyDelimiter, sourceDescription, dataLine);
return false;
}
objectBody.Add(dataLine);
}
OnWarningEvent("Did not find closing body delimiter {0} for {1}",
objectBodyDelimiter, sourceDescription);
return false;
}
catch (Exception ex)
{
OnErrorEvent(ex, "Error in ReadBody for {0}", sourceDescription);
return false;
}
}
private bool ReadHeader(
StreamReader reader,
ICollection<string> headerLines,
StringBuilder objectHeader,
ref string objectBodyDelimiter,
Regex objectNameMatcher,
string sourceDescription)
{
try
{
while (!reader.EndOfStream)
{
var dataLine = reader.ReadLine();
if (string.IsNullOrWhiteSpace(dataLine))
{
headerLines.Add(string.Empty);
continue;
}
headerLines.Add(dataLine);
var nameMatch = objectNameMatcher.Match(dataLine);
if (nameMatch.Success)
{
OnWarningEvent("Found the next object before finding the body delimiter for {0}: {1}",
sourceDescription, dataLine);
return false;
}
// Do not include comments when appending the data line to objectHeader (since that can cause spurious RegEx matches)
objectHeader.AppendFormat(" {0}", RemoveComment(dataLine));
var match = mDollarMatcher.Match(dataLine);
if (!match.Success)
continue;
objectBodyDelimiter = match.Groups["Delimiter"].Value;
return true;
}
return false;
}
catch (Exception ex)
{
OnErrorEvent(ex, "Error in ReadHeader for {0}", sourceDescription);
return false;
}
}
private bool ReadHeaderAndBody(
StreamReader reader,
Regex objectNameMatcher,
string objectType,
string sourceDescription,
ICollection<string> headerLines,
StringBuilder objectHeader,
out List<string> objectBody,
ref string objectBodyDelimiter)
{
var headerSuccess = ReadHeader(
reader,
headerLines,
objectHeader,
ref objectBodyDelimiter,
objectNameMatcher,
sourceDescription);
if (!headerSuccess)
{
OnWarningEvent("Unable to parse {0} header in {1}",
objectType, sourceDescription);
objectBody = new List<string>();
return false;
}
// Read the object body
return ReadBody(
reader,
out objectBody,
objectBodyDelimiter,
objectNameMatcher,
sourceDescription);
}
/// <summary>
/// Read a procedure or function's DDL file
/// </summary>
/// <param name="inputFile"></param>
/// <param name="objectType"></param>
/// <param name="objectNameWithSchema"></param>
/// <param name="objectNameMatcher"></param>
/// <param name="overloadNumberToFind">This is typically 1, but if an object is overloaded, this will be 2 when processing the second instance of an object</param>
/// <param name="argumentList"></param>
/// <param name="headerLinesAfterArguments">Tracks the text that occurs after the closing parenthesis of the procedure or function's argument list</param>
/// <param name="objectBody">Procedure or function text between the starting and ending $$</param>
/// <returns>True if successful, false if an error</returns>
private bool ReadSqlFile(
FileSystemInfo inputFile,
string objectType,
string objectNameWithSchema,
Regex objectNameMatcher,
int overloadNumberToFind,
out List<ArgumentInfo> argumentList,
out List<string> headerLinesAfterArguments,
out List<string> objectBody)
{
argumentList = new List<ArgumentInfo>();
headerLinesAfterArguments = new List<string>();
// Typically $$, but is sometimes $_$
var objectBodyDelimiter = string.Empty;
try
{
// Each line of the object header
var headerLines = new List<string>();
var overloadNumber = 0;
using var reader = new StreamReader(new FileStream(inputFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
while (!reader.EndOfStream)
{
var dataLine = reader.ReadLine();
if (string.IsNullOrWhiteSpace(dataLine))
continue;
if (!dataLine.Trim().StartsWith("CREATE OR REPLACE", StringComparison.OrdinalIgnoreCase))
{
continue;
}
overloadNumber++;
if (overloadNumberToFind > overloadNumber)
{
// Found an earlier overload; need to keep processing to find the next overload
OnStatusEvent("Looking for overload {0} of {1} {2}", overloadNumberToFind, objectType, objectNameWithSchema);
continue;
}
if (!GetObjectNameAndType(dataLine, objectNameMatcher, out var foundName, out var foundType))
{
OnWarningEvent("Unable to parse out the {0} name from line \"{1}\" in file {2}",
objectType, dataLine, PathUtils.CompactPathString(inputFile.FullName, 120));
objectBody = new List<string>();
return false;
}
if (!objectNameWithSchema.Equals(foundName, StringComparison.OrdinalIgnoreCase))
{
OnWarningEvent("{0} name in .sql file did not match the expected name ({1}): see \"{2}\" in file {3}",
objectType, objectNameWithSchema, dataLine, PathUtils.CompactPathString(inputFile.FullName, 120));
objectBody = new List<string>();
return false;
}
if (!objectType.Equals(foundType, StringComparison.OrdinalIgnoreCase))
{
OnWarningEvent("Object type in .sql file did not match the expected type ({0}): see \"{1}\" in file {2}",
objectType, dataLine, PathUtils.CompactPathString(inputFile.FullName, 120));
objectBody = new List<string>();
return false;
}
headerLines.Add(dataLine);
// This is the object header, without any line feeds
var objectHeader = new StringBuilder();
objectHeader.Append(dataLine);
var sourceDescription = string.Format("file {0}", PathUtils.CompactPathString(inputFile.FullName, 120));
var success = ReadHeaderAndBody(
reader,
objectNameMatcher,
objectType,
sourceDescription,
headerLines,
objectHeader,
out objectBody,
ref objectBodyDelimiter);
if (!success)
{
return false;
}
var argumentsFound = ExtractArguments(objectType, sourceDescription, headerLines, objectHeader, argumentList);
if (!argumentsFound)
{
return false;
}
// Find the text that occurs after the closing parenthesis
// This tracks the text that occurs after the closing parenthesis of the procedure or function's argument list
// (it does not include line feeds)
var headerTextAfterArguments = new StringBuilder();
var appendHeaderLines = false;
foreach (var headerLine in headerLines)
{
if (appendHeaderLines)
{
headerTextAfterArguments.AppendFormat(" {0}", headerLine);
continue;
}
var returnsIndex = headerLine.IndexOf("RETURNS ", StringComparison.OrdinalIgnoreCase);
var parenthesisIndex = returnsIndex > 0
? headerLine.Substring(0, returnsIndex).LastIndexOf(')')
: headerLine.LastIndexOf(')');
if (parenthesisIndex < 0)
continue;
if (parenthesisIndex < headerLine.Length - 1)
{
headerTextAfterArguments.Append(headerLine.Substring(parenthesisIndex + 1).Trim());
}
appendHeaderLines = true;
}
if (!objectType.Equals("FUNCTION", StringComparison.OrdinalIgnoreCase))
{
// Populate headerLinesAfterArguments using headerTextAfterArguments
ParseHeaderLinesAfterArguments(
headerTextAfterArguments.ToString().Trim(),
objectBodyDelimiter,
objectType,
inputFile,
headerLinesAfterArguments);
return true;
}
// Determine the return type of the function
// If it is a table, parse the column names and types
var returnMatch = mReturnsMatcher.Match(headerTextAfterArguments.ToString());
if (!returnMatch.Success)
{
OnWarningEvent("Header found, but unable to determine the return type of function {0}", objectNameWithSchema);
return false;
}
var returnType = returnMatch.Groups["ReturnType"].Value;
if (!returnType.Equals("Table", StringComparison.OrdinalIgnoreCase))
{
headerLinesAfterArguments.Add(string.Format("RETURNS {0}", returnType));
ParseHeaderLinesAfterArguments(
returnMatch.Groups["LanguageAndOptions"].Value,
objectBodyDelimiter,
objectType,
inputFile,
headerLinesAfterArguments);
return true;
}
var columnListMatch = mReturnedColumnListMatcher.Match(headerTextAfterArguments.ToString());
if (!columnListMatch.Success)
{
OnWarningEvent("Header found, but unable to determine the column list for the table returned by function {0}",
objectNameWithSchema);
return false;
}
headerLinesAfterArguments.Add("RETURNS TABLE (");
var tableColumns = columnListMatch.Groups["TableColumns"].Value.Split(',').ToList();
var indexEnd = tableColumns.Count - 1;
for (var i = 0; i <= indexEnd; i++)
{
var columnNameAndType = tableColumns[i].Replace("public.citext", "citext");
headerLinesAfterArguments.Add(string.Format(" {0}{1}", columnNameAndType.Trim(), i < indexEnd ? "," : string.Empty));
}
headerLinesAfterArguments.Add(")");
ParseHeaderLinesAfterArguments(
columnListMatch.Groups["LanguageAndOptions"].Value,
objectBodyDelimiter,
objectType,
inputFile,
headerLinesAfterArguments);
return true;
}
if (overloadNumber == 0)
{
OnWarningEvent("Did not find \"CREATE OR REPLACE\" in file {0}", PathUtils.CompactPathString(inputFile.FullName, 120));
}
else if (overloadNumberToFind > 1)
{
OnWarningEvent("Found overload {0} but not overload {1} in file {2}", overloadNumber, overloadNumberToFind, PathUtils.CompactPathString(inputFile.FullName, 120));
}
else
{
OnWarningEvent("Unreachable code encountered while looking for overload {0} in file {1}", overloadNumberToFind, PathUtils.CompactPathString(inputFile.FullName, 120));
}
objectBody = new List<string>();
return false;
}
catch (Exception ex)
{
OnErrorEvent(ex, "Error in ReadSqlFile for {0}", PathUtils.CompactPathString(inputFile.FullName, 120));
objectBody = new List<string>();
return false;
}
}
private string RemoveComment(string dataLine)
{
var commentIndex = dataLine.IndexOf("--", StringComparison.Ordinal);
return commentIndex < 0
? dataLine
: dataLine.Substring(0, commentIndex).Trim();
}
private string ReplaceQuotedCommas(string argumentList)
{
var startIndex = 0;
var updatedArgumentList = new StringBuilder();
while (true)
{
var quoteIndex = argumentList.Substring(startIndex).IndexOf('\'');
if (quoteIndex < 0)
break;
quoteIndex += startIndex;
var nextQuoteIndex = argumentList.Substring(quoteIndex + 1).IndexOf('\'');
if (nextQuoteIndex < 0)
break;
nextQuoteIndex += quoteIndex + 1;
if (nextQuoteIndex == quoteIndex + 1)
{
startIndex = nextQuoteIndex + 1;
continue;
}
var quotedValue = argumentList.Substring(quoteIndex + 1, nextQuoteIndex - quoteIndex - 1);
var commaIndex = quotedValue.IndexOf(',');
if (commaIndex < 0)
{
startIndex = nextQuoteIndex + 1;
continue;
}
if (startIndex < quoteIndex)
{
updatedArgumentList.Append(argumentList, 0, quoteIndex);
}
updatedArgumentList.AppendFormat("'{0}'", quotedValue.Replace(",", COMMA_PLACEHOLDER));
if (nextQuoteIndex < argumentList.Length - 1)
{
updatedArgumentList.Append(argumentList, nextQuoteIndex + 1, argumentList.Length - (nextQuoteIndex + 1));
}
var addedCharacterCount = updatedArgumentList.Length - argumentList.Length;
argumentList = updatedArgumentList.ToString();
startIndex = nextQuoteIndex + 1 + addedCharacterCount;
}
return argumentList;
}
private void ShowUpdateStatus(int objectsUpdated, int objectsProcessed, bool processingComplete = false)
{
OnStatusEvent("{0,-9} procedures or functions {1} updated using .sql files",
string.Format("{0}{1} / {2}", objectsUpdated < 99 ? " " : string.Empty, objectsUpdated, objectsProcessed),
processingComplete ? "were" : "have been");
}
/// <summary>
/// Write the updated header and body for the procedure of function
/// </summary>
/// <param name="writer"></param>
/// <param name="objectType"></param>
/// <param name="objectNameWithSchema"></param>
/// <param name="objectArgumentList"></param>
/// <param name="headerLinesAfterArguments"></param>
/// <param name="objectBody"></param>
/// <param name="argumentNameMap">Argument names for the procedure or function, as read from the input file</param>
/// <returns>True if successful, false if an error</returns>
private bool WriteHeaderAndBody(
TextWriter writer,
string objectType,
string objectNameWithSchema,
IReadOnlyList<ArgumentInfo> objectArgumentList,
IEnumerable<string> headerLinesAfterArguments,
IEnumerable<string> objectBody,
IReadOnlyDictionary<string, string> argumentNameMap)
{
try
{
writer.WriteLine("CREATE OR REPLACE {0} {1} {2}", objectType, objectNameWithSchema, objectArgumentList.Count == 0 ? "()" : "(");