Skip to content

Commit a37d161

Browse files
author
lianggx
committed
增加批量插入数据 InsertRange 的方法,优化反射执行过程
1 parent f053637 commit a37d161

File tree

22 files changed

+223
-224
lines changed

22 files changed

+223
-224
lines changed

.vs/MyStaging/v15/.suo

-12.5 KB
Binary file not shown.

MyStaging.App/DAL/EnumsDal.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static void Generate(string rootpath, string modelpath, string projName)
2929
{
3030
list.Add(new EnumTypeInfo()
3131
{
32-
oid = Convert.ToInt32(dr["oid"]),
33-
typename = dr["typname"].ToString(),
34-
nspname = dr["nspname"].ToString()
32+
Oid = Convert.ToInt32(dr["oid"]),
33+
TypeName = dr["typname"].ToString(),
34+
NspName = dr["nspname"].ToString()
3535
});
3636
}, System.Data.CommandType.Text, _sqltext);
3737

@@ -46,9 +46,9 @@ public static void Generate(string rootpath, string modelpath, string projName)
4646
for (int i = 0; i < list.Count; i++)
4747
{
4848
var item = list[i];
49-
writer.WriteLine($"\tpublic enum {item.typename.ToUpperPascal()}");
49+
writer.WriteLine($"\tpublic enum {item.TypeName.ToUpperPascal()}");
5050
writer.WriteLine("\t{");
51-
string sql = $"select oid,enumlabel from pg_enum WHERE enumtypid = {item.oid} ORDER BY oid asc";
51+
string sql = $"select oid,enumlabel from pg_enum WHERE enumtypid = {item.Oid} ORDER BY oid asc";
5252
PgSqlHelper.ExecuteDataReader(dr =>
5353
{
5454
string c = i < list.Count ? "," : "";
@@ -87,7 +87,7 @@ public static void GenerateMapping(List<EnumTypeInfo> list)
8787
writer.WriteLine("\t\t\tNpgsqlNameTranslator translator = new NpgsqlNameTranslator();");
8888
foreach (var item in list)
8989
{
90-
writer.WriteLine($"\t\t\tNpgsqlConnection.MapEnumGlobally<{item.typename.ToUpperPascal()}>(\"{item.nspname}.{item.typename}\", translator);");
90+
writer.WriteLine($"\t\t\tNpgsqlConnection.MapEnumGlobally<{item.TypeName.ToUpperPascal()}>(\"{item.NspName}.{item.TypeName}\", translator);");
9191
}
9292
}
9393

MyStaging.App/DAL/TablesDal.cs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ public TablesDal(string projectName, string modelpath, string schemaPath, string
3434
this.dalpath = dalpath;
3535
this.schemaName = schemaName;
3636
this.table = table;
37-
Get_Fields();
38-
Get_Primarykey(fieldList[0].Oid);
39-
Get_Constraint();
4037
}
4138

4239
public void Create()
4340
{
41+
Get_Fields();
42+
Get_Primarykey(fieldList[0].Oid);
43+
Get_Constraint();
44+
4445
CreateModel();
4546
CreateSchema();
4647
CreateDal();
@@ -62,7 +63,7 @@ public void CreateModel()
6263
writer.WriteLine();
6364
writer.WriteLine($"namespace {projectName}.Model");
6465
writer.WriteLine("{");
65-
writer.WriteLine($"\t[EntityMapping(name: \"{this.table.name}\", Schema = \"{this.schemaName}\")]");
66+
writer.WriteLine($"\t[EntityMapping(name: \"{this.table.Name}\", Schema = \"{this.schemaName}\")]");
6667
writer.WriteLine($"\tpublic partial class {_classname}");
6768
writer.WriteLine("\t{");
6869

@@ -78,24 +79,24 @@ public void CreateModel()
7879
writer.WriteLine($"\t\tpublic {_type} {item.Field.ToUpperPascal()} {{ get; set; }}");
7980
writer.WriteLine();
8081
}
81-
if (this.table.type == "table")
82+
if (this.table.Type == "table")
8283
{
8384
string dalPath = $"{ projectName }.DAL.";
8485
Hashtable ht = new Hashtable();
8586
foreach (var item in consList)
8687
{
87-
string f_dalName = CreateName(item.nspname, item.table_name);
88-
string pname = $"{item.table_name.ToUpperPascal()}";
88+
string f_dalName = CreateName(item.NspName, item.TablaName);
89+
string pname = $"{item.TablaName.ToUpperPascal()}";
8990
string propertyName = f_dalName;
9091
if (ht.ContainsKey(propertyName) || _classname == propertyName)
9192
{
92-
propertyName += "By" + item.conname.ToUpperPascal();
93+
propertyName += "By" + item.ConlumnName.ToUpperPascal();
9394
}
9495

9596

9697
string tmp_var = propertyName.ToLowerPascal();
9798
writer.WriteLine($"\t\tprivate {f_dalName}Model {tmp_var} = null;");
98-
writer.WriteLine($"\t\t[ForeignKeyMapping(name: \"{item.conname}\"), JsonIgnore] public {f_dalName}Model {propertyName} {{ get {{ if ({tmp_var} == null) {tmp_var} = {dalPath}{f_dalName}.Context.Where(f => f.{item.ref_column.ToUpperPascal()} == this.{item.conname.ToUpperPascal()}).ToOne(); return {tmp_var}; }} }}");
99+
writer.WriteLine($"\t\t[ForeignKeyMapping(name: \"{item.ConlumnName}\"), JsonIgnore] public {f_dalName}Model {propertyName} {{ get {{ if ({tmp_var} == null) {tmp_var} = {dalPath}{f_dalName}.Context.Where(f => f.{item.RefColumn.ToUpperPascal()} == this.{item.ConlumnName.ToUpperPascal()}).ToOne(); return {tmp_var}; }} }}");
99100
writer.WriteLine();
100101
ht.Add(propertyName, "");
101102
}
@@ -157,9 +158,15 @@ protected void CreateSchema()
157158
for (int i = 0; i < fieldList.Count; i++)
158159
{
159160
var fi = fieldList[i];
160-
string specificType = GetspecificType(fi);
161+
string specificType = GetSpecificType(fi);
161162
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+
var pk = pkList.FirstOrDefault(f => f.Field == fi.Field) != null;
164+
var primarykey = "";
165+
if (pk)
166+
{
167+
primarykey = " ,Primarykey = true";
168+
}
169+
var line = $"{{\"{fi.Field}\", new SchemaModel{{ FieldName = \"{fi.Field}\", DbType = NpgsqlDbType.{fi.PgDbType}{ap}, Size = {fi.Length}, SpecificType = {specificType}{primarykey}}} }}";
163170
writer.WriteLine("\t\t\t\t" + line + (i + 1 == fieldList.Count ? "" : ","));
164171
}
165172
writer.WriteLine("\t\t\t};");
@@ -187,7 +194,7 @@ private string CreateName(string schema, string tableName, string separator = ""
187194

188195
private string CreateName()
189196
{
190-
return CreateName(this.schemaName, this.table.name);
197+
return CreateName(this.schemaName, this.table.Name);
191198
}
192199

193200
protected void CreateDal()
@@ -232,7 +239,7 @@ protected void CreateDal()
232239
}
233240
}
234241

235-
if (this.table.type == "table")
242+
if (this.table.Type == "table")
236243
{
237244
writer.WriteLine();
238245
Insert_Generator(writer, _model_classname, _classname);
@@ -320,25 +327,23 @@ void CreateConstructor(string paramString, string onChange = null)
320327

321328
writer.WriteLine($"\t\t\tpublic new {updateName} Where(Expression<Func<{class_model.ToUpperPascal()}, bool>> predicate)");
322329
writer.WriteLine("\t\t\t{");
323-
writer.WriteLine($"\t\t\t\t base.Where(predicate);");
324-
writer.WriteLine($"\t\t\t\t return this;");
330+
writer.WriteLine($"\t\t\t\tbase.Where(predicate);");
331+
writer.WriteLine($"\t\t\t\treturn this;");
325332
writer.WriteLine("\t\t\t}");
326-
writer.WriteLine();
327333

328334
writer.WriteLine($"\t\t\tpublic new {updateName} Where(string formatCommad, params object[] pValue)");
329335
writer.WriteLine("\t\t\t{");
330-
writer.WriteLine($"\t\t\t\t base.Where(formatCommad,pValue);");
331-
writer.WriteLine($"\t\t\t\t return this;");
336+
writer.WriteLine($"\t\t\t\tbase.Where(formatCommad,pValue);");
337+
writer.WriteLine($"\t\t\t\treturn this;");
332338
writer.WriteLine("\t\t\t}");
333-
writer.WriteLine();
334339

335340
foreach (var item in fieldList)
336341
{
337342
if (item.Is_identity) continue;
338343

339344
writer.WriteLine($"\t\t\tpublic {updateName} Set{item.Field.ToUpperPascal()}({item.RelType} {item.Field})");
340345
writer.WriteLine("\t\t\t{");
341-
string specificType = GetspecificType(item);
346+
string specificType = GetSpecificType(item);
342347
string ap = item.Is_array ? " | NpgsqlDbType.Array" : "";
343348
writer.WriteLine($"\t\t\t\tbase.SetField(\"{ item.Field}\", NpgsqlDbType.{item.PgDbType}{ap}, {item.Field}, {item.Length}, {specificType});");
344349
writer.WriteLine($"\t\t\t\treturn this;");
@@ -358,7 +363,6 @@ void CreateConstructor(string paramString, string onChange = null)
358363
writer.WriteLine($"\t\t\t\treturn this;");
359364
writer.WriteLine("\t\t\t}");
360365
}
361-
writer.WriteLine();
362366
}
363367
writer.WriteLine("\t\t}");
364368
}
@@ -405,7 +409,7 @@ from pg_class a
405409
left join pg_type e2 on e2.oid=e.typelem
406410
inner join information_schema.columns f on f.table_schema = b.nspname and f.table_name=a.relname and column_name = c.attname
407411
WHERE b.nspname='{0}' and a.relname='{1}';";
408-
_sqltext = string.Format(_sqltext, this.schemaName, this.table.name);
412+
_sqltext = string.Format(_sqltext, this.schemaName, this.table.Name);
409413

410414

411415
PgSqlHelper.ExecuteDataReader(dr =>
@@ -448,7 +452,7 @@ from pg_class a
448452
}, CommandType.Text, _sqltext);
449453
}
450454

451-
private string GetspecificType(FieldInfo fi)
455+
private string GetSpecificType(FieldInfo fi)
452456
{
453457
string specificType = "null";
454458
if (fi.Data_Type == "e")
@@ -462,13 +466,13 @@ protected void Get_Primarykey(int oid)
462466
string _sqltext = $@"SELECT b.attname, format_type(b.atttypid, b.atttypmod) AS data_type
463467
FROM pg_index a
464468
INNER JOIN pg_attribute b ON b.attrelid = a.indrelid AND b.attnum = ANY(a.indkey)
465-
WHERE a.indrelid = '{schemaName}.{table.name}'::regclass AND a.indisprimary;
469+
WHERE a.indrelid = '{schemaName}.{table.Name}'::regclass AND a.indisprimary;
466470
";
467471
PgSqlHelper.ExecuteDataReader(dr =>
468472
{
469473
PrimarykeyInfo pk = new PrimarykeyInfo();
470474
pk.Field = dr["attname"].ToString();
471-
pk.Typname = dr["data_type"].ToString();
475+
pk.TypeName = dr["data_type"].ToString();
472476
pkList.Add(pk);
473477
}, CommandType.Text, _sqltext);
474478
}
@@ -488,7 +492,7 @@ WHERE conrelid in
488492
SELECT a.oid FROM pg_class a
489493
inner join pg_namespace b on a.relnamespace=b.oid
490494
WHERE b.nspname='{0}' and a.relname='{1}');"
491-
, this.schemaName, this.table.name);
495+
, this.schemaName, this.table.Name);
492496

493497

494498
PgSqlHelper.ExecuteDataReader(dr =>
@@ -500,11 +504,11 @@ SELECT a.oid FROM pg_class a
500504
string nspname = dr["nspname"].ToString();
501505
consList.Add(new ConstraintInfo()
502506
{
503-
conname = conname,
504-
contype = contype,
505-
ref_column = ref_column,
506-
table_name = relname,
507-
nspname = nspname
507+
ConlumnName = conname,
508+
ConlumnType = contype,
509+
RefColumn = ref_column,
510+
TablaName = relname,
511+
NspName = nspname
508512
});
509513
}, CommandType.Text, _sqltext);
510514
}

MyStaging.App/GeneralFactory.cs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static void Build(string outputdir, string projName)
3434
List<TableViewModel> tableList = GetTables(schemaName);
3535
foreach (var item in tableList)
3636
{
37-
Console.WriteLine("{0}:{1}", item.type, item.name);
37+
Console.WriteLine("{0}:{1}", item.Type, item.Name);
3838
TablesDal td = new TablesDal(GeneralFactory.projectName, modelPath, schemaPath, dalPath, schemaName, item);
3939
td.Create();
4040
}
@@ -46,7 +46,7 @@ private static void CreateDir()
4646
modelPath = Path.Combine(outputDir, projectName + ".db", "Model", "Build");
4747
schemaPath = Path.Combine(outputDir, projectName + ".db", "Model", "Schemas");
4848
dalPath = Path.Combine(outputDir, projectName + ".db", "DAL", "Build");
49-
string[] ps = { modelPath, dalPath };
49+
string[] ps = { modelPath, schemaPath, dalPath };
5050
for (int i = 0; i < ps.Length; i++)
5151
{
5252
if (!Directory.Exists(ps[i]))
@@ -76,39 +76,6 @@ private static void CreateCsproj()
7676
writer.WriteLine("\t</ItemGroup>");
7777
writer.WriteLine("</Project>");
7878
}
79-
// sln
80-
81-
string sln_file = Path.Combine(outputDir, projectName, $"{projectName}.sln");
82-
if (!File.Exists(sln_file))
83-
{
84-
using (StreamWriter writer = new StreamWriter(File.Create(sln_file), System.Text.Encoding.UTF8))
85-
{
86-
writer.WriteLine("Microsoft Visual Studio Solution File, Format Version 12.00");
87-
writer.WriteLine("# Visual Studio 15>");
88-
writer.WriteLine($"VisualStudioVersion = 15.0.26430.13");
89-
90-
Guid db_guid = Guid.NewGuid();
91-
writer.WriteLine($"Project(\"{Guid.NewGuid()}\") = \"{projectName}.db\", \"{projectName}.db\\{projectName}.db.csproj\", \"{ db_guid}\"");
92-
writer.WriteLine($"EndProject");
93-
94-
writer.WriteLine("Global");
95-
writer.WriteLine("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution");
96-
writer.WriteLine("\t\tDebug|Any CPU = Debug|Any CPU");
97-
writer.WriteLine("\t\tRelease|Any CPU = Release|Any CPU");
98-
writer.WriteLine("\tEndGlobalSection");
99-
100-
writer.WriteLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution");
101-
writer.WriteLine($"\t\t{db_guid}.Debug|Any CPU.ActiveCfg = Debug|Any CPU");
102-
writer.WriteLine($"\t\t{db_guid}.Debug|Any CPU.Build.0 = Debug|Any CPU");
103-
writer.WriteLine($"\t\t{db_guid}.Release|Any CPU.ActiveCfg = Release|Any CPU");
104-
writer.WriteLine($"\t\t{db_guid}.Release|Any CPU.Build.0 = Release|Any CPU");
105-
writer.WriteLine("\tEndGlobalSection");
106-
writer.WriteLine("\tGlobalSection(SolutionProperties) = preSolution");
107-
writer.WriteLine("\t\tHideSolutionNode = FALSE");
108-
writer.WriteLine("\tEndGlobalSection");
109-
writer.WriteLine("EndGlobal");
110-
}
111-
}
11279
}
11380

11481
private static List<TableViewModel> GetTables(string schema)
@@ -119,7 +86,7 @@ UNION ALL
11986
List<TableViewModel> tableList = new List<TableViewModel>();
12087
PgSqlHelper.ExecuteDataReader(dr =>
12188
{
122-
TableViewModel model = new TableViewModel() { name = dr["table_name"].ToString(), type = dr["type"].ToString() };
89+
TableViewModel model = new TableViewModel() { Name = dr["table_name"].ToString(), Type = dr["type"].ToString() };
12390
tableList.Add(model);
12491
}, CommandType.Text, _sqltext);
12592

MyStaging.App/Models/ConstraintInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace MyStaging.App.Models
66
{
77
public class ConstraintInfo
88
{
9-
public string conname { get; set; }
10-
public string contype { get; set; }
11-
public string ref_column { get; set; }
12-
public string table_name { get; set; }
13-
public string nspname { get; set; }
9+
public string ConlumnName { get; set; }
10+
public string ConlumnType { get; set; }
11+
public string RefColumn { get; set; }
12+
public string TablaName { get; set; }
13+
public string NspName { get; set; }
1414
}
1515
}

MyStaging.App/Models/EnumTypeInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace MyStaging.App.Models
66
{
77
public class EnumTypeInfo
88
{
9-
public int oid { get; set; }
10-
public string typename { get; set; }
11-
public string nspname { get; set; }
9+
public int Oid { get; set; }
10+
public string TypeName { get; set; }
11+
public string NspName { get; set; }
1212
}
1313
}

MyStaging.App/Models/PrimarykeyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace MyStaging.App.Models
77
public class PrimarykeyInfo
88
{
99
public string Field { get; set; }
10-
public string Typname { get; set; }
10+
public string TypeName { get; set; }
1111
}
1212
}

MyStaging.App/Models/TableViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace MyStaging.App.Models
66
{
7-
public class TableViewModel
7+
public class TableViewModel
88
{
9-
public string name { get; set; }
10-
public string type { get; set; }
9+
public string Name { get; set; }
10+
public string Type { get; set; }
1111
}
1212
}

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.....");

0 commit comments

Comments
 (0)