Search in sources :

Example 1 with SqlLocator

use of org.jdbi.v3.sqlobject.locator.SqlLocator in project jdbi by jdbi.

the class UseStringTemplateSqlLocatorImpl method configureForType.

@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
    SqlLocator locator = (type, method, config) -> {
        String templateName = SqlAnnotations.getAnnotationValue(method, sql -> sql).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);
}
Also used : SqlLocator(org.jdbi.v3.sqlobject.locator.SqlLocator) SqlObjects(org.jdbi.v3.sqlobject.SqlObjects) StringTemplateSqlLocator.findStringTemplateGroup(org.jdbi.v3.stringtemplate4.StringTemplateSqlLocator.findStringTemplateGroup) SqlLocator(org.jdbi.v3.sqlobject.locator.SqlLocator) ST(org.stringtemplate.v4.ST) SqlStatements(org.jdbi.v3.core.statement.SqlStatements) STGroup(org.stringtemplate.v4.STGroup) SqlAnnotations(org.jdbi.v3.sqlobject.internal.SqlAnnotations) Annotation(java.lang.annotation.Annotation) TemplateEngine(org.jdbi.v3.core.statement.TemplateEngine) Configurer(org.jdbi.v3.sqlobject.config.Configurer) Method(java.lang.reflect.Method) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) TemplateEngine(org.jdbi.v3.core.statement.TemplateEngine) ST(org.stringtemplate.v4.ST) SqlStatements(org.jdbi.v3.core.statement.SqlStatements) STGroup(org.stringtemplate.v4.STGroup) SqlObjects(org.jdbi.v3.sqlobject.SqlObjects)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)1 SqlStatements (org.jdbi.v3.core.statement.SqlStatements)1 TemplateEngine (org.jdbi.v3.core.statement.TemplateEngine)1 SqlObjects (org.jdbi.v3.sqlobject.SqlObjects)1 Configurer (org.jdbi.v3.sqlobject.config.Configurer)1 SqlAnnotations (org.jdbi.v3.sqlobject.internal.SqlAnnotations)1 SqlLocator (org.jdbi.v3.sqlobject.locator.SqlLocator)1 StringTemplateSqlLocator.findStringTemplateGroup (org.jdbi.v3.stringtemplate4.StringTemplateSqlLocator.findStringTemplateGroup)1 ST (org.stringtemplate.v4.ST)1 STGroup (org.stringtemplate.v4.STGroup)1