Exemplo n.º 1
0
  /**
   * Generate LUA code for SQL like and not like
   *
   * @param item is field1_like_field2
   * @return Lua code
   */
  private String processLikeStatement(String item) {
    Pattern detectFieldPattern = Definitions.getDetectFieldPattern();
    String operation = item.contains("_notlike_") ? "not " : "";
    operation += "string.match(";
    String[] itemSplit = item.split("_like_|_notlike_");

    boolean addComma = false;
    for (String subItem : itemSplit) {
      Matcher detectFieldMatcher = detectFieldPattern.matcher(subItem);

      if (detectFieldMatcher.find()) {
        if (subItem.matches(Definitions.defaultQuotesReplacementName + "\\d+"))
          operation += subItem;
        else operation += "topRec[\"" + subItem + "\"]";
        if (!addComma) {
          operation += ", ";
          addComma = true;
        }
      } else operation += subItem;
    }
    return operation + ") ";
  }
Exemplo n.º 2
0
  /**
   * Parse statements (examples statements is 1 + 2 + value)
   *
   * @param statement - transformation in select statements, e.g. (1+2)/3 + hour(timestamp) + 400
   * @param functions - detected functions in statements, e.g. HOUR, DAY
   * @return - LUA code for a statements.
   */
  public String parseStatement(String statement, HashSet<String> functions) {
    String[] fields =
        statement.split(
            Definitions.transformationOperatorsRegex(
                true)); // split a statements with positive lookahead. This means: "a, b,
                        // c".split(",") => ["a,", "b,", "c"]
    String item = fields[0].trim();

    if (item.equalsIgnoreCase("condition()")
        || item.equalsIgnoreCase("true")
        || item.equalsIgnoreCase("false")) {
      if (fields.length > 1) {
        return item + parseStatement(arrayToString(fields, 1), functions);
      } else {
        return item;
      }
    }

    if (item.equals("") || item.matches(Definitions.defaultQuotesReplacementName + "\\d+")) {
      if (fields.length > 1) {
        item = fields[1];
        fields[1] = "";
      } else {
        return item;
      }
    }

    if (Definitions.transformationsOperators.contains(item.toUpperCase())) {
      return " " + item.toLowerCase() + " "; // operator +, - etc.
    }

    if (Definitions.transformations.contains(item.toUpperCase())) {
      functions.add(item.toLowerCase()); // add LUA function code for transformation
      return item.toLowerCase();
    }

    // split: + field1 etc.
    String[] itemSplit = item.split(Definitions.transformationOperatorsRegex(false));
    if (itemSplit.length > 1)
      return parseStatement(itemSplit[0], functions)
          + // first part of item
          parseStatement(arrayToString(itemSplit, 1), functions)
          + // second part of item
          parseStatement(arrayToString(fields, 1), functions); // all others

    // process parentheses
    Pattern detectLeftParenthesis =
        Pattern.compile("(.*)\\((.+)"); // value before right parenthesis and after
    Matcher matchLeftParenthesis = detectLeftParenthesis.matcher(item);
    if (matchLeftParenthesis.find()) {
      return parseStatement(matchLeftParenthesis.group(1), functions)
          + "("
          + parseStatement(matchLeftParenthesis.group(2), functions)
          + parseStatement(arrayToString(fields, 1), functions);
    }

    Pattern detectRightParenthesis = Pattern.compile("(.+)\\)");
    Matcher matchRightParenthesis = detectRightParenthesis.matcher(item);
    if (matchRightParenthesis.find()) {
      return parseStatement(matchRightParenthesis.group(1), functions) + ")";
    }

    Pattern detectFieldPattern = Definitions.getDetectFieldPattern();
    Matcher detectFieldMatcher = detectFieldPattern.matcher(item);

    if (detectFieldMatcher.find()) {
      if (item.contains("like_")) {
        return processLikeStatement(item)
            + parseStatement(arrayToString(fields, 1), functions); // item is a field in AS;
      } else {
        return "topRec[\""
            + item
            + "\"]"
            + parseStatement(arrayToString(fields, 1), functions); // item is a field in AS
      }
    }

    return statement.trim();
  }