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

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

    if (filter.findFirst()) {
      Expression c = cs.condition;
      HsqlArrayList del;

      del = new HsqlArrayList();

      if (c == null) {
        do {
          del.add(filter.currentRow);
        } while (filter.next());

        count = table.delete(session, del);
      } else {
        do {
          if (c.test(session)) {
            del.add(filter.currentRow);
          }
        } while (filter.next());

        count = table.delete(session, del);
      }
    }

    updateResult.iUpdateCount = count;

    return updateResult;
  }
  /**
   * 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;
  }