String describe(Session session, int blanks) {

      StringBuffer sb = new StringBuffer();
      String b = ValuePool.spaceString.substring(0, blanks);

      sb.append(b).append("index=").append(rangeIndex.getName().name).append("\n");

      if (hasIndexCondition()) {
        if (indexedColumnCount > 0) {
          sb.append(b).append("start conditions=[");

          for (int j = 0; j < indexedColumnCount; j++) {
            if (indexCond != null && indexCond[j] != null) {
              sb.append(indexCond[j].describe(session, blanks));
            }
          }

          sb.append("]\n");
        }

        if (indexEndCondition != null) {
          String temp = indexEndCondition.describe(session, blanks);

          sb.append(b).append("end condition=[").append(temp).append("]\n");
        }
      }

      if (nonIndexCondition != null) {
        String temp = nonIndexCondition.describe(session, blanks);

        sb.append(b).append("other condition=[").append(temp).append("]\n");
      }

      return sb.toString();
    }
  void dropIndexFromRows(Index primaryIndex, Index oldIndex) {

    RowIterator it = primaryIndex.firstRow(this, false);
    int position = oldIndex.getPosition() - 1;

    while (it.hasNext()) {
      Row row = it.getNextRow();
      int i = position - 1;
      NodeAVL backnode = ((RowAVL) row).getNode(0);

      while (i-- > 0) {
        backnode = backnode.nNext;
      }

      backnode.nNext = backnode.nNext.nNext;
    }
  }
  public CachedObject getAccessor(Index key) {

    NodeAVL node = (NodeAVL) accessorList[key.getPosition()];

    if (node == null) {
      return null;
    }

    if (!node.isInMemory()) {
      RowAVL row = (RowAVL) get(node.getPos(), false);

      node = row.getNode(key.getPosition());
      accessorList[key.getPosition()] = node;
    }

    return node;
  }
Ejemplo n.º 4
0
  /** Constructor declaration for PK and UNIQUE */
  public Constraint(HsqlName name, Table t, Index index, int type) {

    core = new ConstraintCore();
    this.name = name;
    constType = type;
    core.mainTable = t;
    core.mainIndex = index;
    core.mainCols = index.getColumns();
  }
  boolean insertIndexNodes(Index primaryIndex, Index newIndex) {

    int position = newIndex.getPosition();
    RowIterator it = primaryIndex.firstRow(this, false);
    int rowCount = 0;
    HsqlException error = null;

    try {
      while (it.hasNext()) {
        Row row = it.getNextRow();

        ((RowAVL) row).insertNode(position);

        // count before inserting
        rowCount++;

        newIndex.insert(null, this, row);
      }

      return true;
    } catch (java.lang.OutOfMemoryError e) {
      error = Error.error(ErrorCode.OUT_OF_MEMORY);
    } catch (HsqlException e) {
      error = e;
    }

    // backtrack on error
    // rowCount rows have been modified
    it = primaryIndex.firstRow(this, false);

    for (int i = 0; i < rowCount; i++) {
      Row row = it.getNextRow();
      NodeAVL backnode = ((RowAVL) row).getNode(0);
      int j = position;

      while (--j > 0) {
        backnode = backnode.nNext;
      }

      backnode.nNext = backnode.nNext.nNext;
    }

    throw error;
  }
  public CachedObject getAccessor(Index key) {

    int position = key.getPosition();

    if (position >= accessorList.length) {
      throw Error.runtimeError(ErrorCode.U_S0500, "RowStoreAVL");
    }

    return accessorList[position];
  }
    boolean addToIndexConditions(Expression e) {

      if (opType == OpTypes.EQUAL || opType == OpTypes.IS_NULL) {
        if (indexedColumnCount < rangeIndex.getColumnCount()) {
          if (rangeIndex.getColumns()[indexedColumnCount] == e.getLeftNode().getColumnIndex()) {
            indexCond[indexedColumnCount] = e;

            indexedColumnCount++;

            opType = e.opType;
            opTypeEnd = OpTypes.MAX;

            return true;
          }
        }
      }

      return false;
    }
Ejemplo n.º 8
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);
    }
  }
  public void setAccessor(Index key, int accessor) {

    CachedObject object = get(accessor, false);

    if (object != null) {
      NodeAVL node = ((RowAVL) object).getNode(key.getPosition());

      object = node;
    }

    setAccessor(key, object);
  }
  /** for result tables */
  void reindex(Session session, Index index) {

    setAccessor(index, null);

    RowIterator it = table.rowIterator(this);

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

      // may need to clear the node before insert
      index.insert(session, this, row);
    }
  }
Ejemplo n.º 11
0
 public Constraint(
     HsqlNameManager.HsqlName paramHsqlName, Table paramTable, Index paramIndex, int paramInt) {
   this.name = paramHsqlName;
   this.constType = paramInt;
   this.core = new ConstraintCore();
   this.core.mainTable = paramTable;
   this.core.mainIndex = paramIndex;
   this.core.mainCols = paramIndex.getColumns();
   for (int i = 0; i < this.core.mainCols.length; i++) {
     Type localType = paramTable.getColumn(this.core.mainCols[i]).getDataType();
     if (localType.isLobType()) throw Error.error(5534);
   }
 }
    private boolean addToIndexEndConditions(Expression e) {

      if (opType == OpTypes.EQUAL || opType == OpTypes.IS_NULL) {
        if (indexedColumnCount < rangeIndex.getColumnCount()) {
          if (rangeIndex.getColumns()[indexedColumnCount] == e.getLeftNode().getColumnIndex()) {
            Expression condition = ExpressionLogical.newNotNullCondition(e.getLeftNode());

            indexCond[indexedColumnCount] = condition;
            indexEndCond[indexedColumnCount] = e;
            indexEndCondition = ExpressionLogical.andExpressions(indexEndCondition, e);
            opType = OpTypes.NOT;
            opTypes[indexedColumnCount] = OpTypes.NOT;
            opTypeEnd = e.opType;
            opTypesEnd[indexedColumnCount] = e.opType;

            indexedColumnCount++;

            return true;
          }
        }
      }

      return false;
    }
  public void setAccessor(Index key, CachedObject accessor) {

    Index index = (Index) key;

    accessorList[index.getPosition()] = accessor;
  }
    /**
     * @param exprList list of expressions
     * @param index Index to use
     * @param colCount number of columns searched
     */
    void addIndexCondition(Expression[] exprList, Index index, int colCount) {

      int indexColCount = index.getColumnCount();

      rangeIndex = index;
      indexCond = new Expression[indexColCount];
      indexEndCond = new Expression[indexColCount];
      opTypes = new int[indexColCount];
      opTypesEnd = new int[indexColCount];
      opType = exprList[0].opType;
      opTypes[0] = exprList[0].opType;

      switch (opType) {
        case OpTypes.NOT:
          indexCond = exprList;
          opTypeEnd = OpTypes.MAX;
          opTypesEnd[0] = OpTypes.MAX;
          break;

        case OpTypes.GREATER:
        case OpTypes.GREATER_EQUAL:
        case OpTypes.GREATER_EQUAL_PRE:
          indexCond = exprList;

          if (exprList[0].getType() == OpTypes.GREATER_EQUAL_PRE) {
            indexEndCond[0] = indexEndCondition = exprList[0].nodes[2];
          }

          opTypeEnd = OpTypes.MAX;
          opTypesEnd[0] = OpTypes.MAX;
          break;

        case OpTypes.SMALLER:
        case OpTypes.SMALLER_EQUAL:
          {
            Expression e = exprList[0].getLeftNode();

            e = new ExpressionLogical(OpTypes.IS_NULL, e);
            e = new ExpressionLogical(OpTypes.NOT, e);
            indexCond[0] = e;
            indexEndCond[0] = indexEndCondition = exprList[0];
            opTypeEnd = opType;
            opTypesEnd[0] = opType;
            opType = OpTypes.NOT;
            opTypes[0] = OpTypes.NOT;

            break;
          }
        case OpTypes.IS_NULL:
        case OpTypes.EQUAL:
          {
            indexCond = exprList;

            for (int i = 0; i < colCount; i++) {
              Expression e = exprList[i];

              indexEndCond[i] = e;
              indexEndCondition = ExpressionLogical.andExpressions(indexEndCondition, e);
              opType = e.opType;
              opTypes[0] = e.opType;
              opTypesEnd[0] = e.opType;
            }

            opTypeEnd = opType;

            break;
          }
        default:
          Error.runtimeError(ErrorCode.U_S0500, "RangeVariable");
      }

      indexedColumnCount = colCount;
      hasIndex = true;
    }
    void addCondition(Expression e) {

      if (e == null) {
        return;
      }

      if (e instanceof ExpressionLogical) {
        if (((ExpressionLogical) e).isTerminal) {
          terminalCondition = e;
        }
      }

      nonIndexCondition = ExpressionLogical.andExpressions(nonIndexCondition, e);

      if (Expression.EXPR_FALSE.equals(nonIndexCondition)) {
        isFalse = true;
      }

      if (rangeIndex == null || rangeIndex.getColumnCount() == 0) {
        return;
      }

      if (indexedColumnCount == 0) {
        return;
      }

      if (e.getIndexableExpression(rangeVar) == null) {
        return;
      }

      int colIndex = e.getLeftNode().getColumnIndex();
      int[] indexCols = rangeIndex.getColumns();

      switch (e.getType()) {
        case OpTypes.GREATER:
        case OpTypes.GREATER_EQUAL:
        case OpTypes.GREATER_EQUAL_PRE:
          {

            // replaces existing condition
            if (opType == OpTypes.NOT) {
              if (indexCols[indexedColumnCount - 1] == colIndex) {
                nonIndexCondition =
                    ExpressionLogical.andExpressions(
                        nonIndexCondition, indexCond[indexedColumnCount - 1]);
                indexCond[indexedColumnCount - 1] = e;
                opType = e.opType;
                opTypes[indexedColumnCount - 1] = e.opType;

                if (e.getType() == OpTypes.GREATER_EQUAL_PRE && indexedColumnCount == 1) {
                  indexEndCond[indexedColumnCount - 1] =
                      ExpressionLogical.andExpressions(
                          indexEndCond[indexedColumnCount - 1], e.nodes[2]);
                }
              }
            } else {
              addToIndexConditions(e);
            }

            break;
          }
        case OpTypes.SMALLER:
        case OpTypes.SMALLER_EQUAL:
          {
            if (opType == OpTypes.GREATER
                || opType == OpTypes.GREATER_EQUAL
                || opType == OpTypes.GREATER_EQUAL_PRE
                || opType == OpTypes.NOT) {
              if (opTypeEnd != OpTypes.MAX) {
                break;
              }

              if (indexCols[indexedColumnCount - 1] == colIndex) {
                indexEndCond[indexedColumnCount - 1] = e;
                indexEndCondition = ExpressionLogical.andExpressions(indexEndCondition, e);
                opTypeEnd = e.opType;
                opTypesEnd[indexedColumnCount - 1] = e.opType;
              }
            } else {
              addToIndexEndConditions(e);
            }

            break;
          }
        default:
      }
    }