use of org.jdbi.v3.core.statement.internal.ErrorListener 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();
}
use of org.jdbi.v3.core.statement.internal.ErrorListener 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();
}
use of org.jdbi.v3.core.statement.internal.ErrorListener in project jdbi by jdbi.
the class SqlScriptParser method parse.
public String parse(CharStream charStream) {
StringBuilder sb = new StringBuilder();
SqlScriptLexer lexer = new SqlScriptLexer(charStream);
lexer.addErrorListener(new ErrorListener());
boolean endOfFile = false;
while (!endOfFile) {
Token t = lexer.nextToken();
switch(t.getType()) {
case Token.EOF:
endOfFile = true;
break;
case SqlScriptLexer.SEMICOLON:
semicolonHandler.handle(t, sb);
break;
case SqlScriptLexer.COMMENT:
case SqlScriptLexer.MULTI_LINE_COMMENT:
break;
case SqlScriptLexer.NEWLINES:
if (sb.length() > 0) {
sb.append(' ');
}
break;
case SqlScriptLexer.QUOTED_TEXT:
case SqlScriptLexer.LITERAL:
case SqlScriptLexer.OTHER:
sb.append(t.getText());
break;
default:
throw new IllegalArgumentException("Unrecognizable token " + t);
}
}
return sb.toString();
}
use of org.jdbi.v3.core.statement.internal.ErrorListener 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);
}
});
}
Aggregations