[Adapdev-commits] Adapdev/src/Adapdev.Data/Schema SchemaBuilder.cs,1.3,1.4
Status: Beta
Brought to you by:
intesar66
From: Trevor L. <tre...@us...> - 2005-04-12 10:23:37
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev.Data/Schema In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23402/src/Adapdev.Data/Schema Modified Files: SchemaBuilder.cs Log Message: Added Progress Dialog and threaded control for loading tables. Index: SchemaBuilder.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.Data/Schema/SchemaBuilder.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** SchemaBuilder.cs 11 Apr 2005 11:24:04 -0000 1.3 --- SchemaBuilder.cs 12 Apr 2005 10:22:56 -0000 1.4 *************** *** 7,11 **** using Adapdev.Data; using Adapdev.Data.Sql; ! /// <summary> /// SchemaBuilder builds the schema for a specified database --- 7,11 ---- using Adapdev.Data; using Adapdev.Data.Sql; ! /// <summary> /// SchemaBuilder builds the schema for a specified database *************** *** 13,16 **** --- 13,17 ---- public class SchemaBuilder { + private static Adapdev.IProgressCallback _callback; private static DbProviderType dbProviderType; *************** *** 22,26 **** public static DatabaseSchema CreateDatabaseSchema(string oledbConnectionString) { ! return SchemaBuilder.CreateDatabaseSchema(oledbConnectionString, Adapdev.Data.DbType.ACCESS, DbProviderType.OLEDB); } --- 23,27 ---- public static DatabaseSchema CreateDatabaseSchema(string oledbConnectionString) { ! return SchemaBuilder.CreateDatabaseSchema(oledbConnectionString, Adapdev.Data.DbType.ACCESS, DbProviderType.OLEDB, null); } *************** *** 31,37 **** /// <param name="providerType">The DbProviderType to set the DatabaseSchema to</param> /// <returns></returns> ! public static DatabaseSchema CreateDatabaseSchema(string oledbConnectionString, string databaseType, string providerType) { ! return SchemaBuilder.CreateDatabaseSchema(oledbConnectionString, DbTypeConverter.Convert(databaseType), DbProviderTypeConverter.Convert(providerType)); } --- 32,38 ---- /// <param name="providerType">The DbProviderType to set the DatabaseSchema to</param> /// <returns></returns> ! public static DatabaseSchema CreateDatabaseSchema(string oledbConnectionString, string databaseType, string providerType, Adapdev.IProgressCallback progress) { ! return SchemaBuilder.CreateDatabaseSchema(oledbConnectionString, DbTypeConverter.Convert(databaseType), DbProviderTypeConverter.Convert(providerType), progress); } *************** *** 42,134 **** /// <param name="providerType">The DbProviderType to set the DatabaseSchema to</param> /// <returns></returns> ! public static DatabaseSchema CreateDatabaseSchema(string oledbConnectionString, Adapdev.Data.DbType databaseType, DbProviderType providerType) { dbProviderType = providerType; ! DataTable schemaTables = SchemaBuilder.GetOleDbSchema(oledbConnectionString, OleDbSchemaGuid.Tables, ""); DatabaseSchema di = new DatabaseSchema(); ! if (schemaTables.Rows.Count > 0) ! { //TODO: Note sure if this is valid. It does not work for Oracle DB's di.Name = schemaTables.Rows[0]["TABLE_CATALOG"].ToString(); if (di.Name == String.Empty) di.Name = "Unknown"; ! } ! // Build the base schema information ! foreach (DataRow dr in schemaTables.Rows) ! { ! TableType tableType = TableTypeConverter.Convert(dr["TABLE_TYPE"].ToString()); ! if (tableType == TableType.TABLE || tableType == TableType.VIEW) ! { ! TableSchema ti = CreateTableSchema(dr); ! CreateColumnSchemas(ti, oledbConnectionString, databaseType); ! di.AddTable(ti); } - } ! // Get the primary key information ! DataTable pkeys = SchemaBuilder.GetOleDbSchema(oledbConnectionString, OleDbSchemaGuid.Primary_Keys, ""); ! foreach (DataRow dr in pkeys.Rows) ! { ! string pkTable = dr["TABLE_NAME"].ToString(); ! TableSchema tip = di[pkTable]; ! if (tip != null) ! { ! ColumnSchema ci = tip[dr["COLUMN_NAME"].ToString()]; ! if (ci != null) ! { ! ci.IsPrimaryKey = true; ! tip.AddColumn(ci); } } ! } ! // Get the foreign key information ! DataTable fkeys = SchemaBuilder.GetOleDbSchema(oledbConnectionString, OleDbSchemaGuid.Foreign_Keys, ""); ! foreach (DataRow dr in fkeys.Rows) ! { ! string fkTable = dr["FK_TABLE_NAME"].ToString(); ! TableSchema tif = di[fkTable]; ! if (tif != null) ! { ! ColumnSchema ci = tif[dr["FK_COLUMN_NAME"].ToString()]; ! if (ci != null) ! { ! ci.IsForeignKey = true; ! tif.AddColumn(ci); } } - } ! // Get the foreign key associations ! foreach (DataRow dr in fkeys.Rows) ! { ! // Get the name of the primary key table ! string pkTable = dr["PK_TABLE_NAME"].ToString(); ! // Get the name of the foreign key table ! string fkTable = dr["FK_TABLE_NAME"].ToString(); ! // Get the name of the foreign key column ! string fkColumn = dr["FK_COLUMN_NAME"].ToString(); ! // Get the table containing the primary key ! TableSchema tif = di[pkTable]; ! // Get the table containing the foreign key ! TableSchema fk = di[fkTable]; ! if (tif != null) ! { ! // Get the primary key ! ColumnSchema ci = tif[dr["PK_COLUMN_NAME"].ToString()]; ! // Get the foreign key ! ColumnSchema cf = fk[fkColumn]; ! if (ci != null) ! { ! // Add the association to the table and column containing the foreign key ! ci.ForeignKeyTables.Add(new ForeignKeyAssociation(ci, cf ,fk)); } } } - return di; } --- 43,151 ---- /// <param name="providerType">The DbProviderType to set the DatabaseSchema to</param> /// <returns></returns> ! public static DatabaseSchema CreateDatabaseSchema(string oledbConnectionString, Adapdev.Data.DbType databaseType, DbProviderType providerType, Adapdev.IProgressCallback progress) { + int recordCount = 0; dbProviderType = providerType; + _callback = progress as Adapdev.IProgressCallback; ! if (_callback != null) { ! _callback.SetText("Obtaining Schema Details",""); ! _callback.SetAutoClose(ProgressAutoCloseTypes.WaitOnEnd); ! } + DataTable schemaTables = SchemaBuilder.GetOleDbSchema(oledbConnectionString, OleDbSchemaGuid.Tables, "","","",""); DatabaseSchema di = new DatabaseSchema(); ! if (schemaTables.Rows.Count > 0) { //TODO: Note sure if this is valid. It does not work for Oracle DB's di.Name = schemaTables.Rows[0]["TABLE_CATALOG"].ToString(); if (di.Name == String.Empty) di.Name = "Unknown"; ! ! // Setup the Progress Display if one is defined. ! if (!StartProgress("Building Table Details",schemaTables.Rows.Count,ref recordCount)) return null; ! // Build the base schema information ! foreach (DataRow dr in schemaTables.Rows) { ! TableType tableType = TableTypeConverter.Convert(dr["TABLE_TYPE"].ToString()); ! if (tableType == TableType.TABLE || tableType == TableType.VIEW) { ! if (!IncrProgress(dr["TABLE_NAME"].ToString(), ref recordCount)) return null; ! ! TableSchema ti = CreateTableSchema(dr); ! CreateColumnSchemas(ti, oledbConnectionString, databaseType); ! di.AddTable(ti); ! } } ! // Get the primary key information ! DataTable pkeys = SchemaBuilder.GetOleDbSchema(oledbConnectionString, OleDbSchemaGuid.Primary_Keys, "","","",""); ! ! // Setup the Progress Display if one is defined. ! if (!StartProgress("Building Primary Key Details",pkeys.Rows.Count,ref recordCount)) return null; ! foreach (DataRow dr in pkeys.Rows) { ! string pkTable = dr["TABLE_NAME"].ToString(); ! if (!IncrProgress(dr["TABLE_NAME"].ToString(), ref recordCount)) return null; ! ! TableSchema tip = di[pkTable]; ! if (tip != null) { ! ColumnSchema ci = tip[dr["COLUMN_NAME"].ToString()]; ! if (ci != null) { ! ci.IsPrimaryKey = true; ! tip.AddColumn(ci); ! } } + } ! // Get the foreign key information ! DataTable fkeys = SchemaBuilder.GetOleDbSchema(oledbConnectionString, OleDbSchemaGuid.Foreign_Keys, "","","",""); ! // Setup the Progress Display if one is defined. ! if (!StartProgress("Building Foreign Key Details",fkeys.Rows.Count,ref recordCount)) return null; ! foreach (DataRow dr in fkeys.Rows) { ! string fkTable = dr["FK_TABLE_NAME"].ToString(); ! if (!IncrProgress(dr["FK_TABLE_NAME"].ToString(), ref recordCount)) return null; ! ! TableSchema tif = di[fkTable]; ! if (tif != null) { ! ColumnSchema ci = tif[dr["FK_COLUMN_NAME"].ToString()]; ! if (ci != null) { ! ci.IsForeignKey = true; ! tif.AddColumn(ci); ! } } } ! // Setup the Progress Display if one is defined. ! if (!StartProgress("Building Foreign Key Relationships",fkeys.Rows.Count,ref recordCount)) return null; ! // Get the foreign key associations ! foreach (DataRow dr in fkeys.Rows) { ! if (!IncrProgress(dr["PK_TABLE_NAME"].ToString(), ref recordCount)) return null; ! ! // Get the name of the primary key table ! string pkTable = dr["PK_TABLE_NAME"].ToString(); ! // Get the name of the foreign key table ! string fkTable = dr["FK_TABLE_NAME"].ToString(); ! // Get the name of the foreign key column ! string fkColumn = dr["FK_COLUMN_NAME"].ToString(); ! ! // Get the table containing the primary key ! TableSchema tif = di[pkTable]; ! // Get the table containing the foreign key ! TableSchema fk = di[fkTable]; ! if (tif != null) { ! // Get the primary key ! ColumnSchema ci = tif[dr["PK_COLUMN_NAME"].ToString()]; ! // Get the foreign key ! ColumnSchema cf = fk[fkColumn]; ! if (ci != null) { ! // Add the association to the table and column containing the foreign key ! ci.ForeignKeyTables.Add(new ForeignKeyAssociation(ci, cf ,fk)); ! } } } + if (!EndProgress("Finished Loading Tables",true)) return null; + } else { + if (!EndProgress("No database schema information found.",false)) return null; } return di; } *************** *** 203,207 **** } catch (Exception ex) { schemaTable = null; ! //throw new ApplicationException ("Could not retrieve column information for table " + tableName + "\n\n" + ex.Message, ex); } return schemaTable; --- 220,224 ---- } catch (Exception ex) { schemaTable = null; ! if (_callback != null) _callback.AddMessage(ProgressMessageTypes.Warning, "Could not load Column information for " + tableName); } return schemaTable; *************** *** 215,232 **** /// <param name="filter"></param> /// <returns></returns> ! public static DataTable GetOleDbSchema(string oledbConnectionString, Guid guid, string filter) ! { ! DataTable schemaTable; OleDbConnection conn = new OleDbConnection(oledbConnectionString); - conn.Open(); ! if (filter.Length > 0) ! { ! schemaTable = conn.GetOleDbSchemaTable(guid, ! new object[] {null, null, null, filter}); ! } ! else ! { ! schemaTable = conn.GetOleDbSchemaTable(guid, null); } conn.Close(); --- 232,257 ---- /// <param name="filter"></param> /// <returns></returns> ! public static DataTable GetOleDbSchema(string oledbConnectionString, Guid guid, string filterCatalog, string filterSchema, string filterName, string filterType) { ! DataTable schemaTable = null; OleDbConnection conn = new OleDbConnection(oledbConnectionString); conn.Open(); ! ! // Note: the Object parameters are as follows: ! // TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE ! try { ! object[] filters = new object[] { ! filterCatalog == string.Empty ? null : filterCatalog, ! filterSchema == string.Empty ? null : filterSchema, ! filterName == string.Empty ? null : filterName, ! filterType == string.Empty ? null : filterType ! }; ! ! if (filters[0]==null && filters[1]==null && filters[2]==null && filters[3]==null) { ! schemaTable = conn.GetOleDbSchemaTable(guid, null); ! } else { ! schemaTable = conn.GetOleDbSchemaTable(guid, filters); ! } ! } catch (Exception ex) { ! Console.WriteLine(ex.Message); } conn.Close(); *************** *** 238,242 **** StringBuilder sb = new StringBuilder(); DataTable schemaTable; ! schemaTable = GetOleDbSchema(oledbConnection, guid, filter); foreach (DataRow row in schemaTable.Rows) --- 263,267 ---- StringBuilder sb = new StringBuilder(); DataTable schemaTable; ! schemaTable = GetOleDbSchema(oledbConnection, guid, "","","",filter); foreach (DataRow row in schemaTable.Rows) *************** *** 258,261 **** --- 283,323 ---- } + + private static bool StartProgress (string message, int max, ref int stepto) + { + if (_callback != null) { + if (_callback.IsAborting) return false; + _callback.SetText(message,""); + _callback.SetRange(0, max); + _callback.StepTo(stepto = 0); + } + return true; + } + + private static bool EndProgress (string message, bool ok) { + if (_callback != null) { + if (_callback.IsAborting) return false; + _callback.SetText(message,""); + _callback.SetRange(0, 1); + if (ok) { + _callback.StepTo(1); + } else { + _callback.StepTo(0); + _callback.AddMessage(ProgressMessageTypes.Critical,"No database schema information found."); + } + } + return true; + } + + private static bool IncrProgress(string message, ref int count) + { + if (_callback != null) { + if (_callback.IsAborting) return false; + _callback.SetText(message); + _callback.StepTo(count++); + } + return true; + } + public static StringBuilder PrintReaderSchema(string oledbConnectionString, string table, Adapdev.Data.DbType databaseType) { |