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

studio.raptor.sqlparser.fast.expression.Parameter 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.api.ErrorCode;
import studio.raptor.sqlparser.fast.message.ParseException;
import studio.raptor.sqlparser.fast.value.Value;
import studio.raptor.sqlparser.fast.value.ValueNull;
import studio.raptor.sqlparser.fast.table.Column;
import studio.raptor.sqlparser.fast.table.ColumnResolver;

/**
 * A parameter of a prepared statement.
 */
public class Parameter extends Expression implements ParameterInterface {

  private final int index;
  private Value value;
  private Column column;

  public Parameter(int index) {
    this.index = index;
  }

  @Override
  public String getSQL() {
    return "?" + (index + 1);
  }

  @Override
  public void setValue(Value v, boolean closeOld) {
    // don't need to close the old value as temporary files are anyway
    // removed
    this.value = v;
  }

  @Override
  public Value getParamValue() {
    if (value == null) {
      // to allow parameters in function tables
      return ValueNull.INSTANCE;
    }
    return value;
  }

  @Override
  public Value getValue() {
    return getParamValue();
  }

  public void setValue(Value v) {
    this.value = v;
  }

  @Override
  public int getType() {
    if (value != null) {
      return value.getType();
    }
    return Value.UNKNOWN;
  }

  @Override
  public void mapColumns(ColumnResolver resolver, int level) {
    // can't map
  }

  @Override
  public void checkSet() {
    if (value == null) {
      throw ParseException.get(ErrorCode.PARAMETER_NOT_SET_1, "#" + (index + 1));
    }
  }

  @Override
  public Expression optimize() {
    return this;
  }

  @Override
  public boolean isConstant() {
    return false;
  }

  @Override
  public boolean isValueSet() {
    return value != null;
  }

  @Override
  public boolean isEverything(ExpressionVisitor visitor) {
    switch (visitor.getType()) {
      case ExpressionVisitor.EVALUATABLE:
        // the parameter _will_be_ evaluatable at execute time
      case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
        // it is checked independently if the value is the same as the last
        // time
      case ExpressionVisitor.NOT_FROM_RESOLVER:
      case ExpressionVisitor.QUERY_COMPARABLE:
      case ExpressionVisitor.GET_DEPENDENCIES:
      case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
      case ExpressionVisitor.DETERMINISTIC:
      case ExpressionVisitor.READONLY:
      case ExpressionVisitor.GET_COLUMNS:
        return true;
      case ExpressionVisitor.INDEPENDENT:
        return value != null;
      default:
        throw ParseException.throwInternalError("type=" + visitor.getType());
    }
  }

  public void setColumn(Column column) {
    this.column = column;
  }

  public int getIndex() {
    return index;
  }

  @Override
  public int getNullable() {
    return 0;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy