HomeActive Query Builder - Metadata handling and filtrationMetadata handling and filtration in Java, VCL and ActiveX EditionsLoading metadata programmatically

2.4. Loading metadata programmatically

Warning:

This article is valid for Java, ActiveX and VCL editions.

The appropriate article for Active Query Builder 2 .NET Editon can be found here:

  1. Adding database objects to the Metadata Container.

    Metadata Container is 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.

  2. Filling database object field lists by demand.

    Active Query Builder allows to add fields for each database object not right after it's addition to the Metadata Container, but fill them when they will be 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;
           }
    }
    
  3. 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"));
           }
    }
    

This page was: Helpful | Not Helpful