Skip to content

Commit

Permalink
joins based on joins
Browse files Browse the repository at this point in the history
  • Loading branch information
greenrobot committed Jun 26, 2015
1 parent 7e09203 commit 9f9422c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
22 changes: 17 additions & 5 deletions DaoCore/src/de/greenrobot/dao/query/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public <J> Join<T, J> join(Class<J> destinationEntityClass, Property destination
public <J> Join<T, J> join(Property sourceProperty, Class<J> destinationEntityClass) {
AbstractDao<J, ?> destinationDao = (AbstractDao<J, ?>) dao.getSession().getDao(destinationEntityClass);
Property destinationProperty = destinationDao.getPkProperty();
return addJoin(sourceProperty, destinationDao, destinationProperty);
return addJoin(tablePrefix, sourceProperty, destinationDao, destinationProperty);
}

/**
Expand All @@ -144,13 +144,25 @@ public <J> Join<T, J> join(Property sourceProperty, Class<J> destinationEntityCl
*/
public <J> Join<T, J> join(Property sourceProperty, Class<J> destinationEntityClass, Property destinationProperty) {
AbstractDao<J, ?> destinationDao = (AbstractDao<J, ?>) dao.getSession().getDao(destinationEntityClass);
return addJoin(sourceProperty, destinationDao, destinationProperty);
return addJoin(tablePrefix, sourceProperty, destinationDao, destinationProperty);
}

private <J> Join<T, J> addJoin(Property sourceProperty,
AbstractDao<J, ?> destinationDao, Property destinationProperty) {
/**
* Expands the query to another entity type by using a JOIN. The given sourceJoin's property is used to match the
* given destinationProperty of the given destinationEntity. Note that destination entity of the given join is used
* as the source for the new join to add. In this way, it is possible to compose complex "join of joins" across
* several entities if required.
*/
public <J> Join<T, J> join(Join<?, T> sourceJoin, Property sourceProperty, Class<J> destinationEntityClass,
Property destinationProperty) {
AbstractDao<J, ?> destinationDao = (AbstractDao<J, ?>) dao.getSession().getDao(destinationEntityClass);
return addJoin(sourceJoin.tablePrefix, sourceProperty, destinationDao, destinationProperty);
}

private <J> Join<T, J> addJoin(String sourceTablePrefix, Property sourceProperty, AbstractDao<J, ?> destinationDao,
Property destinationProperty) {
String joinTablePrefix = "J" + (joins.size() + 1);
Join<T, J> join = new Join<T, J>(tablePrefix, sourceProperty, destinationDao, destinationProperty,
Join<T, J> join = new Join<T, J>(sourceTablePrefix, sourceProperty, destinationDao, destinationProperty,
joinTablePrefix);
joins.add(join);
return join;
Expand Down
25 changes: 24 additions & 1 deletion DaoTest/src/de/greenrobot/daotest/query/JoinTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ public void testJoinMixedParameterValues() {
assertEquals("entity-6", entities.get(1).getSimpleString());
}

public void testJoinOfJoin() {
prepareData();
List<RelationEntity> relationEntities = relationEntityDao.loadAll();
relationEntities.get(2).setParent(relationEntities.get(4));
relationEntities.get(3).setParent(relationEntities.get(5));
relationEntities.get(7).setParent(relationEntities.get(5));
relationEntityDao.updateInTx(relationEntities);

QueryBuilder<RelationEntity> queryBuilder = relationEntityDao.queryBuilder();
Join<RelationEntity, RelationEntity> join1 =
queryBuilder.join(RelationEntityDao.Properties.ParentId, RelationEntity.class);
queryBuilder.join(join1, RelationEntityDao.Properties.TestIdNotNull, TestEntity.class, Properties.Id)
.where(Properties.SimpleInt.lt(6));

Query<RelationEntity> query = queryBuilder.build();
RelationEntity entity = query.uniqueOrThrow();
assertEquals(relationEntities.get(2).getSimpleString(), entity.getSimpleString());

query.setParameter(0, 99);
assertEquals(3, query.list().size());
}

public void testJoinDelete() {
prepareData();
QueryBuilder<RelationEntity> queryBuilder = createQueryBuilder(5);
Expand Down Expand Up @@ -114,14 +136,15 @@ public void testJoinCount() {

private void prepareData() {
List<TestEntity> targetEntities = new ArrayList<TestEntity>();
List<RelationEntity> entities = new ArrayList<RelationEntity>();
for (int i = 0; i < 10; i++) {
TestEntity testEntity = new TestEntity();
testEntity.setSimpleInt(i + 1);
testEntity.setSimpleStringNotNull("string-" + (i + 1));
targetEntities.add(testEntity);
}
testEntityDao.insertInTx(targetEntities);

List<RelationEntity> entities = new ArrayList<RelationEntity>();
for (int i = 0; i < 10; i++) {
RelationEntity entity = new RelationEntity();
entity.setSimpleString("entity-" + (i + 1));
Expand Down

0 comments on commit 9f9422c

Please sign in to comment.