-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAL.cst
446 lines (378 loc) · 12.8 KB
/
DAL.cst
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
<%@ CodeTemplate Language="C#" TargetLanguage="C#" ResponseEncoding="UTF-8" Src="ToolsCodeTemplate.cs" Inherits="ToolsCodeTemplate"%>
<%@ Property Name="TargetTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="ModelsNamespace" Default="MyOffice.Models" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALNamespace" Default="PwbMVC.DAL" Type="System.String" Category="Context" Description="TargetTable that the object is based on." %>
<%@ Property Name="DALClassNameSurfix" Default="Service" Type="System.String" Category="Context" Description="类名称后缀" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<% PrintHeader(); %>
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using <%= ModelsNamespace %>;
namespace <%= DALNamespace %>
{
public partial class <%= GetDALClassName() %>
{
#region Insert
/// <summary>
/// 插入数据
/// </summary>
public bool Insert (<%= GetModelClassName() %> <%= GetModelParamName() %>)
{
<%if(IsIdentityPK())
{%>
string sql ="<%= GetAutoIncInsertSQLLine()%>";
MySqlParameter[] para = new MySqlParameter[]
{
<%
for(int i=0; i<TargetTable.NonPrimaryKeyColumns.Count; i++)
{
ColumnSchema column = TargetTable.NonPrimaryKeyColumns[i];
%>
new MySqlParameter("@<%= column.Name %>", ToDBValue(<%= GetModelParamName() %>.<%= column.Name %>)),
<%
}
%>
};
int affectRows = (int)MySqlDBHelper.ExecuteNonQuery(sql, para);
if(affectRows==1)
{
return true;
}else
{
return false;
}
<%}else
{%>
string sql ="<%= GetCommonInsertSQLLine()%>";
MySqlParameter[] para = new MySqlParameter[]
{
<%
for(int i=0; i<TargetTable.Columns.Count; i++)
{
ColumnSchema column = TargetTable.Columns[i];
%>
new MySqlParameter("@<%= column.Name %>", ToDBValue(<%= GetModelParamName() %>.<%= column.Name %>)),
<%
}
%>
};
int AddId = (int)MySqlDBHelper.ExecuteNonQuery(sql, para);
if(AddId==1)
{
return true;
}else
{
return false;
}
<%}%>
}
#endregion
#region Delete
/// <summary>
/// 根据主键删除数据
/// </summary>
public int DeleteBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetCamelPKName() %>)
{
string sql = "DELETE from <%= TargetTable.Database %>.<%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>";
MySqlParameter[] para = new MySqlParameter[]
{
new MySqlParameter("@<%= GetPKName() %>", <%= GetCamelPKName() %>)
};
return MySqlDBHelper.ExecuteNonQuery(sql, para);
}
#endregion
#region Update
/// <summary>
/// 根据主键更新数据
/// </summary>
public int Update(<%= GetModelClassName() %> <%= GetModelParamName() %>)
{
string sql ="UPDATE <%= TargetTable.Database %>.<%= TargetTable.Name %> " +"SET " +
" <%= TargetTable.NonPrimaryKeyColumns[0].Name %> = @<%= TargetTable.NonPrimaryKeyColumns[0].Name %>"
<%
for(int i=1; i<TargetTable.NonPrimaryKeyColumns.Count; i++)
{
ColumnSchema column = TargetTable.NonPrimaryKeyColumns[i];
%>
+", <%= column.Name %> = @<%= column.Name %>"
<%
}
%>
+" WHERE <%= GetPKName() %> = @<%= GetPKName() %>";
MySqlParameter[] para = new MySqlParameter[]
{
new MySqlParameter("@<%= GetPKName() %>", <%= GetModelParamName() %>.<%= GetPKName() %>)
<%
for(int i=0; i<TargetTable.NonPrimaryKeyColumns.Count; i++)
{
ColumnSchema column = TargetTable.NonPrimaryKeyColumns[i];
%>
,new MySqlParameter("@<%= column.Name %>", ToDBValue(<%= GetModelParamName() %>.<%= column.Name %>))
<%
}
%>
};
return MySqlDBHelper.ExecuteNonQuery(sql, para);
}
#endregion
#region select
/// <summary>
/// 根据主键查询数据
/// </summary>
public <%= GetModelClassName() %> GetBy<%= GetPKPropertyName() %>(<%= GetPKPropertyType() %> <%= GetCamelPKName() %>)
{
string sql = "SELECT * FROM <%= TargetTable.Database %>.<%= TargetTable.Name %> WHERE <%= GetPKPropertyName() %> = @<%= GetPKPropertyName() %>";
using(MySqlDataReader reader = MySqlDBHelper.ExecuteDataReader(sql, new MySqlParameter("@<%= GetPKPropertyName() %>", <%= GetCamelPKName() %>)))
{
if (reader.Read())
{
return ToModel(reader);
}
else
{
return null;
}
}
}
///<summary>
///根据字段名获取数据记录IEnumerable<>
///</summary>
public IEnumerable<<%= GetModelClassName() %>> GetBycolumnName(string columnName,string columnContent)
{
string sql = "SELECT * FROM <%= TargetTable.Database %>.<%= TargetTable.Name %> where "+columnName+"='"+ @columnContent + "'";
using(MySqlDataReader reader = MySqlDBHelper.ExecuteDataReader(sql,new MySqlParameter(columnName, columnContent)))
{
return ToModels(reader);
}
}
///<summary>
///根据字段名获取数据记录IEnumerable<>
///</summary>
public IEnumerable<<%= GetModelClassName() %>> GetBycolumnNames(string[] columnNames,string[] columnContents)
{
string sql = "SELECT * FROM <%= TargetTable.Database %>.<%= TargetTable.Name %> where ";
if (columnNames.Length!= columnContents.Length)
{
return null;
}
else
{
for (int i = 0; i < columnNames.Length; i++)
{
sql += columnNames[i] +"= '"+ columnContents[i] + "' and ";
}
sql = sql.Substring(0, sql.Length - 5);
}
using(MySqlDataReader reader = MySqlDBHelper.ExecuteDataReader(sql))
{
return ToModels(reader);
}
}
#endregion
/// <summary>
/// 把DataRow转换成Model
/// </summary>
public <%= GetModelClassName() %> ToModel(MySqlDataReader dr)
{
<%= GetModelClassName() %> <%= GetModelParamName() %> = new <%= GetModelClassName() %>();
<% foreach(ColumnSchema column in TargetTable.Columns) %>
<% { %>
<%= GetModelParamName() %>.<%= GetPropertyName(column) %> = (<%=GetPropertyType(column)%>)ToModelValue(dr,"<%=column.Name%>");
<% } %>
return <%= GetModelParamName() %>;
}
///<summary>
/// 获得总记录数
///</summary>
public int GetTotalCount()
{
string sql = "SELECT count(*) FROM <%= TargetTable.Database %>.<%= TargetTable.Name %>";
return (int)MySqlDBHelper.ExecuteScalar(sql);
}
#region 获得分页记录集IEnumerable<>
///<summary>
/// 获得分页记录集IEnumerable<>
///</summary>
public IEnumerable<<%= GetModelClassName() %>> GetPagedData(int minrownum,int maxrownum)
{
string sql = "SELECT * from(SELECT *,(row_number() over(order by <%=this.GetPKName()%>))-1 rownum FROM <%= TargetTable.Database %>.<%= TargetTable.Name %>) t where rownum>=@minrownum and rownum<=@maxrownum";
using(MySqlDataReader reader = MySqlDBHelper.ExecuteDataReader(sql,
new MySqlParameter("@minrownum",minrownum),
new MySqlParameter("@maxrownum",maxrownum)))
{
return ToModels(reader);
}
}
#endregion
///<summary>
/// 获得总记录集IEnumerable<>
///</summary>
public IEnumerable<<%= GetModelClassName() %>> GetAll()
{
string sql = "SELECT * FROM <%= TargetTable.Database %>.<%= TargetTable.Name %>";
using(MySqlDataReader reader = MySqlDBHelper.ExecuteDataReader(sql))
{
return ToModels(reader);
}
}
#region 把MySqlDataReader转换成IEnumerable<>
///<summary>
/// 把MySqlDataReader转换成IEnumerable<>
///</summary>
protected IEnumerable<<%= GetModelClassName() %>> ToModels(MySqlDataReader reader)
{
var list = new List<<%= GetModelClassName() %>>();
while(reader.Read())
{
list.Add(ToModel(reader));
}
return list;
}
#endregion
#region 判断数据是否为空
///<summary>
/// 判断数据是否为空
///</summary>
protected object ToDBValue(object value)
{
if(value==null)
{
return DBNull.Value;
}
else
{
return value;
}
}
#endregion
#region 判断数据表中是否包含该字段
///<summary>
/// 判断数据表中是否包含该字段
///</summary>
protected object ToModelValue(MySqlDataReader reader,string columnName)
{
if(reader.IsDBNull(reader.GetOrdinal(columnName)))
{
return null;
}
else
{
return reader[columnName];
}
}
#endregion
}
}
<script runat="template">
//CS_IsUnsigned 表示此列 是否是 无符号的,其ExtendedProperties["IsUnsigned"].Value是bool类型,True表示是无符号的否则是有符号的或无法设置为无符号(为我自己添加的)。
//CS_ColumnDefaultIsNull 某列的默认值是否是Null,True或False。
//CS_Default 某列的默认值。
//CS_Description 列的注释。
//CS_IsIdentity 是否是标识列。
//CS_IdentitySeed 是标识列的前提下该列的种子值(起始值)。
//CS_IdentityIncrement 标识列的前提下该列的自增长值。
//CS_ColumnDefault 和CS_Default一样,是某列的默认值
//CS_SystemType 和 CS_ColumnType一样,都是某列在数据库中的 具体类型(通过SQL语句写出的类型)
//CS_ColumnExtra 存储内容是什么暂时不知。
//数据表的ExtendedProperties:
//CS_ShowCreateTable 存储的是执行SQL语句:Show Create Table。。。后的第二部分内容
//CS_Description 该表的注释
/// <summary>
/// 是否为列
/// </summary>
/// <returns></returns>
public bool IsIdentityPK() {
foreach (ColumnSchema column in TargetTable.Columns) {
if ((bool) column.ExtendedProperties["CS_IsIdentity"].Value) {
return true;
}
}
return false;
}
public string GetDALClassName() {
return GetModelClassName() + DALClassNameSurfix;
}
// User
public string GetModelClassName() {
return GetModelClassName(TargetTable);
}
// user
public string GetModelMemberVarName() {
return GetModelParamName();
}
/// <summary>
/// 获取类名称的驼峰形式
/// </summary>
/// <returns></returns>
public string GetModelParamName() {
return ConvertToCamel(GetModelClassName());
}
/// <summary>
/// 获取插入SQL语句,不包括主键
/// </summary>
/// <returns></returns>
public string GetAutoIncInsertSQLLine() {
string result;
result = "INSERT INTO " +TargetTable.Database+"."+TargetTable.Name + " (";
foreach (ColumnSchema column in TargetTable.NonPrimaryKeyColumns) {
result += column.Name + ", ";
}
result = result.Substring(0, result.Length - 2);
result += ") ";
result += " VALUES (";
foreach (ColumnSchema column in TargetTable.NonPrimaryKeyColumns) {
result += "@" + column.Name + ", ";
}
result = result.Substring(0, result.Length - 2);
result += ")";
//result += ");select @@IDENTITY";
return result;
}
public string GetCommonInsertSQLLine() {
string result;
result = "INSERT INTO "+TargetTable.Database+"."+TargetTable.Name + " (";
foreach (ColumnSchema column in TargetTable.Columns) {
result += column.Name + ", ";
}
result = result.Substring(0, result.Length - 2);
result += ") ";
result += " VALUES (";
foreach (ColumnSchema column in TargetTable.Columns) {
result += "@" + column.Name + ", ";
}
result = result.Substring(0, result.Length - 2);
result += ")";
return result;
}
public string GetPKPropertyType() {
return GetPKType(TargetTable);
}
public string GetPKPropertyName() {
return ConvertToPascal(GetPKName());
}
/// <summary>
/// 获取主键驼峰名称
/// </summary>
/// <returns></returns>
public string GetCamelPKName() {
return ConvertToCamel(GetPKName());
}
// Id
public string GetPKName() {
return GetPKName(TargetTable);
}
/// <summary>
/// 获取输出文件名称.cs
/// </summary>
/// <returns></returns>
public override string GetFileName() {
return this.GetDALClassName() + ".cs";
}
</script>