11using MyStaging . App . DAL ;
22using MyStaging . App . Models ;
3- using MyStaging . Helpers ;
43using System ;
54using System . Collections . Generic ;
6- using System . Data ;
75using System . IO ;
6+ using System . Linq ;
87
98namespace 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}
0 commit comments