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

studio.raptor.sqlparser.fast.command.Prepared 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.command;

import java.util.ArrayList;
import studio.raptor.sqlparser.fast.expression.Expression;
import studio.raptor.sqlparser.fast.expression.Parameter;
import studio.raptor.sqlparser.fast.table.Table;
import studio.raptor.sqlparser.fast.util.StatementBuilder;
import studio.raptor.sqlparser.fast.value.Value;

/**
 * A prepared statement.
 */
public abstract class Prepared {

  protected Table table;

  /**
   * The SQL string.
   */
  protected String sqlStatement;

  /**
   * Whether to create a new object (for indexes).
   */
  protected boolean create = true;

  /**
   * The list of parameters.
   */
  protected ArrayList parameters;

  /**
   * If the query should be prepared before each execution. This is set for
   * queries with LIKE ?, because the query plan depends on the parameter
   * value.
   */
  protected boolean prepareAlways;

  /**
   * Create a new object.
   */
  public Prepared() {
  }

  public Table getTable() {
    return table;
  }

  public void setTable(Table table) {
    this.table = table;
  }

  /**
   * Get the SQL snippet of the value list.
   *
   * @param values the value list
   * @return the SQL snippet
   */
  protected static String getSQL(Value[] values) {
    StatementBuilder buff = new StatementBuilder();
    for (Value v : values) {
      buff.appendExceptFirst(", ");
      if (v != null) {
        buff.append(v.getSQL());
      }
    }
    return buff.toString();
  }

  /**
   * Get the SQL snippet of the expression list.
   *
   * @param list the expression list
   * @return the SQL snippet
   */
  protected static String getSQL(Expression[] list) {
    StatementBuilder buff = new StatementBuilder();
    for (Expression e : list) {
      buff.appendExceptFirst(", ");
      if (e != null) {
        buff.append(e.getSQL());
      }
    }
    return buff.toString();
  }

  /**
   * Get the command type as defined in CommandInterface
   *
   * @return the statement type
   */
  public abstract int getType();

  /**
   * Set the parameter list of this statement.
   *
   * @param parameters the parameter list
   */
  public void setParameterList(ArrayList parameters) {
    this.parameters = parameters;
  }

  /**
   * Get the parameter list.
   *
   * @return the parameter list
   */
  public ArrayList getParameters() {
    return parameters;
  }

  /**
   * Get the SQL statement.
   *
   * @return the SQL statement
   */
  public String getSQL() {
    return sqlStatement;
  }

  /**
   * Set the SQL statement.
   *
   * @param sql the SQL statement
   */
  public void setSQL(String sql) {
    this.sqlStatement = sql;
  }

  /**
   * Set the prepare always flag.
   * If set, the statement is re-compiled whenever it is executed.
   *
   * @param prepareAlways the new value
   */
  public void setPrepareAlways(boolean prepareAlways) {
    this.prepareAlways = prepareAlways;
  }

  /**
   * Convert the statement to a String.
   *
   * @return the SQL statement
   */
  @Override
  public String toString() {
    return sqlStatement;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy