Skip to content

Commit f053637

Browse files
author
lianggx
committed
重构插入数据的逻辑
重构删除数据的逻辑 增加 Schema ,移除 insertCmdText 和 deleteCmdText ,使代码更简洁,增加批量插入数据的方法 InsertRange,优化了代码生成
1 parent dc1b302 commit f053637

File tree

21 files changed

+614
-263
lines changed

21 files changed

+614
-263
lines changed

.vs/MyStaging/v15/.suo

19 KB
Binary file not shown.

MyStaging.App/DAL/TablesDal.cs

Lines changed: 83 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class TablesDal
1717
#region identity
1818
private string projectName = string.Empty;
1919
private string modelpath = string.Empty;
20+
private string schemaPath = string.Empty;
2021
private string dalpath = string.Empty;
2122
private string schemaName = string.Empty;
2223
private TableViewModel table = null;
@@ -25,10 +26,11 @@ public class TablesDal
2526
private List<ConstraintInfo> consList = new List<ConstraintInfo>();
2627
#endregion
2728

28-
public TablesDal(string projectName, string modelpath, string dalpath, string schemaName, TableViewModel table)
29+
public TablesDal(string projectName, string modelpath, string schemaPath, string dalpath, string schemaName, TableViewModel table)
2930
{
3031
this.projectName = projectName;
3132
this.modelpath = modelpath;
33+
this.schemaPath = schemaPath;
3234
this.dalpath = dalpath;
3335
this.schemaName = schemaName;
3436
this.table = table;
@@ -37,10 +39,16 @@ public TablesDal(string projectName, string modelpath, string dalpath, string sc
3739
Get_Constraint();
3840
}
3941

40-
public void Generate()
42+
public void Create()
4143
{
42-
string _classname = CreateName() + "Model";
44+
CreateModel();
45+
CreateSchema();
46+
CreateDal();
47+
}
4348

49+
public void CreateModel()
50+
{
51+
string _classname = CreateName() + "Model";
4452
string _fileName = $"{modelpath}/{_classname}.cs";
4553
using (StreamWriter writer = new StreamWriter(File.Create(_fileName), System.Text.Encoding.UTF8))
4654
{
@@ -109,8 +117,56 @@ public void Generate()
109117
writer.WriteLine("\t}");
110118
writer.WriteLine("}");
111119
writer.Flush();
120+
}
121+
}
122+
123+
protected void CreateSchema()
124+
{
125+
string className = CreateName();
126+
string modelName = className + "Model";
127+
string schemaName = className + "Schema";
112128

113-
CreateDal();
129+
string _fileName = $"{schemaPath}/{schemaName}.cs";
130+
using (StreamWriter writer = new StreamWriter(File.Create(_fileName), System.Text.Encoding.UTF8))
131+
{
132+
writer.WriteLine("using MyStaging.Common;");
133+
writer.WriteLine("using MyStaging.Helpers;");
134+
writer.WriteLine("using MyStaging.Schemas;");
135+
writer.WriteLine("using NpgsqlTypes;");
136+
writer.WriteLine("using System.Collections.Generic;");
137+
writer.WriteLine("using System.Reflection;");
138+
writer.WriteLine();
139+
writer.WriteLine($"namespace {projectName}.Model.Schemas");
140+
writer.WriteLine("{");
141+
writer.WriteLine($"\tpublic partial class {schemaName} : ISchemaModel");
142+
writer.WriteLine("\t{");
143+
writer.WriteLine($"\t\tpublic static {schemaName} Instance => new {schemaName}();");
144+
writer.WriteLine();
145+
writer.WriteLine($"\t\tprivate static Dictionary<string, SchemaModel> schemas {{ get; }}");
146+
writer.WriteLine();
147+
writer.WriteLine($"\t\tpublic Dictionary<string, SchemaModel> SchemaSet => schemas;");
148+
writer.WriteLine();
149+
writer.WriteLine($"\t\tprivate static List<PropertyInfo> properties;");
150+
writer.WriteLine();
151+
writer.WriteLine($"\t\tpublic List<PropertyInfo> Properties => properties;");
152+
writer.WriteLine();
153+
writer.WriteLine($"\t\tstatic {schemaName}()");
154+
writer.WriteLine("\t\t{");
155+
writer.WriteLine("\t\t\tschemas = new Dictionary<string, SchemaModel>");
156+
writer.WriteLine("\t\t\t{");
157+
for (int i = 0; i < fieldList.Count; i++)
158+
{
159+
var fi = fieldList[i];
160+
string specificType = GetspecificType(fi);
161+
string ap = fi.Is_array ? " | NpgsqlDbType.Array" : "";
162+
var line = $"{{\"{fi.Field}\",new SchemaModel{{ FieldName=\"{fi.Field}\", DbType= NpgsqlDbType.{fi.PgDbType}{ap}, Size={fi.Length}, SpecificType={specificType} }} }}";
163+
writer.WriteLine("\t\t\t\t" + line + (i + 1 == fieldList.Count ? "" : ","));
164+
}
165+
writer.WriteLine("\t\t\t};");
166+
writer.WriteLine($"\t\t\tproperties = ContractUtils.GetProperties(typeof({modelName}));");
167+
writer.WriteLine("\t\t}");
168+
writer.WriteLine("\t}");
169+
writer.WriteLine("}");
114170
}
115171
}
116172

@@ -128,6 +184,7 @@ private string CreateName(string schema, string tableName, string separator = ""
128184

129185
return _classname;
130186
}
187+
131188
private string CreateName()
132189
{
133190
return CreateName(this.schemaName, this.table.name);
@@ -149,45 +206,16 @@ protected void CreateDal()
149206
writer.WriteLine("using MyStaging.Common;");
150207
writer.WriteLine("using NpgsqlTypes;");
151208
writer.WriteLine("using System.Linq.Expressions;");
209+
writer.WriteLine("using System.Collections.Generic;");
152210
writer.WriteLine($"using {projectName}.Model;");
211+
writer.WriteLine($"using {projectName}.Model.Schemas;");
153212
writer.WriteLine();
154213
writer.WriteLine($"namespace {projectName}.DAL");
155214
writer.WriteLine("{");
156215
writer.WriteLine($"\tpublic partial class {_classname} : QueryContext<{_model_classname}>");
157216
writer.WriteLine("\t{");
158217

159-
StringBuilder sb_field = new StringBuilder();
160-
StringBuilder sb_param = new StringBuilder();
161-
162-
for (int i = 0; i < fieldList.Count; i++)
163-
{
164-
var item = fieldList[i];
165-
if (item.Is_identity) continue;
166-
sb_field.Append($"\\\"{item.Field}\\\"");
167-
sb_param.Append($"@{item.Field}");
168-
if (fieldList.Count > i + 1)
169-
{
170-
sb_field.Append(",");
171-
sb_param.Append(",");
172-
}
173-
}
174-
175-
string insert_sql = $"INSERT INTO \\\"{this.schemaName}\\\".\\\"{this.table.name}\\\"({sb_field.ToString()}) VALUES({sb_param.ToString()}) RETURNING {sb_field.ToString()};";
176-
writer.WriteLine($"\t\tconst string insertCmdText = \"{insert_sql}\";");
177-
178-
StringBuilder sb_primarykey = new StringBuilder();
179-
for (int i = 0; i < pkList.Count; i++)
180-
{
181-
var item = pkList[i];
182-
sb_primarykey.Append($"\\\"{item.Field}\\\"=@{item.Field}");
183-
if (pkList.Count > i + 1)
184-
sb_primarykey.Append(" and ");
185-
}
186-
string pkString = sb_primarykey.ToString();
187-
188-
writer.WriteLine($"\t\tconst string deleteCmdText = \"DELETE FROM \\\"{this.schemaName}\\\".\\\"{this.table.name}\\\" WHERE {pkString}\";");
189218
writer.WriteLine($"\t\tpublic static {_classname} Context {{ get {{ return new {_classname}(); }} }}");
190-
writer.WriteLine();
191219

192220
foreach (var item in fieldList)
193221
{
@@ -207,7 +235,7 @@ protected void CreateDal()
207235
if (this.table.type == "table")
208236
{
209237
writer.WriteLine();
210-
Insert_Generator(writer, fieldList, _model_classname, _classname);
238+
Insert_Generator(writer, _model_classname, _classname);
211239
writer.WriteLine();
212240
Delete_Generator(writer, _model_classname, _classname);
213241
writer.WriteLine();
@@ -220,22 +248,11 @@ protected void CreateDal()
220248
}
221249
}
222250

223-
protected void Insert_Generator(StreamWriter writer, List<FieldInfo> fieldList, string class_model, string className)
251+
protected void Insert_Generator(StreamWriter writer, string class_model, string className)
224252
{
225-
writer.WriteLine($"\t\tpublic static {class_model} Insert({class_model} model)");
226-
writer.WriteLine("\t\t{");
227-
string _cn = className.ToLower();
228-
writer.WriteLine($"\t\t\t{className} {_cn} = Context;");
229-
foreach (var item in fieldList)
230-
{
231-
if (item.Is_identity) continue;
232-
string specificType = GetspecificType(item);
233-
string ap = item.Is_array ? " | NpgsqlDbType.Array" : "";
234-
writer.WriteLine($"\t\t\t{_cn}.AddParameter(\"{ item.Field}\", NpgsqlDbType.{item.PgDbType}{ap}, model.{item.Field.ToUpperPascal()}, {item.Length}, {specificType});");
235-
}
236-
writer.WriteLine();
237-
writer.WriteLine($"\t\t\treturn {_cn}.InsertOnReader(insertCmdText);");
238-
writer.WriteLine("\t\t}");
253+
writer.WriteLine($"\t\tpublic static InsertBuilder<{class_model}> InsertBuilder => new InsertBuilder<{class_model}>({className}Schema.Instance);");
254+
writer.WriteLine($"\t\tpublic static {class_model} Insert({class_model} model) => InsertBuilder.Insert(model);");
255+
writer.WriteLine($"\t\tpublic static int InsertRange(List<{class_model}> models) => InsertBuilder.InsertRange(models).SaveChange();");
239256
}
240257

241258
protected void Update_Generator(StreamWriter writer, string class_model, string dal_name)
@@ -248,7 +265,10 @@ protected void Update_Generator(StreamWriter writer, string class_model, string
248265
d_key.Add(fs.RelType + " " + fs.Field);
249266
d_key_fields.Add(fs.Field);
250267
}
268+
251269
string updateName = CreateName() + "UpdateBuilder";
270+
writer.WriteLine($"\t\tpublic static {updateName} UpdateBuilder => new {updateName}();");
271+
252272
if (d_key.Count > 0)
253273
{
254274
writer.WriteLine($"\t\tpublic static {updateName} Update({string.Join(",", d_key)})");
@@ -258,9 +278,6 @@ protected void Update_Generator(StreamWriter writer, string class_model, string
258278
writer.WriteLine();
259279
}
260280

261-
writer.WriteLine($"\t\tpublic static {updateName} UpdateBuilder {{ get {{ return new {updateName}(); }} }}");
262-
writer.WriteLine();
263-
264281
string dkString = d_key.Count > 0 ? $"{ string.Join(",", d_key)}" : "";
265282
var modelUpper = class_model.ToUpperPascal();
266283
writer.WriteLine($"\t\tpublic class {updateName} : UpdateBuilder<{modelUpper}>");
@@ -318,25 +335,27 @@ void CreateConstructor(string paramString, string onChange = null)
318335
foreach (var item in fieldList)
319336
{
320337
if (item.Is_identity) continue;
321-
NpgsqlDbType _dbtype = PgsqlType.SwitchToSql(item.Data_Type, item.Db_type);
322338

323339
writer.WriteLine($"\t\t\tpublic {updateName} Set{item.Field.ToUpperPascal()}({item.RelType} {item.Field})");
324340
writer.WriteLine("\t\t\t{");
325341
string specificType = GetspecificType(item);
326342
string ap = item.Is_array ? " | NpgsqlDbType.Array" : "";
327-
writer.WriteLine($"\t\t\t\treturn base.SetField(\"{ item.Field}\", NpgsqlDbType.{_dbtype}{ap}, {item.Field}, {item.Length}, {specificType}) as {updateName};");
343+
writer.WriteLine($"\t\t\t\tbase.SetField(\"{ item.Field}\", NpgsqlDbType.{item.PgDbType}{ap}, {item.Field}, {item.Length}, {specificType});");
344+
writer.WriteLine($"\t\t\t\treturn this;");
328345
writer.WriteLine("\t\t\t}");
329346

330347
if (item.Is_array)
331348
{
332349
writer.WriteLine($"\t\t\tpublic {updateName} Set{item.Field.ToUpperPascal()}Append({item.CsType} {item.Field})");
333350
writer.WriteLine("\t\t\t{");
334-
writer.WriteLine($"\t\t\t\treturn base.SetArrayAppend(\"{ item.Field}\", NpgsqlDbType.{_dbtype}, {item.Field}, {item.Length}, {specificType}) as {updateName};");
351+
writer.WriteLine($"\t\t\t\tbase.SetArrayAppend(\"{ item.Field}\", NpgsqlDbType.{item.PgDbType}, {item.Field}, {item.Length}, {specificType});");
352+
writer.WriteLine($"\t\t\t\treturn this;");
335353
writer.WriteLine("\t\t\t}");
336354

337355
writer.WriteLine($"\t\t\tpublic {updateName} Set{item.Field.ToUpperPascal()}Remove({item.CsType} {item.Field})");
338356
writer.WriteLine("\t\t\t{");
339-
writer.WriteLine($"\t\t\t\treturn base.SetArrayRemove(\"{ item.Field}\", NpgsqlDbType.{_dbtype}, {item.Field}, {item.Length}, {specificType}) as {updateName};");
357+
writer.WriteLine($"\t\t\t\tbase.SetArrayRemove(\"{ item.Field}\", NpgsqlDbType.{item.PgDbType}, {item.Field}, {item.Length}, {specificType});");
358+
writer.WriteLine($"\t\t\t\treturn this;");
340359
writer.WriteLine("\t\t\t}");
341360
}
342361
writer.WriteLine();
@@ -346,33 +365,22 @@ void CreateConstructor(string paramString, string onChange = null)
346365

347366
protected void Delete_Generator(StreamWriter writer, string class_model, string className)
348367
{
368+
string deletebuilder = $"DeleteBuilder<{class_model}>";
369+
writer.WriteLine($"\t\tpublic static {deletebuilder} DeleteBuilder => new {deletebuilder}();");
370+
349371
if (pkList.Count > 0)
350372
{
351373
List<string> d_key = new List<string>();
374+
List<string> d_key_param = new List<string>();
352375
foreach (var item in pkList)
353376
{
354377
FieldInfo fs = fieldList.FirstOrDefault(f => f.Field == item.Field);
355378
d_key.Add(fs.RelType + " " + fs.Field);
379+
d_key_param.Add("f." + fs.Field.ToUpperPascal() + " == " + fs.Field);
356380
}
357381

358-
writer.WriteLine($"\t\tpublic static int Delete({string.Join(",", d_key)})");
359-
writer.WriteLine("\t\t{");
360-
string _cn = className.ToLower();
361-
writer.WriteLine($"\t\t\t{className} {_cn} = Context;");
362-
int len = pkList.Count;
363-
foreach (var item in pkList)
364-
{
365-
FieldInfo fi = fieldList.FirstOrDefault(f => f.Field == item.Field);
366-
string specificType = GetspecificType(fi);
367-
string ap = fi.Is_array ? " | NpgsqlDbType.Array" : "";
368-
writer.WriteLine($"\t\t\t{_cn}.AddParameter(\"{ item.Field}\", NpgsqlDbType.{fi.PgDbType}{ap}, {item.Field}, {fi.Length}, {specificType});");
369-
}
370-
writer.WriteLine($"\t\t\treturn {_cn}.ExecuteNonQuery(deleteCmdText);");
371-
writer.WriteLine("\t\t}");
372-
writer.WriteLine();
382+
writer.WriteLine($"\t\tpublic static int Delete({string.Join(",", d_key)}) => DeleteBuilder.Where(f => {string.Join(" && ", d_key_param)}).SaveChange();");
373383
}
374-
string deletebuilder = $"DeleteBuilder<{class_model}>";
375-
writer.WriteLine($"\t\tpublic static {deletebuilder} DeleteBuilder {{ get {{ return new {deletebuilder}(); }} }}");
376384
}
377385

378386
#region primary key / constraint

MyStaging.App/GeneralFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace MyStaging.App
1010
{
1111
public class GeneralFactory
1212
{
13+
private static string schemaPath = string.Empty;
1314
private static string modelPath = string.Empty;
1415
private static string dalPath = string.Empty;
1516
private static string projectName = string.Empty;
@@ -34,15 +35,16 @@ public static void Build(string outputdir, string projName)
3435
foreach (var item in tableList)
3536
{
3637
Console.WriteLine("{0}:{1}", item.type, item.name);
37-
TablesDal td = new TablesDal(GeneralFactory.projectName, modelPath, dalPath, schemaName, item);
38-
td.Generate();
38+
TablesDal td = new TablesDal(GeneralFactory.projectName, modelPath, schemaPath, dalPath, schemaName, item);
39+
td.Create();
3940
}
4041
}
4142
}
4243

4344
private static void CreateDir()
4445
{
4546
modelPath = Path.Combine(outputDir, projectName + ".db", "Model", "Build");
47+
schemaPath = Path.Combine(outputDir, projectName + ".db", "Model", "Schemas");
4648
dalPath = Path.Combine(outputDir, projectName + ".db", "DAL", "Build");
4749
string[] ps = { modelPath, dalPath };
4850
for (int i = 0; i < ps.Length; i++)

MyStaging.App/MyStaging.App.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<AssemblyName>MyStaging.App</AssemblyName>
77
<ApplicationIcon />
88
<StartupObject></StartupObject>
9+
<Version>1.0.1</Version>
910
</PropertyGroup>
1011

1112
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

MyStaging.App/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ static void Main(string[] args)
4848
}
4949
//outPutPath = @"D:\TestProjects\mystaging";
5050
//projName = "MyStaging.xUnitTest";
51-
//PgSqlHelper.InitConnection(null, "Host=127.0.0.1;Port=5432;Username=postgres;Password=postgres;Database=mystaging;Pooling=true;Maximum Pool Size=100");
52-
PgSqlHelper.InitConnection(null, connection.ToString());
51+
// PgSqlHelper.InitConnection(null, "Host=127.0.0.1;Port=5432;Username=postgres;Password=postgres;Database=mystaging;Pooling=true;Maximum Pool Size=100");
52+
//PgSqlHelper.InitConnection(null, connection.ToString());
5353
GeneralFactory.Build(outPutPath, projName);
5454

5555
Console.WriteLine("success.....");

MyStaging.App/Test.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)