Search in sources :

Example 1 with AstStringPart

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

the class DB2Transformer 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);
        AstConcatExpression concat = new AstConcatExpression(SqlParserTreeConstants.JJTCONCATEXPRESSION);
        if (replacedEscapes(string, concat))
            string.replaceWith(concat);
    }
}
Also used : AstConcatExpression(com.developmentontheedge.sql.model.AstConcatExpression) AstStringPart(com.developmentontheedge.sql.model.AstStringPart)

Example 2 with AstStringPart

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

the class DB2Transformer method replacedEscapes.

private boolean replacedEscapes(AstStringConstant string, AstConcatExpression 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(parserContext.getFunction("CHR").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 3 with AstStringPart

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

the class OracleTransformer method replacedEscapes.

private boolean replacedEscapes(AstStringConstant string, AstConcatExpression 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(parserContext.getFunction("CHR").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 4 with AstStringPart

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

the class ContextApplier method applySessionTag.

private void applySessionTag(AstBeSessionTag child) {
    String name = child.getName();
    Object value = context.getSessionVariable(name);
    if (value == null) {
        if (child.getDefault() != null) {
            value = SqlTypeUtils.parseValue(child.getDefault(), child.getType());
        } else {
            value = "";
        }
    }
    SimpleNode replacement;
    // TODO: support refColumn; smart quoting - in server module
    if (child.jjtGetParent() instanceof AstStringConstant) {
        replacement = new AstStringPart(value.toString());
    } else {
        if (SqlTypeUtils.isNumber(value.getClass())) {
            replacement = AstNumericConstant.of((Number) value);
        } else {
            replacement = new AstStringConstant(value.toString());
        }
    }
    child.replaceWith(replacement);
}
Also used : AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 5 with AstStringPart

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

the class OracleTransformer 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);
        AstConcatExpression concat = new AstConcatExpression(SqlParserTreeConstants.JJTCONCATEXPRESSION);
        if (replacedEscapes(string, concat))
            string.replaceWith(concat);
    }
}
Also used : AstConcatExpression(com.developmentontheedge.sql.model.AstConcatExpression) AstStringPart(com.developmentontheedge.sql.model.AstStringPart)

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