use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class SqlQueryBuilder method join.
private void join(StringBuilder sb, String joinType, QueryState queryState, PersistentEntity associatedEntity, PersistentEntity associationOwner, String leftTableAlias, String rightTableAlias, List<Association> leftPropertyAssociations, PersistentProperty leftProperty, List<Association> rightPropertyAssociations, PersistentProperty rightProperty) {
final boolean escape = shouldEscape(associationOwner);
List<String> onLeftColumns = new ArrayList<>();
List<String> onRightColumns = new ArrayList<>();
Association association = null;
if (leftProperty instanceof Association) {
association = (Association) leftProperty;
} else if (rightProperty instanceof Association) {
association = (Association) rightProperty;
}
if (association != null) {
Optional<Association> inverse = association.getInverseSide().map(Function.identity());
Association owner = inverse.orElse(association);
boolean isOwner = leftProperty == owner;
AnnotationValue<Annotation> joinColumnsHolder = owner.getAnnotationMetadata().getAnnotation(ANN_JOIN_COLUMNS);
if (joinColumnsHolder != null) {
onLeftColumns.addAll(joinColumnsHolder.getAnnotations("value").stream().map(ann -> ann.stringValue(isOwner ? "name" : "referencedColumnName").orElse(null)).filter(Objects::nonNull).collect(Collectors.toList()));
onRightColumns.addAll(joinColumnsHolder.getAnnotations("value").stream().map(ann -> ann.stringValue(isOwner ? "referencedColumnName" : "name").orElse(null)).filter(Objects::nonNull).collect(Collectors.toList()));
}
}
if (onLeftColumns.isEmpty()) {
traversePersistentProperties(leftProperty, (associations, p) -> {
String column = leftProperty.getOwner().getNamingStrategy().mappedName(merge(leftPropertyAssociations, associations), p);
onLeftColumns.add(column);
});
if (onLeftColumns.isEmpty()) {
throw new MappingException("Cannot join on entity [" + leftProperty.getOwner().getName() + "] that has no declared ID");
}
}
if (onRightColumns.isEmpty()) {
traversePersistentProperties(rightProperty, (associations, p) -> {
String column = rightProperty.getOwner().getNamingStrategy().mappedName(merge(rightPropertyAssociations, associations), p);
onRightColumns.add(column);
});
}
join(sb, queryState.getQueryModel(), joinType, getTableName(associatedEntity), rightTableAlias, leftTableAlias, escape ? onLeftColumns.stream().map(this::quote).collect(Collectors.toList()) : onLeftColumns, escape ? onRightColumns.stream().map(this::quote).collect(Collectors.toList()) : onRightColumns);
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class SqlQueryBuilder method flatMapEmbedded.
private Stream<? extends PersistentProperty> flatMapEmbedded(PersistentProperty pp) {
if (pp instanceof Embedded) {
Embedded embedded = (Embedded) pp;
PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
return embeddedEntity.getPersistentProperties().stream().flatMap(this::flatMapEmbedded);
}
return Stream.of(pp);
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class AbstractSqlLikeQueryBuilder method buildSelectClause.
private void buildSelectClause(QueryModel query, QueryState queryState, StringBuilder queryString) {
String logicalName = queryState.getRootAlias();
PersistentEntity entity = queryState.getEntity();
buildSelect(queryState, queryString, query.getProjections(), logicalName, entity);
String tableName = getTableName(entity);
queryString.append(FROM_CLAUSE).append(tableName).append(getTableAsKeyword()).append(logicalName);
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class AbstractSqlLikeQueryBuilder method buildDelete.
@Override
public QueryResult buildDelete(@NonNull AnnotationMetadata annotationMetadata, @NonNull QueryModel query) {
PersistentEntity entity = query.getPersistentEntity();
QueryState queryState = newQueryState(query, false, isAliasForBatch());
StringBuilder queryString = queryState.getQuery();
String tableAlias = queryState.getRootAlias();
StringBuilder buffer = appendDeleteClause(queryString);
String tableName = getTableName(entity);
buffer.append(tableName).append(SPACE);
if (tableAlias != null) {
buffer.append(getTableAsKeyword()).append(tableAlias);
}
buildWhereClause(annotationMetadata, query.getCriteria(), queryState);
return QueryResult.of(queryState.getFinalQuery(), queryState.getQueryParts(), queryState.getParameterBindings(), queryState.getAdditionalRequiredParameters());
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class SqlQueryBuilder method selectAllColumns.
@Override
protected void selectAllColumns(QueryState queryState, StringBuilder queryBuffer) {
PersistentEntity entity = queryState.getEntity();
selectAllColumns(entity, queryState.getRootAlias(), queryBuffer);
QueryModel queryModel = queryState.getQueryModel();
Collection<JoinPath> allPaths = queryModel.getJoinPaths();
if (CollectionUtils.isNotEmpty(allPaths)) {
Collection<JoinPath> joinPaths = allPaths.stream().filter(jp -> {
Join.Type jt = jp.getJoinType();
return jt.name().contains("FETCH");
}).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(joinPaths)) {
for (JoinPath joinPath : joinPaths) {
Association association = joinPath.getAssociation();
if (association instanceof Embedded) {
// joins on embedded don't make sense
continue;
}
PersistentEntity associatedEntity = association.getAssociatedEntity();
NamingStrategy namingStrategy = associatedEntity.getNamingStrategy();
String aliasName = getAliasName(joinPath);
String joinPathAlias = getPathOnlyAliasName(joinPath);
queryBuffer.append(COMMA);
boolean includeIdentity = false;
if (association.isForeignKey()) {
// in the case of a foreign key association the ID is not in the table
// so we need to retrieve it
includeIdentity = true;
}
traversePersistentProperties(associatedEntity, includeIdentity, true, (propertyAssociations, prop) -> {
String columnName;
if (computePropertyPaths()) {
columnName = namingStrategy.mappedName(propertyAssociations, prop);
} else {
columnName = asPath(propertyAssociations, prop);
}
queryBuffer.append(aliasName).append(DOT).append(queryState.shouldEscape() ? quote(columnName) : columnName).append(AS_CLAUSE).append(joinPathAlias).append(columnName).append(COMMA);
});
queryBuffer.setLength(queryBuffer.length() - 1);
}
}
}
}
Aggregations