Class ExpressionAlias

java.lang.Object
de.calamanari.adl.sql.cnv.ExpressionAlias
All Implemented Interfaces:
Comparable<ExpressionAlias>

public class ExpressionAlias extends Object implements Comparable<ExpressionAlias>
An ExpressionAlias represents what we know (information gathered while building the query) about an alias query (WITH) for certain detail expression.

The term alias refers to an entity that identifies a positive condition on a single attribute in memory.
Aliases are always created from a given CoreExpression. The latter is either a SimpleExpression or a not further nested CombinedExpression. Such a combined expression can be either an OR of MatchExpressions related to the same argument or an AND of NegationExpressions related to the same argument reflecting an SQL IN resp. NOT IN clause.

Important: As mentioned the expression expression stored in the alias is always positive. So, even if you created the alias from an AND of negations (aka NOT IN) then the expression will be the corresponding OR.

The motivation for this approach was to avoid having the same logical condition multiple times (negated). The alias allows to keep track across a larger expression if the same condition is referenced again (or its inversion).

Not all aliases later occur with their name in the created SQL. If possible the raw table names will be preferred over referencing the aliases.

The identity of an alias is solely based on its name.

Author:
Karl Eilebrecht
  • Constructor Details

    • ExpressionAlias

      public ExpressionAlias(String name, de.calamanari.adl.irl.CoreExpression expression)
      Parameters:
      name - the identity, two instances with the same name are equal, NOT NULL
      expression - the sub-expression this alias stands for, NOT NULL
  • Method Details

    • getExpression

      public de.calamanari.adl.irl.CoreExpression getExpression()
      This returns the expression (resp. the positive form) behind this alias. Even if you have created the alias with an AND of negations this method will return an OR of match expressions. The negation is counted here: getNegativeReferenceCount()
      Returns:
      the core expression backing this alias
    • getName

      public String getName()
      Returns:
      technical (SQL-conforming) name of this alias
    • getPositiveReferenceCount

      public int getPositiveReferenceCount()
      Returns:
      Number of times this alias was referenced positively (e.g. color = red)
    • getNegativeReferenceCount

      public int getNegativeReferenceCount()
      Returns:
      Number of times this alias was referenced negatively (e.g. color != red)
    • registerPositiveReference

      public void registerPositiveReference()
      Increments the number of positive references to this alias
    • registerNegativeReference

      public void registerNegativeReference()
      Increments the number of negative references to this alias (counts a negation)
    • requiresAllRowsOfTableQueryInUnion

      public boolean requiresAllRowsOfTableQueryInUnion()
      This method determines if a query required the positive result set and its inversion within the same query.

      When we select rows from a table given a certain condition (the alias stands for, e.g. color=blue) and in the same query anywhere else we reference the inversion of that selection (here: color!=blue), then obviously the result set of the alias cannot serve as (part of) a base query that consists of an alias union.

      In this case the alias condition must be dropped and the whole table must be included in the union not to miss any rows.

      Returns:
      true if the alias must be replaced with its full table row set (select *) if the alias should be included in a base query union
    • isReferenceMatch

      public boolean isReferenceMatch()
      Returns:
      checks if the expression of this alias is a reference match
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(ExpressionAlias o)
      Specified by:
      compareTo in interface Comparable<ExpressionAlias>
    • toString

      public String toString()
      Overrides:
      toString in class Object