use of io.micronaut.data.model.query.builder.sql.Dialect in project micronaut-data by micronaut-projects.
the class SchemaGenerator method createSchema.
/**
* Initialize the schema for the configuration.
* @param beanLocator The bean locator
*/
@PostConstruct
public void createSchema(BeanLocator beanLocator) {
RuntimeEntityRegistry runtimeEntityRegistry = beanLocator.getBean(RuntimeEntityRegistry.class);
for (DataJdbcConfiguration configuration : configurations) {
Dialect dialect = configuration.getDialect();
SchemaGenerate schemaGenerate = configuration.getSchemaGenerate();
if (schemaGenerate != null && schemaGenerate != SchemaGenerate.NONE) {
String name = configuration.getName();
List<String> packages = configuration.getPackages();
Collection<BeanIntrospection<Object>> introspections;
if (CollectionUtils.isNotEmpty(packages)) {
introspections = BeanIntrospector.SHARED.findIntrospections(MappedEntity.class, packages.toArray(new String[0]));
} else {
introspections = BeanIntrospector.SHARED.findIntrospections(MappedEntity.class);
}
PersistentEntity[] entities = introspections.stream().filter(i -> !i.getBeanType().getName().contains("$")).filter(i -> !java.lang.reflect.Modifier.isAbstract(i.getBeanType().getModifiers())).map(beanIntrospection -> runtimeEntityRegistry.getEntity(beanIntrospection.getBeanType())).toArray(PersistentEntity[]::new);
if (ArrayUtils.isNotEmpty(entities)) {
DataSource dataSource = DelegatingDataSource.unwrapDataSource(beanLocator.getBean(DataSource.class, Qualifiers.byName(name)));
try {
try (Connection connection = dataSource.getConnection()) {
SqlQueryBuilder builder = new SqlQueryBuilder(dialect);
if (dialect.allowBatch() && configuration.isBatchGenerate()) {
switch(schemaGenerate) {
case CREATE_DROP:
try {
String sql = builder.buildBatchDropTableStatement(entities);
if (DataSettings.QUERY_LOG.isDebugEnabled()) {
DataSettings.QUERY_LOG.debug("Dropping Tables: \n{}", sql);
}
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.executeUpdate();
}
} catch (SQLException e) {
if (DataSettings.QUERY_LOG.isTraceEnabled()) {
DataSettings.QUERY_LOG.trace("Drop Unsuccessful: " + e.getMessage());
}
}
case CREATE:
String sql = builder.buildBatchCreateTableStatement(entities);
if (DataSettings.QUERY_LOG.isDebugEnabled()) {
DataSettings.QUERY_LOG.debug("Creating Tables: \n{}", sql);
}
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.executeUpdate();
}
break;
default:
}
} else {
switch(schemaGenerate) {
case CREATE_DROP:
for (PersistentEntity entity : entities) {
try {
String[] statements = builder.buildDropTableStatements(entity);
for (String sql : statements) {
if (DataSettings.QUERY_LOG.isDebugEnabled()) {
DataSettings.QUERY_LOG.debug("Dropping Table: \n{}", sql);
}
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.executeUpdate();
}
}
} catch (SQLException e) {
if (DataSettings.QUERY_LOG.isTraceEnabled()) {
DataSettings.QUERY_LOG.trace("Drop Unsuccessful: " + e.getMessage());
}
}
}
case CREATE:
for (PersistentEntity entity : entities) {
String[] sql = builder.buildCreateTableStatements(entity);
for (String stmt : sql) {
if (DataSettings.QUERY_LOG.isDebugEnabled()) {
DataSettings.QUERY_LOG.debug("Executing CREATE statement: \n{}", stmt);
}
try {
try (PreparedStatement ps = connection.prepareStatement(stmt)) {
ps.executeUpdate();
}
} catch (SQLException e) {
if (DataSettings.QUERY_LOG.isWarnEnabled()) {
DataSettings.QUERY_LOG.warn("CREATE Statement Unsuccessful: " + e.getMessage());
}
}
}
}
break;
default:
}
}
} catch (SQLException e) {
throw new DataAccessException("Unable to create database schema: " + e.getMessage(), e);
}
} catch (NoSuchBeanException e) {
throw new ConfigurationException("No DataSource configured for setting [" + DataJdbcConfiguration.PREFIX + name + "]. Ensure the DataSource is configured correctly and try again.", e);
}
}
}
}
}
use of io.micronaut.data.model.query.builder.sql.Dialect in project micronaut-data by micronaut-projects.
the class DefaultJdbcRepositoryOperations method deleteAll.
@Override
public <T> Optional<Number> deleteAll(@NonNull DeleteBatchOperation<T> operation) {
return Optional.ofNullable(executeWrite(connection -> {
SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(operation.getRepositoryType(), DEFAULT_SQL_BUILDER);
JdbcOperationContext ctx = new JdbcOperationContext(operation.getAnnotationMetadata(), operation.getRepositoryType(), queryBuilder.dialect(), connection);
Dialect dialect = queryBuilder.dialect();
RuntimePersistentEntity<T> persistentEntity = getEntity(operation.getRootEntity());
if (isSupportsBatchDelete(persistentEntity, dialect)) {
DBOperation dbOperation = new StoredQuerySqlOperation(queryBuilder, operation.getStoredQuery());
JdbcEntitiesOperations<T> op = new JdbcEntitiesOperations<>(ctx, getEntity(operation.getRootEntity()), operation, dbOperation);
op.delete();
return op.rowsUpdated;
}
return sum(operation.split().stream().map(deleteOp -> {
DBOperation dbOperation = new StoredQuerySqlOperation(queryBuilder, operation.getStoredQuery());
JdbcEntityOperations<T> op = new JdbcEntityOperations<>(ctx, getEntity(deleteOp.getRootEntity()), deleteOp.getEntity(), dbOperation);
op.delete();
return op.rowsUpdated;
}));
}));
}
Aggregations