Ejemplo n.º 1
0
  public String generateSQL(MappingTask mappingTask) {
    if (mappingTask.getSourceProxy().getMappingData().isNested()
        || mappingTask.getTargetProxy().getMappingData().isNested()) {
      throw new IllegalMappingTaskException(
          "Data Sources are nested. SQL can be generated for relational sources only");
    }
    StringBuilder result = new StringBuilder();
    result.append(
        "-- This script was automatically generated by the ++Spicy mapping tool. (http://db.unibas.it/projects/spicy/)\n\n");
    result.append("BEGIN TRANSACTION;\n");
    result.append("SET CONSTRAINTS ALL DEFERRED;\n");
    if (mappingTask.getConfig().useSortInSkolems()) {
      result.append(mappingTask.getDBMSHandler().generateSortArrayFunction());
    }

    // Generate the SQL script for the first step in chaining scenarios

    if (mappingTask.getSourceProxy() instanceof ChainingDataSourceProxy) {
      ChainingDataSourceProxy proxy = (ChainingDataSourceProxy) mappingTask.getSourceProxy();
      GenerateSQLForSourceToTargetExchange stGeneratorFirstStep =
          new GenerateSQLForSourceToTargetExchange();
      result.append(stGeneratorFirstStep.generateSQL(proxy.getMappingTask(), CHAINING_FIRST_STEP));
    }

    result.append(this.getDeleteTablesScript(mappingTask));
    GenerateSQLForSourceToTargetExchange stGenerator = new GenerateSQLForSourceToTargetExchange();

    if (!mappingTask.getConfig().useHashTextForSkolems()
        && mappingTask.getConfig().useSkolemTable()) {
      result.append(generateSQLForSkolemTable(mappingTask));
    }
    if (mappingTask.getSourceProxy() instanceof ChainingDataSourceProxy) {
      result.append(stGenerator.generateSQL(mappingTask, CHAINING_LAST_STEP));
    } else {
      result.append(stGenerator.generateSQL(mappingTask, CHAINING_NO_CHAINING));
    }
    allTablesToDelete.addAll(stGenerator.getAllTablesToDelete());
    if (mappingTask.getConfig().useCreateTableInSTExchange()) {
      allTablesToDelete.addAll(stGenerator.getAllViewsToDelete());
    } else {
      allViewsToDelete.addAll(stGenerator.getAllViewsToDelete());
    }
    result.append(generateFinalInserts(mappingTask));
    result.append("COMMIT;\n");
    if (!mappingTask.getConfig().useDebugMode()) {
      result.append(this.getAllViewsToDeleteScript());
      result.append(this.getAllTablesToDeleteScript());
    }
    return result.toString();
  }
Ejemplo n.º 2
0
 private void generatePartsForJoins(
     List<VariableJoinCondition> joinConditions,
     Map<VariableJoinCondition, ISkolemPart> partsForJoins,
     Map<SetAlias, ISkolemPart> partsByAlias,
     MappingTask mappingTask) {
   for (VariableJoinCondition joinCondition : joinConditions) {
     SetAlias fromVariable = joinCondition.getFromVariable();
     SetAlias toVariable = joinCondition.getToVariable();
     ISkolemPart fromGroupPart = findPartForAlias(fromVariable, partsByAlias);
     ISkolemPart fromPathsPart = generatePartForPaths(joinCondition.getFromPaths());
     ISkolemPart toGroupPart = findPartForAlias(toVariable, partsByAlias);
     ISkolemPart toPathsPart = generatePartForPaths(joinCondition.getToPaths());
     ISkolemPart appendFrom = new AppendSkolemPart(false, "", "", ".");
     appendFrom.addChild(fromGroupPart);
     appendFrom.addChild(fromPathsPart);
     ISkolemPart appendTo = new AppendSkolemPart(false, "", "", ".");
     appendTo.addChild(toGroupPart);
     appendTo.addChild(toPathsPart);
     ISkolemPart root =
         new AppendSkolemPart(mappingTask.getConfig().useSortInSkolems(), "[", "]", "=");
     if (joinCondition.getFromPaths().toString().compareTo(joinCondition.getToPaths().toString())
         >= 0) {
       root.addChild(appendFrom);
       root.addChild(appendTo);
     } else {
       root.addChild(appendTo);
       root.addChild(appendFrom);
     }
     partsForJoins.put(joinCondition, root);
   }
 }
Ejemplo n.º 3
0
 private List<FORule> rewriteTgdsWithoutSelfJoins(
     List<FORule> originalTgds, MappingTask mappingTask) {
   if (mappingTask.getConfig().rewriteSubsumptions() == false
       && mappingTask.getConfig().rewriteCoverages() == false) {
     return new ArrayList<FORule>(originalTgds);
   }
   List<FORule> rewrittenRules = new ArrayList<FORule>();
   for (FORule tgd : originalTgds) {
     tgd.getComplexSourceQuery()
         .setProvenance(SpicyEngineConstants.PREMISE + " of " + tgd.getId());
     FORule rule = tgd.clone();
     addNegationsForSubsumptions(tgd, rule, mappingTask);
     addNegationsForCoverages(tgd, rule, mappingTask);
     rewrittenRules.add(rule);
   }
   removeClonesFromConclusions(rewrittenRules);
   Collections.sort(rewrittenRules);
   return rewrittenRules;
 }
Ejemplo n.º 4
0
 private String generateFinalInserts(MappingTask mappingTask) {
   StringBuilder result = new StringBuilder();
   if (mappingTask.getConfig().useSkolemTable()) {
     result.append(
         "\n------------------------------  FINAL INSERTS  -----------------------------------\n\n");
     result.append(
         "\n------------------------------  INSERT INTO TARGET DATABASE  ---------------------------\n");
     result.append(generateFinalInsert(mappingTask));
   }
   return result.toString();
 }
Ejemplo n.º 5
0
 //////////////////////////// first part: tuple part
 private ISkolemPart generatePartForTuples(
     Map<SetAlias, ISkolemPart> partsByAlias, MappingTask mappingTask) {
   List<SetAlias> variables = new ArrayList<SetAlias>(partsByAlias.keySet());
   Collections.sort(variables);
   ISkolemPart append =
       new AppendSkolemPart(mappingTask.getConfig().useSortInSkolems(), "", "", ", ", ", ");
   for (SetAlias setVariable : variables) {
     append.addChild(partsByAlias.get(setVariable));
   }
   return append;
 }
Ejemplo n.º 6
0
 private void addNegationsForCoverages(FORule tgd, FORule rule, MappingTask mappingTask) {
   CoverageMap coverageMap = mappingTask.getMappingData().getSTCoverageMap();
   List<Coverage> coverages = coverageMap.getCoverage(tgd);
   if ((coverages.isEmpty() || !mappingTask.getConfig().rewriteCoverages())) {
     return;
   }
   for (Coverage coverage : coverages) {
     NegatedComplexQuery negatedQuery = generateNegatedQueryForCoverage(coverage);
     negatedQuery.setProvenance(SpicyEngineConstants.COVERAGE + " of " + tgd.getId());
     rule.getComplexSourceQuery().addNegatedComplexQuery(negatedQuery);
   }
 }
Ejemplo n.º 7
0
 private void addNegationsForSubsumptions(FORule tgd, FORule rule, MappingTask mappingTask) {
   if (logger.isDebugEnabled()) logger.debug("********* Adding negations for tgd: \n" + tgd);
   SubsumptionMap tgdDag = mappingTask.getMappingData().getSTSubsumptionMap();
   if (logger.isDebugEnabled()) logger.debug("********* Subsumption Map: \n" + tgdDag);
   List<Subsumption> subsumptions = tgdDag.getFathers(tgd);
   if (logger.isDebugEnabled()) logger.debug("Subsumptions: " + subsumptions);
   if (logger.isDebugEnabled()) logger.debug("Config : " + mappingTask.getConfig());
   if ((subsumptions.isEmpty() || !mappingTask.getConfig().rewriteSubsumptions())) {
     return;
   }
   for (Subsumption subsumption : subsumptions) {
     List<VariableCorrespondence> leftCorrespondences = subsumption.getLeftCorrespondences();
     List<VariableCorrespondence> rightCorrespondences = subsumption.getRightCorrespondences();
     TargetEqualities equalities = new TargetEqualities(leftCorrespondences, rightCorrespondences);
     NegatedComplexQuery negatedQuery =
         new NegatedComplexQuery(
             subsumption.getRightTgd().getComplexSourceQuery().clone(), equalities);
     negatedQuery.setProvenance(subsumption.getId());
     rule.getComplexSourceQuery().addNegatedComplexQuery(negatedQuery);
   }
 }
Ejemplo n.º 8
0
 //////////////////////////// third part: variable part
 private ISkolemPart generatePartForVariable(
     SkolemFunctionGenerator generator,
     Map<VariableJoinCondition, ISkolemPart> partsForJoins,
     MappingTask mappingTask) {
   if (generator.getJoinConditions().isEmpty()) {
     return NullSkolemPart.getInstance();
   }
   ISkolemPart append =
       new AppendSkolemPart(mappingTask.getConfig().useSortInSkolems(), "", "", ", ");
   for (VariableJoinCondition variableJoinCondition : generator.getJoinConditions()) {
     append.addChild(partsForJoins.get(variableJoinCondition));
   }
   return append;
 }
Ejemplo n.º 9
0
 public List<FORule> rewriteTgds(List<FORule> originalTgds, MappingTask mappingTask) {
   if (mappingTask.isLoadedFromParser() && mappingTask.getConfig().noRewriting()) {
     return cleanLoadedTgds(originalTgds);
   }
   List<FORule> rewrittenRules = null;
   if (mappingTask.getMappingData().hasSelfJoinsInTgdConclusions()) {
     RewriteTgdsWithSelfJoins selfJoinRewriter = new RewriteTgdsWithSelfJoins();
     rewrittenRules = selfJoinRewriter.rewriteTgdsWithSelfJoins(originalTgds, mappingTask);
   } else {
     rewrittenRules = rewriteTgdsWithoutSelfJoins(originalTgds, mappingTask);
   }
   //        RenameVariables renamer = new RenameVariables();
   //        List<FORule> result = renamer.renameAliasesInRules(rewrittenRules);
   //        return result;
   return rewrittenRules;
 }
Ejemplo n.º 10
0
 private static String generateSQLForSkolemTable(MappingTask mappingTask) {
   StringBuilder result = new StringBuilder();
   result.append(
       "\n--------------------------  SKOLEM TABLE  -------------------------------\n\n");
   result.append(CREATE_TABLE).append(SKOLEM_TABLE_NAME).append("(\n");
   result
       .append(INDENT)
       .append(SKOLEM_TABLE_COLUMN_ID)
       .append(" ")
       .append(mappingTask.getDBMSHandler().getAutoGeneratedColumnType())
       .append(",\n");
   result.append(INDENT).append(SKOLEM_TABLE_COLUMN_SKOLEM);
   result.append(GenerateSQL.getSkolemColumnType(mappingTask.getConfig()));
   allTablesToDelete.add(SKOLEM_TABLE_NAME);
   result.append(");\n");
   return result.toString();
 }
Ejemplo n.º 11
0
 private ISkolemPart generateAppendsForSubGenerator(
     SkolemFunctionGenerator generator, MappingTask mappingTask) {
   ISkolemPart append =
       new AppendSkolemPart(mappingTask.getConfig().useSortInSkolems(), "(", ")", ", ");
   for (GeneratorWithPath subGeneratorWithPath : generator.getSubGenerators()) {
     VariablePathExpression subGeneratorPath = subGeneratorWithPath.getTargetPath();
     IValueGenerator subGenerator = subGeneratorWithPath.getGenerator();
     ISkolemPart appendForGenerator = new AppendSkolemPart(false, "", "", ": ");
     StringSkolemPart stringPart =
         new StringSkolemPart(
             SpicyEngineUtility.removeRootLabel(subGeneratorPath.getAbsolutePath()));
     SubGeneratorSkolemPart generatorPart = new SubGeneratorSkolemPart(subGenerator);
     appendForGenerator.addChild(stringPart);
     appendForGenerator.addChild(generatorPart);
     append.addChild(appendForGenerator);
   }
   return append;
 }
Ejemplo n.º 12
0
 private ISkolemPart generatePartForAlias(
     SetAlias variable, List<GeneratorWithPath> subGenerators, MappingTask mappingTask) {
   if (logger.isDebugEnabled())
     logger.debug("Generating part for set alias: " + variable + "\nGenerators: " + subGenerators);
   ISkolemPart generatorsAppend =
       new AppendSkolemPart(mappingTask.getConfig().useSortInSkolems(), "[", "]", ", ");
   for (GeneratorWithPath subGeneratorWithPath : subGenerators) {
     IValueGenerator subGenerator = subGeneratorWithPath.getGenerator();
     PathExpression absolutePath = subGeneratorWithPath.getTargetPath().getAbsolutePath();
     ISkolemPart appendForSubGenerator = new AppendSkolemPart(false, "", "", ": ");
     appendForSubGenerator.addChild(
         new StringSkolemPart(SpicyEngineUtility.removeRootLabel(absolutePath)));
     appendForSubGenerator.addChild(new SubGeneratorSkolemPart(subGenerator));
     generatorsAppend.addChild(appendForSubGenerator);
   }
   if (logger.isDebugEnabled()) logger.debug("Final result: " + generatorsAppend);
   return generatorsAppend;
 }
Ejemplo n.º 13
0
 public ISkolemPart generateSkolemFunction(
     SkolemFunctionGenerator generator, MappingTask mappingTask) {
   if (generator.isLeafGenerator()) {
     if (generator.getType() == SkolemFunctionGenerator.STANDARD) {
       if (mappingTask.getConfig().useLocalSkolem()) {
         return generateLocalSkolemFunction(generator, mappingTask);
       } else {
         return generateHyperGraphSkolemFunction(generator, mappingTask);
       }
     } else if (generator.getType() == SkolemFunctionGenerator.KEY) {
       return generateSkolemFunctionForKey(generator, mappingTask);
     } else if (generator.getType() == SkolemFunctionGenerator.EGD_BASED) {
       return generateEGDSkolemFunction(generator, mappingTask);
     }
     throw new IllegalArgumentException(
         "Incorrect type for leaf generator: " + generator + " - Type: " + generator.getType());
   } else {
     return generateSkolemFunctionForIntermediateNode(generator, mappingTask);
   }
 }
Ejemplo n.º 14
0
 //////////////////////////// second part: joins part
 private ISkolemPart generatePartForJoins(
     SkolemFunctionGenerator generator,
     Map<VariableJoinCondition, ISkolemPart> partsForJoins,
     MappingTask mappingTask) {
   List<VariableJoinCondition> joinConditions =
       generator.getTgd().getTargetView().getAllJoinConditions();
   if (joinConditions.isEmpty()) {
     return NullSkolemPart.getInstance();
   }
   List<VariableJoinCondition> sortedConditions =
       new ArrayList<VariableJoinCondition>(joinConditions);
   Collections.sort(sortedConditions);
   ISkolemPart append =
       new AppendSkolemPart(mappingTask.getConfig().useSortInSkolems(), "", "", ", ");
   for (VariableJoinCondition variableJoinCondition : sortedConditions) {
     ISkolemPart joinPart = partsForJoins.get(variableJoinCondition);
     append.addChild(joinPart);
   }
   return append;
 }