public override void LoadSchemas(MetadataList schemas, MetadataLoadingOptions loadingOptions) { if (!Connected) { Connect(); } SQLContext sqlContext = schemas.SQLContext; string olddb = Connection.Database; try { try { // load tables string[] restrictions = new string[1]; if (sqlContext.SyntaxProvider.IsSupportDatabases()) { MetadataNamespace database = schemas.Parent.Database; if (database != null) { restrictions[0] = database.Name; } else { return; } } else { restrictions[0] = null; } DataTable schemaTable = _connection.GetSchema("Tables", restrictions); DataColumn schemaColumn = (schemaTable.Columns["TABLE_SCHEM"] ?? schemaTable.Columns["TABLE_OWNER"]) ?? schemaTable.Columns["TABLEOWNER"] ?? schemaTable.Columns["OWNERNAME"]; if (schemaColumn == null) { return; } using (MetadataNamespacesFetcherFromStringList mnf = new MetadataNamespacesFetcherFromStringList(schemas, MetadataType.Schema, loadingOptions)) { for (int i = 0; i < schemaTable.Rows.Count; i++) { DataRow currentRow = schemaTable.Rows[i]; if (!currentRow.IsNull(schemaColumn)) { string schemaName = Convert.ToString(currentRow[schemaColumn]); if (mnf.AllNamespaces.IndexOf(schemaName) == -1) { mnf.AllNamespaces.Add(schemaName); } } } if (mnf.AllNamespaces.Count > 1 || mnf.AllNamespaces.Count == 1 && !string.IsNullOrEmpty(mnf.AllNamespaces[0])) { mnf.LoadMetadata(); } } } catch (Exception exception) { throw new QueryBuilderException(exception.Message, exception); } } finally { if (Connection.Database != olddb) { Connection.ChangeDatabase(olddb); } } }
public override void LoadSchemas(MetadataList schemas, MetadataLoadingOptions loadingOptions) { if (!Connected) { Connect(); } SQLContext sqlContext = schemas.SQLContext; string olddb = Connection.Database; // load from OLEDB catalog try { string[] restrictions = new string[4]; if (sqlContext.SyntaxProvider.IsSupportDatabases()) { MetadataNamespace database = schemas.Parent.Database; if (database != null) { restrictions[0] = database.Name; } else { return; } } else { restrictions[0] = null; } try { if (!string.IsNullOrEmpty(restrictions[0]) && Connection.Database != restrictions[0]) { Connection.ChangeDatabase(restrictions[0]); } DataTable schemaTable = _connection.GetSchema("Tables", restrictions); DataColumn schemaColumn = schemaTable.Columns["TABLE_SCHEMA"] ?? schemaTable.Columns["TABLE_SCHEM"]; using (MetadataNamespacesFetcherFromStringList mnf = new MetadataNamespacesFetcherFromStringList(schemas, MetadataType.Schema, loadingOptions)) { for (int i = 0; i < schemaTable.Rows.Count; i++) { DataRow currentRow = schemaTable.Rows[i]; if (!currentRow.IsNull(schemaColumn)) { string schemaName = Convert.ToString(currentRow[schemaColumn]); if (mnf.AllNamespaces.IndexOf(schemaName) == -1) { mnf.AllNamespaces.Add(schemaName); } } } mnf.LoadMetadata(); } } catch { // loading from OLEDB catalog failed } } finally { try { if (Connection.Database != olddb) { Connection.ChangeDatabase(olddb); } } catch { // switch database back failed } } }