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(); }
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); }
//////////////// 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; }
private boolean containsSameAliases( ComplexConjunctiveQuery complexQuery, SimpleConjunctiveQuery simpleQuery) { for (SetAlias simple : simpleQuery.getVariables()) { if (SpicyEngineUtility.containsVariableWithSameId(complexQuery.getGenerators(), simple)) { return true; } } return false; }
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); }
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(); }