/**
   * 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;
  }
Пример #3
0
  /**
   * Retrieves the result of freeing the statement with the given id.
   *
   * @param csid the numeric identifier of the statement
   * @return the result of freeing the indicated statement
   */
  private Result sqlFreeStatement(int csid) {

    boolean existed;
    Result result;

    existed = compiledStatementManager.freeStatement(csid, iId);
    result = new Result(ResultConstants.UPDATECOUNT);

    if (existed) {
      result.iUpdateCount = 1;
    }

    return result;
  }
  /**
   * 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;
  }
  /**
   * Executes an INSERT_VALUES statement. It is assumed that the argument is of the correct type.
   *
   * @param cs a CompiledStatement of type CompiledStatement.INSERT_VALUES
   * @throws HsqlException if a database access error occurs
   * @return the result of executing the statement
   */
  private Result executeInsertValuesStatement(CompiledStatement cs) throws HsqlException {

    Table t = cs.targetTable;
    Object[] row = t.getNewRowData(session, cs.checkColumns);
    int[] cm = cs.columnMap; // column map
    Expression[] acve = cs.columnValues;
    Expression cve;
    int[] ct = t.getColumnTypes(); // column types
    int ci; // column index
    int len = acve.length;

    for (int i = 0; i < len; i++) {
      cve = acve[i];
      ci = cm[i];
      row[ci] = cve.getValue(session, ct[ci]);
    }

    t.insert(session, row);

    updateResult.iUpdateCount = 1;

    return updateResult;
  }