Search in sources :

Example 6 with AstStringPart

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

the class SqlServerTransformer method replacedEscapes.

private boolean replacedEscapes(AstStringConstant string, AstFunNode concat) {
    boolean find = false;
    for (int i = 0; i < string.jjtGetNumChildren(); i++) {
        if (string.child(i) instanceof AstStringPart) {
            AstStringPart child = (AstStringPart) string.child(i);
            Matcher matcher = Pattern.compile("\\\\([bfnrt])").matcher(child.getContent());
            while (matcher.find()) {
                find = true;
                StringBuffer buffer = new StringBuffer();
                matcher.appendReplacement(buffer, "");
                for (int j = 0; j < i; j++) {
                    getLastString(concat).addChild(string.child(j));
                }
                if (!"".equals(buffer.toString()))
                    getLastString(concat).addChild(new AstStringPart(buffer.toString(), true));
                concat.addChild(CHAR.node(AstNumericConstant.of(AstStringConstant.getASCII(matcher.group(1).charAt(0)))));
            }
            if (find) {
                String tail = matcher.appendTail(new StringBuffer()).toString();
                if (!"".equals(tail))
                    getLastString(concat).addChild(new AstStringPart(tail));
                string.removeChild(child);
            }
        }
    }
    return find;
}
Also used : Matcher(java.util.regex.Matcher) AstStringPart(com.developmentontheedge.sql.model.AstStringPart)

Example 7 with AstStringPart

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

the class SqlServerTransformer method transformString.

@Override
protected void transformString(AstStringConstant string) {
    for (AstStringPart child : string.children().select(AstStringPart.class)) {
        String content = child.getContent().replace("\\'", "''");
        child.setContent(string.isEscape() ? content.replaceAll("\\\\([^bfnrt])", "$1") : content, true);
    }
    if (string.isEscape()) {
        string.setEscape(false);
        AstFunNode concat = DefaultParserContext.FUNC_PLUS.node();
        if (replacedEscapes(string, concat))
            string.replaceWith(concat);
    }
}
Also used : AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstStringPart(com.developmentontheedge.sql.model.AstStringPart)

Example 8 with AstStringPart

use of com.developmentontheedge.sql.model.AstStringPart 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 9 with AstStringPart

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

the class ContextApplier method applySubQuery.

private void applySubQuery(AstBeSqlSubQuery subQuery) {
    if (subQuery.getExec() != null || subQuery.getQueryName() != null) {
        String name = subQuery.getQueryName();
        if (name == null) {
            throw new IllegalStateException("Empty subQuery without queryName parameter: " + subQuery.format());
        }
        String entity = subQuery.getEntityName();
        String subQueryText = context.resolveQuery(entity, name);
        if (subQueryText == null) {
            throw new IllegalStateException("Unable to resolve subquery: " + (entity == null ? "" : entity + ".") + name);
        }
        AstStart start = SqlQuery.parse(subQueryText);
        subQuery.addChild(start.getQuery());
    }
    if (subQuery.getQuery() != null) {
        if (subQuery.getOutColumns() != null)
            new ColumnsApplier().keepOnlyOutColumns(subQuery);
        if (subQuery.getLimit() != null)
            new LimitsApplier(0, subQuery.getLimit()).transformQuery(subQuery.getQuery());
        String keyStr = subQuery.getFilterKeys();
        String valPropStr = subQuery.getFilterValProperties();
        if (keyStr != null && valPropStr != null) {
            String[] keys = keyStr.split(",");
            String[] valProps = valPropStr.split(",");
            Map<ColumnRef, String> conditions = new HashMap<>();
            for (int i = 0; i < keys.length; i++) {
                // ColumnRef.resolve(subQuery.getQuery(), keys[i])
                conditions.put(new ColumnRef(null, keys[i]), "<var:" + valProps[i] + "/>");
            // subQuery.addParameter(keys[i], valProps[i]);
            }
        // new FilterApplier().addFilter(subQuery.getQuery(), conditions);
        }
    }
    // AstBeSqlVar beSqlVar = subQuery.getAstBeSqlVar();
    // AstStart start = SqlQuery.parse(context.getParameter(beSqlVar.getName()));
    // subQuery.addChild(start.getQuery());
    // subQuery.replaceWith(beSqlVar);
    // subQuery.removeChildren();
    String key = "<sql> SubQuery# " + (context.getSubQueries().size() + 1) + "</sql>";
    context.getSubQueries().put(key, subQuery);
    subQuery.replaceWith(new AstStringPart(key));
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) HashMap(java.util.HashMap) AstStringPart(com.developmentontheedge.sql.model.AstStringPart)

Example 10 with AstStringPart

use of com.developmentontheedge.sql.model.AstStringPart 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

AstStringPart (com.developmentontheedge.sql.model.AstStringPart)10 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)3 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)3 Matcher (java.util.regex.Matcher)3 AstConcatExpression (com.developmentontheedge.sql.model.AstConcatExpression)2 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)2 AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)1 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)1 AstOrderingElement (com.developmentontheedge.sql.model.AstOrderingElement)1 AstStart (com.developmentontheedge.sql.model.AstStart)1 HashMap (java.util.HashMap)1