22using System . Data ;
33using System . Data . Common ;
44using System . Collections . Generic ;
5+ using System . Linq ;
56using DbParallel . DataAccess ;
7+ using T4SQL . MetaData ;
68
79namespace T4SQL . SqlBuilder
810{
@@ -18,7 +20,7 @@ private static string GetProcedure(string sp)
1820 return EngineConfig . DatabasePackage + sp ;
1921 }
2022
21- public static ServerEnvironment GetDbServerEnv ( this DbAccess dbAccess )
23+ public static DbmsEnvironment GetDbServerEnv ( this DbAccess dbAccess )
2224 {
2325 const string sp = "GET_DB_SERVER_ENV" ;
2426 DbParameter outDatabase_Platform = null ;
@@ -28,14 +30,16 @@ public static ServerEnvironment GetDbServerEnv(this DbAccess dbAccess)
2830
2931 dbAccess . ExecuteNonQuery ( GetProcedure ( sp ) , parameters =>
3032 {
31- outDatabase_Platform = parameters . Add ( ) . SetName ( "outDatabase_Platform" ) . SetDirection ( ParameterDirection . Output ) . SetSize ( 32 ) ;
32- outDatabase_Product = parameters . Add ( ) . SetName ( "outDatabase_Product" ) . SetDirection ( ParameterDirection . Output ) . SetSize ( 256 ) ;
33- outProduct_Version = parameters . Add ( ) . SetName ( "outProduct_Version" ) . SetDirection ( ParameterDirection . Output ) . SetSize ( 64 ) ;
34- outServer_Name = parameters . Add ( ) . SetName ( "outServer_Name" ) . SetDirection ( ParameterDirection . Output ) . SetSize ( 64 ) ;
33+ outDatabase_Platform = parameters . AddOutput ( "outDatabase_Platform" , 32 ) ;
34+ outDatabase_Product = parameters . AddOutput ( "outDatabase_Product" , 256 ) ;
35+ outProduct_Version = parameters . AddOutput ( "outProduct_Version" , 64 ) ;
36+ outServer_Name = parameters . AddOutput ( "outServer_Name" , 64 ) ;
3537 } ) ;
3638
37- return new ServerEnvironment ( tableName => dbAccess . ListTableColumns ( tableName ) , outDatabase_Platform . Parameter < string > ( ) ,
38- outDatabase_Product . Parameter < string > ( ) , outProduct_Version . Parameter < string > ( ) , outServer_Name . Parameter < string > ( ) ) ;
39+ return new DbmsEnvironment ( tableName => dbAccess . ListTableColumns ( tableName ) ,
40+ tableName => dbAccess . LoadForeignKeys ( tableName ) ,
41+ outDatabase_Platform . Parameter < string > ( ) , outDatabase_Product . Parameter < string > ( ) ,
42+ outProduct_Version . Parameter < string > ( ) , outServer_Name . Parameter < string > ( ) ) ;
3943 }
4044
4145 public static void LoadEngineConfig ( this DbAccess dbAccess )
@@ -45,7 +49,7 @@ public static void LoadEngineConfig(this DbAccess dbAccess)
4549
4650 dbAccess . ExecuteNonQuery ( GetProcedure ( sp ) , parameters =>
4751 {
48- outPollInterval = parameters . Add ( ) . SetName ( "outPoll_Interval" ) . SetDirection ( ParameterDirection . Output ) . SetDbType ( DbType . Byte ) ;
52+ outPollInterval = parameters . AddOutput ( "outPoll_Interval" ) . SetDbType ( DbType . Byte ) ;
4953 } ) ;
5054
5155 EngineConfig . EnginePollInterval = outPollInterval . Parameter < byte > ( ) * 1000 ;
@@ -57,7 +61,7 @@ private static EngineMain.ServiceMode Ping(DbAccess dbAccess, string sp)
5761
5862 dbAccess . ExecuteNonQuery ( GetProcedure ( sp ) , parameters =>
5963 {
60- outParameter = parameters . Add ( ) . SetName ( "outSwitch_To_Mode" ) . SetDirection ( ParameterDirection . Output ) . SetSize ( EngineMain . ServiceModeMaxLen ) ;
64+ outParameter = parameters . AddOutput ( "outSwitch_To_Mode" , EngineMain . ServiceModeMaxLen ) ;
6165 } ) ;
6266
6367 EngineMain . ServiceMode newMode ;
@@ -111,20 +115,54 @@ public static void RegisterTemplatesSpec(this DbAccess dbAccess, string inClass_
111115 } ) ;
112116 }
113117
114- internal static List < string > ListTableColumns ( this DbAccess dbAccess , string tableName )
118+ internal static IEnumerable < DbmsColumn > ListTableColumns ( this DbAccess dbAccess , string tableName )
115119 {
116120 const string sp = "LIST_COLUMN" ;
117- List < string > columns = new List < string > ( ) ;
118121
119- dbAccess . ExecuteReader ( GetProcedure ( sp ) , parameters =>
122+ return dbAccess . ExecuteReader < DbmsColumn > ( GetProcedure ( sp ) , parameters =>
120123 {
121124 parameters . Add ( "inTable_Name" , tableName ) ;
122- } , reader =>
125+ } , map =>
123126 {
124- columns . Add ( reader . Field < string > ( "COLUMN_NAME" ) ) ;
127+ map . Add ( "COLUMN_NAME" , t => t . ColumnName ) ;
128+ map . Add ( "IS_NULLABLE" , t => t . IsNullable ) ;
125129 } ) ;
130+ }
131+
132+ private static void LoadForeignKeys ( this DbAccess dbAccess , DbmsRelationTree foreignKeyBaseTable )
133+ {
134+ const string sp = "GET_FOREIGN_KEY" ;
135+ DbParameter outTable_Name = null ;
136+
137+ dbAccess . ExecuteReader ( GetProcedure ( sp ) , parameters =>
138+ {
139+ parameters . Add ( "inTable_Name" , foreignKeyBaseTable . TableName ) ;
140+ outTable_Name = parameters . AddOutput ( "outTable_Name" , 128 ) ;
141+ } , reader =>
142+ {
143+ foreignKeyBaseTable . AddForeignKeyColumn (
144+ reader . Field < string > ( "CONSTRAINT_NAME" ) ,
145+ reader . Field < string > ( "FOREIGN_KEY_COLUMN" ) ,
146+ reader . Field < bool > ( "FOREIGN_KEY_NULLABLE" ) ,
147+ reader . Field < string > ( "REFERENCED_TABLE" ) ,
148+ reader . Field < string > ( "REFERENCED_COLUMN" ) ,
149+ reader . Field < bool > ( "REFERENCED_NULLABLE" ) ) ;
150+ } ) ;
151+
152+ foreignKeyBaseTable . TableName = outTable_Name . Parameter < string > ( ) ;
153+ foreignKeyBaseTable . Columns = ListTableColumns ( dbAccess , foreignKeyBaseTable . TableName ) . ToArray ( ) ;
154+
155+ foreach ( DbmsForeignKey fk in foreignKeyBaseTable . ForeignKeys )
156+ LoadForeignKeys ( dbAccess , fk . PrimaryUniqueKeyBaseTable ) ;
157+ }
158+
159+ internal static DbmsRelationTree LoadForeignKeys ( this DbAccess dbAccess , string tableName )
160+ {
161+ DbmsRelationTree rootTable = new DbmsRelationTree ( tableName ) ;
162+
163+ LoadForeignKeys ( dbAccess , rootTable ) ;
126164
127- return columns ;
165+ return rootTable ;
128166 }
129167
130168 internal static void LoadDefaultProperties ( this DbAccess dbAccess , Action < DbDataReader > setDefaultProperty )
0 commit comments