[Adapdev-commits] Adapdev/src/Adapdev.Data/Schema SchemaBuilder.cs,1.5,1.6
Status: Beta
Brought to you by:
intesar66
From: Trevor L. <tre...@us...> - 2005-04-14 03:44:20
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev.Data/Schema In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3367/src/Adapdev.Data/Schema Modified Files: SchemaBuilder.cs Log Message: Changes to support filtering of Schemas for Oracle Index: SchemaBuilder.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.Data/Schema/SchemaBuilder.cs,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** SchemaBuilder.cs 13 Apr 2005 04:23:41 -0000 1.5 --- SchemaBuilder.cs 14 Apr 2005 03:43:40 -0000 1.6 *************** *** 56,148 **** DataTable schemaTables = SchemaBuilder.GetOleDbSchema(oledbConnectionString, OleDbSchemaGuid.Tables, "",schemaFilter,"",""); 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; --- 56,149 ---- DataTable schemaTables = SchemaBuilder.GetOleDbSchema(oledbConnectionString, OleDbSchemaGuid.Tables, "",schemaFilter,"",""); DatabaseSchema di = new DatabaseSchema(); ! if (schemaTables != null) { ! 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 ! if (!StartProgress("Building Table Details",schemaTables.Rows.Count,ref recordCount)) return null; ! 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, "",schemaFilter,"",""); ! if (pkeys != null) { ! 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, "",schemaFilter,"",""); + if (fkeys != null) { + 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 (fkeys != null) { ! if (!StartProgress("Building Foreign Key Relationships",fkeys.Rows.Count,ref recordCount)) return null; ! 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; ! } } else { if (!EndProgress("No database schema information found.",false)) return null; *************** *** 220,224 **** } catch (Exception ex) { schemaTable = null; ! if (_callback != null) _callback.AddMessage(ProgressMessageTypes.Warning, "Could not load Column information for " + tableName); } return schemaTable; --- 221,225 ---- } catch (Exception ex) { schemaTable = null; ! if (_callback != null) _callback.AddMessage(ProgressMessageTypes.Warning, "Could not load Column information for " + tableName + ". " + ex.Message); } return schemaTable; *************** *** 237,257 **** 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,245 ---- conn.Open(); try { ! schemaTable = conn.GetOleDbSchemaTable(guid, getFilters(guid, filterCatalog, filterSchema, filterName, filterType)); } catch (Exception ex) { ! if (_callback != null) _callback.AddMessage(ProgressMessageTypes.Critical, "Error obtaining Schema Information: " + ex.Message); } conn.Close(); *************** *** 259,262 **** --- 247,269 ---- } + private static object[] getFilters(Guid guid, string filterCatalog, string filterSchema, string filterName, string filterType) { + // Different OleDbSchemaGuid's require a different number of parameters. + // These parameter depend on what we are trying to retirve from the database + // so this function returns the correct parameter sets. + // This should be a Switch statement, but the compiler did not like it. + + filterCatalog = filterCatalog == string.Empty ? null : filterCatalog; + filterSchema = filterSchema == string.Empty ? null : filterSchema; + filterName = filterName == string.Empty ? null : filterName; + filterType = filterType == string.Empty ? null : filterType; + + if (guid.Equals(OleDbSchemaGuid.Tables)) return new object[] {filterCatalog, filterSchema, filterName, filterType}; + if (guid.Equals(OleDbSchemaGuid.Views)) return new object[] {filterCatalog, filterSchema, filterName}; + if (guid.Equals(OleDbSchemaGuid.Primary_Keys)) return new object[] {filterCatalog, filterSchema, filterName}; + if (guid.Equals(OleDbSchemaGuid.Foreign_Keys)) return new object[] {filterCatalog, filterSchema, filterName, filterCatalog, filterSchema, filterName}; + if (guid.Equals(OleDbSchemaGuid.Columns)) return new object[] {filterCatalog, filterSchema, filterName, string.Empty}; + return null; + } + public static StringBuilder PrintOleDbSchema(string oledbConnection, Guid guid, string filter) { *************** *** 286,294 **** 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; --- 293,303 ---- private static bool StartProgress (string message, int max, ref int stepto) { ! if (max > 0) { ! if (_callback != null) { ! if (_callback.IsAborting) return false; ! _callback.SetText(message,""); ! _callback.SetRange(0, max); ! _callback.StepTo(stepto = 0); ! } } return true; |