Search in sources :

Example 1 with ESCAPED_TEXT

use of org.jdbi.v3.core.internal.lexer.DefineStatementLexer.ESCAPED_TEXT in project jdbi by jdbi.

the class DefinedAttributeTemplateEngine method render.

@Override
public String render(String template, StatementContext ctx) {
    StringBuilder b = new StringBuilder();
    DefineStatementLexer lexer = new DefineStatementLexer(new ANTLRStringStream(template));
    try {
        Token t = lexer.nextToken();
        while (t.getType() != EOF) {
            switch(t.getType()) {
                case COMMENT:
                case LITERAL:
                case QUOTED_TEXT:
                case DOUBLE_QUOTED_TEXT:
                    b.append(t.getText());
                    break;
                case DEFINE:
                    String text = t.getText();
                    String key = text.substring(1, text.length() - 1);
                    Object value = ctx.getAttribute(key);
                    if (value == null) {
                        throw new UnableToCreateStatementException("Undefined attribute for token '" + text + "'", ctx);
                    }
                    b.append(value);
                    break;
                case ESCAPED_TEXT:
                    b.append(t.getText().substring(1));
                    break;
                default:
                    break;
            }
            t = lexer.nextToken();
        }
        return b.toString();
    } catch (RuntimeException e) {
        throw new UnableToCreateStatementException("Error rendering SQL template: '" + template + "'", e, ctx);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) DefineStatementLexer(org.jdbi.v3.core.internal.lexer.DefineStatementLexer) Token(org.antlr.runtime.Token)

Example 2 with ESCAPED_TEXT

use of org.jdbi.v3.core.internal.lexer.DefineStatementLexer.ESCAPED_TEXT in project jdbi by jdbi.

the class ColonPrefixSqlParser method internalParse.

@Override
ParsedSql internalParse(String sql) {
    ParsedSql.Builder parsedSql = ParsedSql.builder();
    ColonStatementLexer lexer = new ColonStatementLexer(CharStreams.fromString(sql));
    lexer.addErrorListener(new ErrorListener());
    Token t = lexer.nextToken();
    while (t.getType() != EOF) {
        switch(t.getType()) {
            case COMMENT:
            case LITERAL:
            case QUOTED_TEXT:
            case DOUBLE_QUOTED_TEXT:
                parsedSql.append(t.getText());
                break;
            case NAMED_PARAM:
                parsedSql.appendNamedParameter(t.getText().substring(1));
                break;
            case POSITIONAL_PARAM:
                parsedSql.appendPositionalParameter();
                break;
            case ESCAPED_TEXT:
                parsedSql.append(t.getText().substring(1));
                break;
            default:
                break;
        }
        t = lexer.nextToken();
    }
    return parsedSql.build();
}
Also used : ErrorListener(org.jdbi.v3.core.statement.internal.ErrorListener) Token(org.antlr.v4.runtime.Token) ColonStatementLexer(org.jdbi.v3.core.internal.lexer.ColonStatementLexer)

Example 3 with ESCAPED_TEXT

use of org.jdbi.v3.core.internal.lexer.DefineStatementLexer.ESCAPED_TEXT in project jdbi by jdbi.

the class HashPrefixSqlParser method internalParse.

@Override
ParsedSql internalParse(String sql) {
    ParsedSql.Builder parsedSql = ParsedSql.builder();
    HashStatementLexer lexer = new HashStatementLexer(CharStreams.fromString(sql));
    lexer.addErrorListener(new ErrorListener());
    Token t = lexer.nextToken();
    while (t.getType() != EOF) {
        switch(t.getType()) {
            case COMMENT:
            case LITERAL:
            case QUOTED_TEXT:
            case DOUBLE_QUOTED_TEXT:
                parsedSql.append(t.getText());
                break;
            case NAMED_PARAM:
                parsedSql.appendNamedParameter(t.getText().substring(1));
                break;
            case POSITIONAL_PARAM:
                parsedSql.appendPositionalParameter();
                break;
            case ESCAPED_TEXT:
                parsedSql.append(t.getText().substring(1));
                break;
            default:
                break;
        }
        t = lexer.nextToken();
    }
    return parsedSql.build();
}
Also used : ErrorListener(org.jdbi.v3.core.statement.internal.ErrorListener) HashStatementLexer(org.jdbi.v3.core.internal.lexer.HashStatementLexer) Token(org.antlr.v4.runtime.Token)

Example 4 with ESCAPED_TEXT

use of org.jdbi.v3.core.internal.lexer.DefineStatementLexer.ESCAPED_TEXT in project jdbi by jdbi.

the class DefinedAttributeTemplateEngine method parse.

@Override
public Optional<Function<StatementContext, String>> parse(String template, ConfigRegistry config) {
    StringBuilder buf = new StringBuilder();
    List<BiConsumer<StatementContext, StringBuilder>> preparation = new ArrayList<>();
    Runnable pushBuf = () -> {
        // NOPMD
        if (buf.length() > 0) {
            String bit = buf.toString();
            buf.setLength(0);
            preparation.add((ctx, b) -> b.append(bit));
        }
    };
    DefineStatementLexer lexer = new DefineStatementLexer(CharStreams.fromString(template));
    lexer.addErrorListener(new ErrorListener());
    Token t = lexer.nextToken();
    while (t.getType() != EOF) {
        switch(t.getType()) {
            case COMMENT:
            case LITERAL:
            case QUOTED_TEXT:
            case DOUBLE_QUOTED_TEXT:
                buf.append(t.getText());
                break;
            case DEFINE:
                pushBuf.run();
                String text = t.getText();
                String key = text.substring(1, text.length() - 1);
                preparation.add((ctx, b) -> {
                    Object value = ctx.getAttribute(key);
                    if (value == null) {
                        throw new UnableToCreateStatementException("Undefined attribute for token '" + text + "'", ctx);
                    }
                    b.append(value);
                });
                break;
            case ESCAPED_TEXT:
                buf.append(t.getText().substring(1));
                break;
            default:
                break;
        }
        t = lexer.nextToken();
    }
    pushBuf.run();
    return Optional.of(ctx -> {
        try {
            StringBuilder result = new StringBuilder();
            preparation.forEach(a -> a.accept(ctx, result));
            return result.toString();
        } catch (RuntimeException e) {
            throw new UnableToCreateStatementException("Error rendering SQL template: '" + template + "'", e, ctx);
        }
    });
}
Also used : ESCAPED_TEXT(org.jdbi.v3.core.internal.lexer.DefineStatementLexer.ESCAPED_TEXT) LITERAL(org.jdbi.v3.core.internal.lexer.DefineStatementLexer.LITERAL) Token(org.antlr.v4.runtime.Token) COMMENT(org.jdbi.v3.core.internal.lexer.DefineStatementLexer.COMMENT) QUOTED_TEXT(org.jdbi.v3.core.internal.lexer.DefineStatementLexer.QUOTED_TEXT) Function(java.util.function.Function) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) DOUBLE_QUOTED_TEXT(org.jdbi.v3.core.internal.lexer.DefineStatementLexer.DOUBLE_QUOTED_TEXT) ArrayList(java.util.ArrayList) List(java.util.List) CharStreams(org.antlr.v4.runtime.CharStreams) DEFINE(org.jdbi.v3.core.internal.lexer.DefineStatementLexer.DEFINE) BiConsumer(java.util.function.BiConsumer) Optional(java.util.Optional) ErrorListener(org.jdbi.v3.core.statement.internal.ErrorListener) DefineStatementLexer(org.jdbi.v3.core.internal.lexer.DefineStatementLexer) EOF(org.antlr.v4.runtime.Recognizer.EOF) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) ErrorListener(org.jdbi.v3.core.statement.internal.ErrorListener) DefineStatementLexer(org.jdbi.v3.core.internal.lexer.DefineStatementLexer) BiConsumer(java.util.function.BiConsumer)

Aggregations

Token (org.antlr.v4.runtime.Token)3 ErrorListener (org.jdbi.v3.core.statement.internal.ErrorListener)3 DefineStatementLexer (org.jdbi.v3.core.internal.lexer.DefineStatementLexer)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 Token (org.antlr.runtime.Token)1 CharStreams (org.antlr.v4.runtime.CharStreams)1 EOF (org.antlr.v4.runtime.Recognizer.EOF)1 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)1 ColonStatementLexer (org.jdbi.v3.core.internal.lexer.ColonStatementLexer)1 COMMENT (org.jdbi.v3.core.internal.lexer.DefineStatementLexer.COMMENT)1 DEFINE (org.jdbi.v3.core.internal.lexer.DefineStatementLexer.DEFINE)1 DOUBLE_QUOTED_TEXT (org.jdbi.v3.core.internal.lexer.DefineStatementLexer.DOUBLE_QUOTED_TEXT)1 ESCAPED_TEXT (org.jdbi.v3.core.internal.lexer.DefineStatementLexer.ESCAPED_TEXT)1 LITERAL (org.jdbi.v3.core.internal.lexer.DefineStatementLexer.LITERAL)1 QUOTED_TEXT (org.jdbi.v3.core.internal.lexer.DefineStatementLexer.QUOTED_TEXT)1