示例#1
0
  void prepareCheckConstraint(Session session, Table table, boolean checkValues) {

    // to ensure no subselects etc. are in condition
    check.checkValidCheckConstraint();

    if (table == null) {
      check.resolveTypes(session, null);
    } else {
      QuerySpecification s = Expression.getCheckSelect(session, table, check);
      Result r = s.getResult(session, 1);

      if (r.getNavigator().getSize() != 0) {
        String[] info = new String[] {table.getName().name, ""};

        throw Error.error(ErrorCode.X_23504, ErrorCode.CONSTRAINT, info);
      }

      rangeVariable = s.rangeVariables[0];

      // removes reference to the Index object in range variable
      rangeVariable.setForCheckConstraint();
    }

    if (check.getType() == OpTypes.NOT
        && check.getLeftNode().getType() == OpTypes.IS_NULL
        && check.getLeftNode().getLeftNode().getType() == OpTypes.COLUMN) {
      notNullColumnIndex = check.getLeftNode().getLeftNode().getColumnIndex();
      isNotNull = true;
    }
  }
    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;
    }
    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;
    }
    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:
      }
    }