Search in sources :

Example 11 with AstStringConstant

use of com.developmentontheedge.sql.model.AstStringConstant in project be5 by DevelopmentOnTheEdge.

the class ContextApplier method applyVar.

private void applyVar(String key, AstBeSqlVar varNode, Function<String, String> varResolver) {
    String value = varResolver.apply(context.getSubQueries().get(key).translateVar(varNode.getName()));
    if (value == null)
        value = varNode.getDefault();
    SimpleNode constant;
    if (varNode.jjtGetParent() instanceof AstBeSqlSubQuery && value != null && !"".equals(value.trim())) {
        constant = SqlQuery.parse(value).getQuery();
    } else {
        if (value == null) {
            value = "null";
        }
        constant = varNode.jjtGetParent() instanceof AstStringConstant ? new AstStringPart(value) : new AstIdentifierConstant(value);
    }
    varNode.replaceWith(constant);
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 12 with AstStringConstant

use of com.developmentontheedge.sql.model.AstStringConstant in project be5 by DevelopmentOnTheEdge.

the class FilterApplier method toNode.

private SimpleNode toNode(Object value) {
    if (SqlTypeUtils.isNumber(value.getClass()))
        return AstNumericConstant.of((Number) value);
    String strValue = value.toString();
    Matcher matcher = BeSqlVar_PATTERN.matcher(strValue);
    if (matcher.find()) {
        return new AstBeSqlVar(matcher.group(1));
    }
    return new AstStringConstant(strValue);
}
Also used : Matcher(java.util.regex.Matcher) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstBeSqlVar(com.developmentontheedge.sql.model.AstBeSqlVar)

Example 13 with AstStringConstant

use of com.developmentontheedge.sql.model.AstStringConstant in project be5 by DevelopmentOnTheEdge.

the class GenericDbmsTransformer method recursiveProcessing.

private void recursiveProcessing(SimpleNode node) {
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        SimpleNode child = node.child(i);
        if (child instanceof AstFunNode) {
            transformFunction((AstFunNode) child);
        }
        if (child instanceof AstSelect) {
            transformSelect((AstSelect) child);
        }
        if (child instanceof AstIdentifierConstant) {
            transformIdentifier((AstIdentifierConstant) child);
        }
        if (child instanceof AstStringConstant) {
            transformString((AstStringConstant) child);
        }
        if (child instanceof AstCast) {
            transformCastExpression((AstCast) child);
        }
        if (child instanceof AstExtract) {
            transformExtractExpression((AstExtract) child);
        }
        if (child instanceof AstPosition) {
            transformPosition((AstPosition) child);
        }
        if (child instanceof AstInterval) {
            transformInterval((AstInterval) child);
        }
        if (child instanceof AstWith) {
            transformWith((AstWith) child);
        }
        if (child instanceof AstExcept) {
            transformExcept((AstExcept) child);
        }
        recursiveProcessing(child);
    }
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstCast(com.developmentontheedge.sql.model.AstCast) AstPosition(com.developmentontheedge.sql.model.AstPosition) AstWith(com.developmentontheedge.sql.model.AstWith) AstExtract(com.developmentontheedge.sql.model.AstExtract) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstExcept(com.developmentontheedge.sql.model.AstExcept) AstInterval(com.developmentontheedge.sql.model.AstInterval) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 14 with AstStringConstant

use of com.developmentontheedge.sql.model.AstStringConstant in project be5 by DevelopmentOnTheEdge.

the class GenericDbmsTransformer method transformFunction.

protected void transformFunction(AstFunNode node) {
    String name = node.getFunction().getName().toLowerCase();
    if (node.getFunction() instanceof DbSpecificFunction && !((DbSpecificFunction) node.getFunction()).isApplicable(getDbms()) && !node.withinDbmsTransform()) {
        throw new IllegalStateException("Function/operator '" + node.getFunction().getName() + "' is unsupported for " + getDbms());
    }
    if (DbSpecificFunction.needsTransformation(getDbms()).test(node.getFunction()))
        expandDbFunction(node, getDbms());
    if ("concat".equals(name) || ("||".equals(name) && node.jjtGetNumChildren() > 1))
        transformConcat(node);
    else if ("coalesce".equals(name))
        transformCoalesce(node);
    else if ("substr".equals(name))
        transformSubstr(node);
    else if ("length".equals(name))
        transformLength(node);
    else if ("chr".equals(name))
        transformChr(node);
    else if ("if".equals(name))
        transformIf(node);
    else if ("date_format".equals(name) || ("to_char".equals(name) && node.jjtGetNumChildren() == 2)) {
        SimpleNode child = node.child(1);
        if (child instanceof AstStringConstant) {
            String formatString = ((AstStringConstant) child).getValue();
            DateFormat df = DateFormat.byFormatString(formatString);
            if (df == null)
                throw new IllegalArgumentException("Unknown date format: " + formatString);
            transformDateFormat(node, df);
        } else
            throw new IllegalArgumentException("Date format is not a String");
    } else if ("to_char".equals(name) && node.jjtGetNumChildren() == 1 || "to_number".equals(name) || "to_key".equals(name))
        transformCastOracle(node);
    else if ("lpad".equals(name))
        transformLpad(node);
    else if ("trunc".equals(name))
        transformTrunc(node);
    else if ("now".equals(name))
        transformNow(node);
    else if ("to_date".equals(name))
        transformToDate(node);
    else if ("year".equals(name) || "month".equals(name) || "day".equals(name))
        transformYearMonthDay(node);
    else {
        DateFormat df = DateFormat.byFunction(name);
        if (df != null)
            transformDateFormat(node, df);
    }
    if ("date_trunc".equals(name)) {
        String datePart = ((AstStringConstant) node.child(0)).getValue();
        if (datePart.equalsIgnoreCase("'month'") || datePart.equalsIgnoreCase("'year'"))
            transformDateTrunc(node);
    }
    if ("instr".equals(name))
        transformInstr(node);
    if ("add_months".equals(name) || "add_days".equals(name) || "add_millis".equals(name))
        transformDateAdd(node);
    if ("last_day".equals(name))
        transformLastDay(node);
    if ("seconddiff".equals(name))
        transformDateTimeDiff(node, "SECOND");
    if ("minutediff".equals(name))
        transformDateTimeDiff(node, "MINUTE");
    if ("hourdiff".equals(name))
        transformDateTimeDiff(node, "HOUR");
    if ("daydiff".equals(name))
        transformDateTimeDiff(node, "DAY");
    if ("monthdiff".equals(name))
        transformDateTimeDiff(node, "MONTH");
    if ("yeardiff".equals(name))
        transformDateTimeDiff(node, "YEAR");
    if ("timestampdiff".equals(name)) {
        SimpleNode child = node.child(0);
        String datePart = child instanceof AstFieldReference ? ((AstIdentifierConstant) child.child(0)).getValue() : "";
        transformDateTimeDiff(node, datePart);
    }
    if ("mod".equals(name) || "%".equals(name))
        transformMod(node);
    if ("least".equals(name) || "greatest".equals(name))
        transformLeastGreatest(node);
    if ("&".equals(name))
        transformBitAnd(node);
    if ("|".equals(name))
        transformBitOr(node);
    if ("right".equals(name))
        transformRight(node);
    if ("left".equals(name))
        transformLeft(node);
    if ("decode".equals(name))
        transformDecode(node);
    if ("string_agg".equals(name))
        transformStringAgg(node);
    if ("regexp_like".equals(name) || "~".equals(name))
        transformRegexpLike(node);
    if ("translate".equals(name))
        transformTranslate(node);
    if ("levenshtein".equals(name))
        transformLevenshtein(node);
}
Also used : DbSpecificFunction(com.developmentontheedge.sql.model.DbSpecificFunction) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 15 with AstStringConstant

use of com.developmentontheedge.sql.model.AstStringConstant in project be5 by DevelopmentOnTheEdge.

the class ContextApplier method applyParameters.

private SimpleNode applyParameters(AstBeParameterTag paramNode, String value, boolean tableRefAddend) {
    String defValue = paramNode.getDefValue() != null ? paramNode.getDefValue() : "";
    String prefix = paramNode.getPrefix() != null ? paramNode.getPrefix() : "";
    String postfix = paramNode.getPostfix() != null ? paramNode.getPostfix() : "";
    String regex = paramNode.getRegex();
    String repl = paramNode.getReplacement();
    String changeCase = paramNode.getCase();
    String type = paramNode.getType();
    boolean safeStr = "yes".equals(paramNode.getSafeStr()) && !tableRefAddend;
    if (value == null)
        value = "";
    if (value.equals(""))
        value = defValue;
    if (regex != null && repl != null)
        value = value.replaceAll(regex, repl);
    if ("upper".equals(changeCase))
        value = value.toUpperCase();
    if ("lower".equals(changeCase))
        value = value.toLowerCase();
    if ("capitalize".equals(changeCase)) {
        value = value.toLowerCase();
        if (value.length() > 0)
            value = value.substring(0, 1).toUpperCase() + value.substring(1);
    }
    value = prefix + value + postfix;
    SimpleNode constant;
    if (paramNode.jjtGetParent() instanceof AstStringConstant) {
        constant = new AstStringPart(value);
    } else if (type != null) {
        if (SqlTypeUtils.isNumber(type)) {
            if (!value.equals("")) {
                constant = AstNumericConstant.of((Number) SqlTypeUtils.parseValue(value, type));
            } else {
                constant = new AstIdentifierConstant(value);
            }
        } else {
            constant = new AstStringConstant(value);
        }
    } else if (!safeStr) {
        constant = new AstIdentifierConstant(value);
    } else if (paramNode.jjtGetParent() instanceof AstOrderingElement) {
        constant = new AstIdentifierConstant(value, false);
    } else {
        constant = new AstStringConstant(value);
    }
    return constant;
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Aggregations

AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)15 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)10 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)5 Function (com.developmentontheedge.sql.model.Function)4 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)4 AstCast (com.developmentontheedge.sql.model.AstCast)3 AstInterval (com.developmentontheedge.sql.model.AstInterval)3 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)3 AstStringPart (com.developmentontheedge.sql.model.AstStringPart)3 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)2 AstBeListPlaceHolder (com.developmentontheedge.sql.model.AstBeListPlaceHolder)1 AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)1 AstBeSqlVar (com.developmentontheedge.sql.model.AstBeSqlVar)1 AstExcept (com.developmentontheedge.sql.model.AstExcept)1 AstExtract (com.developmentontheedge.sql.model.AstExtract)1 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)1 AstFirstDayOf (com.developmentontheedge.sql.model.AstFirstDayOf)1 AstInValueList (com.developmentontheedge.sql.model.AstInValueList)1 AstNumericConstant (com.developmentontheedge.sql.model.AstNumericConstant)1 AstOrderingElement (com.developmentontheedge.sql.model.AstOrderingElement)1