public override int GetHashCode() { int hash = 1; if (MetadataNamespace.Length != 0) { hash ^= MetadataNamespace.GetHashCode(); } if (Key.Length != 0) { hash ^= Key.GetHashCode(); } if (Value.Length != 0) { hash ^= Value.GetHashCode(); } if (Type != global::Envoy.Config.Filter.Http.HeaderToMetadata.V3Alpha.Config.Types.ValueType.String) { hash ^= Type.GetHashCode(); } if (Encode != global::Envoy.Config.Filter.Http.HeaderToMetadata.V3Alpha.Config.Types.ValueEncode.None) { hash ^= Encode.GetHashCode(); } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
public override int GetHashCode() { int hash = 1; if (MetadataNamespace.Length != 0) { hash ^= MetadataNamespace.GetHashCode(); } if (Key.Length != 0) { hash ^= Key.GetHashCode(); } if (Value.Length != 0) { hash ^= Value.GetHashCode(); } if (Type != 0) { hash ^= Type.GetHashCode(); } if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
internal MetadataModule(ICompilation compilation, Metadata.PEFile peFile, TypeSystemOptions options) { this.Compilation = compilation; this.PEFile = peFile; this.metadata = peFile.Metadata; this.options = options; this.TypeProvider = new TypeProvider(this); // assembly metadata if (metadata.IsAssembly) { var asmdef = metadata.GetAssemblyDefinition(); this.AssemblyName = metadata.GetString(asmdef.Name); this.FullAssemblyName = metadata.GetFullAssemblyName(); } else { var moddef = metadata.GetModuleDefinition(); this.AssemblyName = metadata.GetString(moddef.Name); this.FullAssemblyName = this.AssemblyName; } this.rootNamespace = new MetadataNamespace(this, null, string.Empty, metadata.GetNamespaceDefinitionRoot()); if (!options.HasFlag(TypeSystemOptions.Uncached)) { // create arrays for resolved entities, indexed by row index this.typeDefs = new MetadataTypeDefinition[metadata.TypeDefinitions.Count + 1]; this.fieldDefs = new MetadataField[metadata.FieldDefinitions.Count + 1]; this.methodDefs = new MetadataMethod[metadata.MethodDefinitions.Count + 1]; this.propertyDefs = new MetadataProperty[metadata.PropertyDefinitions.Count + 1]; this.eventDefs = new MetadataEvent[metadata.EventDefinitions.Count + 1]; } }
////////////////////////////////////////////////////////////////////////// /// 1st way: /// This method demonstrates the direct access to the internal metadata /// objects collection (MetadataContainer). ////////////////////////////////////////////////////////////////////////// public void Way1(object sender, EventArgs e) { ActiveTabs = 0; var queryBuilder1 = QueryBuilderStore.Get("LoadMetadata"); ResetQueryBuilderMetadata(queryBuilder1); queryBuilder1.SyntaxProvider = new GenericSyntaxProvider(); // prevent QueryBuilder to request metadata queryBuilder1.MetadataLoadingOptions.OfflineMode = true; queryBuilder1.MetadataProvider = null; MetadataContainer metadataContainer = queryBuilder1.MetadataContainer; metadataContainer.BeginUpdate(); try { metadataContainer.Clear(); MetadataNamespace schemaDbo = metadataContainer.AddSchema("dbo"); // prepare metadata for table "Orders" MetadataObject orders = schemaDbo.AddTable("Orders"); // fields orders.AddField("OrderId"); orders.AddField("CustomerId"); // prepare metadata for table "Order Details" MetadataObject orderDetails = schemaDbo.AddTable("Order Details"); // fields orderDetails.AddField("OrderId"); orderDetails.AddField("ProductId"); // foreign keys MetadataForeignKey foreignKey = orderDetails.AddForeignKey("OrderDetailsToOrders"); using (MetadataQualifiedName referencedName = new MetadataQualifiedName()) { referencedName.Add("Orders"); referencedName.Add("dbo"); foreignKey.ReferencedObjectName = referencedName; } foreignKey.Fields.Add("OrderId"); foreignKey.ReferencedFields.Add("OrderId"); } finally { metadataContainer.EndUpdate(); } queryBuilder1.MetadataStructure.Refresh(); }
internal MetadataModule(ICompilation compilation, Metadata.PEFile peFile, TypeSystemOptions options) { this.Compilation = compilation; this.PEFile = peFile; this.metadata = peFile.Metadata; this.options = options; this.TypeProvider = new TypeProvider(this); // assembly metadata if (metadata.IsAssembly) { var asmdef = metadata.GetAssemblyDefinition(); try { this.AssemblyName = metadata.GetString(asmdef.Name); this.AssemblyVersion = asmdef.Version; this.FullAssemblyName = metadata.GetFullAssemblyName(); } catch (BadImageFormatException) { this.AssemblyName = "<ERR: invalid assembly name>"; this.FullAssemblyName = "<ERR: invalid assembly name>"; } } else { try { var moddef = metadata.GetModuleDefinition(); this.AssemblyName = metadata.GetString(moddef.Name); } catch (BadImageFormatException) { this.AssemblyName = "<ERR: invalid assembly name>"; } this.FullAssemblyName = this.AssemblyName; } var customAttrs = metadata.GetModuleDefinition().GetCustomAttributes(); this.NullableContext = customAttrs.GetNullableContext(metadata) ?? Nullability.Oblivious; this.minAccessibilityForNRT = FindMinimumAccessibilityForNRT(metadata, customAttrs); this.rootNamespace = new MetadataNamespace(this, null, string.Empty, metadata.GetNamespaceDefinitionRoot()); if (!options.HasFlag(TypeSystemOptions.Uncached)) { // create arrays for resolved entities, indexed by row index this.typeDefs = new MetadataTypeDefinition[metadata.TypeDefinitions.Count + 1]; this.fieldDefs = new MetadataField[metadata.FieldDefinitions.Count + 1]; this.methodDefs = new MetadataMethod[metadata.MethodDefinitions.Count + 1]; this.propertyDefs = new MetadataProperty[metadata.PropertyDefinitions.Count + 1]; this.eventDefs = new MetadataEvent[metadata.EventDefinitions.Count + 1]; this.referencedAssemblies = new IModule[metadata.AssemblyReferences.Count + 1]; } }
protected void btn1_Click(object sender, EventArgs e) { var queryBuilder1 = QueryBuilderControl1.QueryBuilder; // prevent QueryBuilder to request metadata queryBuilder1.OfflineMode = true; queryBuilder1.MetadataProvider = null; MetadataContainer metadataContainer = queryBuilder1.MetadataContainer; metadataContainer.BeginUpdate(); try { metadataContainer.Items.Clear(); MetadataNamespace schemaDbo = metadataContainer.AddSchema("dbo"); // prepare metadata for table "Orders" MetadataObject orders = schemaDbo.AddTable("Orders"); // fields orders.AddField("OrderId"); orders.AddField("CustomerId"); // prepare metadata for table "Order Details" MetadataObject orderDetails = schemaDbo.AddTable("Order Details"); // fields orderDetails.AddField("OrderId"); orderDetails.AddField("ProductId"); // foreign keys MetadataForeignKey foreignKey = orderDetails.AddForeignKey("OrderDetailsToOrders"); using (MetadataQualifiedName referencedName = new MetadataQualifiedName()) { referencedName.Add("Orders"); referencedName.Add("dbo"); foreignKey.ReferencedObjectName = referencedName; } foreignKey.Fields.Add("OrderId"); foreignKey.ReferencedFields.Add("OrderId"); } finally { metadataContainer.EndUpdate(); } queryBuilder1.MetadataStructure.Refresh(); StatusBar1.Message.Information("Metadata loaded"); }
private void fillProgrammaticallyMenuItem_Click(object sender, EventArgs e) { ResetQueryBuilder(); // Fill the query builder metadata programmatically // setup the query builder with metadata and syntax providers queryBuilder1.SyntaxProvider = genericSyntaxProvider1; queryBuilder1.MetadataLoadingOptions.OfflineMode = true; // prevent querying obejects from database // create database and schema MetadataNamespace database = queryBuilder1.MetadataContainer.AddDatabase("MyDB"); database.Default = true; MetadataNamespace schema = database.AddSchema("MySchema"); schema.Default = true; // create table MetadataObject tableOrders = schema.AddTable("Orders"); tableOrders.AddField("OrderID"); tableOrders.AddField("OrderDate"); tableOrders.AddField("CustomerID"); tableOrders.AddField("ResellerID"); // create another table MetadataObject tableCustomers = schema.AddTable("Customers"); tableCustomers.AddField("CustomerID"); tableCustomers.AddField("CustomerName"); tableCustomers.AddField("CustomerAddress"); // add a relation between these two tables MetadataForeignKey relation = tableCustomers.AddForeignKey("FK_CustomerID"); relation.Fields.Add("CustomerID"); relation.ReferencedObjectName = tableOrders.GetQualifiedName(); relation.ReferencedFields.Add("CustomerID"); //create view MetadataObject viewResellers = schema.AddView("Resellers"); viewResellers.AddField("ResellerID"); viewResellers.AddField("ResellerName"); // kick the query builder to fill metadata tree queryBuilder1.InitializeDatabaseSchemaTree(); WarnAboutGenericSyntaxProvider(); // show warning (just for demonstration purposes) }
private void LoadForDatabases() { foreach (var database in _databases) { if (_loadingAborted) { break; } DatabaseLoadingStart?.Invoke(this, database); MetadataNamespace db = _container.AddDatabase(database); db.Items.Load(LoadFields ? MetadataType.All : MetadataType.All & ~MetadataType.ObjectMetadata, true); } }
////////////////////////////////////////////////////////////////////////// /// 1st way: /// This method demonstrates the direct access to the internal metadata /// objects collection (MetadataContainer). ////////////////////////////////////////////////////////////////////////// private void btn1Way_Click(object sender, EventArgs e) { // prevent QueryBuilder to request metadata QBuilder.MetadataLoadingOptions.OfflineMode = true; QBuilder.MetadataProvider = null; MetadataContainer metadataContainer = QBuilder.MetadataContainer; metadataContainer.BeginUpdate(); try { metadataContainer.Clear(); MetadataNamespace schemaDbo = metadataContainer.AddSchema("dbo"); // prepare metadata for table "Orders" MetadataObject orders = schemaDbo.AddTable("Orders"); // fields orders.AddField("OrderId"); orders.AddField("CustomerId"); // prepare metadata for table "Order Details" MetadataObject orderDetails = schemaDbo.AddTable("Order Details"); // fields orderDetails.AddField("OrderId"); orderDetails.AddField("ProductId"); // foreign keys MetadataForeignKey foreignKey = orderDetails.AddForeignKey("OrderDetailsToOrders"); using (MetadataQualifiedName referencedName = new MetadataQualifiedName()) { referencedName.Add("Orders"); referencedName.Add("dbo"); foreignKey.ReferencedObjectName = referencedName; } foreignKey.Fields.Add("OrderId"); foreignKey.ReferencedFields.Add("OrderId"); } finally { metadataContainer.EndUpdate(); } QBuilder.InitializeDatabaseSchemaTree(); }
public override bool GetReferencingObjectNames(UpdatableObjectList <MetadataQualifiedName> list, MetadataObject forObject, MetadataLoadingOptions loadingOptions) { if (forObject.Server == null) { MetadataNamespace database = forObject.Database; MetadataNamespace schema = forObject.Schema; string[] restrictions = new string[6]; restrictions[0] = database != null ? database.Name : null; restrictions[1] = schema != null ? schema.Name : null; restrictions[2] = forObject.Name; using (DataTable foreignKeys = _connection.GetSchema("ForeignKeys", restrictions)) { int databaseField = foreignKeys.Columns.IndexOf("FKTABLE_CAT"); int schemaField = foreignKeys.Columns.IndexOf("FKTABLE_SCHEM"); int nameField = foreignKeys.Columns.IndexOf("FKTABLE_NAME"); foreach (DataRow row in foreignKeys.Rows) { object referencingName = nameField != -1 ? row[nameField] : null; object referencingSchema = schemaField != -1 ? row[schemaField] : null; object referencingDatabase = databaseField != -1 ? row[databaseField] : null; MetadataQualifiedName name = new MetadataQualifiedName(); if (referencingName != null) { name.Add(referencingName.ToString(), MetadataType.Objects); } if (referencingSchema != null) { name.Add(referencingSchema.ToString(), MetadataType.Schema); } if (referencingDatabase != null) { name.Add(referencingDatabase.ToString(), MetadataType.Schema); } list.Add(name); } return(true); } } return(false); }
public override void LoadDatabases(MetadataList databases, MetadataLoadingOptions loadingOptions) { if (!Connected) { Connect(); } // load from OLEDB catalogs try { DataTable schemaTable = _connection.GetSchema("Tables"); string databaseFieldName = "TABLE_CATALOG"; if (schemaTable.Columns[databaseFieldName] == null) { databaseFieldName = "TABLE_CAT"; } using (MetadataNamespacesFetcherFromDatatable mdf = new MetadataNamespacesFetcherFromDatatable(databases, MetadataType.Database, loadingOptions)) { mdf.Datatable = schemaTable; mdf.NameFieldName = databaseFieldName; mdf.LoadMetadata(); } } catch { // loading from OLEDB catalog failed } // load default database string currentDatabase = Connection.Database; if (!string.IsNullOrEmpty(currentDatabase)) { MetadataNamespace database = databases.FindItem <MetadataNamespace>(currentDatabase, MetadataType.Database); if (database == null) { database = new MetadataNamespace(databases, MetadataType.Database); database.Name = currentDatabase; databases.Add(database); } database.Default = true; } }
public override void LoadSchemas(MetadataList schemas, MetadataLoadingOptions loadingOptions) { MetadataNamespace database = schemas.Parent.Database; if (schemas.Parent.Server == null && database != null) { if (!Connected) { Connect(); } try { string[] restrictions = new string[1]; restrictions[0] = database.Name; using (DataTable schemaTable = _connection.GetSchema("Tables", restrictions)) { using (MetadataNamespacesFetcherFromDatatable mdf = new MetadataNamespacesFetcherFromDatatable(schemas, MetadataType.Schema, loadingOptions)) { mdf.Datatable = schemaTable; mdf.NameFieldName = "TABLE_SCHEMA"; mdf.LoadMetadata(); } } using (DataTable schemaTable = _connection.GetSchema("Views", restrictions)) { using (MetadataNamespacesFetcherFromDatatable mdf = new MetadataNamespacesFetcherFromDatatable(schemas, MetadataType.Schema, loadingOptions)) { mdf.Datatable = schemaTable; mdf.NameFieldName = "TABLE_SCHEMA"; mdf.LoadMetadata(); } } } catch (Exception exception) { throw new QueryBuilderException(exception.Message, exception); } } }
public override void LoadObjects(MetadataList objects, MetadataLoadingOptions loadingOptions) { MetadataNamespace database = objects.Parent.Database; MetadataNamespace schema = objects.Parent.Schema; if (objects.Parent.Server == null && database != null && schema != null) { if (!Connected) { Connect(); } try { string[] restrictions = new string[2]; restrictions[0] = database.Name; restrictions[1] = schema.Name; using (DataTable schemaTable = _connection.GetSchema("Tables", restrictions)) { MetadataObjectFetcherFromDatatable mof = new MetadataObjectFetcherFromDatatable(objects, loadingOptions); mof.Datatable = schemaTable; mof.NameFieldName = "TABLE_NAME"; mof.TypeFieldName = "TABLE_TYPE"; mof.TableType = "BASE TABLE"; mof.ViewType = "VIEW"; mof.DefaultObjectType = MetadataType.Table; mof.LoadMetadata(); } } catch (Exception exception) { throw new QueryBuilderException(exception.Message, exception); } } }
public override void LoadFields(MetadataList fields, MetadataLoadingOptions loadingOptions) { MetadataItem obj = fields.Parent.Object; MetadataNamespace schema = obj.Schema; MetadataNamespace database = obj.Database; if (schema != null && database != null && obj.Server == null) { string[] restrictions = new string[3]; restrictions[0] = database.Name; restrictions[1] = schema.Name; restrictions[2] = obj.Name; using (DataTable dataTable = _connection.GetSchema("Columns", restrictions)) { MetadataFieldsFetcherFromDatatable mff = new MetadataFieldsFetcherFromDatatable(fields.SQLContext); mff.Datatable = dataTable; mff.NameFieldName = "COLUMN_NAME"; mff.NullableFieldName = "IS_NULLABLE"; mff.NullableValue = "YES"; mff.ServerTypeFieldName = "DATA_TYPE"; mff.SizeFieldName = "CHARACTER_MAXIMUM_LENGTH"; mff.PrecisionFieldName = "NUMERIC_PRECISION"; mff.ScaleFieldName = "NUMERIC_SCALE"; mff.DefaultValueFieldName = "COLUMN_DEFAULT"; mff.LoadFields(fields); } base.LoadFields(fields, loadingOptions); } }
public override void LoadDatabases(MetadataList databases, MetadataLoadingOptions loadingOptions) { if (databases.Parent.Server == null) { if (!Connected) { Connect(); } string currentDatabase = Connection.Database; if (!String.IsNullOrEmpty(currentDatabase)) { MetadataNamespace database = databases.FindItem <MetadataNamespace>(currentDatabase, MetadataType.Database); if (database == null) { database = new MetadataNamespace(databases, MetadataType.Database); database.Name = currentDatabase; databases.Add(database); } database.Default = true; } } }
public override void LoadObjects(MetadataList objects, MetadataLoadingOptions loadingOptions) { if (!Connected) { Connect(); } SQLContext sqlContext = objects.SQLContext; string olddb = Connection.Database; try { // load tables string[] restrictions = new string[4]; if (sqlContext.SyntaxProvider.IsSupportDatabases()) { MetadataNamespace database = objects.Parent.Database; if (database != null) { restrictions[0] = database.Name; } else { return; } } else { restrictions[0] = null; } if (sqlContext.SyntaxProvider.IsSupportSchemas()) { MetadataNamespace schema = objects.Parent.Schema; if (schema != null) { restrictions[1] = schema.Name; } else { return; } } else { restrictions[1] = null; } try { if (!string.IsNullOrEmpty(restrictions[0]) && Connection.Database != restrictions[0]) { Connection.ChangeDatabase(restrictions[0]); } DataTable schemaTable = _connection.GetSchema("Tables", restrictions); MetadataObjectFetcherFromDatatable mof = new MetadataObjectFetcherFromDatatable(objects, loadingOptions) { NameFieldName = "TABLE_NAME", TypeFieldName = "TABLE_TYPE", TableType = new[] { "TABLE", "LINK", "PASS-THROUGH", "SYNONYM", "BASE TABLE" }, SystemTableType = new[] { "SYSTEM TABLE", "ACCESS TABLE" }, ViewType = "VIEW", SystemViewType = "SYSTEM VIEW", SynonymType = "ALIAS", Datatable = schemaTable }; mof.LoadMetadata(); } catch { // loading from OLEDB catalog failed } } finally { if (Connection.Database != olddb) { Connection.ChangeDatabase(olddb); } } }
private void LoadFieldsFromGetSchema(MetadataList fields, MetadataLoadingOptions loadingOptions) { SQLContext sqlContext = fields.SQLContext; string olddb = Connection.Database; try { // load tables string[] restrictions = new string[3]; if (sqlContext.SyntaxProvider.IsSupportDatabases()) { MetadataNamespace database = fields.Parent.Database; if (database != null) { restrictions[0] = database.Name; } else { return; } } else { restrictions[0] = null; } if (sqlContext.SyntaxProvider.IsSupportSchemas()) { MetadataNamespace schema = fields.Parent.Schema; if (schema != null) { restrictions[1] = schema.Name; } else { return; } } else { restrictions[1] = null; } MetadataObject obj = fields.Parent.Object; if (obj == null) { return; } restrictions[2] = obj.Name; try { if (!string.IsNullOrEmpty(restrictions[0]) && Connection.Database != restrictions[0]) { Connection.ChangeDatabase(restrictions[0]); } DataTable schemaTable = _connection.GetSchema("Columns", restrictions); schemaTable.DefaultView.Sort = "ORDINAL_POSITION"; DataTable ordinalSortedColumns = schemaTable.DefaultView.ToTable(); MetadataFieldsFetcherFromDatatable mof = new MetadataFieldsFetcherFromDatatable(sqlContext); mof.NameFieldName = "COLUMN_NAME"; mof.ServerTypeFieldName = "TYPE_NAME"; mof.SizeFieldName = "COLUMN_SIZE"; mof.ScaleFieldName = "DECIMAL_DIGITS"; mof.DescriptionFieldName = "REMARKS"; mof.Datatable = ordinalSortedColumns; mof.LoadFields(fields); } catch { // loading from OLEDB catalog failed } } finally { if (Connection.Database != olddb) { Connection.ChangeDatabase(olddb); } } }
public override void LoadObjects(MetadataList objects, MetadataLoadingOptions loadingOptions) { if (!Connected) { Connect(); } SQLContext sqlContext = objects.SQLContext; string olddb = Connection.Database; try { try { // load tables string[] restrictions = new string[2]; if (sqlContext.SyntaxProvider.IsSupportDatabases()) { MetadataNamespace database = objects.Parent.Database; if (database != null) { restrictions[0] = database.Name; } else { return; } } else { restrictions[0] = null; } if (sqlContext.SyntaxProvider.IsSupportSchemas()) { MetadataNamespace schema = objects.Parent.Schema; if (schema != null) { restrictions[1] = schema.Name; } else { return; } } else { restrictions[1] = null; } MetadataObjectFetcherFromDatatable mof = new MetadataObjectFetcherFromDatatable(objects, loadingOptions); mof.NameFieldName = "TABLE_NAME"; mof.TypeFieldName = "TABLE_TYPE"; mof.TableType = new string[] { "TABLE", "LINK", "PASS-THROUGH", "SYNONYM" }; mof.SystemTableType = new string[] { "SYSTEM TABLE", "ACCESS TABLE" }; mof.ViewType = new string[] { "VIEW" }; mof.SystemViewType = "SYSTEM VIEW"; using (DataTable schemaTable = _connection.GetSchema("Tables", restrictions)) { // hack for old outdated ODBC drivers which uses TABLENAME instead TABLE_NAME if (schemaTable.Columns.IndexOf("TABLENAME") != -1) { mof.NameFieldName = "TABLENAME"; } // hack for old outdated ODBC drivers which uses TABLETYPE instead TABLE_TYPE if (schemaTable.Columns.IndexOf("TABLETYPE") != -1) { mof.NameFieldName = "TABLETYPE"; } mof.DefaultObjectType = MetadataType.Table; mof.Datatable = schemaTable; mof.LoadMetadata(); } using (DataTable schemaTable = _connection.GetSchema("Views", restrictions)) { // hack for old outdated ODBC drivers which uses TABLENAME instead TABLE_NAME if (schemaTable.Columns.IndexOf("TABLENAME") != -1) { mof.NameFieldName = "TABLENAME"; } // hack for old outdated ODBC drivers which uses TABLETYPE instead TABLE_TYPE if (schemaTable.Columns.IndexOf("TABLETYPE") != -1) { mof.NameFieldName = "TABLETYPE"; } mof.DefaultObjectType = MetadataType.View; mof.Datatable = schemaTable; mof.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; 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); } } }
protected override void OnLoad(EventArgs e) { // Set required syntax provider queryBuilder.SyntaxProvider = new MSSQLSyntaxProvider(); try { // Load metadata from XML file (for demonstration purposes) queryBuilder.MetadataLoadingOptions.OfflineMode = true; queryBuilder.MetadataContainer.ImportFromXML("Northwind.xml"); // =========================================================================== // CREATE VIRTUAL DATABASE OBJECTS AND FIELDS // =========================================================================== queryBuilder.MetadataContainer.BeginUpdate(); MetadataObject o; MetadataField f; // Virtual fields for real object // =========================================================================== o = queryBuilder.MetadataContainer.FindItem <MetadataObject>("Orders"); Debug.Assert(o != null); // first test field - simple expression f = o.AddField("_OrderId_plus_1"); f.Expression = "orders.OrderId + 1"; // second test field - correlated sub-query f = o.AddField("_CustomerName"); f.Expression = "(select c.CompanyName from Customers c where c.CustomerId = orders.CustomerId)"; // Virtual object (table) with virtual fields // =========================================================================== // find parent "dbo" node MetadataNamespace dboSchemaNode = queryBuilder.MetadataContainer.FindItem <MetadataNamespace>("dbo", MetadataType.Namespaces); o = dboSchemaNode.AddTable("Orders_tbl"); o.Expression = "Orders"; // first test field - simple expression f = o.AddField("_tbl_OrderId_plus_1"); f.Expression = "Orders_tbl.OrderId + 1"; // second test field - correlated sub-query f = o.AddField("_tbl_CustomerName"); f.Expression = "(select c.CompanyName from Customers c where c.CustomerId = Orders_tbl.CustomerId)"; // Virtual object (sub-query) with virtual fields // =========================================================================== o = dboSchemaNode.AddTable("Orders_qry"); o.Expression = "(select OrderId, CustomerId, OrderDate from Orders) as dummy_alias"; // first test field - simple expression f = o.AddField("_qry_OrderId_plus_1"); f.Expression = "Orders_qry.OrderId + 1"; // second test field - correlated sub-query f = o.AddField("_qry_CustomerName"); f.Expression = "(select c.CompanyName from Customers c where c.CustomerId = Orders_qry.CustomerId)"; // kick queryBuilder to initialize its metadata tree queryBuilder.InitializeDatabaseSchemaTree(); queryBuilder.SQL = "SELECT dummy_alias._qry_OrderId_plus_1, dummy_alias._qry_CustomerName FROM Orders_qry dummy_alias"; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { queryBuilder.MetadataContainer.EndUpdate(); } base.OnLoad(e); }
// HOWTO: Fill metadata container with custom objects public void FillMetadataContainer() { MetadataNamespace database = _query.SQLContext.MetadataContainer.AddDatabase("MyDB"); database.Default = true; // hides the default database prefix from object names MetadataNamespace schema = database.AddSchema("MySchema"); schema.Default = true; // hides the default schema prefix from object names // create table MetadataObject tableOrders = schema.AddTable("Orders"); tableOrders.AddField("OrderID"); tableOrders.AddField("OrderDate"); tableOrders.AddField("City"); tableOrders.AddField("CustomerID"); tableOrders.AddField("ResellerID"); // create another table MetadataObject tableCustomers = schema.AddTable("Customers"); tableCustomers.AddField("CustomerID"); tableCustomers.AddField("CustomerName"); tableCustomers.AddField("CustomerAddress"); MetadataField fieldCustValue = tableCustomers.AddField("CustomerValue"); fieldCustValue.FieldType = System.Data.DbType.Double; MetadataField fieldCustBirthDate = tableCustomers.AddField("CustomerBirthDay"); fieldCustBirthDate.FieldType = System.Data.DbType.DateTime; MetadataField fieldCustCity = tableCustomers.AddField("City"); fieldCustCity.FieldType = System.Data.DbType.String; fieldCustCity.Size = 50; // add a relation between these two tables MetadataForeignKey relation = tableCustomers.AddForeignKey("FK_CustomerID"); relation.Fields.Add("CustomerID"); relation.ReferencedObjectName = tableOrders.GetQualifiedName(); relation.ReferencedFields.Add("CustomerID"); // create another table MetadataObject salesOrderHeader = schema.AddTable("SalesOrderHeader"); salesOrderHeader.AddField("SalesOrderID"); salesOrderHeader.AddField("OrderDate"); // create another table MetadataObject salesOrderDetail = schema.AddTable("SalesOrderDetail"); salesOrderDetail.AddField("SalesOrderID"); salesOrderDetail.AddField("UnitPrice"); //create a view MetadataObject viewResellers = schema.AddView("Resellers"); viewResellers.AddField("ResellerID"); viewResellers.AddField("ResellerName"); }
public override void LoadObjects(MetadataList objects, MetadataLoadingOptions loadingOptions) { if (!Connected) { Connect(); } SQLContext sqlContext = objects.SQLContext; try { // load tables and views string[] restrictions = new string[2]; if (sqlContext.SyntaxProvider.IsSupportDatabases()) { MetadataNamespace database = objects.Parent.Database; if (database != null) { restrictions[0] = database.Name; } else { return; } } else { restrictions[0] = null; } if (sqlContext.SyntaxProvider.IsSupportSchemas()) { MetadataNamespace schema = objects.Parent.Schema; if (schema != null) { restrictions[1] = schema.Name; } else { return; } } else { restrictions[1] = null; } DataTable schemaTable = _connection.GetSchema(DB2MetaDataCollectionNames.Tables, restrictions); MetadataObjectFetcherFromDatatable mof = new MetadataObjectFetcherFromDatatable(objects, loadingOptions); mof.NameFieldName = "TABLE_NAME"; mof.TypeFieldName = "TABLE_TYPE"; mof.TableType = new string[] { "TABLE" }; mof.SystemTableType = new string[] { "SYSTEM TABLE" }; mof.ViewType = new string[] { "VIEW", "MATERIALIZED QUERY TABLE" }; mof.SynonymType = "ALIAS"; mof.Datatable = schemaTable; mof.LoadMetadata(); } catch (Exception exception) { throw new QueryBuilderException(exception.Message, exception); } }
} // buildBindingList(...) private void TreatField(MetadataItem mdi) { MetadataField mdf = mdi as MetadataField; if (mdf == null) { return; } // ColumnQN colQN = new ColumnQN(); colQN.IsSystem = mdi.System; colQN.IsDefault = mdi.Default; colQN.ObjectType = Enum.GetName(typeof(MetadataType), mdi.Type); if (mdi.Parent != null) { colQN.ParentType = Enum.GetName(typeof(MetadataType), mdi.Parent.Type); } colQN.Remarks = mdf.Description; #region --- Server.Database.Schema.Object.Column Names --- if (mdf.Server != null) { MetadataNamespace srv = mdf.Server; colQN.ServerAltName = srv.AltName; colQN.ServerName = srv.Name; colQN.ServerNameFull = srv.NameFull; colQN.ServerNameQuoted = srv.NameQuoted; } if (mdf.Database != null) { MetadataNamespace database = mdf.Database; colQN.DatabaseAltName = database.AltName; colQN.DatabaseName = database.Name; colQN.DatabaseNameFull = database.NameFull; colQN.DatabaseNameQuoted = database.NameQuoted; } if (mdf.Schema != null) { MetadataNamespace schema = mdf.Schema; colQN.SchemaAltName = schema.AltName; colQN.SchemaName = schema.Name; colQN.SchemaNameFull = schema.NameFull; colQN.SchemaNameQuoted = schema.NameQuoted; } if (mdf.Object != null) { MetadataObject obj = mdf.Object; colQN.ParentAltName = obj.AltName; colQN.ParentName = obj.Name; colQN.ParentNameFull = obj.NameFull; colQN.ParentNameQuoted = obj.NameQuoted; } { colQN.AltName = mdf.AltName; colQN.Name = mdf.Name; colQN.NameFull = mdf.NameFull; colQN.NameQuoted = mdf.NameQuoted; } #endregion string idescription = mdi.Description; object itag = mdi.Tag; string iuserData = mdi.UserData; MetadataNamespace package = mdf.Package; //string writeXMLNode = WriteXMLNode(mdf); // { colQN.DatabaseTypeName = mdf.FieldTypeName; colQN.StorageTypeName = Enum.GetName(typeof(DbType), mdf.FieldType); colQN.Size = mdf.Size; colQN.Precision = mdf.Precision; colQN.Scale = mdf.Scale; colQN.IsPrimaryKey = mdf.PrimaryKey; colQN.IsNullable = mdf.Nullable; colQN.IsReadOnly = mdf.ReadOnly; { colQN.IsVirtual = mdf.IsVirtual(); colQN.Expression = mdf.Expression; } // string writeXMLNode = WriteXMLNode(mdf); } // // colQN.Serialize(@"D:\TEMP\CS\QWERTY.XML"); // this._colQnBindingList.Add(colQN); }
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 } } }