コード例 #1
0
ファイル: GenerateSQL.java プロジェクト: alexmemory/spicy
  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
 public void visitComplexConjunctiveQuery(ComplexConjunctiveQuery complexQuery) {
   stack.add(0, complexQuery);
   //        List<FormulaVariable> universalVariables =
   // variableFinder.getUniversalVariables(variableMaps, stack);
   //        variables.add(0, universalVariables);
   result.append(
       generateConjunctionString(
           complexQuery.getGenerators(),
           variables,
           mappingTask.getSourceProxy().getIntermediateSchema()));
   result.append(generateBuiltinsForSelections(complexQuery));
   List<Expression> equalities = variableFinder.getEqualities(variableMaps, stack);
   if (!equalities.isEmpty()) {
     result.append(generateEqualityString(equalities, variables, false));
   }
   // intersection
   if (complexQuery.hasIntersection()) {
     if (useSaveFormat) {
       result.append(", \n");
     } else {
       result
           .append("\n")
           .append(generateIndent())
           .append(utility.SECONDARY_INDENT)
           .append("and ");
     }
     SimpleConjunctiveQuery intersectionQuery = complexQuery.getConjunctionForIntersection();
     stack.add(0, intersectionQuery);
     List<FormulaVariable> existentialVariables =
         variableFinder.getExistentialVariables(variableMaps, stack);
     variables.add(0, existentialVariables);
     if (!existentialVariables.isEmpty() && !useSaveFormat) {
       result.append("exist ");
       result.append(utility.printVariables(existentialVariables));
     }
     if (!useSaveFormat) {
       result.append(" (// base view ").append(intersectionQuery.getId()).append("\n");
     }
     result.append(
         generateConjunctionString(
             intersectionQuery.getGenerators(),
             variables,
             mappingTask.getSourceProxy().getIntermediateSchema()));
     List<Expression> intersectionEqualities = variableFinder.getEqualities(variableMaps, stack);
     if (!intersectionEqualities.isEmpty()) {
       result.append(generateEqualityString(intersectionEqualities, variables, false));
     }
     variables.remove(0);
     stack.remove(0);
     if (!useSaveFormat) {
       result.append("\n").append(generateIndent()).append(utility.SECONDARY_INDENT).append(")");
     }
   }
   //        variables.remove(0);
   stack.remove(0);
 }
コード例 #3
0
 ////////////////    JOIN GRAPH CONSTRUCTION    ////////////////////
 JoinGraph initializeJoinGraph(SimpleConjunctiveQuery view) {
   JoinGraph joinGraph = new JoinGraph();
   for (SetAlias variable : view.getGenerators()) {
     joinGraph.addNode(variable);
   }
   for (VariableJoinCondition joinCondition : view.getJoinConditions()) {
     addJoinConditionToGraph(joinCondition, joinGraph, view);
   }
   return joinGraph;
 }
コード例 #4
0
ファイル: RewriteTgds.java プロジェクト: alexmemory/spicy
 private boolean containsSameAliases(
     ComplexConjunctiveQuery complexQuery, SimpleConjunctiveQuery simpleQuery) {
   for (SetAlias simple : simpleQuery.getVariables()) {
     if (SpicyEngineUtility.containsVariableWithSameId(complexQuery.getGenerators(), simple)) {
       return true;
     }
   }
   return false;
 }
コード例 #5
0
 private SetAlias findVariable(SetAlias joinVariable, SimpleConjunctiveQuery view) {
   for (SetAlias viewVariable : view.getVariables()) {
     if (viewVariable.getGenerators().contains(joinVariable)) {
       return viewVariable;
     }
   }
   throw new IllegalArgumentException(
       "Unable to find variable " + joinVariable + " in view " + view);
 }
コード例 #6
0
ファイル: GenerateSQL.java プロジェクト: alexmemory/spicy
 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();
 }