Active Query Builder support area

How to take additional actions when the user adds an object to the Design Pane?

Last modified:


Active Query Builder allows customizing objects added to the Design Pane via the QueryBuilder.DatasourceAdded event.

The following code checks all fields of the newly added datasource. The next sample illustrates adding of alias for a newly added datasource.

Adding fields of the just-added object to the Query Columns List and adding them to the SELECT list.

private void queryBuilder1_DataSourceAdded(SQLQuery sender, DataSource addedObject)
{
    // Select all fields
    QueryColumnList queryColumnList = addedObject.ParentUnionSubQuery.QueryColumnList;
using (new UpdateRegion(queryColumnList)); { MetadataSelection<MetadataField> fields = addedObject.Metadata.Fields; foreach (MetadataField field in fields) { QueryColumnListItem queryColumn = queryColumnList.FindField(addedObject, field.Name); if (queryColumn == null) queryColumn = queryColumnList.AddField(addedObject, field.Name); queryColumn.Selected = true; } } }

 

Adding an alias to the just-added object.

private void queryBuilder1_DataSourceAdded(SQLQuery sender, DataSource addedObject)
{
    if (addedObject.MetadataObject != null)
    {
        using (AstTokenIdentifier alias = sender.CreateUniqueAlias(addedObject.ParentUnionSubQuery, addedObject.MetadataObject.Name))
        {
            addedObject.Alias = alias.Token;
        }
    }
}

 

Changing type of all joins with the just-added object from INNER to LEFT OUTER.

private void queryBuilder1_DataSourceAdded(SQLQuery sender, DataSource addedObject)
{
    UnionSubQuery currentSubQuery = addedObject.ParentUnionSubQuery;
using (new UpdateRegion(currentSubQuery)) { // find all links referencing new dataSource var links = currentSubQuery.GetChildrenRecursive<Link>(false) .Where(l => l.LeftDataSource == addedObject || l.RightDataSource == addedObject); // modify foreach (var link in links) { if (link.LeftDataSource == addedObject) link.RightType = LinkSideType.Outer; else link.LeftType = LinkSideType.Outer; } } }

Adding fields of the just-added object to the Query Columns List and adding them to the SELECT list.

private void queryBuilder_DatasourceAdded(UnionSubQuery query, DataSource addedObject)
{
    // Select all fields

    CriteriaList cl = query.CriteriaList;
    CriteriaItem ci;
     
    cl.BeginUpdate();
     
    try
    {
        for (int i = 0; i < addedObject.Fields.Count; i++)
        {
            ci = cl.FindField(addedObject, addedObject.Fields[i].Name);
             
            if (ci == null)
            {
                ci = cl.AddField(addedObject, addedObject.Fields[i].Name);
            }
             
            ci.Select = true;
        }
    }
    finally
    {
        cl.EndUpdate();
    }
}

 

Adding an alias to the just-added object.

private void queryBuilder_DatasourceAdded(UnionSubQuery query, DataSource addedObject)
{
    if (addedObject.MetadataObject != null)
    {
        using (AstTokenIdentifier alias = query.CreateUniqueAlias(addedObject.MetadataObject.Name))
        {
            addedObject.Alias = alias.Token;
        }
    }
}

Is this article helpful for you?