Skip to content

Commit

Permalink
Merge branch 'atomfrede-18269-workaround-removal-of-criteria'
Browse files Browse the repository at this point in the history
  • Loading branch information
deepu105 committed Apr 6, 2022
2 parents 59757dc + dc3eba3 commit c68607e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.r2dbc.repository.support.SimpleR2dbcRepository;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.Comparison;
import org.springframework.data.relational.core.sql.Condition;
import org.springframework.data.relational.core.sql.Conditions;
import org.springframework.data.relational.core.sql.Expression;
import org.springframework.data.relational.core.sql.Select;
import org.springframework.data.relational.core.sql.SelectBuilder.SelectFromAndJoin<% if (reactiveEagerRelations.length > 0) { %>Condition<% } %>;
Expand Down Expand Up @@ -121,15 +124,10 @@ _%>
@Override
public Flux<<%= persistClass %>> findAllBy(Pageable pageable) {
return findAllBy(pageable, null);
return createQuery(pageable, null).all();
}
@Override
public Flux<<%= persistClass %>> findAllBy(Pageable pageable, Criteria criteria) {
return createQuery(pageable, criteria).all();
}
RowsFetchSpec<<%= persistClass %>> createQuery(Pageable pageable, Criteria criteria) {
RowsFetchSpec<<%= persistClass %>> createQuery(Pageable pageable, Condition whereClause) {
List<Expression> columns = <%= entityClass %>SqlHelper.getColumns(entityTable, EntityManager.ENTITY_ALIAS);
<%_ reactiveEagerRelations.forEach(function(rel) { _%>
columns.addAll(<%= rel.otherEntityNameCapitalized %>SqlHelper.getColumns(<%= rel.relationshipName %>Table, "<%= rel.relationshipName %>"));
Expand All @@ -138,19 +136,20 @@ _%>
<%_ reactiveEagerRelations.forEach(function(rel) { _%>
.leftOuterJoin(<%= rel.relationshipName %>Table).on(Column.create("<%= rel.joinColumnNames[0] %>", entityTable)).equals(Column.create("<%= rel.otherEntity.primaryKey.fields[0].columnName %>", <%= rel.relationshipName %>Table ))
<%_ }); _%>;
String select = entityManager.createSelect(selectFrom, <%= persistClass %>.class, pageable, criteria);
// we do not support Criteria here for now as of https://github.com/jhipster/generator-jhipster/issues/18269
String select = entityManager.createSelect(selectFrom, <%= persistClass %>.class, pageable, whereClause);
return db.sql(select).map(this::process);
}
@Override
public Flux<<%= persistClass %>> findAll() {
return findAllBy(null, null);
return findAllBy(null);
}
@Override
public Mono<<%= persistClass %>> findById(<%= primaryKey.type %> id) {
return createQuery(null, where(EntityManager.ENTITY_ALIAS + ".<%= primaryKey.fields[0].columnName %>").is(id)).one();
Comparison whereClause = Conditions.isEqual(entityTable.column("<%= primaryKey.fields[0].columnName %>"), Conditions.just(id.toString()));
return createQuery(null, whereClause).one();
}
<%_ if (implementsEagerLoadApis) { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ interface <%= entityClass %>RepositoryInternal {
Flux<<%= persistClass %>> findAll();

Mono<<%= persistClass %>> findById(<%= primaryKey.type %> id);

Flux<<%= persistClass %>> findAllBy(Pageable pageable, Criteria criteria);
// this is not supported at the moment because of https://github.com/jhipster/generator-jhipster/issues/18269
// Flux<<%= persistClass %>> findAllBy(Pageable pageable, Criteria criteria);

<%_ if (implementsEagerLoadApis) { _%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.springframework.data.r2dbc.core.StatementMapper;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.query.UpdateMapper;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.sql.Condition;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.sql.Conditions;
import org.springframework.data.relational.core.sql.OrderByField;
Expand Down Expand Up @@ -90,19 +91,28 @@ public class EntityManager {
* Creates an SQL select statement from the given fragment and pagination parameters.
* @param selectFrom a representation of a select statement.
* @param entityType the entity type which holds the table name.
* @param pageable page parameter, or null, if everything needs to be returned
* @param pageable page parameter, or null, if everything needs to be returned.
* @param where condition or null. The condition to apply as where clause.
* @return sql select statement
*/
public String createSelect(SelectFromAndJoin selectFrom, Class<?> entityType, Pageable pageable, Criteria criteria) {
public String createSelect(SelectFromAndJoin selectFrom, Class<?> entityType, Pageable pageable, Condition where) {
if (pageable != null) {
if (criteria != null) {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()).where(Conditions.just(criteria.toString())), entityType, pageable.getSort());
if (where != null) {
return createSelectImpl(
selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()).where(where),
entityType,
pageable.getSort()
);
} else {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()), entityType, pageable.getSort());
return createSelectImpl(
selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()),
entityType,
pageable.getSort()
);
}
} else {
if (criteria != null) {
return createSelectImpl(selectFrom.where(Conditions.just(criteria.toString())), entityType, null);
if (where != null) {
return createSelectImpl(selectFrom.where(where), entityType, null);
} else {
return createSelectImpl(selectFrom, entityType, null);
}
Expand All @@ -114,37 +124,32 @@ public class EntityManager {
* @param selectFrom a representation of a select statement.
* @param entityType the entity type which holds the table name.
* @param pageable page parameter, or null, if everything needs to be returned
* @param where condition or null. The condition to apply as where clause.
* @return sql select statement
*/
public String createSelect(SelectFromAndJoinCondition selectFrom, Class<?> entityType, Pageable pageable, Criteria criteria) {
public String createSelect(SelectFromAndJoinCondition selectFrom, Class<?> entityType, Pageable pageable, Condition where) {
if (pageable != null) {
if (criteria != null) {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()).where(Conditions.just(criteria.toString())), entityType, pageable.getSort());
if (where != null) {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()).where(where), entityType, pageable.getSort());
} else {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()), entityType, pageable.getSort());
}
} else {
if (criteria != null) {
return createSelectImpl(selectFrom.where(Conditions.just(criteria.toString())), entityType, null);
if (where != null) {
return createSelectImpl(selectFrom.where(where), entityType, null);
} else {
return createSelectImpl(selectFrom, entityType, null);
}
}
}

private String createSelectImpl(SelectOrdered selectFrom, Class<?> entityType, Sort sortParameter) {
if (sortParameter != null && sortParameter.isSorted()) {
RelationalPersistentEntity<?> entity = getPersistentEntity(entityType);
if (entity != null) {
Sort sort = updateMapper.getMappedObject(sortParameter, entity);
selectFrom = selectFrom.orderBy(createOrderByFields(Table.create(entity.getTableName()).as(EntityManager.ENTITY_ALIAS), sort));
}
}
return createSelect(selectFrom.build());
}

private RelationalPersistentEntity<?> getPersistentEntity(Class<?> entityType) {
return r2dbcEntityTemplate.getConverter().getMappingContext().getPersistentEntity(entityType);
/**
* Generate an actual SQL from the given {@link Select}.
* @param select a representation of a select statement.
* @return the generated SQL select.
*/
public String createSelect(Select select) {
return sqlRenderer.render(select);
}

/**
Expand All @@ -166,15 +171,6 @@ public class EntityManager {
return r2dbcEntityTemplate.getDatabaseClient().sql(statementMapper.getMappedObject(delete)).fetch().rowsUpdated();
}

/**
* Generate an actual SQL from the given {@link Select}.
* @param select a representation of a select statement.
* @return the generated SQL select.
*/
public String createSelect(Select select) {
return sqlRenderer.render(select);
}

/**
* Inserts the given entity into the database - and sets the id, if it's an autoincrement field.
* @param <S> the type of the persisted entity.
Expand Down Expand Up @@ -222,6 +218,21 @@ public class EntityManager {
return r2dbcEntityTemplate.getDatabaseClient().sql(statementMapper.getMappedObject(deleteSpec)).then();
}

private String createSelectImpl(SelectOrdered selectFrom, Class<?> entityType, Sort sortParameter) {
if (sortParameter != null && sortParameter.isSorted()) {
RelationalPersistentEntity<?> entity = getPersistentEntity(entityType);
if (entity != null) {
Sort sort = updateMapper.getMappedObject(sortParameter, entity);
selectFrom = selectFrom.orderBy(createOrderByFields(Table.create(entity.getTableName()).as(EntityManager.ENTITY_ALIAS), sort));
}
}
return createSelect(selectFrom.build());
}

private RelationalPersistentEntity<?> getPersistentEntity(Class<?> entityType) {
return r2dbcEntityTemplate.getConverter().getMappingContext().getPersistentEntity(entityType);
}

private static Collection<? extends OrderByField> createOrderByFields(Table table, Sort sortToUse) {

List<OrderByField> fields = new ArrayList<>();
Expand All @@ -236,5 +247,4 @@ public class EntityManager {

return fields;
}

}

0 comments on commit c68607e

Please sign in to comment.