use of io.micronaut.data.model.query.builder.sql.SqlQueryBuilder in project micronaut-data by micronaut-projects.
the class DefaultJdbcRepositoryOperations method updateAll.
@NonNull
@Override
public <T> Iterable<T> updateAll(@NonNull UpdateBatchOperation<T> operation) {
return executeWrite(connection -> {
final AnnotationMetadata annotationMetadata = operation.getAnnotationMetadata();
final Class<?> repositoryType = operation.getRepositoryType();
SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(repositoryType, DEFAULT_SQL_BUILDER);
final RuntimePersistentEntity<T> persistentEntity = getEntity(operation.getRootEntity());
DBOperation dbOperation = new StoredQuerySqlOperation(queryBuilder, operation.getStoredQuery());
JdbcOperationContext ctx = new JdbcOperationContext(annotationMetadata, repositoryType, queryBuilder.dialect(), connection);
if (!isSupportsBatchUpdate(persistentEntity, queryBuilder.dialect())) {
return operation.split().stream().map(updateOp -> {
JdbcEntityOperations<T> op = new JdbcEntityOperations<>(ctx, persistentEntity, updateOp.getEntity(), dbOperation);
op.update();
return op.getEntity();
}).collect(Collectors.toList());
}
JdbcEntitiesOperations<T> op = new JdbcEntitiesOperations<>(ctx, persistentEntity, operation, dbOperation);
op.update();
return op.getEntities();
});
}
use of io.micronaut.data.model.query.builder.sql.SqlQueryBuilder in project micronaut-data by micronaut-projects.
the class DefaultJdbcRepositoryOperations method update.
@NonNull
@Override
public <T> T update(@NonNull UpdateOperation<T> operation) {
final AnnotationMetadata annotationMetadata = operation.getAnnotationMetadata();
final Class<?> repositoryType = operation.getRepositoryType();
SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(repositoryType, DEFAULT_SQL_BUILDER);
DBOperation dbOperation = new StoredQuerySqlOperation(queryBuilder, operation.getStoredQuery());
return executeWrite(connection -> {
JdbcOperationContext ctx = new JdbcOperationContext(annotationMetadata, repositoryType, queryBuilder.dialect(), connection);
JdbcEntityOperations<T> op = new JdbcEntityOperations<>(ctx, getEntity(operation.getRootEntity()), operation.getEntity(), dbOperation);
op.update();
return op.getEntity();
});
}
use of io.micronaut.data.model.query.builder.sql.SqlQueryBuilder in project micronaut-data by micronaut-projects.
the class AbstractSqlRepositoryOperations method prepareStatement.
/**
* Prepare a statement for execution.
*
* @param connection The connection
* @param statementFunction The statement function
* @param preparedQuery The prepared query
* @param isUpdate Is this an update
* @param isSingleResult Is it a single result
* @param <T> The query declaring type
* @param <R> The query result type
* @return The prepared statement
*/
protected <T, R> PS prepareStatement(Cnt connection, StatementSupplier<PS> statementFunction, @NonNull PreparedQuery<T, R> preparedQuery, boolean isUpdate, boolean isSingleResult) throws Exc {
SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(preparedQuery.getRepositoryType(), DEFAULT_SQL_BUILDER);
RuntimePersistentEntity<T> persistentEntity = getEntity(preparedQuery.getRootEntity());
PreparedQueryDBOperation pqSqlOperation = new PreparedQueryDBOperation(preparedQuery, queryBuilder);
pqSqlOperation.checkForParameterToBeExpanded(persistentEntity, null);
if (!isUpdate) {
pqSqlOperation.attachPageable(preparedQuery.getPageable(), isSingleResult, persistentEntity, queryBuilder);
}
String query = pqSqlOperation.getQuery();
if (QUERY_LOG.isDebugEnabled()) {
QUERY_LOG.debug("Executing Query: {}", query);
}
final PS ps;
try {
ps = statementFunction.create(query);
} catch (Exception e) {
throw new DataAccessException("Unable to prepare query [" + query + "]: " + e.getMessage(), e);
}
pqSqlOperation.setParameters(this, connection, ps, persistentEntity, null, null);
return ps;
}
use of io.micronaut.data.model.query.builder.sql.SqlQueryBuilder in project micronaut-data by micronaut-projects.
the class AbstractSqlRepositoryOperations method resolveEntityUpdate.
/**
* Resolves a stored update for the given entity.
*
* @param annotationMetadata The repository annotation metadata
* @param repositoryType The repository type
* @param rootEntity The root entity
* @param persistentEntity The persistent entity
* @return The insert
*/
@NonNull
protected DBOperation resolveEntityUpdate(AnnotationMetadata annotationMetadata, Class<?> repositoryType, @NonNull Class<?> rootEntity, @NonNull RuntimePersistentEntity<?> persistentEntity) {
final QueryKey key = new QueryKey(repositoryType, rootEntity);
// noinspection unchecked
return entityUpdates.computeIfAbsent(key, (queryKey) -> {
final SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(repositoryType, DEFAULT_SQL_BUILDER);
final String idName;
final PersistentProperty identity = persistentEntity.getIdentity();
if (identity != null) {
idName = identity.getName();
} else {
idName = TypeRole.ID;
}
final QueryModel queryModel = QueryModel.from(persistentEntity).idEq(new QueryParameter(idName));
List<String> updateProperties = persistentEntity.getPersistentProperties().stream().filter(p -> !((p instanceof Association) && ((Association) p).isForeignKey()) && p.getAnnotationMetadata().booleanValue(AutoPopulated.class, "updateable").orElse(true)).map(PersistentProperty::getName).collect(Collectors.toList());
final QueryResult queryResult = queryBuilder.buildUpdate(annotationMetadata, queryModel, updateProperties);
return new QueryResultSqlOperation(queryBuilder, queryResult);
});
}
use of io.micronaut.data.model.query.builder.sql.SqlQueryBuilder in project micronaut-data by micronaut-projects.
the class DefaultJdbcRepositoryOperations method persist.
@NonNull
@Override
public <T> T persist(@NonNull InsertOperation<T> operation) {
final AnnotationMetadata annotationMetadata = operation.getAnnotationMetadata();
final Class<?> repositoryType = operation.getRepositoryType();
SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(repositoryType, DEFAULT_SQL_BUILDER);
return executeWrite(connection -> {
JdbcOperationContext ctx = new JdbcOperationContext(annotationMetadata, repositoryType, queryBuilder.dialect(), connection);
JdbcEntityOperations<T> op = new JdbcEntityOperations<>(ctx, new StoredQuerySqlOperation(queryBuilder, operation.getStoredQuery()), getEntity(operation.getRootEntity()), operation.getEntity(), true);
op.persist();
return op;
}).getEntity();
}
Aggregations