Check if any data sources are unjoined in the query

Answered

Comments

4 comments

  • Official comment
    Avatar
    Sergey Smagin (Product manager)

    Hello, Marc.

    Your code looks just fine. 

    The only point is that it checks only the active UnionSubQuery object while the query may have unions and sub-queries. You must iterate through all of them to check the whole query. This article has samples of collecting all union sub-queries for the v.2 (on which the current ASP.NET version is based) and v.3: Analyzing complex SQL query structure (with unions and sub-queries)

    Comment actions Permalink
  • Avatar
    Marc

    Thanks. Here's my revised function. Does this look OK to you?

       private bool queryHasUnjoinedDataSources() {

                // collect all Union Sub-queries
                var allUnionSubQueries = QueryBuilderControl1.QueryBuilder.Query.GetChildrenRecursive<UnionSubQuery>(true);

                foreach (UnionSubQuery usq in allUnionSubQueries) {

                    var dataSources = new List<DataSource>();
                    usq.FromClause.GetDatasources(dataSources);

                     if (dataSources.Count > 1) {
                        //get the linked data sources for this usq
                        var links = new ArrayList();
                       usq.FromClause.GetLinksRecursive(links);

    var linkedDataSources = new System.Collections.Generic.HashSet<DataSource>();
                        foreach (Link link in links) {
                            linkedDataSources.Add(link.LeftDatasource);
                            linkedDataSources.Add(link.RightDatasource);
                        }

        foreach (DataSource dataSource in allDataSources) {
                        if (!linkedDataSources.Contains(dataSource)) {
                             return true;
                         }
                     }
                    }
                }

                 //if we got here, all of the dataSources have joins
                return false;

            }

     

    0
    Comment actions Permalink
  • Avatar
    Sergey Smagin (Product manager)

    Hello, 

    The code looks fine.

    One notice: the "dataSources[0].Query" can be replaced with "usq".

    0
    Comment actions Permalink
  • Avatar
    Marc

    Thank you

    0
    Comment actions Permalink

Please sign in to leave a comment.