Class QueryGraph


  • public class QueryGraph
    extends java.lang.Object
    A class to create a network of all the classes used in a query, using foreign keys and other relationships.

    A undirected, possibly-cyclic, database-agnostic, graph of DBRow nodes connected by bi-directional edges created from known foreign keys and the expressions used in the query.

    This class is used to identify cartesian joins by establishing which tables are included in the join, and which are not. If any node cannot be reached by traversing the graph, a AccidentalCartesianJoinException may be thrown during querying.

    Ignored foreign keys are not included as edges.

    DBRows returned by DBExpression.getTablesInvolved() will be used to creates edges from expressions in the query.

    Support DBvolution at Patreon

    Author:
    Gregory Graham
    • Constructor Summary

      Constructors 
      Constructor Description
      QueryGraph​(java.util.List<DBRow> allQueryTables, java.util.List<BooleanExpression> expressions)
      Create a graph of the tables and connections for all the DBRows and BooleanExpressions provided.
    • Constructor Detail

      • QueryGraph

        public QueryGraph​(java.util.List<DBRow> allQueryTables,
                          java.util.List<BooleanExpression> expressions)
        Create a graph of the tables and connections for all the DBRows and BooleanExpressions provided.
        Parameters:
        allQueryTables - all the tables in the query
        expressions - the boolean expressions that connect the tables
    • Method Detail

      • clear

        public QueryGraph clear()
        Removes all state and prepares the graph for re-initialization.
        Returns:
        this QueryGraph.
      • addAndConnectToRelevant

        public final void addAndConnectToRelevant​(java.util.List<DBRow> otherTables,
                                                  java.util.List<BooleanExpression> expressions)
        Add the provided DBRows/tables and expressions to this QueryGraph, generating new nodes and edges as required.
        Parameters:
        otherTables - the tables to add to the query
        expressions - the expressions that connect the tables
      • addOptionalAndConnectToRelevant

        public final void addOptionalAndConnectToRelevant​(java.util.List<DBRow> otherTables,
                                                          java.util.List<BooleanExpression> expressions)
        Add the provided DBRows/tables and expressions to this QueryGraph, generating new nodes and edges as required.

        The DBrows/tables will be added as optional (that is "outer join" tables) and displayed as such.

        Parameters:
        otherTables - the tables to add to the query
        expressions - the expressions that connect the tables
      • addAndConnectToRelevant

        public final void addAndConnectToRelevant​(java.util.List<DBRow> otherTables,
                                                  java.util.List<BooleanExpression> expressions,
                                                  boolean requiredTables)
        Add the provided DBRows/tables and expressions to this QueryGraph, generating new nodes and edges as required.

        The DBrows/tables will be added as optional (that is "outer join" tables) if requiredTables is FALSE.

        Parameters:
        otherTables - the tables to add to the query
        expressions - the expressions that connect the tables
        requiredTables - TRUE if the tables are requires, FALSE if the tables are optional
      • willCreateCartesianJoin

        public boolean willCreateCartesianJoin()
        Scans the QueryGraph to detect disconnected DBRows/tables and returns TRUE if a disconnection exists.

        Support DBvolution at Patreon

        Returns:
        TRUE if the current graph contains a discontinuity which will cause a cartesian join to occur.
      • willCreateFullOuterJoin

        public boolean willCreateFullOuterJoin()
        Scans the QueryGraph to detect full outer join.
        Returns:
        TRUE contains only optional tables, FALSE otherwise.
      • toList

        public java.util.List<DBRow> toList()
        Return tables in the QueryGraph as a list.

        Starting from a semi-random table (see getStartTable()) traverse the graph and add all nodes found to the list.

        This method does not check for discontinuities. If there is a cartesian join/discontinuity present only some of the nodes will be returned. Use toListIncludingCartesian() if you need to span a discontinuity.

        Some optimization is attempted, by trying to include all the required/inner tables first before adding the optional/outer tables. This avoids a common problem of a query that spans the intersection of 2 optional/outer tables, creating mid-query cartesian join that could have been avoided by including a related required/inner table first.

        Returns:
        a list of all DBRows in this QueryGraph in a smart an order as possible.
      • toListReversable

        public java.util.List<DBRow> toListReversable​(boolean reversed)
        Return tables in the QueryGraph as a list.

        Starting from a semi-random table (see getStartTable()) traverse the graph and add all nodes found to the list.

        This method does not check for discontinuities. If there is a cartesian join/discontinuity present only some of the nodes will be returned. Use toListIncludingCartesian() if you need to span a discontinuity.

        Some optimization is attempted, by trying to include all the required/inner tables first before adding the optional/outer tables. This avoids a common problem of a query that spans the intersection of 2 optional/outer tables, creating mid-query cartesian join that could have been avoided by including a related required/inner table first.

        Parameters:
        reversed - TRUE if the list should be reversed, FALSE otherwise
        Returns:
        a list of all DBRows in this QueryGraph in a smart an order as possible.
      • toListIncludingCartesian

        public java.util.List<DBRow> toListIncludingCartesian()
        Return all tables in the QueryGraph as a list.

        Starting from a semi-random table (see getStartTable()) traverse the graph and add all nodes found to the list.

        This method scans across discontinuities. If there is a cartesian join/discontinuity present all of the nodes will be returned. Use toList() if you need to avoid spanning a discontinuity.

        Some optimization is attempted, by trying to include all the required/inner tables first before adding the optional/outer tables. This avoids a common problem of a query that spans the intersection of 2 optional/outer tables, creating mid-query cartesian join that could have been avoided by including a related required/inner table first.

        Returns:
        a list of all DBRows in this QueryGraph in a smart an order as possible.
      • toListIncludingCartesianReversable

        public java.util.List<DBRow> toListIncludingCartesianReversable​(java.lang.Boolean reverse)
        Return all tables in the QueryGraph as a list.

        Starting from a semi-random table (see getStartTable()) traverse the graph and add all nodes found to the list.

        This method scans across discontinuities. If there is a cartesian join/discontinuity present all of the nodes will be returned. Use toList() if you need to avoid spanning a discontinuity.

        Some optimization is attempted, by trying to include all the required/inner tables first before adding the optional/outer tables. This avoids a common problem of a query that spans the intersection of 2 optional/outer tables, creating mid-query cartesian join that could have been avoided by including a related required/inner table first.

        Parameters:
        reverse - TRUE if the list needs to be reversed, FALSE otherwise
        Returns:
        a list of all DBRows in this QueryGraph in a smart an order as possible.
      • getJungGraph

        public edu.uci.ics.jung.graph.Graph<QueryGraphNode,DBExpression> getJungGraph()
        Create a Jung graph of this QueryGraph for display purposes.

        Other graphs are available but we use Graph.

        Returns:
        a Jung Graph.