Loading metadata programmatically (VCL, ActiveX and Java)
Last modified:
Warning:
This article is valid for Java, ActiveX, and VCL editions.
The appropriate article for Active Query Builder .NET Editon can be found here:
-
Adding database objects to the Metadata Container.
Metadata Container is a collection of Metadata Objects. Each object holds the list of their fields and relations (foreign keys).public void FillMetadataContainer() { // create table MetadataObject tableOrders = queryBuilder.MetadataContainer.AddTable("MySchema", "Orders", false, "MyDB"); tableOrders.Fields.AddField("OrderID"); tableOrders.Fields.AddField("OrderDate"); tableOrders.Fields.AddField("CustomerID"); tableOrders.Fields.AddField("ResellerID"); // create another table MetadataObject tableCustomers = queryBuilder.MetadataContainer.AddTable("MySchema", "Customers", false, "MyDB"); tableCustomers.Fields.AddField("CustomerID"); tableCustomers.Fields.AddField("CustomerName"); tableCustomers.Fields.AddField("CustomerAddress"); // add a relation between these two tables MetadataRelation relation = tableCustomers.Relations.Add(); relation.ChildSchemaStr = "MySchema"; relation.ChildDatabaseStr = "MyDB"; relation.ChildNameStr = "Orders"; relation.KeyFields.Add("CustomerID"); relation.ChildFields.Add("CustomerID"); //create view MetadataObject viewResellers = queryBuilder.MetadataContainer.AddView("MySchema", "Resellers", false, "MyDB"); viewResellers.Fields.AddField("ResellerID"); viewResellers.Fields.AddField("ResellerName"); }
To add case sensitive object, database, schema or field names you should quote them according to the quotation rules of your database server. -
Filling database object field lists by demand.
Active Query Builder allows adding fields for each database object not right after it's addition to the Metadata Container, but fill them when they are used in some query at the first time using the QueryBuilder.MetadataContainer.ObjectMetadataLoading event handler:void MetadataContainer_ObjectMetadataLoading(MetadataContainer sender, MetadataObject metadataObject) { if (metadataObject.NameStr == "Orders") { MetadataFieldList fields = metadataObject.Fields; fields.AddField("Field 1").FieldTypeName = "nvarchar"; fields.AddField("Field 2").FieldTypeName = "int"; fields.Loaded = true; } }
-
Modifying field lists after loading from the database.
You can remove unwanted fields after they were loaded from the database in the QueryBuilder.MetadataContainer.ObjectMetadataLoaded event handler:void MetadataContainer_ObjectMetadataLoaded(MetadataContainer sender, MetadataObject metadataObject) { if (metadataObject.NameStr == "Orders") { metadataObject.Fields.Delete(metadataObject.Fields.FindFieldByName("Freight")); metadataObject.Fields.Delete(metadataObject.Fields.FindFieldByName("ShipVia")); } }