-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStoredProcedureConverterOptions.cs
148 lines (120 loc) · 5.38 KB
/
StoredProcedureConverterOptions.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using PRISM;
namespace SQLServer_Stored_Procedure_Converter
{
public class StoredProcedureConverterOptions
{
// Ignore Spelling: App, Postgres
/// <summary>
/// Program date
/// </summary>
public const string PROGRAM_DATE = "January 23, 2025";
[Option("StoredProcFile", "Input", "I", ArgPosition = 1, Required = true,
HelpShowsDefault = false, IsInputFilePath = true,
HelpText = "File with SQL Server stored procedures to convert")]
public string SQLServerStoredProcedureFile { get; set; }
[Option("OutputFile", "O", ArgPosition = 2, HelpShowsDefault = false,
HelpText = "Output file path")]
public string OutputFilePath { get; set; }
[Option("Schema", HelpShowsDefault = false,
HelpText = "Schema to use for stored procedures")]
public string SchemaName { get; set; } = "public";
[Option("Map", "M", HelpShowsDefault = false, IsInputFilePath = true,
HelpText = "Column name map file (typically created by sqlserver2pgsql.pl); tab-delimited file with five columns:\n" +
"SourceTable SourceName Schema NewTable NewName")]
public string ColumnNameMapFile { get; set; }
[Option("SnakeCase", "Snake", HelpShowsDefault = true,
HelpText = "When true, convert procedure and function names to snake_case")]
public bool ConvertNamesToSnakeCase { get; set; } = true;
/// <summary>
/// List of stored procedures to skip when converting the source file
/// </summary>
public SortedSet<string> StoredProcedureNamesToSkip { get; } = new(StringComparer.OrdinalIgnoreCase);
[Option("StoredProceduresToSkip", "SkipList", HelpShowsDefault = false,
HelpText = "Comma separated list of stored procedure names to skip while converting the source file")]
// ReSharper disable once UnusedMember.Global
public string StoredProceduresToSkip
{
get => string.Join(", ", StoredProcedureNamesToSkip);
set
{
StoredProcedureNamesToSkip.Clear();
if (string.IsNullOrWhiteSpace(value))
return;
foreach (var item in value.Split(','))
{
StoredProcedureNamesToSkip.Add(item.Trim());
}
}
}
[Option("Verbose", "V", HelpShowsDefault = true,
HelpText = "When true, display each updated code block")]
public bool VerboseOutput { get; set; }
/// <summary>
/// Constructor
/// </summary>
public StoredProcedureConverterOptions()
{
SQLServerStoredProcedureFile = string.Empty;
OutputFilePath = string.Empty;
ColumnNameMapFile = string.Empty;
}
/// <summary>
/// Get the program version
/// </summary>
public static string GetAppVersion()
{
return Assembly.GetExecutingAssembly().GetName().Version + " (" + PROGRAM_DATE + ")";
}
/// <summary>
/// Show the options at the console
/// </summary>
public void OutputSetOptions()
{
Console.WriteLine("Options:");
Console.WriteLine();
Console.WriteLine(" {0,-35} {1}", "Input file with stored procedures:", PathUtils.CompactPathString(SQLServerStoredProcedureFile, 80));
Console.WriteLine(" {0,-35} {1}", "Output file path:", PathUtils.CompactPathString(OutputFilePath, 80));
if (!string.IsNullOrWhiteSpace(SchemaName))
{
Console.WriteLine(" {0,-35} {1}", "Schema name:", SchemaName);
}
if (!string.IsNullOrWhiteSpace(ColumnNameMapFile))
{
Console.WriteLine(" {0,-35} {1}", "Column name map file:", PathUtils.CompactPathString(ColumnNameMapFile, 80));
}
Console.WriteLine(" {0,-35} {1}", "Convert names to snake case:", ConvertNamesToSnakeCase);
Console.WriteLine();
}
/// <summary>
/// Validate the options
/// </summary>
public bool ValidateArgs(out string errorMessage)
{
if (string.IsNullOrWhiteSpace(SQLServerStoredProcedureFile))
{
errorMessage = "Use /I to specify the input file";
return false;
}
if (string.IsNullOrWhiteSpace(OutputFilePath))
{
OutputFilePath = GetDefaultOutputFilePath();
}
errorMessage = string.Empty;
return true;
}
public string GetDefaultOutputFilePath()
{
return GetDefaultOutputFilePath(SQLServerStoredProcedureFile);
}
public string GetDefaultOutputFilePath(string inputFilePath)
{
var inputFile = new FileInfo(inputFilePath);
var newFileName = Path.GetFileNameWithoutExtension(inputFile.Name) + "_postgres.sql";
return string.IsNullOrEmpty(inputFile.DirectoryName) ? newFileName : Path.Combine(inputFile.DirectoryName, newFileName);
}
}
}