Search in sources :

Example 1 with MyBatisDataSourceRuntimeConfig

use of io.quarkiverse.mybatis.runtime.config.MyBatisDataSourceRuntimeConfig in project quarkus-mybatis by quarkiverse.

the class MyBatisProcessor method generateSqlSessionFactory.

@Record(ExecutionTime.STATIC_INIT)
@BuildStep
void generateSqlSessionFactory(MyBatisRuntimeConfig myBatisRuntimeConfig, ConfigurationFactoryBuildItem configurationFactoryBuildItem, SqlSessionFactoryBuilderBuildItem sqlSessionFactoryBuilderBuildItem, List<MyBatisMapperBuildItem> myBatisMapperBuildItems, List<MyBatisMappedTypeBuildItem> myBatisMappedTypeBuildItems, List<MyBatisMappedJdbcTypeBuildItem> myBatisMappedJdbcTypeBuildItems, List<JdbcDataSourceBuildItem> jdbcDataSourcesBuildItem, BuildProducer<SqlSessionFactoryBuildItem> sqlSessionFactory, MyBatisRecorder recorder) {
    List<String> mappedTypes = myBatisMappedTypeBuildItems.stream().map(m -> m.getMappedTypeName().toString()).collect(Collectors.toList());
    List<String> mappedJdbcTypes = myBatisMappedJdbcTypeBuildItems.stream().map(m -> m.getMappedJdbcTypeName().toString()).collect(Collectors.toList());
    List<Pair<String, Boolean>> dataSources = new ArrayList<>();
    if (myBatisRuntimeConfig.dataSource.isPresent()) {
        String dataSourceName = myBatisRuntimeConfig.dataSource.get();
        Optional<JdbcDataSourceBuildItem> jdbcDataSourceBuildItem = jdbcDataSourcesBuildItem.stream().filter(i -> i.getName().equals(dataSourceName)).findFirst();
        if (!jdbcDataSourceBuildItem.isPresent()) {
            throw new ConfigurationError("Can not find datasource " + dataSourceName);
        }
        dataSources.add(Pair.of(dataSourceName, true));
    } else {
        dataSources = jdbcDataSourcesBuildItem.stream().map(dataSource -> Pair.of(dataSource.getName(), dataSource.isDefault())).collect(Collectors.toList());
        if (dataSources.isEmpty()) {
            throw new ConfigurationError("No datasource found");
        }
    }
    dataSources.forEach(dataSource -> {
        MyBatisDataSourceRuntimeConfig dataSourceConfig = myBatisRuntimeConfig.dataSources.get(dataSource.getKey());
        List<String> mappers = myBatisMapperBuildItems.stream().filter(m -> m.getDataSourceName().equals(dataSource.getKey())).map(m -> m.getMapperName().toString()).collect(Collectors.toList());
        sqlSessionFactory.produce(new SqlSessionFactoryBuildItem(recorder.createSqlSessionFactory(configurationFactoryBuildItem.getFactory(), sqlSessionFactoryBuilderBuildItem.getBuilder(), myBatisRuntimeConfig, dataSourceConfig, dataSource.getKey(), mappers, mappedTypes, mappedJdbcTypes), dataSource.getKey(), dataSource.getValue(), false));
    });
}
Also used : Overridable(io.quarkus.deployment.annotations.Overridable) SelectProvider(org.apache.ibatis.annotations.SelectProvider) CombinedIndexBuildItem(io.quarkus.deployment.builditem.CombinedIndexBuildItem) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) Pair(org.apache.commons.lang3.tuple.Pair) FeatureBuildItem(io.quarkus.deployment.builditem.FeatureBuildItem) MappedJdbcTypes(org.apache.ibatis.type.MappedJdbcTypes) Map(java.util.Map) AnnotationTarget(org.jboss.jandex.AnnotationTarget) NativeImageProxyDefinitionBuildItem(io.quarkus.deployment.builditem.nativeimage.NativeImageProxyDefinitionBuildItem) Result(org.apache.ibatis.annotations.Result) ExecutionTime(io.quarkus.deployment.annotations.ExecutionTime) MyBatisRecorder(io.quarkiverse.mybatis.runtime.MyBatisRecorder) NativeImageResourceBuildItem(io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem) MappedTypes(org.apache.ibatis.type.MappedTypes) Collectors(java.util.stream.Collectors) List(java.util.List) ProxyFactory(org.apache.ibatis.javassist.util.proxy.ProxyFactory) AnnotationInstance(org.jboss.jandex.AnnotationInstance) Mapper(org.apache.ibatis.annotations.Mapper) Optional(java.util.Optional) ConfigurationError(io.quarkus.deployment.configuration.ConfigurationError) DeleteProvider(org.apache.ibatis.annotations.DeleteProvider) MyBatisConfigurationFactory(io.quarkiverse.mybatis.runtime.MyBatisConfigurationFactory) MyBatisDataSourceRuntimeConfig(io.quarkiverse.mybatis.runtime.config.MyBatisDataSourceRuntimeConfig) SyntheticBeanBuildItem(io.quarkus.arc.deployment.SyntheticBeanBuildItem) ReflectiveClassBuildItem(io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem) Record(io.quarkus.deployment.annotations.Record) MapperDataSource(io.quarkiverse.mybatis.runtime.meta.MapperDataSource) Logger(org.jboss.logging.Logger) DotName(org.jboss.jandex.DotName) XMLLanguageDriver(org.apache.ibatis.scripting.xmltags.XMLLanguageDriver) UpdateProvider(org.apache.ibatis.annotations.UpdateProvider) Singleton(javax.inject.Singleton) Function(java.util.function.Function) ArrayList(java.util.ArrayList) MyBatisRuntimeConfig(io.quarkiverse.mybatis.runtime.config.MyBatisRuntimeConfig) PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) BuildStep(io.quarkus.deployment.annotations.BuildStep) Results(org.apache.ibatis.annotations.Results) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) EnumTypeHandler(org.apache.ibatis.type.EnumTypeHandler) Named(javax.inject.Named) RawLanguageDriver(org.apache.ibatis.scripting.defaults.RawLanguageDriver) ResultMap(org.apache.ibatis.annotations.ResultMap) ResultType(org.apache.ibatis.annotations.ResultType) RuntimeInitializedClassBuildItem(io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem) MyBatisXMLConfigDelegateBuilder(io.quarkiverse.mybatis.runtime.MyBatisXMLConfigDelegateBuilder) LruCache(org.apache.ibatis.cache.decorators.LruCache) JdbcDataSourceBuildItem(io.quarkus.agroal.spi.JdbcDataSourceBuildItem) InsertProvider(org.apache.ibatis.annotations.InsertProvider) Log4jImpl(org.apache.ibatis.logging.log4j.Log4jImpl) ConfigurationError(io.quarkus.deployment.configuration.ConfigurationError) MyBatisDataSourceRuntimeConfig(io.quarkiverse.mybatis.runtime.config.MyBatisDataSourceRuntimeConfig) ArrayList(java.util.ArrayList) JdbcDataSourceBuildItem(io.quarkus.agroal.spi.JdbcDataSourceBuildItem) Pair(org.apache.commons.lang3.tuple.Pair) Record(io.quarkus.deployment.annotations.Record) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with MyBatisDataSourceRuntimeConfig

use of io.quarkiverse.mybatis.runtime.config.MyBatisDataSourceRuntimeConfig in project quarkus-mybatis by quarkiverse.

the class MyBatisProcessor method runInitialSql.

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
void runInitialSql(List<SqlSessionFactoryBuildItem> sqlSessionFactoryBuildItems, MyBatisRuntimeConfig myBatisRuntimeConfig, MyBatisRecorder recorder) {
    sqlSessionFactoryBuildItems.forEach(sqlSessionFactoryBuildItem -> {
        MyBatisDataSourceRuntimeConfig dataSourceConfig = myBatisRuntimeConfig.dataSources.get(sqlSessionFactoryBuildItem.getDataSourceName());
        Optional<String> optionalInitialSql;
        if (sqlSessionFactoryBuildItem.isDefaultDataSource() || sqlSessionFactoryBuildItems.size() == 1) {
            optionalInitialSql = dataSourceConfig != null && dataSourceConfig.initialSql.isPresent() ? dataSourceConfig.initialSql : myBatisRuntimeConfig.initialSql;
        } else {
            optionalInitialSql = dataSourceConfig != null ? dataSourceConfig.initialSql : Optional.empty();
        }
        optionalInitialSql.ifPresent(initialSql -> recorder.runInitialSql(sqlSessionFactoryBuildItem.getSqlSessionFactory(), initialSql));
    });
}
Also used : MyBatisDataSourceRuntimeConfig(io.quarkiverse.mybatis.runtime.config.MyBatisDataSourceRuntimeConfig) Record(io.quarkus.deployment.annotations.Record) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

MyBatisDataSourceRuntimeConfig (io.quarkiverse.mybatis.runtime.config.MyBatisDataSourceRuntimeConfig)2 BuildStep (io.quarkus.deployment.annotations.BuildStep)2 Record (io.quarkus.deployment.annotations.Record)2 MyBatisConfigurationFactory (io.quarkiverse.mybatis.runtime.MyBatisConfigurationFactory)1 MyBatisRecorder (io.quarkiverse.mybatis.runtime.MyBatisRecorder)1 MyBatisXMLConfigDelegateBuilder (io.quarkiverse.mybatis.runtime.MyBatisXMLConfigDelegateBuilder)1 MyBatisRuntimeConfig (io.quarkiverse.mybatis.runtime.config.MyBatisRuntimeConfig)1 MapperDataSource (io.quarkiverse.mybatis.runtime.meta.MapperDataSource)1 JdbcDataSourceBuildItem (io.quarkus.agroal.spi.JdbcDataSourceBuildItem)1 SyntheticBeanBuildItem (io.quarkus.arc.deployment.SyntheticBeanBuildItem)1 BuildProducer (io.quarkus.deployment.annotations.BuildProducer)1 ExecutionTime (io.quarkus.deployment.annotations.ExecutionTime)1 Overridable (io.quarkus.deployment.annotations.Overridable)1 CombinedIndexBuildItem (io.quarkus.deployment.builditem.CombinedIndexBuildItem)1 FeatureBuildItem (io.quarkus.deployment.builditem.FeatureBuildItem)1 NativeImageProxyDefinitionBuildItem (io.quarkus.deployment.builditem.nativeimage.NativeImageProxyDefinitionBuildItem)1 NativeImageResourceBuildItem (io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem)1 ReflectiveClassBuildItem (io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem)1 RuntimeInitializedClassBuildItem (io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem)1 ConfigurationError (io.quarkus.deployment.configuration.ConfigurationError)1