-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPocosForDapper-Extensions.zeus
376 lines (312 loc) · 7.75 KB
/
PocosForDapper-Extensions.zeus
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
##|TYPE Template
##|UNIQUEID 53221e98-7c7c-4e97-a6d9-bafc9abf920a
##|TITLE POCOsForDapper-Extensions
##|NAMESPACE
##|SOURCE_TYPE Source
##|OUTPUT_LANGUAGE C#
##|GUI_ENGINE .Net Script
##|GUI_LANGUAGE C#
##|GUI_BEGIN
public class GeneratedGui : DotNetScriptGui
{
public GeneratedGui(ZeusContext context) : base(context)
{
DnpUtils.ReadInputFromCache(context);
}
//Listbox of tables
private GuiListBox lstTable;
//-----------------------------------------
// The User Interface Entry Point
//-----------------------------------------
public override void Setup()
{
ui.Width = 400;
ui.Height = 500;
//Output path of genarated files.
string sOutputPath = "";
if(input.Contains("defaultOutputPath"))
{
sOutputPath = (string)input["defaultOutputPath"];
}
ui.AddLabel("lblPath", "Output file path: ", "Select the output path.");
ui.AddTextBox("txtPath", sOutputPath, "Select the Output Path.");
ui.AddFilePicker("btnPath", "Select Path", "Select the Output Path.", "txtPath", true);
//Combobox for selecting a database.
ui.AddLabel("lblDatabases", "Select a database:", "Select a database in the dropdown below.");
GuiComboBox cmbDatabases = ui.AddComboBox("cmbDatabase", "Select a database.");
//Listbox for selecting a table.
ui.AddLabel("lblTables", "Select a table", "Select a table.");
lstTable = ui.AddListBox("lstTables", "Table");
lstTable.Height = 200;
ui.AddLabel("lblNamespace", "NameSpace", "Write the namespace of genareted class.");
string nameSpaceCache = (string)input["txtNameSpace"];
ui.AddTextBox("txtNameSpace", nameSpaceCache, "Write the namespace of genareted class.");
if(MyMeta.DefaultDatabase != null)
{
//Set Initial Values
cmbDatabases.BindData(MyMeta.Databases);
cmbDatabases.SelectedValue = MyMeta.DefaultDatabase.Name;
BindTables(cmbDatabases.SelectedValue);
}
//Set EventHandler
cmbDatabases.AttachEvent("onchange", "cmbDatabases_onchange");
ui.ShowGui = true;
}
private void BindTables(string databaseName)
{
var db = MyMeta.Databases[databaseName];
lstTable.BindData(db.Tables);
}
private void cmbDatabases_onchange(GuiControl control)
{
GuiComboBox cmbDatabases = (GuiComboBox)ui["cmbDatabase"];
BindTables(cmbDatabases.SelectedText);
}
}
##|GUI_END
##|BODY_MODE Markup
##|BODY_ENGINE .Net Script
##|BODY_LANGUAGE C#
##|BODY_TAG_START <%
##|BODY_TAG_END %>
##|BODY_BEGIN
<%
public class GeneratedTemplate : DotNetScriptTemplate
{
public GeneratedTemplate(ZeusContext context) : base(context) {}
//---------------------------------------------------
// Render() is where you want to write your logic
//---------------------------------------------------
public override void Render()
{
//System.Diagnostics.Debugger.Launch();
//System.Diagnostics.Debugger.Break();
if(context.Objects.ContainsKey("DnpUtils"))
{
DnpUtils.SaveInputToCache(context);
}
string buffer = "";
//Output path
string strFilenameBase = (string)input["txtPath"];
if(!strFilenameBase.EndsWith("\\"))
{
strFilenameBase += "\\";
}
//NameSpace
string settedNameSpace = (string)input["txtNameSpace"];
//Base Class
string settedBaseClass = (string)input["txtBaseClass"];
//Database
string databaseName = (string)input["cmbDatabase"];
IDatabase database = MyMeta.Databases[databaseName];
ArrayList tableNames = (ArrayList)input["lstTables"];
foreach(string tblName in tableNames)
{
ITable dbTable = (ITable)database.Tables[tblName];
//Create [tablename].cs
%>
using System;
<%
if(!string.IsNullOrEmpty(settedNameSpace))
{
output.writeln("namespace " + settedNameSpace);
output.writeln("{");
}
if(!string.IsNullOrEmpty(dbTable.Description))
{
output.writeln("\t/// <summary>");
output.write("\t/// ");
output.writeln(dbTable.Description);
output.writeln("\t/// </summary>");
}
%> public class <%= TableToClassName(dbTable) %>
: ModelBase
{
public <%= TableToClassName(dbTable) %>()
{
}
public override string GetTableName()
{
return "<%= dbTable.Alias %>";
}
<%
//Fields from database colmns
foreach(IColumn dbColumn in dbTable.Columns)
{
if(!string.IsNullOrEmpty(dbColumn.Description))
{
output.writeln("\t\t\t/// <summary>");
output.write("\t\t\t/// ");
output.writeln(dbColumn.Description);
output.writeln("\t\t\t/// </summary>");
}
%>
public <%= ColumnToCSharpType(dbColumn) %> <%= ColumnToFieldName(dbColumn) %>
{
set;
get;
}
<%
} //end of foreach dbTable.Columns
%>
} <% //end of POCO class %>
public class <%= ToPascalCase(dbTable.Alias) + "Mapper"%>
: DapperExtensions.Mapper.ClassMapper<<%= TableToClassName(dbTable) %>>
{
public <%= ToPascalCase(dbTable.Alias) + "Mapper"%>()<% //constantor %>
{
base.Table("<%=dbTable.Alias%>");
<%
foreach(IColumn dbColumn in dbTable.Columns)
{
output.write("\t\t\t");
output.writeln(ToColumnMap(dbColumn));
}
%> }<%// end of contractor %>
}<%// end of MapperClass %>
<%
if(!string.IsNullOrEmpty(settedNameSpace))
{
output.writeln("}"); //end of namespace
}
//Save the output file for this Table
string filename = strFilenameBase + dbTable.Alias + ".cs";
output.save(filename, "d");
buffer += output.text;
output.clear();
} //end of foreach tableNames
if(tableNames.Count > 1)
{
output.write("// Files created in the output directory");
}
else
{
output.write(buffer);
}
}
private string TableToClassName(ITable tbl)
{
string result = tbl.Alias;
return result;
}
private string ColumnToFieldName(IColumn col)
{
string result = col.Alias;
return result;
}
private string ToColumnMap(IColumn col)
{
string result =
"Map(f => f." + ColumnToFieldName(col) + ").Column(\"" + col.Alias + "\")";
if(col.IsInPrimaryKey)
{
result += ".Key(DapperExtensions.Mapper.KeyType.Assigned)";
}
result += ";";
return result;
}
private string ToPascalCase(string src)
{
string[] parts = src.ToLower().Split('_');
string result = "";
foreach(string item in parts)
{
if(item.Length > 0)
{
result += char.ToUpper(item[0]);
if(item.Length > 1)
{
result += item.Substring(1);
}
}
}
return result;
}
private string ColumnToCSharpType(IColumn col)
{
string src = col.LanguageType;
if(!col.IsNullable)
{
return src;
}
if(src.EndsWith("[]"))
{
//Array
return src;
}
string langType;
switch(src)
{
case "bool":
langType = "System.Boolean";
break;
case "byte":
langType = "System.Byte";
break;
case "sbyte":
langType = "System.SByte";
break;
case "char":
langType = "System.Char";
break;
case "decimal":
langType = "System.Decimal";
break;
case "double":
langType = "System.Double";
break;
case "float":
langType = "System.Single";
break;
case "int":
langType = "System.Int32";
break;
case "uint":
langType = "System.UInt32";
break;
case "long":
langType = "System.Int64";
break;
case "ulong":
langType = "System.UInt64";
break;
case "object":
langType = "System.Object";
break;
case "short":
langType = "System.Int16";
break;
case "ushort":
langType = "System.UInt16";
break;
case "string":
langType = "System.String";
break;
case "DateTime":
langType = "System.DateTime";
break;
default:
if(src.IndexOf('.') == -1)
{
//in case full name is not given
langType = "System." + src;
}
else
{
langType = src;
}
break;
}
Type t = Type.GetType(langType);
if(t.IsValueType)
{
return src + "?";
}
else
{
return src;
}
}
}
%>
##|BODY_END