Exemple #1
0
  public void addColumn(String columnName, DataType type) {
    ColumnMeta c = new ColumnMeta(this.tableName, columnName, type, null, true);
    columns.add(c);

    IColumn ic = ASTNodeFactory.getInstance().createColumn();
    ic.setTableName(this.tableName);
    ic.setColumnName(columnName);
    ic.setDataType(type);
    originalSelectColumns.add(ic);
  }
  /**
   * 根据range结果,构造filter
   *
   * @param range
   * @param column
   * @return
   */
  protected List<IFilter> buildFilter(Range range, Object column) {
    List<IFilter> filters = new ArrayList(2);
    if (range == null) {
      return filters;
    }

    if (range.isSingleValue()) {
      IBooleanFilter en =
          ASTNodeFactory.getInstance().createBooleanFilter().setOperation(OPERATION.EQ);
      en.setColumn(column);
      en.setValue(range.getMaxValue());
      filters.add(en);
      return filters;
    }

    if (range.getMinValue() != null) {
      IBooleanFilter gn;
      if (range.isMinIncluded()) {
        gn = ASTNodeFactory.getInstance().createBooleanFilter().setOperation(OPERATION.GT_EQ);
      } else {
        gn = ASTNodeFactory.getInstance().createBooleanFilter().setOperation(OPERATION.GT);
      }

      gn.setColumn(column);
      gn.setValue(range.getMinValue());
      filters.add(gn);
    }

    if (range.getMaxValue() != null) {
      IBooleanFilter ln;
      if (range.isMaxIncluded()) {
        ln = ASTNodeFactory.getInstance().createBooleanFilter().setOperation(OPERATION.LT_EQ);
      } else {
        ln = ASTNodeFactory.getInstance().createBooleanFilter().setOperation(OPERATION.LT);
      }

      ln.setColumn(column);
      ln.setValue(range.getMaxValue());
      filters.add(ln);
    }
    return filters;
  }
  /** 将0=1/1=1/true的恒等式进行优化 */
  private static IFilter shortestFilter(IFilter root) throws EmptyResultFilterException {
    IFilter filter = FilterUtils.toDNFAndFlat(root);
    List<List<IFilter>> DNFfilter = FilterUtils.toDNFNodesArray(filter);

    List<List<IFilter>> newDNFfilter = new ArrayList<List<IFilter>>();
    for (List<IFilter> andDNFfilter : DNFfilter) {
      boolean isShortest = false;
      List<IFilter> newAndDNFfilter = new ArrayList<IFilter>();
      for (IFilter one : andDNFfilter) {
        if (one.getOperation() == OPERATION.CONSTANT) {
          boolean flag = false;
          if (((IBooleanFilter) one).getColumn() instanceof ISelectable) { // 可能是个not函数
            newAndDNFfilter.add(one); // 不能丢弃
          } else {
            String value = ((IBooleanFilter) one).getColumn().toString();
            if (StringUtils.isNumeric(value)) {
              flag = BooleanUtils.toBoolean(Integer.valueOf(value));
            } else {
              flag = BooleanUtils.toBoolean(((IBooleanFilter) one).getColumn().toString());
            }
            if (!flag) {
              isShortest = true;
              break;
            }
          }
        } else {
          newAndDNFfilter.add(one);
        }
      }

      if (!isShortest) {
        if (newAndDNFfilter.isEmpty()) {
          // 代表出现为true or xxx,直接返回true
          IBooleanFilter f = ASTNodeFactory.getInstance().createBooleanFilter();
          f.setOperation(OPERATION.CONSTANT);
          f.setColumn("1");
          f.setColumnName(ObjectUtils.toString("1"));
          return f;
        } else { // 针对非false的情况
          newDNFfilter.add(newAndDNFfilter);
        }
      }
    }

    if (newDNFfilter.isEmpty()) {
      throw new EmptyResultFilterException("空结果");
    }

    return FilterUtils.DNFToOrLogicTree(newDNFfilter);
  }
 public static ISequenceVal buildSequenceVal(ISelectable column) {
   return ASTNodeFactory.getInstance()
       .createSequenceValue(ISequenceManager.AUTO_SEQ_PREFIX + column.getTableName());
 }