Search in sources :

Example 1 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry 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)

Example 2 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.

the class RegisterCollectorFactoryImpl method configureForType.

@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
    RegisterCollectorFactory registerCollectorFactory = (RegisterCollectorFactory) annotation;
    JdbiCollectors collectors = registry.get(JdbiCollectors.class);
    Class<? extends CollectorFactory> type = registerCollectorFactory.value();
    try {
        collectors.register(type.newInstance());
    } catch (InstantiationException | IllegalAccessException e) {
        throw new IllegalStateException("Unable to instantiate container factory", e);
    }
}
Also used : RegisterCollectorFactory(org.jdbi.v3.sqlobject.config.RegisterCollectorFactory) JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors)

Example 3 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.

the class RegisterColumnMapperImpl method configureForType.

@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
    RegisterColumnMapper registerColumnMapper = (RegisterColumnMapper) annotation;
    ColumnMappers mappers = registry.get(ColumnMappers.class);
    try {
        mappers.register(registerColumnMapper.value().newInstance());
    } catch (Exception e) {
        throw new IllegalStateException("unable to create a specified column mapper", e);
    }
}
Also used : ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) RegisterColumnMapper(org.jdbi.v3.sqlobject.config.RegisterColumnMapper)

Example 4 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.

the class RegisterColumnMappersImpl method configureForType.

@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
    Configurer delegate = new RegisterColumnMapperImpl();
    RegisterColumnMappers registerColumnMappers = (RegisterColumnMappers) annotation;
    Stream.of(registerColumnMappers.value()).forEach(anno -> delegate.configureForType(registry, anno, sqlObjectType));
}
Also used : RegisterColumnMappers(org.jdbi.v3.sqlobject.config.RegisterColumnMappers) Configurer(org.jdbi.v3.sqlobject.config.Configurer)

Example 5 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.

the class RegisterConstructorMapperImpl method configureForType.

@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
    RegisterConstructorMapper registerConstructorMapper = (RegisterConstructorMapper) annotation;
    RowMappers mappers = registry.get(RowMappers.class);
    Class<?> type = registerConstructorMapper.value();
    String prefix = registerConstructorMapper.prefix();
    if (prefix.isEmpty()) {
        mappers.register(ConstructorMapper.factory(type));
    } else {
        mappers.register(ConstructorMapper.factory(type, prefix));
    }
}
Also used : RegisterConstructorMapper(org.jdbi.v3.sqlobject.config.RegisterConstructorMapper) RowMappers(org.jdbi.v3.core.mapper.RowMappers)

Aggregations

Configurer (org.jdbi.v3.sqlobject.config.Configurer)10 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)8 RowMappers (org.jdbi.v3.core.mapper.RowMappers)5 SqlStatements (org.jdbi.v3.core.statement.SqlStatements)5 ColumnMappers (org.jdbi.v3.core.mapper.ColumnMappers)4 Type (java.lang.reflect.Type)3 Optional (java.util.Optional)3 ExtensionMethod (org.jdbi.v3.core.extension.ExtensionMethod)3 TemplateEngine (org.jdbi.v3.core.statement.TemplateEngine)3 Method (java.lang.reflect.Method)2 Types (java.sql.Types)2 Argument (org.jdbi.v3.core.argument.Argument)2 Arguments (org.jdbi.v3.core.argument.Arguments)2 JdbiCollectors (org.jdbi.v3.core.collector.JdbiCollectors)2 GenericTypes (org.jdbi.v3.core.generic.GenericTypes)2 ColumnMapperFactory (org.jdbi.v3.core.mapper.ColumnMapperFactory)2 MapEntryMappers (org.jdbi.v3.core.mapper.MapEntryMappers)2 SqlParser (org.jdbi.v3.core.statement.SqlParser)2 UseSqlParser (org.jdbi.v3.sqlobject.config.UseSqlParser)2 UseTemplateEngine (org.jdbi.v3.sqlobject.config.UseTemplateEngine)2