예제 #1
0
  public static String projectionOnTargetValuesFromCorrespondences(
      List<VariableCorrespondence> correspondences, String fromClause, String whereClause) {
    StringBuilder result = new StringBuilder();
    result.append(INDENT).append("select distinct \n");
    for (int i = 0; i < correspondences.size(); i++) {
      VariableCorrespondence correspondence = correspondences.get(i);

      if (correspondence.getSourcePaths() != null) {
        result
            .append(DOUBLE_INDENT)
            .append(GenerateSQL.attributeNameWithVariable(correspondence.getFirstSourcePath()));
      } else {
        String sourcePathName = correspondence.getSourceValue().toString();
        sourcePathName = sourcePathName.replaceAll("\"", "\'");
        result.append(DOUBLE_INDENT).append(sourcePathName);
      }

      result
          .append(" as ")
          .append(GenerateSQL.attributeNameInVariable(correspondence.getFirstSourcePath()));
      if (i != correspondences.size() - 1) result.append(",\n");
    }
    result.append("\n");
    result.append(INDENT).append(fromClause);
    if (!whereClause.equals("")) {
      result.append(INDENT).append(whereClause);
    }
    return result.toString();
  }
예제 #2
0
  public String convertToPostgres(Expression transformationFunctionExpression, boolean withRel) {

    String functionExpression =
        transformationFunctionExpression.getJepExpression().toStringForSql();
    // first fix attribute names
    for (VariablePathExpression vpe : transformationFunctionExpression.getAttributePaths()) {
      if (functionExpression.contains(vpe.toString())) {
        String expressionToCheckForSigns =
            transformationFunctionExpression.getJepExpression().toString();
        int startIndex = expressionToCheckForSigns.indexOf(vpe.toString());
        int endIndex = startIndex + vpe.toString().length();
        boolean castToFloat = false;
        // check so that the String index will not get out of bounds
        if (startIndex - 2 >= 0) {
          if (math_sign.contains(expressionToCheckForSigns.charAt(startIndex - 2))) {
            castToFloat = true;
          }
          // to catch the '>=' or '<=' case
          else if (expressionToCheckForSigns.charAt(startIndex - 2) == '=' && startIndex - 3 >= 0) {
            if (expressionToCheckForSigns.charAt(startIndex - 3) == '<'
                || expressionToCheckForSigns.charAt(startIndex - 3) == '>') {
              castToFloat = true;
            }
          }
        }
        if (endIndex + 2 <= expressionToCheckForSigns.length()) {
          if (math_sign.contains(expressionToCheckForSigns.charAt(endIndex + 1))) {
            castToFloat = true;
          }
        }
        String newAttrName;
        // replace them with the corresponding attribute name
        if (withRel) {
          newAttrName = GenerateSQL.attributeNameWithVariable(vpe);
        } else {
          newAttrName = GenerateSQL.attributeNameInVariable(vpe);
        }
        // if previous or next character is one of {+,-,*,/,<,>} cast it to float
        if (castToFloat) {
          functionExpression =
              functionExpression.replaceAll(vpe.toString(), "cast(" + newAttrName + " as float)");
        } else {
          functionExpression = functionExpression.replaceAll(vpe.toString(), newAttrName);
        }
      }
    }
    return replaceExpression(functionExpression);
  }
예제 #3
0
 //    private static List<SetAlias> extractDifferentVariables(List<SetAlias> variables) {
 //        List<SetAlias> result = new ArrayList<SetAlias>();
 //        for (SetAlias variable : variables) {
 //            if (!containsVariableWithSameName(variable, result)) {
 //                result.add(variable);
 //            }
 //        }
 //        return result;
 //    }
 //    private static boolean containsVariableWithSameName(SetAlias variable, List<SetAlias>
 // variables) {
 //        for (SetAlias setAlias : variables) {
 //            if (setAlias.hasSameId(variable)) {
 //                return true;
 //            }
 //        }
 //        return false;
 //    }
 private static String generateProjectionOnAllAttributes(
     List<SetAlias> variables, MappingTask mappingTask) {
   StringBuilder result = new StringBuilder();
   for (int i = 0; i < variables.size(); i++) {
     SetAlias variable = variables.get(i);
     List<VariablePathExpression> attributes =
         variable.getAttributes(mappingTask.getSourceProxy().getIntermediateSchema());
     for (int j = 0; j < attributes.size(); j++) {
       VariablePathExpression attribute = attributes.get(j);
       result.append(DOUBLE_INDENT).append(GenerateSQL.attributeNameWithVariable(attribute));
       result.append(" AS ").append(attributeNameInVariable(attribute));
       if (j != attributes.size() - 1) result.append(",\n");
     }
     if (i != variables.size() - 1) result.append(",\n");
   }
   result.append("\n");
   return result.toString();
 }