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));
});
}
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));
});
}
Aggregations