@Override public String render(String sql, StatementContext ctx) { try { Template template = new Template(null, sql, null); StringWriter writer = new StringWriter(); template.process(ctx.getAttributes(), writer); return writer.toString(); } catch (IOException | TemplateException e) { throw new IllegalStateException("Failed to process template: " + sql, e); } } }
@Override public String render(String template, StatementContext ctx) { MessageFormat msgFormat = new MessageFormat(template); validateKeys(ctx.getAttributes().keySet(), msgFormat.getFormats().length); Object[] args = ctx.getAttributes() .entrySet() .stream() .map(x -> new AbstractMap.SimpleImmutableEntry<>(Integer.valueOf(x.getKey()), x.getValue())) .sorted(Comparator.comparingInt(AbstractMap.SimpleImmutableEntry::getKey)) .map(AbstractMap.SimpleImmutableEntry::getValue) .toArray(Object[]::new); return msgFormat.format(args); }
@Override public String render(String template, StatementContext ctx) { StringSubstitutor substitutor = new StringSubstitutor(ctx.getAttributes()); customizer.accept(substitutor); return substitutor.replace(template); }
@Override public void beforeTemplating(PreparedStatement stmt, StatementContext ctx) throws SQLException { final Set<String> alreadyDefined = ctx.getAttributes().keySet(); final Binding binding = ctx.getBinding(); binding.getNames().stream() .filter(name -> !alreadyDefined.contains(name)) .forEach(name -> binding.findForName(name, ctx).ifPresent( a -> ctx.define(name, a instanceof NullArgument ? false : true))); } }
@Override public String render(String sql, StatementContext ctx) { ST template = new ST(new STGroup(), sql); ctx.getAttributes().forEach(template::add); return template.render(); } }
@Before public void before() { when(ctx.getAttributes()).thenReturn(attributes); }
@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { SqlLocator locator = (type, method, config) -> { String templateName = SqlAnnotations.getAnnotationValue(method).orElseGet(method::getName); findTemplate(sqlObjectType, templateName); return templateName; }; TemplateEngine templateEngine = (templateName, ctx) -> { Template template = findTemplate(sqlObjectType, templateName); StringWriter writer = new StringWriter(); try { template.process(ctx.getAttributes(), writer); return writer.toString(); } catch (TemplateException | IOException e) { throw new IllegalStateException("Failed to render template " + templateName, e); } }; registry.get(SqlObjects.class).setSqlLocator(locator); registry.get(SqlStatements.class).setTemplateEngine(templateEngine); }
@Before public void setUp() { templateEngine = new MessageFormatTemplateEngine(); attributes = new HashMap<>(); ctx = mock(StatementContext.class); when(ctx.getAttributes()).thenReturn(attributes); }
@Override public void logBeforeExecution(StatementContext context) { attributes.add(context.getAttributes()); context.getBinding().findForPosition(0).ifPresent(a -> bindings.add(a.toString())); }
@Override public void logException(StatementContext context, SQLException ex) { attributes.add(context.getAttributes()); context.getBinding().findForPosition(0).ifPresent(a -> bindings.add(a.toString())); }
@Override public void logAfterExecution(StatementContext context) { attributes.add(context.getAttributes()); context.getBinding().findForPosition(0).ifPresent(a -> bindings.add(a.toString())); }
@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { SqlLocator locator = (type, method, config) -> { String templateName = SqlAnnotations.getAnnotationValue(method).orElseGet(method::getName); STGroup group = findStringTemplateGroup(type); if (!group.isDefined(templateName)) { throw new IllegalStateException("No StringTemplate group " + templateName + " for class " + sqlObjectType); } return templateName; }; TemplateEngine templateEngine = (templateName, ctx) -> { STGroup group = findStringTemplateGroup(sqlObjectType); ST template = group.getInstanceOf(templateName); ctx.getAttributes().forEach(template::add); return template.render(); }; registry.get(SqlObjects.class).setSqlLocator(locator); registry.get(SqlStatements.class).setTemplateEngine(templateEngine); }