Skip to content

Commit c4cae3b

Browse files
committed
升级脚手架,支持生成插件Postgis
1 parent aa326e4 commit c4cae3b

File tree

9 files changed

+176
-97
lines changed

9 files changed

+176
-97
lines changed

.vs/MyStaging/v15/.suo

1 KB
Binary file not shown.

MyStaging.App/DAL/EnumsDal.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ namespace MyStaging.App.DAL
99
{
1010
public class EnumsDal
1111
{
12-
private static string projectName = string.Empty;
13-
private static string modelPath = string.Empty;
14-
private static string rootPath = string.Empty;
12+
private string projectName = string.Empty;
13+
private string modelPath = string.Empty;
14+
private string rootPath = string.Empty;
15+
private List<PluginsViewModel> plugins;
1516

16-
public static void Generate(string rootpath, string modelpath, string projName)
17+
public EnumsDal(string rootpath, string modelpath, string projName, List<PluginsViewModel> plugins)
1718
{
18-
rootPath = rootpath;
19-
modelPath = modelpath;
20-
projectName = projName;
19+
this.rootPath = rootpath;
20+
this.modelPath = modelpath;
21+
this.projectName = projName;
22+
this.plugins = plugins;
23+
}
2124

25+
public void Generate()
26+
{
2227
string _sqltext = @"
2328
select a.oid,a.typname,b.nspname from pg_type a
2429
INNER JOIN pg_namespace b on a.typnamespace = b.oid
@@ -35,7 +40,7 @@ public static void Generate(string rootpath, string modelpath, string projName)
3540
});
3641
}, System.Data.CommandType.Text, _sqltext);
3742

38-
string _fileName = Path.Combine(modelpath, "_Enums.cs");
43+
string _fileName = Path.Combine(modelPath, "_Enums.cs");
3944
using (StreamWriter writer = new StreamWriter(File.Create(_fileName), System.Text.Encoding.UTF8))
4045
{
4146
writer.WriteLine("using System;");
@@ -62,7 +67,7 @@ public static void Generate(string rootpath, string modelpath, string projName)
6267
GenerateMapping(list);
6368
}
6469

65-
public static void GenerateMapping(List<EnumTypeInfo> list)
70+
public void GenerateMapping(List<EnumTypeInfo> list)
6671
{
6772
string _fileName = Path.Combine(rootPath, "_startup.cs");
6873
using (StreamWriter writer = new StreamWriter(File.Create(_fileName), System.Text.Encoding.UTF8))
@@ -90,6 +95,10 @@ public static void GenerateMapping(List<EnumTypeInfo> list)
9095
writer.WriteLine("\t\t\tType[] jsonTypes = { typeof(JToken), typeof(JObject), typeof(JArray) };");
9196
writer.WriteLine("\t\t\tNpgsqlNameTranslator translator = new NpgsqlNameTranslator();");
9297
writer.WriteLine("\t\t\tNpgsqlConnection.GlobalTypeMapper.UseJsonNet(jsonTypes);");
98+
foreach (var item in plugins)
99+
{
100+
writer.WriteLine($"\t\t\t{item.Mapper}");
101+
}
93102
foreach (var item in list)
94103
{
95104
writer.WriteLine($"\t\t\tNpgsqlConnection.GlobalTypeMapper.MapEnum<{item.TypeName.ToUpperPascal()}>(\"{item.NspName}.{item.TypeName}\", translator);");

MyStaging.App/DAL/SchemaDal.cs

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,73 @@
1-
using MyStaging;
1+
using MyStaging.App.Models;
22
using MyStaging.Helpers;
33
using System;
44
using System.Collections.Generic;
55
using System.Data;
6-
using System.Text;
76

87
namespace MyStaging.App.DAL
98
{
109
public class SchemaDal
1110
{
12-
/// <summary>
13-
/// generate schema name list
14-
/// </summary>
15-
/// <returns></returns>
16-
public static List<string> Get_List()
11+
public SchemaDal()
1712
{
18-
List<string> schemaList = new List<string>();
19-
string[] notin = {
20-
"'geometry_columns'",
21-
"'raster_columns'",
22-
"'spatial_ref_sys'",
23-
"'raster_overviews'",
24-
"'us_gaz'",
25-
"'topology'",
26-
"'zip_lookup_all'",
27-
"'pg_toast'",
28-
"'pg_temp_1'",
29-
"'pg_toast_temp_1'",
30-
"'pg_catalog'",
31-
"'information_schema'",
32-
"'tiger'",
33-
"'tiger_data'"
34-
};
35-
string sql = $@"SELECT schema_name FROM information_schema.schemata WHERE SCHEMA_NAME NOT IN({string.Join(",", notin)}) ORDER BY SCHEMA_NAME; ";
13+
Filters.AddRange(new string[]{
14+
"geometry_columns",
15+
"raster_columns",
16+
"spatial_ref_sys",
17+
"raster_overviews",
18+
"us_gaz",
19+
"topology",
20+
"zip_lookup_all",
21+
"pg_toast",
22+
"pg_temp_1",
23+
"pg_toast_temp_1",
24+
"pg_catalog",
25+
"information_schema",
26+
"tiger",
27+
"tiger_data"
28+
});
29+
}
30+
31+
public List<string> Filters { get; set; } = new List<string>();
32+
33+
public List<SchemaViewModel> List()
34+
{
35+
string[] filters = new string[this.Filters.Count];
36+
for (int i = 0; i < Filters.Count; i++)
37+
{
38+
filters[i] = $"'{Filters[i]}'";
39+
}
40+
41+
List<SchemaViewModel> schemas = new List<SchemaViewModel>();
42+
string sql = $@"SELECT schema_name FROM information_schema.schemata WHERE SCHEMA_NAME NOT IN({string.Join(",", filters)}) ORDER BY SCHEMA_NAME; ";
3643
PgSqlHelper.ExecuteDataReader(dr =>
3744
{
38-
schemaList.Add(dr[0].ToString());
45+
var schema = new SchemaViewModel { Name = dr[0].ToString() };
46+
schemas.Add(schema);
3947
}, CommandType.Text, sql);
4048

49+
foreach (var item in schemas)
50+
{
51+
Console.WriteLine("正在生成模式:{0}", item.Name);
52+
item.Tables = GetTables(item.Name);
53+
}
54+
55+
return schemas;
56+
}
57+
58+
private List<TableViewModel> GetTables(string schema)
59+
{
60+
string _sqltext = $@"SELECT table_name,'table' as type FROM INFORMATION_SCHEMA.tables WHERE table_schema='{schema}' AND table_type='BASE TABLE'
61+
UNION ALL
62+
SELECT table_name,'view' as type FROM INFORMATION_SCHEMA.views WHERE table_schema = '{schema}'";
63+
List<TableViewModel> tableList = new List<TableViewModel>();
64+
PgSqlHelper.ExecuteDataReader(dr =>
65+
{
66+
TableViewModel model = new TableViewModel() { Name = dr["table_name"].ToString(), Type = dr["type"].ToString() };
67+
tableList.Add(model);
68+
}, CommandType.Text, _sqltext);
4169

42-
return schemaList;
70+
return tableList;
4371
}
4472
}
4573
}

MyStaging.App/GeneralFactory.cs

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,83 @@
11
using MyStaging.App.DAL;
22
using MyStaging.App.Models;
3-
using MyStaging.Helpers;
43
using System;
54
using System.Collections.Generic;
6-
using System.Data;
75
using System.IO;
6+
using System.Linq;
87

98
namespace MyStaging.App
109
{
1110
public class GeneralFactory
1211
{
13-
private static string schemaPath = string.Empty;
14-
private static string modelPath = string.Empty;
15-
private static string dalPath = string.Empty;
16-
private static string projectName = string.Empty;
17-
private static string outputDir = string.Empty;
12+
private string schemaPath = string.Empty;
13+
private string modelPath = string.Empty;
14+
private string dalPath = string.Empty;
15+
private string projectName = string.Empty;
16+
private string outputDir = string.Empty;
1817

19-
public static void Build(string outputdir, string projName)
18+
public GeneralFactory(string outputdir, string projName)
2019
{
21-
if (string.IsNullOrEmpty(outputdir) || string.IsNullOrEmpty(projName))
22-
throw new ArgumentNullException("outputdir 和 projName", "不能为空");
20+
if (string.IsNullOrEmpty(outputdir))
21+
throw new ArgumentNullException(nameof(outputdir));
22+
23+
if (string.IsNullOrEmpty(projName))
24+
throw new ArgumentNullException(nameof(projName));
25+
2326
outputDir = outputdir;
2427
projectName = projName;
28+
}
29+
30+
public void Build()
31+
{
32+
var schemas = new SchemaDal().List();
33+
var plugins = FindPlugins(schemas);
2534

2635
CreateDir();
27-
CreateCsproj();
28-
EnumsDal.Generate(Path.Combine(outputdir, projName + ".db"), modelPath, GeneralFactory.projectName);
36+
CreateCsproj(plugins);
37+
CreateEnums(plugins);
38+
CreateModels(schemas);
39+
}
2940

30-
List<string> schemaList = SchemaDal.Get_List();
31-
foreach (var schemaName in schemaList)
41+
private List<PluginsViewModel> FindPlugins(List<SchemaViewModel> schemas)
42+
{
43+
var plugins = new List<PluginsViewModel>();
44+
foreach (var item in schemas)
3245
{
33-
Console.WriteLine("正在生成模式:{0}", schemaName);
34-
List<TableViewModel> tableList = GetTables(schemaName);
35-
foreach (var item in tableList)
46+
if (item.Tables.FirstOrDefault(f => f.Name == "geometry_columns") != null)
3647
{
37-
Console.WriteLine("{0}:{1}", item.Type, item.Name);
38-
TablesDal td = new TablesDal(GeneralFactory.projectName, modelPath, schemaPath, dalPath, schemaName, item);
48+
plugins.Add(new PluginsViewModel
49+
{
50+
Name = item.Name,
51+
Mapper = "NpgsqlConnection.GlobalTypeMapper.UseLegacyPostgis();",
52+
Package = "<PackageReference Include=\"Npgsql.LegacyPostgis\" Version=\"4.0.9\" />"
53+
});
54+
}
55+
}
56+
57+
return plugins;
58+
}
59+
60+
private void CreateModels(List<SchemaViewModel> schemas)
61+
{
62+
foreach (var item in schemas)
63+
{
64+
Console.WriteLine("building:{0}", item.Name);
65+
foreach (var table in item.Tables)
66+
{
67+
Console.WriteLine("{0}:{1}", table.Type, table.Name);
68+
TablesDal td = new TablesDal(projectName, modelPath, schemaPath, dalPath, item.Name, table);
3969
td.Create();
4070
}
4171
}
4272
}
4373

44-
private static void CreateDir()
74+
private void CreateEnums(List<PluginsViewModel> plugins)
75+
{
76+
var enumsDal = new EnumsDal(Path.Combine(outputDir, projectName + ".db"), modelPath, projectName, plugins);
77+
enumsDal.Generate();
78+
}
79+
80+
private void CreateDir()
4581
{
4682
modelPath = Path.Combine(outputDir, projectName + ".db", "Model", "Build");
4783
schemaPath = Path.Combine(outputDir, projectName + ".db", "Model", "Schemas");
@@ -54,10 +90,9 @@ private static void CreateDir()
5490
}
5591
}
5692

57-
private static void CreateCsproj()
93+
private void CreateCsproj(List<PluginsViewModel> plugins)
5894
{
5995
string path = Path.Combine(outputDir, $"{projectName}.db");
60-
6196
string csproj = Path.Combine(path, $"{projectName}.db.csproj");
6297
if (File.Exists(csproj))
6398
return;
@@ -72,26 +107,14 @@ private static void CreateCsproj()
72107
writer.WriteLine("\t</PropertyGroup>");
73108
writer.WriteLine();
74109
writer.WriteLine("\t<ItemGroup>");
75-
writer.WriteLine("\t\t<PackageReference Include=\"MyStaging\" Version=\"1.0.0\" />");
110+
writer.WriteLine("\t\t<PackageReference Include=\"MyStaging\" Version=\"2.*\" />");
111+
foreach (var item in plugins)
112+
{
113+
writer.WriteLine($"\t\t{item.Package}");
114+
}
76115
writer.WriteLine("\t</ItemGroup>");
77116
writer.WriteLine("</Project>");
78117
}
79118
}
80-
81-
private static List<TableViewModel> GetTables(string schema)
82-
{
83-
string _sqltext = $@"SELECT table_name,'table' as type FROM INFORMATION_SCHEMA.tables WHERE table_schema='{schema}' AND table_type='BASE TABLE'
84-
UNION ALL
85-
SELECT table_name,'view' as type FROM INFORMATION_SCHEMA.views WHERE table_schema = '{schema}'";
86-
List<TableViewModel> tableList = new List<TableViewModel>();
87-
PgSqlHelper.ExecuteDataReader(dr =>
88-
{
89-
TableViewModel model = new TableViewModel() { Name = dr["table_name"].ToString(), Type = dr["type"].ToString() };
90-
tableList.Add(model);
91-
}, CommandType.Text, _sqltext);
92-
93-
return tableList;
94-
}
95-
96119
}
97120
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MyStaging.App.Models
6+
{
7+
public class PluginsViewModel
8+
{
9+
public string Name { get; set; }
10+
public string Mapper { get; set; }
11+
public string Package { get; set; }
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MyStaging.App.Models
6+
{
7+
public class SchemaViewModel
8+
{
9+
public string Name { get; set; }
10+
public List<TableViewModel> Tables { get; set; }
11+
}
12+
}

MyStaging.App/Program.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,22 @@ static void Main(string[] args)
4646
}
4747
i++;
4848
}
49-
//outPutPath = @"D:\MyGitHub\mystaging";
50-
//projName = "MyStaging.xUnitTest";
49+
50+
//var outPutPath = @"D:\MyGitHub\mystaging";
51+
//var projName = "MyStaging.SuperApp";
5152
//MyStaging.Common.StagingOptions options = new Common.StagingOptions()
5253
//{
53-
// ConnectionMaster = "Host=127.0.0.1;Port=5432;Username=postgres;Password=postgres;Database=mystaging;Pooling=true;Maximum Pool Size=100"
54+
// ConnectionMaster = "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456;Database=mystaging;Pooling=true;Maximum Pool Size=10;"
5455
//};
55-
// PgSqlHelper.InitConnection(options);
56+
//PgSqlHelper.InitConnection(options);
57+
5658
var options = new Common.StagingOptions()
5759
{
5860
ConnectionMaster = connection.ToString()
5961
};
6062
PgSqlHelper.InitConnection(options);
61-
GeneralFactory.Build(outPutPath, projName);
63+
GeneralFactory factory = new GeneralFactory(outPutPath, projName);
64+
factory.Build();
6265

6366
Console.WriteLine("success.....");
6467
}

0 commit comments

Comments
 (0)