/**
   * Executes an UPDATE statement. It is assumed that the argument is of the correct type.
   *
   * @param cs a CompiledStatement of type CompiledStatement.UPDATE
   * @throws HsqlException if a database access error occurs
   * @return the result of executing the statement
   */
  private Result executeUpdateStatement(CompiledStatement cs) throws HsqlException {

    Table table = cs.targetTable;
    TableFilter filter = cs.tf;
    int count = 0;

    if (filter.findFirst()) {
      int[] colmap = cs.columnMap; // column map
      Expression[] colvalues = cs.columnValues;
      Expression condition = cs.condition; // update condition
      int len = colvalues.length;
      HashMappedList rowset = new HashMappedList();
      int size = table.getColumnCount();
      int[] coltypes = table.getColumnTypes();
      boolean success = false;

      do {
        if (condition == null || condition.test(session)) {
          try {
            Row row = filter.currentRow;
            Object[] ni = table.getNewRow();

            System.arraycopy(row.getData(), 0, ni, 0, size);

            for (int i = 0; i < len; i++) {
              int ci = colmap[i];

              ni[ci] = colvalues[i].getValue(session, coltypes[ci]);
            }

            rowset.add(row, ni);
          } catch (HsqlInternalException e) {
          }
        }
      } while (filter.next());

      session.beginNestedTransaction();

      try {
        count = table.update(session, rowset, colmap);
        success = true;
      } finally {

        // update failed (constraint violation) or succeeded
        session.endNestedTransaction(!success);
      }
    }

    updateResult.iUpdateCount = count;

    return updateResult;
  }
  /**
   * Executes an INSERT_SELECT statement. It is assumed that the argument is of the correct type.
   *
   * @param cs a CompiledStatement of type CompiledStatement.INSERT_SELECT
   * @throws HsqlException if a database access error occurs
   * @return the result of executing the statement
   */
  private Result executeInsertSelectStatement(CompiledStatement cs) throws HsqlException {

    Table t = cs.targetTable;
    Select s = cs.select;
    int[] ct = t.getColumnTypes(); // column types
    Result r = s.getResult(session.getMaxRows(), session);
    Record rc = r.rRoot;
    int[] cm = cs.columnMap; // column map
    boolean[] ccl = cs.checkColumns; // column check list
    int len = cm.length;
    Object[] row;
    int count;
    boolean success = false;

    session.beginNestedTransaction();

    try {
      while (rc != null) {
        row = t.getNewRowData(session, ccl);

        for (int i = 0; i < len; i++) {
          int j = cm[i];

          if (ct[j] != r.metaData.colType[i]) {
            row[j] = Column.convertObject(rc.data[i], ct[j]);
          } else {
            row[j] = rc.data[i];
          }
        }

        rc.data = row;
        rc = rc.next;
      }

      count = t.insert(session, r);
      success = true;
    } finally {
      session.endNestedTransaction(!success);
    }

    updateResult.iUpdateCount = count;

    return updateResult;
  }