예제 #1
0
  private static String generateWhereClauseForIntersection(ComplexConjunctiveQuery view) {
    StringBuilder result = new StringBuilder();
    result.append(INDENT).append("where \n");
    List<VariableSelectionCondition> allSelectionConditions = view.getAllSelections();
    List<VariableJoinCondition> joinConditions =
        new ArrayList<VariableJoinCondition>(view.getJoinConditions());
    for (SimpleConjunctiveQuery simpleConjunctiveQuery : view.getConjunctions()) {
      joinConditions.addAll(simpleConjunctiveQuery.getAllJoinConditions());
    }
    if (!joinConditions.isEmpty()) {
      for (int i = 0; i < joinConditions.size(); i++) {
        VariableJoinCondition joinCondition = joinConditions.get(i);
        result
            .append(DOUBLE_INDENT)
            .append(generateSQLStringForJoinConditionForIntersection(joinCondition, view));
        result.append(" AND\n");
      }
    }
    // check selection conditions
    for (int i = 0; i < allSelectionConditions.size(); i++) {
      VariableSelectionCondition condition = allSelectionConditions.get(i);
      result.append(DOUBLE_INDENT).append(GenerateSQL.sqlString(condition.getCondition()));
      if (i != allSelectionConditions.size() - 1 || view.hasIntersection()) result.append(" AND\n");
    }
    if (view.hasIntersection()) {
      List<VariablePathExpression> leftIntersectionPaths =
          generateTargetPaths(view.getIntersectionEqualities().getLeftCorrespondences());
      List<VariablePathExpression> rightIntersectionPaths =
          generateTargetPaths(view.getIntersectionEqualities().getRightCorrespondences());
      List<VariableCorrespondence> allCorrespondences = new ArrayList<VariableCorrespondence>();
      allCorrespondences.addAll(view.getIntersectionEqualities().getLeftCorrespondences());
      allCorrespondences.addAll(view.getIntersectionEqualities().getRightCorrespondences());

      for (int i = 0; i < leftIntersectionPaths.size(); i++) {
        VariablePathExpression leftPath = leftIntersectionPaths.get(i);
        VariablePathExpression rightPath = rightIntersectionPaths.get(i);
        VariablePathExpression leftSourcePath =
            findSourcePathWithEqualsId(allCorrespondences, leftPath);
        if (leftSourcePath == null) {
          leftSourcePath = leftPath;
        }
        VariablePathExpression rightSourcePath =
            findSourcePathWithEqualsId(allCorrespondences, rightPath);
        if (rightSourcePath == null) {
          rightSourcePath = rightPath;
        }
        result
            .append(DOUBLE_INDENT)
            .append(attributeNameWithVariable(leftSourcePath))
            .append(" = ")
            .append(attributeNameWithVariable(rightSourcePath));
        if (i != leftIntersectionPaths.size() - 1) result.append(" AND\n");
      }
    }
    result.append("\n");
    return result.toString();
  }
예제 #2
0
 private static String generateWhereClauseWithoutWhere(ComplexConjunctiveQuery view) {
   if (!isNeededAWhereClause(view)) {
     return "";
   }
   StringBuilder result = new StringBuilder();
   List<VariableSelectionCondition> allSelectionConditions = view.getAllSelections();
   List<SimpleConjunctiveQuery> conjunctions = view.getConjunctions();
   List<VariableJoinCondition> joinConditions =
       new ArrayList<VariableJoinCondition>(view.getJoinConditions());
   for (SimpleConjunctiveQuery simpleConjunctiveQuery : conjunctions) {
     joinConditions.addAll(simpleConjunctiveQuery.getAllJoinConditions());
   }
   if (!joinConditions.isEmpty()) {
     result.append("\n");
     for (int i = 0; i < joinConditions.size(); i++) {
       VariableJoinCondition joinCondition = joinConditions.get(i);
       List<VariableCorrespondence> correspondences = new ArrayList<VariableCorrespondence>();
       for (List<VariableCorrespondence> list : view.getCorrespondencesForConjunctions()) {
         correspondences.addAll(list);
       }
       result
           .append(DOUBLE_INDENT)
           .append(generateSQLStringForJoinConditionSourcePath(joinCondition, correspondences));
       if (i != joinConditions.size() - 1 || !allSelectionConditions.isEmpty())
         result.append(" AND\n");
     }
   }
   // check selection conditions
   for (int i = 0; i < allSelectionConditions.size(); i++) {
     VariableSelectionCondition condition = allSelectionConditions.get(i);
     result.append(DOUBLE_INDENT).append(GenerateSQL.sqlString(condition.getCondition()));
     if (i != allSelectionConditions.size() - 1) result.append(" AND\n");
   }
   result.append("\n");
   return result.toString();
 }