private String getColumns() {

    StringBuilder sb = new StringBuilder();

    sb.append("( ");
    for (int i = 0; i < table.getColumns().size(); i++) {

      if (isDeltaColumn(table, i)) {
        continue;
      }

      sb.append(table.getColumns().get(i));

      if (!table.getColumnTypes().isEmpty() && isDate(table.getColumnTypes().get(i))) {
        sb.append(" date \"" + config.getObjectDefinition().getDateFormat() + " HH24:MI:SS" + "\"");
      }
      if (table.getColumnTypes().isEmpty() && isDate(table.getName(), table.getColumns().get(i))) {
        sb.append(" date \"" + "dd-mm-yy HH24:MI:SS" + "\"");
      }

      if (isDeltaColumn(table, i)) {}

      if (i == table.getColumns().size() - 1) {
        sb.append(" ) \n");
      } else {
        sb.append(", ");
      }
    }

    return sb.toString();
  }
  /**
   * 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;
  }
  /**
   * Finds the first row in the table (using an index if there is one) and checks it against the
   * eEnd (range) and eAnd (other conditions) Expression objects. (fredt)
   *
   * @return true if first row was found, else false
   */
  boolean findFirst() throws HsqlException {

    nonJoinIsNull = false;
    isCurrentOuter = false;

    if (filterIndex == null) {
      filterIndex = filterTable.getPrimaryIndex();
    }

    if (this.isMultiFindFirst) {
      Object[] data = filterTable.getNewRow();
      int[] types = filterTable.getColumnTypes();

      for (int i = 0; i < findFirstExpressions.length; i++) {
        Expression e = findFirstExpressions[i];

        if (e != null) {
          data[i] = e.getValue(null, types[i]);
        }
      }

      currentNode = filterIndex.findFirst(data);
    } else if (eStart == null) {
      currentNode = eEnd == null ? filterIndex.first() : filterIndex.findFirstNotNull();
    } else {
      int type = eStart.getArg().getDataType();
      Object o = eStart.getArg2().getValue(null, type);

      currentNode = filterIndex.findFirst(o, eStart.getType());
    }

    while (currentNode != null) {
      currentData = currentNode.getData();
      currentRow = currentNode.getRow();

      if (!(eEnd == null || eEnd.test(null))) {
        break;
      }

      if (eAnd == null || eAnd.test(null)) {
        return true;
      }

      currentNode = filterIndex.next(currentNode);
    }

    currentData = emptyData;
    currentRow = null;

    return false;
  }
  Result executeSetStatement(Session session) {

    Table table = targetTable;
    int[] colMap = updateColumnMap; // column map
    Expression[] colExpressions = updateExpressions;
    Type[] colTypes = table.getColumnTypes();
    int index = targetRangeVariables[TriggerDef.NEW_ROW].rangePosition;
    Object[] oldData = session.sessionContext.rangeIterators[index].getCurrent();
    Object[] data =
        StatementDML.getUpdatedData(session, table, colMap, colExpressions, colTypes, oldData);

    ArrayUtil.copyArray(data, oldData, data.length);

    return Result.updateOneResult;
  }
  /**
   * 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;
  }
  /**
   * Constructor when read from the disk into the Cache.
   *
   * @param t table
   * @param in data source
   * @throws IOException
   * @throws HsqlException
   */
  public CachedRow(Table t, RowInputInterface in) throws IOException, HsqlException {

    tTable = t;
    iPos = in.getPos();
    storageSize = in.getSize();

    int indexcount = t.getIndexCount();

    nPrimaryNode = Node.newNode(this, in, 0, t);

    Node n = nPrimaryNode;

    for (int i = 1; i < indexcount; i++) {
      n.nNext = Node.newNode(this, in, i, t);
      n = n.nNext;
    }

    oData = in.readData(tTable.getColumnTypes());
  }
Пример #7
0
  /**
   * Check used before creating a new foreign key cosntraint, this method checks all rows of a table
   * to ensure they all have a corresponding row in the main table.
   */
  void checkReferencedRows(Session session, Table table, int[] rowColArray) {

    Index mainIndex = getMainIndex();
    PersistentStore store = session.sessionData.getRowStore(table);
    RowIterator it = table.rowIterator(session);

    while (true) {
      Row row = it.getNextRow();

      if (row == null) {
        break;
      }

      Object[] rowData = row.getData();

      if (ArrayUtil.hasNull(rowData, rowColArray)) {
        if (core.matchType == OpTypes.MATCH_SIMPLE) {
          continue;
        }
      } else if (mainIndex.exists(session, store, rowData, rowColArray)) {
        continue;
      }

      if (ArrayUtil.hasAllNull(rowData, rowColArray)) {
        continue;
      }

      String colValues = "";

      for (int i = 0; i < rowColArray.length; i++) {
        Object o = rowData[rowColArray[i]];

        colValues += table.getColumnTypes()[i].convertToString(o);
        colValues += ",";
      }

      String[] info = new String[] {getName().name, getMain().getName().name};

      throw Error.error(ErrorCode.X_23502, ErrorCode.CONSTRAINT, info);
    }
  }
  /**
   * 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;
  }