package com.readytalk.oss.dbms;

import static com.readytalk.oss.dbms.util.Util.append;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

/**
 * Class representing a template for updates which is not bound to any
 * specific parameters, analogous to a prepared statement in JDBC.
 */
public final class UpdateTemplate implements PatchTemplate {
  private final int parameterCount;

  /**
   * The table into which rows are to be inserted.
   */
  public final TableReference tableReference;

  /**
   * The expression which, when evaluated, determines whether a given
   * row should be updated.
   */
  public final Expression test;

  /**
   * An immutable list of columns for which values are to be inserted.
   */
  public final List<Column> columns;

  /**
   * An immutable list of expressions which, when resolved, yield the
   * values to be inserted.
   */
  public final List<Expression> values;

  /**
   * Defines a patch template which represents an update operation on
   * the specified table to be applied to rows satisfying the
   * specified test.  The values to be updated are specified as two
   * ordered lists of equal length: a list of columns and a list of
   * expressions representing the values to be placed into those
   * columns.
   */
  public UpdateTemplate(TableReference tableReference,
                        Expression test,
                        List<Column> columns,
                        List<Expression> values)
  {
    this.tableReference = tableReference;
    this.test = test;
    this.columns = Collections.unmodifiableList(new ArrayList(columns));
    this.values = Collections.unmodifiableList(new ArrayList(values));
    this.parameterCount = ParameterCounter.countParameters
      (append(this.values, test));

    if (this.columns.size() != this.values.size()) {
      throw new IllegalArgumentException
        ("column and value lists must be of equal length");
    }
  }

  /**
   * {@inheritDoc}
   */
  public int parameterCount() {
    return parameterCount;
  }
}

@@ -431,18 +431,16 @@
 table in
-to
  which r
@@ -445,37 +445,36 @@
  rows are to be 
-inser
+upda
 ted.%0A   */%0A  pub
@@ -721,29 +721,28 @@
 s are to be 
-inser
+upda
 ted.%0A   */%0A 
@@ -876,13 +876,12 @@
  be 
-inser
+upda
 ted.

fix UpdateTemplate javadoc wording