All Downloads are FREE. Search and download functionalities are using the official Maven repository.

studio.raptor.sqlparser.fast.expression.Expression Maven / Gradle / Ivy

/*
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
 * and the EPL 1.0 (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 */
package studio.raptor.sqlparser.fast.expression;

import studio.raptor.sqlparser.fast.table.ColumnResolver;
import studio.raptor.sqlparser.fast.util.StringUtils;
import studio.raptor.sqlparser.fast.value.Value;

/**
 * An expression is a operation, a value, or a function in a query.
 */
public abstract class Expression {

  /**
   * Map the columns of the resolver to expression columns.
   *
   * @param resolver the column resolver
   * @param level the subquery nesting level
   */
  public abstract void mapColumns(ColumnResolver resolver, int level);

  /**
   * Return the resulting value for the current row.
   *
   * @return the result
   */
  public abstract Value getValue();

  /**
   * Return the data type. The data type may not be known before the
   * optimization phase.
   *
   * @return the type
   */
  public abstract int getType();

  /**
   * Try to optimize the expression.
   *
   * @return the optimized expression
   */
  public abstract Expression optimize();

  /**
   * Get the SQL statement of this expression.
   * This may not always be the original SQL statement,
   * specially after optimization.
   *
   * @return the SQL statement
   */
  public abstract String getSQL();

  /**
   * Check if this expression and all sub-expressions can fulfill a criteria.
   * If any part returns false, the result is false.
   *
   * @param visitor the visitor
   * @return if the criteria can be fulfilled
   */
  public abstract boolean isEverything(ExpressionVisitor visitor);

  /**
   * Check if this expression will always return the same value.
   *
   * @return if the expression is constant
   */
  public boolean isConstant() {
    return false;
  }

  /**
   * Is the value of a parameter set.
   *
   * @return true if set
   */
  public boolean isValueSet() {
    return false;
  }

  /**
   * Get the column name or alias name of this expression.
   *
   * @return the column name
   */
  public String getColumnName() {
    return getAlias();
  }

  /**
   * Get the table name, or null
   *
   * @return the table name
   */
  public String getTableName() {
    return null;
  }

  /**
   * Get the table alias name or null
   * if this expression does not represent a column.
   *
   * @return the table alias name
   */
  public String getTableAlias() {
    return null;
  }

  /**
   * Get the alias name of a column or SQL expression
   * if it is not an aliased expression.
   *
   * @return the alias name
   */
  public String getAlias() {
    return StringUtils.unEnclose(getSQL());
  }

  /**
   * Only returns true if the expression is a wildcard.
   *
   * @return if this expression is a wildcard
   */
  public boolean isWildcard() {
    return false;
  }

  /**
   * Returns the main expression, skipping aliases.
   *
   * @return the expression
   */
  public Expression getNonAliasExpression() {
    return this;
  }


  /**
   * Convert this expression to a String.
   *
   * @return the string representation
   */
  @Override
  public String toString() {
    return getSQL();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy