Search in sources :

Example 1 with DefineStatementLexer

use of org.jdbi.v3.core.internal.lexer.DefineStatementLexer 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)

Aggregations

ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 Token (org.antlr.runtime.Token)1 DefineStatementLexer (org.jdbi.v3.core.internal.lexer.DefineStatementLexer)1