org.eclipse.datatools.modelbase.sql.query.helper
Class JoinHelper

java.lang.Object
  extended by org.eclipse.datatools.modelbase.sql.query.helper.JoinHelper

public class JoinHelper
extends java.lang.Object

This is a helper class that provides methods that assist in creating and manipulating SQL joins.


Field Summary
static java.lang.String BOTH
           
static java.lang.String copyright
           
static java.lang.String GENERIC_BINARY
           
static java.lang.String GENERIC_BLOB
           
static java.lang.String GENERIC_CHARACTER
           
static java.lang.String GENERIC_DATE
           
static java.lang.String GENERIC_DECIMAL
           
static java.lang.String GENERIC_INTEGER
           
static java.lang.String GENERIC_OTHER
           
static java.lang.String GENERIC_TIME
           
static java.lang.String GENERIC_TIMESTAMP
           
static int JOIN_COL_TYPE_MISMATCH
           
static int JOIN_COL_USED
           
static int JOIN_SAME_TABLE
           
static int JOIN_VALID
           
static java.lang.String LEFT
           
static java.lang.String NONE
           
static java.lang.String RIGHT
           
 
Constructor Summary
JoinHelper()
           
 
Method Summary
static TableJoined addJoin(java.util.List fromClause, TableExpression sourceTable, TableExpression targetTable, ValueExpressionColumn sourceColumn, ValueExpressionColumn targetColumn, int joinType)
          Creates a new joined table or join condition containing the given join source and target objects, and updates the given FROM clause list.
static TableJoined addJoinedTable(java.util.List fromClause, TableReference joinSource, TableReference joinTarget, int joinType)
          Creates a new joined table containing the given join source and target objects, and adds it to the given FROM clause list.
static PredicateBasic buildPredicateBasic(QueryValueExpression leftExpr, QueryValueExpression rightExpr, java.lang.String oper)
          Creates a PredicateBasic object using the given expressions and operator.
static QuerySearchCondition buildSearchCondition(QuerySearchCondition currentSearchCon, QueryValueExpression leftExpr, QueryValueExpression rightExpr, java.lang.String oper)
          Creates a new predicate and adds it to the given search condition.
static int checkJoin(java.util.List fromClause, TableExpression sourceTable, TableExpression targetTable, ValueExpressionColumn sourceColumn, ValueExpressionColumn targetColumn, boolean isMove)
          Checks whether or not a join between the given join source and target objects is valid.
static boolean checkJoinType(ValueExpressionColumn sourceColumn, ValueExpressionColumn targetColumn)
          Determines if source and target columns are of compatible type for joining.
static boolean conditionContainsTable(QuerySearchCondition condition, TableExpression table)
          Checks to see if the given SQLSearchCondition (ON clause) contains the given table.
static TableJoined findClosestContainingJoin(TableExpression joinSource, TableExpression joinTarget)
          Finds and returns the closest enclosing join containing both the given source and target tables, working from the leafs of the join tree to the root.
static java.util.List findConditionsContainingTable(QuerySearchCondition cond, TableExpression table)
          Finds and returns the individual conditions (predicates) in the given QuerySearchCondition (ON clause) that contain references to the given table.
static java.util.List findJoinsWithTableInCondition(TableExpression table, TableJoined join)
          Searches upwards in the join tree to see if the given table exists in any of the conditions in the joins.
static TableReference findOutermostContainingJoin(java.util.List fromClause, TableExpression targetTable)
          Finds and returns the outermost table reference in the From clause that contains the given table.
static java.lang.String getGenericType(java.lang.Object datatype)
          Returns a string indicating the "generic" type for the given datatype object.
static java.util.List getLeftJoinsForTable(TableExpression tableExpr)
          Returns a list of TableJoined objects of which the given TableExpression is the left side.
static TableReference getNestedTable(TableReference tableRef)
          Returns the table "nested" in the given table, if any.
static java.util.List getRightJoinsForTable(TableExpression tableExpr)
          Returns a list of TableJoined objects of which the given TableExpression is the right side.
static java.util.List getTablesInJoin(TableJoined join)
          Gets a list of TableExpression objects embedded inside the given join object.
static java.util.List getTablesInNestedTable(TableNested tableNested)
          Returns a list of TableExpressions contained in the given TableNested object.
static void removeJoin(java.util.List fromClause, TableJoined joinedTable)
          Removes the given joined table from the given FROM clause list.
static void removeJoinCondition(java.util.List fromClause, TableJoined joinedTable, PredicateBasic joinCond)
          Removes the given join condition (PredicateBasic) from the given joined table.
static void removeJoinConditionsForTables(java.util.List fromClause, java.util.List tableList, java.util.List joinList)
          Unravels the content (left or right) of a join by removing all ON clause conditions in the given join list that refer to the tables in the given table list.
static void removeJoinsForTable(java.util.List fromClause, TableExpression table)
          Removes any joins from the FROM clause list that contain or reference the given table.
static QuerySearchCondition removePredicateFromCondition(Predicate pred, QuerySearchCondition searchCon)
          Removes the given predicate from the given search condition.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

copyright

public static final java.lang.String copyright
See Also:
Constant Field Values

GENERIC_CHARACTER

public static final java.lang.String GENERIC_CHARACTER
See Also:
Constant Field Values

GENERIC_INTEGER

public static final java.lang.String GENERIC_INTEGER
See Also:
Constant Field Values

GENERIC_DECIMAL

public static final java.lang.String GENERIC_DECIMAL
See Also:
Constant Field Values

GENERIC_BINARY

public static final java.lang.String GENERIC_BINARY
See Also:
Constant Field Values

GENERIC_DATE

public static final java.lang.String GENERIC_DATE
See Also:
Constant Field Values

GENERIC_TIME

public static final java.lang.String GENERIC_TIME
See Also:
Constant Field Values

GENERIC_TIMESTAMP

public static final java.lang.String GENERIC_TIMESTAMP
See Also:
Constant Field Values

GENERIC_BLOB

public static final java.lang.String GENERIC_BLOB
See Also:
Constant Field Values

GENERIC_OTHER

public static final java.lang.String GENERIC_OTHER
See Also:
Constant Field Values

JOIN_COL_TYPE_MISMATCH

public static final int JOIN_COL_TYPE_MISMATCH
See Also:
Constant Field Values

JOIN_COL_USED

public static final int JOIN_COL_USED
See Also:
Constant Field Values

JOIN_SAME_TABLE

public static final int JOIN_SAME_TABLE
See Also:
Constant Field Values

JOIN_VALID

public static final int JOIN_VALID
See Also:
Constant Field Values

LEFT

public static final java.lang.String LEFT
See Also:
Constant Field Values

RIGHT

public static final java.lang.String RIGHT
See Also:
Constant Field Values

BOTH

public static final java.lang.String BOTH
See Also:
Constant Field Values

NONE

public static final java.lang.String NONE
See Also:
Constant Field Values
Constructor Detail

JoinHelper

public JoinHelper()
Method Detail

addJoin

public static TableJoined addJoin(java.util.List fromClause,
                                  TableExpression sourceTable,
                                  TableExpression targetTable,
                                  ValueExpressionColumn sourceColumn,
                                  ValueExpressionColumn targetColumn,
                                  int joinType)
Creates a new joined table or join condition containing the given join source and target objects, and updates the given FROM clause list. If a join does not already exist between the two tables, a new joined table object is created and added to the FROM clause. If the given tables are already joined, a new joined table is not created, but a new join condition is added to the ON clause of the join. The new or modified joined table is returned.

Parameters:
fromClause - the list of objects (tables and joins) in the FROM clause
sourceTable - the source (left side) table of the join
targetTable - the target (right side) table of the join
sourceColumn - the source column expression of the join
targetColumn - the target column expression of the join
joinType - the type of join (inner, left outer, etc.)
Returns:
the new or modified join object

addJoinedTable

public static TableJoined addJoinedTable(java.util.List fromClause,
                                         TableReference joinSource,
                                         TableReference joinTarget,
                                         int joinType)
Creates a new joined table containing the given join source and target objects, and adds it to the given FROM clause list. The new join table is returned. If the source and target are already part of the same joined table in the FROM clause list, then a new joined table is not created, and the existing joined table is returned.

Parameters:
fromClause - the list of objects (tables and joins) in the FROM clause
joinSource - the source (left side) of the join
joinTarget - the target (right side) of the join
joinType - the type of join (inner, left outer, etc.)
Returns:
the joined table containing the join source and target objects

buildPredicateBasic

public static PredicateBasic buildPredicateBasic(QueryValueExpression leftExpr,
                                                 QueryValueExpression rightExpr,
                                                 java.lang.String oper)
Creates a PredicateBasic object using the given expressions and operator.

Parameters:
leftExpr - the left side expression
rightExpr - the right side expression
oper - the operator
Returns:
the new PredicateBasic object

buildSearchCondition

public static QuerySearchCondition buildSearchCondition(QuerySearchCondition currentSearchCon,
                                                        QueryValueExpression leftExpr,
                                                        QueryValueExpression rightExpr,
                                                        java.lang.String oper)
Creates a new predicate and adds it to the given search condition.

Parameters:
currentSearchCon - the existing search condition or null
leftExpr - the left side expression of the new predicate
rightExpr - the right side expression of the new predicate
oper - the operator string (eg: "=" )
Returns:
the new search condition object

checkJoin

public static int checkJoin(java.util.List fromClause,
                            TableExpression sourceTable,
                            TableExpression targetTable,
                            ValueExpressionColumn sourceColumn,
                            ValueExpressionColumn targetColumn,
                            boolean isMove)
Checks whether or not a join between the given join source and target objects is valid. Returns one of the following status codes:
JOIN_VALID
OK to join
JOIN_COL_TYPE_MISMATCH
the source and target have incompatible datatypes
JOIN_SAME_TABLE
the source and column are in the same table
JOIN_COL_USED
one of the columns is already used in a join between the same two tables

Parameters:
fromClause - the list of objects (tables and joins) in the FROM clause
sourceTable - the source (left side) table of the join
targetTable - the target (right side) table of the join
sourceColumn - the source column of the join
targetColumn - the target column of the join
isMove - true when a join is being moved, false when a join is being created
Returns:
a result code

checkJoinType

public static boolean checkJoinType(ValueExpressionColumn sourceColumn,
                                    ValueExpressionColumn targetColumn)
Determines if source and target columns are of compatible type for joining.

Parameters:
sourceColumn - the source column to check
targetColumn - the target column to check
Returns:
true when the columns are of join compatible, otherwise false

conditionContainsTable

public static boolean conditionContainsTable(QuerySearchCondition condition,
                                             TableExpression table)
Checks to see if the given SQLSearchCondition (ON clause) contains the given table.

Parameters:
condition - the condition to search
table - the table to search for
Returns:
true if the condition contains the table, otherwise false

findClosestContainingJoin

public static TableJoined findClosestContainingJoin(TableExpression joinSource,
                                                    TableExpression joinTarget)
Finds and returns the closest enclosing join containing both the given source and target tables, working from the leafs of the join tree to the root. Returns null if the tables are not contained in the same join tree.

Parameters:
joinSource - the source (left side) of a join
joinTarget - the target (right side) of a join
Returns:
closest enclosing join or null if the tables are not part of the same join

findConditionsContainingTable

public static java.util.List findConditionsContainingTable(QuerySearchCondition cond,
                                                           TableExpression table)
Finds and returns the individual conditions (predicates) in the given QuerySearchCondition (ON clause) that contain references to the given table.

Parameters:
cond - the QuerySearchCondition to search
table - the TableExpression to use to search for predicates
Returns:
a list of PredicateBasic objects containing references to the table

findJoinsWithTableInCondition

public static java.util.List findJoinsWithTableInCondition(TableExpression table,
                                                           TableJoined join)
Searches upwards in the join tree to see if the given table exists in any of the conditions in the joins.

Parameters:
table - the table to be searched for
join - the join at which the search starts
Returns:
the list of joins whose onclause condition contains the table, or an empty list if no such join is found

findOutermostContainingJoin

public static TableReference findOutermostContainingJoin(java.util.List fromClause,
                                                         TableExpression targetTable)
Finds and returns the outermost table reference in the From clause that contains the given table. That might be a joined table reference, or the given table itself if it is not contained in any join.

Parameters:
fromClause - the list of objects (tables and joins) in the FROM clause
targetTable - the table reference to be looked for
Returns:
the joined table containing the table, or the table itself if it is not part of a join

getLeftJoinsForTable

public static java.util.List getLeftJoinsForTable(TableExpression tableExpr)
Returns a list of TableJoined objects of which the given TableExpression is the left side. Traverses up the join tree finding the joins.

Parameters:
tableExpr - the TableExpression for which to find the joins

getNestedTable

public static TableReference getNestedTable(TableReference tableRef)
Returns the table "nested" in the given table, if any. If there is no nested table, then the given table is returned. Nested tables are used to represent parenthesized table expressions explicitly in places where the parens are implied. For example, it's possible to say "SELECT * FROM (TABLE1 JOIN TABLE2 ON A = B)".

Parameters:
tableRef - the table reference from which the nested table is wanted
Returns:
the nested table reference

getRightJoinsForTable

public static java.util.List getRightJoinsForTable(TableExpression tableExpr)
Returns a list of TableJoined objects of which the given TableExpression is the right side. Traverses up the join tree finding the joins.

Parameters:
tableExpr - the TableExpression for which to find the joins

getGenericType

public static java.lang.String getGenericType(java.lang.Object datatype)
Returns a string indicating the "generic" type for the given datatype object. All string types, for example, map to "GENERIC_CHARACTER". See the class static constants for the list of generic types.

Parameters:
datatype - the datatype for which a generic type is needed

getTablesInJoin

public static java.util.List getTablesInJoin(TableJoined join)
Gets a list of TableExpression objects embedded inside the given join object.

Parameters:
join - the join from which the tables are to be obtained
Returns:
the list of TableExpression objects in the given join.

getTablesInNestedTable

public static java.util.List getTablesInNestedTable(TableNested tableNested)
Returns a list of TableExpressions contained in the given TableNested object.

Parameters:
tableNested - the TableNested object from which the TableExpressions are to be obtained
Returns:
the list of TableExpressions

removeJoin

public static void removeJoin(java.util.List fromClause,
                              TableJoined joinedTable)
Removes the given joined table from the given FROM clause list. Depending on the structure of the joined table, one or both the left and right side of the join may be removed from the join tree that contains it and added directly to the FROM clause list.

Parameters:
fromClause - the list of objects (tables and joins) in the FROM clause
the - join to be removed

removeJoinCondition

public static void removeJoinCondition(java.util.List fromClause,
                                       TableJoined joinedTable,
                                       PredicateBasic joinCond)
Removes the given join condition (PredicateBasic) from the given joined table. If removing the condition leaves the ON clause empty, then the join itself is removed from the join tree.

Parameters:
fromClause - the list of objects (tables and joins) in the FROM clause
joinedTable - the join from which the join condition is to be removed
joinCond - the join condition to be removed

removeJoinConditionsForTables

public static void removeJoinConditionsForTables(java.util.List fromClause,
                                                 java.util.List tableList,
                                                 java.util.List joinList)
Unravels the content (left or right) of a join by removing all ON clause conditions in the given join list that refer to the tables in the given table list. Joins who's ON clause become empty are removed from the join tree.

Parameters:
fromClause - the list of objects (tables and joins) in the FROM clause
tableList - a list of tables for which joins are to be removed
joinList - a list of joins to be searched for conditions involving the tables and updated or removed

removeJoinsForTable

public static void removeJoinsForTable(java.util.List fromClause,
                                       TableExpression table)
Removes any joins from the FROM clause list that contain or reference the given table.

Parameters:
fromClause - the list of objects (tables and joins) in the FROM clause
table - the table for which joins should be removed

removePredicateFromCondition

public static QuerySearchCondition removePredicateFromCondition(Predicate pred,
                                                                QuerySearchCondition searchCon)
Removes the given predicate from the given search condition.

Parameters:
pred - the Predicate which needs to be removed from the condition
searchCon - the SearchCondition from which predicate needs to be removed
Returns:
the modified searchCondition after removing the predicate