use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class AbstractSqlLikeQueryBuilder method getAliasName.
/**
* Get the alias name.
*
* @param joinPath The join path
* @return The alias
*/
public String getAliasName(JoinPath joinPath) {
return joinPath.getAlias().orElseGet(() -> {
String joinPathAlias = getPathOnlyAliasName(joinPath);
PersistentEntity owner = joinPath.getAssociationPath()[0].getOwner();
String ownerAlias = getAliasName(owner);
if (ownerAlias.endsWith("_") && joinPathAlias.startsWith("_")) {
return ownerAlias + joinPathAlias.substring(1);
} else {
return ownerAlias + joinPathAlias;
}
});
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class AbstractCriteriaMethodMatch method extractPredicates.
@Nullable
private Predicate extractPredicates(List<ParameterElement> queryParams, PersistentEntityRoot<?> root, SourcePersistentEntityCriteriaBuilder cb) {
if (CollectionUtils.isNotEmpty(queryParams)) {
PersistentEntity rootEntity = root.getPersistentEntity();
List<Predicate> predicates = new ArrayList<>(queryParams.size());
for (ParameterElement queryParam : queryParams) {
String paramName = queryParam.getName();
PersistentProperty prop = rootEntity.getPropertyByName(paramName);
ParameterExpression<Object> param = cb.parameter(queryParam);
if (prop == null) {
if (TypeRole.ID.equals(paramName) && (rootEntity.hasIdentity() || rootEntity.hasCompositeIdentity())) {
predicates.add(cb.equal(root.id(), param));
} else {
throw new MatchFailedException("Cannot query persistentEntity [" + rootEntity.getSimpleName() + "] on non-existent property: " + paramName);
}
} else if (prop == rootEntity.getIdentity()) {
predicates.add(cb.equal(root.id(), param));
} else if (prop == rootEntity.getVersion()) {
predicates.add(cb.equal(root.version(), param));
} else {
predicates.add(cb.equal(root.get(prop.getName()), param));
}
}
if (predicates.isEmpty()) {
return null;
}
return cb.and(predicates);
}
return null;
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class AbstractCriteriaMethodMatch method findProperty.
@Nullable
protected final <T> io.micronaut.data.model.jpa.criteria.PersistentPropertyPath<Object> findProperty(PersistentEntityRoot<T> root, String propertyName) {
propertyName = NameUtils.decapitalize(propertyName);
PersistentEntity entity = root.getPersistentEntity();
PersistentProperty prop = entity.getPropertyByName(propertyName);
PersistentPropertyPath pp;
if (prop == null) {
Optional<String> propertyPath = entity.getPath(propertyName);
if (propertyPath.isPresent()) {
String path = propertyPath.get();
pp = entity.getPropertyPath(path);
if (pp == null) {
return null;
}
} else {
return null;
}
} else {
pp = PersistentPropertyPath.of(Collections.emptyList(), prop, propertyName);
}
PersistentEntityFrom<?, ?> path = root;
for (Association association : pp.getAssociations()) {
path = path.join(association.getName());
}
Path<Object> exp;
if (pp.getProperty() instanceof Association && ((Association) pp.getProperty()).getKind() != Relation.Kind.EMBEDDED) {
exp = path.join(pp.getProperty().getName());
} else {
exp = path.get(pp.getProperty().getName());
}
return CriteriaUtils.requireProperty(exp);
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class SourcePersistentEntityCriteriaQueryImpl method getQueryResultTypeName.
@Override
public String getQueryResultTypeName() {
if (selection instanceof ISelection) {
String[] result = new String[1];
((SelectionVisitable) selection).accept(new SelectionVisitor() {
@Override
public void visit(Predicate predicate) {
}
@Override
public void visit(PersistentPropertyPath<?> persistentPropertyPath) {
result[0] = ((SourcePersistentPropertyPath) persistentPropertyPath).getProperty().getType().getName();
}
@Override
public void visit(AliasedSelection<?> aliasedSelection) {
((SelectionVisitable) aliasedSelection.getSelection()).accept(this);
}
@Override
public void visit(PersistentEntityRoot<?> entityRoot) {
result[0] = ((SourcePersistentEntity) entityRoot.getPersistentEntity()).getType().getName();
}
@Override
public void visit(AggregateExpression<?, ?> aggregateExpression) {
switch(aggregateExpression.getType()) {
case COUNT:
case COUNT_DISTINCT:
result[0] = Long.class.getName();
break;
case MAX:
case MIN:
result[0] = requireProperty(aggregateExpression.getExpression()).getProperty().getTypeName();
break;
case SUM:
case AVG:
ClassElement type = ((SourcePersistentProperty) requireProperty(aggregateExpression.getExpression()).getProperty()).getType();
if (aggregateExpression.getExpressionType() != null) {
result[0] = aggregateExpression.getExpressionType().getName();
}
if (TypeUtils.isNumber(type)) {
result[0] = Number.class.getName();
} else {
result[0] = type.getName();
}
break;
default:
return;
}
}
@Override
public void visit(CompoundSelection<?> compoundSelection) {
if (compoundSelection.getCompoundSelectionItems().size() == 1) {
// Multiple selection shouldn't result in one type
compoundSelection.getCompoundSelectionItems().forEach(s -> ((SelectionVisitable) s).accept(this));
}
}
@Override
public void visit(LiteralExpression<?> literalExpression) {
result[0] = literalExpression.getValue().getClass().getName();
}
@Override
public void visit(IdExpression<?, ?> idExpression) {
SourcePersistentEntity persistentEntity = (SourcePersistentEntity) idExpression.getRoot().getPersistentEntity();
if (persistentEntity.hasCompositeIdentity()) {
throw new IllegalStateException("IdClass is unknown!");
}
result[0] = persistentEntity.getIdentity().getType().getName();
}
});
return result[0];
}
return null;
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class R2dbcSchemaGenerator method createSchema.
/**
* Creates the schema.
*
* @param beanLocator The bean locator
*/
@PostConstruct
protected void createSchema(BeanLocator beanLocator) {
RuntimeEntityRegistry runtimeEntityRegistry = beanLocator.getBean(RuntimeEntityRegistry.class);
for (DataR2dbcConfiguration configuration : configurations) {
SchemaGenerate schemaGenerate = configuration.getSchemaGenerate();
if (schemaGenerate != null && schemaGenerate != SchemaGenerate.NONE) {
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(e -> runtimeEntityRegistry.getEntity(e.getBeanType())).toArray(PersistentEntity[]::new);
if (ArrayUtils.isNotEmpty(entities)) {
R2dbcOperations r2dbcOperations = configuration.getR2dbcOperations();
SqlQueryBuilder builder = new SqlQueryBuilder(configuration.getDialect());
Mono.from(r2dbcOperations.withConnection(connection -> {
List<String> createStatements = Arrays.stream(entities).flatMap(entity -> Arrays.stream(builder.buildCreateTableStatements(entity))).collect(Collectors.toList());
Flux<Void> createTablesFlow = Flux.fromIterable(createStatements).concatMap(sql -> {
if (DataSettings.QUERY_LOG.isDebugEnabled()) {
DataSettings.QUERY_LOG.debug("Creating Table: \n{}", sql);
}
return execute(connection, sql).onErrorResume((throwable -> {
if (LOG.isWarnEnabled()) {
LOG.warn("Unable to create table :{}", throwable.getMessage());
}
return Mono.empty();
}));
});
switch(schemaGenerate) {
case CREATE_DROP:
List<String> dropStatements = Arrays.stream(entities).flatMap(entity -> Arrays.stream(builder.buildDropTableStatements(entity))).collect(Collectors.toList());
return Flux.fromIterable(dropStatements).concatMap(sql -> {
if (DataSettings.QUERY_LOG.isDebugEnabled()) {
DataSettings.QUERY_LOG.debug("Dropping Table: \n{}", sql);
}
return execute(connection, sql).onErrorResume((throwable -> Mono.empty()));
}).thenMany(createTablesFlow).then();
case CREATE:
default:
return createTablesFlow.then();
}
})).block();
}
}
}
}
Aggregations