-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
199 lines (169 loc) · 8.66 KB
/
Program.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
using System;
using System.Collections.Generic;
using Jannesen.Tools.DBTools.DBSchema;
// DBTools export TVCN-SERVER-DEV\TAS2 "file:C:\Temp\TAS2-36-schema.xml"
// DBTools compare-report "file:C:\Temp\TAS2-36-schema.xml" "sql:TVCN-SERVER-DEV\TAS2" "C:\Temp\TAS2.36 to 2.37 report.txt"
// DBTools schema-update "file:C:\Temp\TAS2-36-schema.xml" "sql:TVCN-SERVER-DEV\TAS2" "C:\Temp\TAS2.36 to 2.37 report.sql"
// DBTools code-update N:\TVCN\TVCN-SERVER\TAS\2.38.01\Schema\TAS2.38.01-schema.xml "sql:TVCN-SERVER-DEV\TAS2" "C:\Temp\T.sql"
namespace Jannesen.Tools.DBTools
{
public sealed class Program
{
static void Main(string[] aargs)
{
try {
var args = new List<string>(aargs);
var options = new Options();
while (args.Count > 1 && args[0].StartsWith("-", StringComparison.Ordinal)) {
switch(args[0]) {
case "--refactor":
options.Refactor = true;
break;
case "--include-code":
options.IncludeCode = true;
break;
default:
throw new Exception("Syntax error, unknown option '" + args[0] + "'.");
}
args.RemoveAt(0);
}
if (args.Count < 1) {
CmdHelp();
return;
}
int p = 0;
while (p < args.Count) {
switch(args[p]) {
case "help":
CmdHelp();
p += 1;
break;
case "export":
if (args.Count - p < 3)
throw new Exception("Syntax error, missing argument.");
CmdExport(options, args[p + 1], args[p + 2]);
p += 3;
break;
case "compare-report":
if (args.Count - p < 4)
throw new Exception("Syntax error, missing argument.");
CmdCompareReport(options, args[p + 1], args[p + 2], args[p + 3], false);
p += 4;
break;
case "compare-diff":
if (args.Count - p < 4)
throw new Exception("Syntax error, missing argument.");
CmdCompareReport(options, args[p + 1], args[p + 2], args[p + 3], true);
p += 4;
break;
case "schema-create":
if (args.Count - p < 3)
throw new Exception("Syntax error, missing argument.");
CmdSchemaCreate(options, args[p + 1], args[p + 2]);
p += 3;
break;
case "schema-update":
if (args.Count - p < 4)
throw new Exception("Syntax error, missing argument.");
CmdSchemaUpdate(options, args[p + 1], args[p + 2], args[p + 3]);
p += 4;
break;
case "code-update":
if (args.Count - p < 4)
throw new Exception("Syntax error, missing argument.");
CmdCodeUpdate(options, args[p + 1], args[p + 2], args[p + 3]);
p += 4;
break;
case "code-grep":
if (args.Count - p < 3)
throw new Exception("Syntax error, missing argument.");
CmdCodeGrep(options, args[p + 1], args[p + 2]);
p += 3;
break;
default:
throw new Exception("Syntax error, unknown command.");
}
}
}
catch(Exception err) {
while (err != null) {
System.Diagnostics.Debug.WriteLine("ERROR: " + err.Message);
Console.WriteLine("ERROR: " + err.Message);
err = err.InnerException;
}
}
}
static void CmdHelp()
{
Console.WriteLine("DBTools <cmd> <args>...");
Console.WriteLine(" export sql:<server-name>\\<database-name> <outputfile-name>");
Console.WriteLine(" Export the database schema and sql-code to xml file.");
Console.WriteLine("");
Console.WriteLine(" compare-report <cur-source> <new-source> <outputfile-name>");
Console.WriteLine(" Compare 2 version and generate a report with the changes.");
Console.WriteLine("");
Console.WriteLine(" compare-diff <cur-source> <new-source> <outputfile-name>");
Console.WriteLine(" Compare 2 version and generate a diff with the changes.");
Console.WriteLine("");
Console.WriteLine(" schema-create <cur-source> <outputfile-name>");
Console.WriteLine(" Create sql script for creating a empty new database.");
Console.WriteLine("");
Console.WriteLine(" schema-update <cur-source> <new-source> <outputfile-name>");
Console.WriteLine(" Compare the current schema with a new schema and generate a sql");
Console.WriteLine(" script to convert database schema to new schema.");
Console.WriteLine(" PS: The datacopy code needs manual editing.");
Console.WriteLine("");
Console.WriteLine(" code-update <cur-source> <new-source> <outputfile-name>");
Console.WriteLine(" Compare the current sql-code with the new sql-code and generate");
Console.WriteLine(" a sql script to update the sql-code");
Console.WriteLine("");
Console.WriteLine(" code-grep <source> <regex>");
Console.WriteLine(" Print sql-code lines that contain a match pattern.");
Console.WriteLine("");
Console.WriteLine(" <source>");
Console.WriteLine(" sql:<server-name>\\<database-name>");
Console.WriteLine(" file:<filename-name>");
}
static void CmdExport(Options options, string databaseSource, string outputFileName)
{
if (!databaseSource.StartsWith("sql:", StringComparison.Ordinal))
throw new Exception("Syntax error, invalid database source.");
DBSchemaDatabase.ExportToFile(options, databaseSource.Substring(4), outputFileName);
}
static void CmdCompareReport(Options options, string curSchemaName, string newSchemaName, string outputFileName, bool includediff)
{
DBSchemaCompare compare = new DBSchemaCompare(options);
compare.CurSchema.LoadFrom(curSchemaName);
compare.NewSchema.LoadFrom(newSchemaName);
compare.Report(outputFileName, includediff);
}
static void CmdSchemaCreate(Options options, string schemaName, string outputFileName)
{
DBSchemaCompare compare = new DBSchemaCompare(options);
compare.NewSchema.LoadFrom(schemaName);
compare.SchemaUpdate(outputFileName, true);
}
static void CmdSchemaUpdate(Options options, string curSchemaName, string newSchemaName, string outputFileName)
{
DBSchemaCompare compare = new DBSchemaCompare(options);
compare.CurSchema.LoadFrom(curSchemaName);
compare.NewSchema.LoadFrom(newSchemaName);
compare.SchemaUpdate(outputFileName, false);
}
static void CmdCodeUpdate(Options options, string curSchemaName, string newSchemaName, string outputFileName)
{
options.IncludeCode = true;
DBSchemaCompare compare = new DBSchemaCompare(options);
compare.CurSchema.LoadFrom(curSchemaName);
compare.NewSchema.LoadFrom(newSchemaName);
compare.CodeUpdate(outputFileName);
}
static void CmdCodeGrep(Options options, string schemaName, string regex)
{
options.IncludeCode = true;
DBSchemaDatabase schema = new DBSchemaDatabase(options);
schema.LoadFrom(schemaName);
schema.CodeGrep(regex);
}
}
}