forked from yuvalsol/POCOGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
380 lines (293 loc) · 13.8 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
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
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using POCOGenerator;
using POCOGenerator.Objects;
namespace MultipleFilesDemo
{
class Program
{
static void Main()
{
IGenerator generator = GeneratorFactory.GetGenerator();
try { generator.Settings.Connection.ConnectionString = File.ReadAllText("ConnectionString.txt"); } catch { }
if (string.IsNullOrEmpty(generator.Settings.Connection.ConnectionString))
generator.Settings.Connection.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=AdventureWorks2014;Integrated Security=True";
generator.Settings.DatabaseObjects.IncludeAll = true;
generator.Settings.POCO.Using = true;
generator.Settings.POCO.Namespace = "MultipleFilesDemo";
generator.Settings.POCO.CommentsWithoutNull = true;
// wrap using & namespace around each class
generator.Settings.POCO.WrapAroundEachClass = true;
string root = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Console.WriteLine("Root Path:");
Console.WriteLine(root);
Console.WriteLine();
string path = root;
int indent = 0;
// key: schema, value: files count
OrderedDictionary filesCount = new OrderedDictionary(StringComparer.OrdinalIgnoreCase);
#region Server
// create server and namespace folders
generator.ServerGenerating += (object sender, ServerGeneratingEventArgs e) =>
{
// create server folder
string server = e.Server.ToString();
string folder = string.Join("_", server.Split(Path.GetInvalidFileNameChars()));
path = Path.Combine(path, folder);
if (Directory.Exists(path))
{
try { Directory.Delete(path, true); }
catch { }
}
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
Console.WriteLine("{0}", folder);
// create namespace folder
if (sender is IGenerator g)
{
string @namespace = g.Settings.POCO.Namespace;
if (string.IsNullOrEmpty(@namespace) == false)
{
folder = string.Join("_", @namespace.Split(Path.GetInvalidFileNameChars()));
path = Path.Combine(path, folder);
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
indent += INDENT_SIZE;
Console.WriteLine("{0}{1}", new string(' ', indent), folder);
}
}
};
#region Database
// create database folder
generator.DatabaseGenerating += (object sender, DatabaseGeneratingEventArgs e) =>
{
string database = e.Database.ToString();
string folder = string.Join("_", database.Split(Path.GetInvalidFileNameChars()));
path = Path.Combine(path, folder);
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
indent += INDENT_SIZE;
Console.WriteLine("{0}{1}", new string(' ', indent), folder);
};
#region Tables
// create Tables folder
generator.TablesGenerating += (object sender, TablesGeneratingEventArgs e) =>
{
DbGroupGenerating("Tables", ref path, ref indent, filesCount);
};
// get namespace for table
generator.TableGenerating += (object sender, TableGeneratingEventArgs e) =>
{
e.Namespace = GetNamespace(e.Namespace, e.Table.Database, "Tables", e.Table.Schema);
};
// save table poco
generator.TablePOCO += (object sender, TablePOCOEventArgs e) =>
{
DbObjectPOCO(e.ClassName, e.POCO, e.Table.Schema, path, filesCount);
};
// take the path one step up once all the tables are written
generator.TablesGenerated += (object sender, TablesGeneratedEventArgs e) =>
{
DbGroupGenerated(ref path, ref indent, filesCount);
};
#endregion
#region ComplexTypeTables
// create Tables folder for complex type tables
generator.ComplexTypeTablesGenerating += (object sender, ComplexTypeTablesGeneratingEventArgs e) =>
{
DbGroupGenerating("Tables", ref path, ref indent, filesCount);
};
// get namespace for complex type table
generator.ComplexTypeTableGenerating += (object sender, ComplexTypeTableGeneratingEventArgs e) =>
{
e.Namespace = GetNamespace(e.Namespace, e.ComplexTypeTable.Database, "Tables", e.ComplexTypeTable.Schema);
};
// save complex type table poco
generator.ComplexTypeTablePOCO += (object sender, ComplexTypeTablePOCOEventArgs e) =>
{
DbObjectPOCO(e.ClassName, e.POCO, e.ComplexTypeTable.Schema, path, filesCount);
};
// take the path one step up once all the complex type tables are written
generator.ComplexTypeTablesGenerated += (object sender, ComplexTypeTablesGeneratedEventArgs e) =>
{
DbGroupGenerated(ref path, ref indent, filesCount);
};
#endregion
#region Views
// create Views folder
generator.ViewsGenerating += (object sender, ViewsGeneratingEventArgs e) =>
{
DbGroupGenerating("Views", ref path, ref indent, filesCount);
};
// get namespace for view
generator.ViewGenerating += (object sender, ViewGeneratingEventArgs e) =>
{
e.Namespace = GetNamespace(e.Namespace, e.View.Database, "Views", e.View.Schema);
};
// save view poco
generator.ViewPOCO += (object sender, ViewPOCOEventArgs e) =>
{
DbObjectPOCO(e.ClassName, e.POCO, e.View.Schema, path, filesCount);
};
// take the path one step up once all the views are written
generator.ViewsGenerated += (object sender, ViewsGeneratedEventArgs e) =>
{
DbGroupGenerated(ref path, ref indent, filesCount);
};
#endregion
#region Procedures
// create Procedures folder
generator.ProceduresGenerating += (object sender, ProceduresGeneratingEventArgs e) =>
{
DbGroupGenerating("Procedures", ref path, ref indent, filesCount);
};
// get namespace for procedure
generator.ProcedureGenerating += (object sender, ProcedureGeneratingEventArgs e) =>
{
e.Namespace = GetNamespace(e.Namespace, e.Procedure.Database, "Procedures", e.Procedure.Schema);
};
// save procedure poco
generator.ProcedurePOCO += (object sender, ProcedurePOCOEventArgs e) =>
{
DbObjectPOCO(e.ClassName, e.POCO, e.Procedure.Schema, path, filesCount);
};
// take the path one step up once all the procedures are written
generator.ProceduresGenerated += (object sender, ProceduresGeneratedEventArgs e) =>
{
DbGroupGenerated(ref path, ref indent, filesCount);
};
#endregion
#region Functions
// create Functions folder
generator.FunctionsGenerating += (object sender, FunctionsGeneratingEventArgs e) =>
{
DbGroupGenerating("Functions", ref path, ref indent, filesCount);
};
// get namespace for function
generator.FunctionGenerating += (object sender, FunctionGeneratingEventArgs e) =>
{
e.Namespace = GetNamespace(e.Namespace, e.Function.Database, "Functions", e.Function.Schema);
};
// save function poco
generator.FunctionPOCO += (object sender, FunctionPOCOEventArgs e) =>
{
DbObjectPOCO(e.ClassName, e.POCO, e.Function.Schema, path, filesCount);
};
// take the path one step up once all the functions are written
generator.FunctionsGenerated += (object sender, FunctionsGeneratedEventArgs e) =>
{
DbGroupGenerated(ref path, ref indent, filesCount);
};
#endregion
#region TVPs
// create TVPs folder
generator.TVPsGenerating += (object sender, TVPsGeneratingEventArgs e) =>
{
DbGroupGenerating("TVPs", ref path, ref indent, filesCount);
};
// get namespace for tvp
generator.TVPGenerating += (object sender, TVPGeneratingEventArgs e) =>
{
e.Namespace = GetNamespace(e.Namespace, e.TVP.Database, "TVPs", e.TVP.Schema);
};
// save tvp poco
generator.TVPPOCO += (object sender, TVPPOCOEventArgs e) =>
{
DbObjectPOCO(e.ClassName, e.POCO, e.TVP.Schema, path, filesCount);
};
// take the path one step up once all the tvps are written
generator.TVPsGenerated += (object sender, TVPsGeneratedEventArgs e) =>
{
DbGroupGenerated(ref path, ref indent, filesCount);
};
#endregion
// take the path one step up once the database is written
generator.DatabaseGenerated += (object sender, DatabaseGeneratedEventArgs e) =>
{
path = Path.GetDirectoryName(path);
indent -= INDENT_SIZE;
};
#endregion
// take the path one step up once the server is written
generator.ServerGenerated += (object sender, ServerGeneratedEventArgs e) =>
{
path = Path.GetDirectoryName(path);
};
#endregion
GeneratorResults results = generator.Generate();
PrintError(results, generator.Error);
Console.WriteLine();
Console.WriteLine("Press any key to continue . . .");
Console.ReadKey(true);
}
private static void PrintError(GeneratorResults results, Exception Error)
{
bool isError = (results & GeneratorResults.Error) == GeneratorResults.Error;
bool isWarning = (results & GeneratorResults.Warning) == GeneratorResults.Warning;
if (results != GeneratorResults.None)
Console.WriteLine();
if (isError)
Console.WriteLine("Error Result: {0}", results);
else if (isWarning)
Console.WriteLine("Warning Result: {0}", results);
if (Error != null)
{
Console.WriteLine("Error: {0}", Error.Message);
Console.WriteLine("Error Stack Trace:");
Console.WriteLine(Error.StackTrace);
}
}
private const int INDENT_SIZE = 4;
private static void DbGroupGenerating(string dbGroup, ref string path, ref int indent, OrderedDictionary filesCount)
{
path = Path.Combine(path, dbGroup);
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
indent += INDENT_SIZE;
Console.WriteLine("{0}{1}", new string(' ', indent), dbGroup);
filesCount.Clear();
}
private static string GetNamespace(string @namespace, Database database, string dbGroup, string schema)
{
string fullNamespace = string.Format("{0}.{1}", database, dbGroup);
if (string.IsNullOrEmpty(@namespace) == false)
fullNamespace = string.Format("{0}.{1}", @namespace, fullNamespace);
if (string.IsNullOrEmpty(schema) == false)
fullNamespace = string.Format("{0}.{1}", fullNamespace, schema);
return fullNamespace;
}
private static void DbObjectPOCO(string className, string poco, string schema, string path, OrderedDictionary filesCount)
{
schema = string.Join("_", (schema ?? string.Empty).Split(Path.GetInvalidFileNameChars()));
path = Path.Combine(path, schema);
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
string fileName = string.Join("_", className.Split(Path.GetInvalidFileNameChars())) + ".cs";
path = Path.Combine(path, fileName);
File.WriteAllText(path, poco);
if (filesCount.Contains(schema))
filesCount[schema] = (int)filesCount[schema] + 1;
else
filesCount.Add(schema, 1);
}
private static void DbGroupGenerated(ref string path, ref int indent, OrderedDictionary filesCount)
{
path = Path.GetDirectoryName(path);
if (filesCount.Count > 0)
{
indent += INDENT_SIZE;
foreach (DictionaryEntry item in filesCount)
{
string schema = (string)item.Key;
int count = (int)item.Value;
Console.WriteLine("{0}{1}: {2} {3}", new string(' ', indent), schema, count, (count == 1 ? "file" : "files"));
}
indent -= INDENT_SIZE;
}
indent -= INDENT_SIZE;
}
}
}