Skip to content

Commit 19bf618

Browse files
committed
added order for to-many
1 parent c260191 commit 19bf618

File tree

10 files changed

+167
-33
lines changed

10 files changed

+167
-33
lines changed

DaoCore/src/de/greenrobot/dao/QueryBuilder.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public class QueryBuilder<T> {
4747
public static boolean LOG_VALUES;
4848

4949
private StringBuilder orderBuilder;
50-
private StringBuilder tableBuilder;
5150
private StringBuilder joinBuilder;
5251

5352
private final List<WhereCondition> whereConditions;
@@ -134,18 +133,21 @@ protected void checkCondition(WhereCondition whereCondition) {
134133
}
135134
}
136135

136+
/** Not supported yet. */
137137
public <J> QueryBuilder<J> join(Class<J> entityClass, Property toOneProperty) {
138138
throw new UnsupportedOperationException();
139139
// return new QueryBuilder<J>();
140140
}
141141

142+
/** Not supported yet. */
142143
public <J> QueryBuilder<J> joinToMany(Class<J> entityClass, Property toManyProperty) {
143144
throw new UnsupportedOperationException();
144145
// @SuppressWarnings("unchecked")
145146
// AbstractDao<J, ?> joinDao = (AbstractDao<J, ?>) dao.getSession().getDao(entityClass);
146147
// return new QueryBuilder<J>(joinDao, "TX");
147148
}
148149

150+
/** Adds the given properties to the ORDER BY section using ascending order. */
149151
public QueryBuilder<T> orderAsc(Property... properties) {
150152
for (Property property : properties) {
151153
checkOrderBuilder();
@@ -154,6 +156,7 @@ public QueryBuilder<T> orderAsc(Property... properties) {
154156
return this;
155157
}
156158

159+
/** Adds the given properties to the ORDER BY section using descending order. */
157160
public QueryBuilder<T> orderDesc(Property... properties) {
158161
for (Property property : properties) {
159162
checkOrderBuilder();
@@ -162,6 +165,16 @@ public QueryBuilder<T> orderDesc(Property... properties) {
162165
return this;
163166
}
164167

168+
/**
169+
* Adds the given raw SQL string to the ORDER BY section. Do not use this for standard properties: ordedAsc and
170+
* orderDesc are prefered.
171+
*/
172+
public QueryBuilder<T> orderRaw(String rawOrder) {
173+
checkOrderBuilder();
174+
orderBuilder.append(rawOrder);
175+
return this;
176+
}
177+
165178
protected StringBuilder append(StringBuilder builder, Property property) {
166179
checkProperty(property);
167180
builder.append(tablePrefix).append('.').append(property.columnName);
@@ -184,6 +197,10 @@ protected void checkProperty(Property property) {
184197
}
185198
}
186199

200+
/**
201+
* Builds a reusable query object (Query objects can be executed more efficiently than creating a QueryBuilder for
202+
* each execution.
203+
*/
187204
public Query<T> build() {
188205
String select;
189206
if (joinBuilder == null || joinBuilder.length() == 0) {

DaoGenerator/src-generator-testentities/de/greenrobot/daogenerator/gentest/TestDaoGenerator.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import de.greenrobot.daogenerator.DaoGenerator;
2121
import de.greenrobot.daogenerator.Entity;
2222
import de.greenrobot.daogenerator.Property;
23+
import de.greenrobot.daogenerator.Property.PropertyBuilder;
2324
import de.greenrobot.daogenerator.Schema;
25+
import de.greenrobot.daogenerator.ToMany;
2426

2527
/**
2628
* Generates test entities for test project DaoTest.
@@ -122,11 +124,18 @@ protected void createRelation() {
122124
protected void createToMany() {
123125
Entity toManyTargetEntity = schema.addEntity("ToManyTargetEntity");
124126
Property toManyIdProperty = toManyTargetEntity.addLongProperty("toManyId").getProperty();
127+
Property toManyIdDescProperty = toManyTargetEntity.addLongProperty("toManyIdDesc").getProperty();
125128
toManyTargetEntity.addIdProperty();
126129

127130
Entity toManyEntity = schema.addEntity("ToManyEntity");
128-
toManyEntity.addIdProperty();
129-
toManyEntity.addToMany(toManyTargetEntity, toManyIdProperty);
131+
Property idProperty = toManyEntity.addIdProperty().getProperty();
132+
133+
ToMany toMany = toManyEntity.addToMany(toManyTargetEntity, toManyIdProperty);
134+
toMany.orderAsc(idProperty);
135+
136+
ToMany toManyDesc = toManyEntity.addToMany(toManyTargetEntity, toManyIdDescProperty);
137+
toManyDesc.setName("ToManyDescList");
138+
toManyDesc.orderDesc(idProperty);
130139
}
131140

132141
protected void createDate() {

DaoGenerator/src-template/dao.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ as property>${property.columnName}<#if property_has_next>,</#if></#list>);";
265265
<#list toMany.targetProperties as property>
266266
queryBuilder.where(Properties.${property.propertyName?cap_first}.eq(${property.propertyName}));
267267
</#list>
268+
<#if toMany.order?has_content>
269+
queryBuilder.orderRaw("${toMany.order}");
270+
</#if>
268271
${toMany.sourceEntity.className?uncap_first}_${toMany.name?cap_first}Query = queryBuilder.build();
269272
} else {
270273
<#list toMany.targetProperties as property>

DaoGenerator/src/de/greenrobot/daogenerator/Entity.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,20 @@ public ToOne addToOne(Entity target, Property fkProperty) {
131131
return toOne;
132132
}
133133

134-
public ToMany addToMany(Entity target, Property fkProperty) {
135-
Property[] sourceProperties = null;
136-
Property[] targetProperties = { fkProperty };
134+
/** Add a to-many relationship; the target entity is joined to the PK property of this entity. */
135+
public ToMany addToMany(Entity target, Property targetProperty) {
136+
Property[] targetProperties = { targetProperty };
137+
return addToMany(null, target, targetProperties);
138+
}
139+
140+
/** Add a to-many relationship; the target entity is joined to the PK property of this entity. */
141+
public ToMany addToMany(Property sourceProperty, Entity target, Property targetProperty) {
142+
Property[] sourceProperties = { sourceProperty };
143+
Property[] targetProperties = { targetProperty };
144+
return addToMany(sourceProperties, target, targetProperties);
145+
}
137146

147+
private ToMany addToMany(Property[] sourceProperties, Entity target, Property[] targetProperties) {
138148
ToMany toMany = new ToMany(schema, this, sourceProperties, target, targetProperties);
139149
toManyRelations.add(toMany);
140150
target.incomingToManyRelations.add(toMany);
@@ -363,10 +373,15 @@ void init3ndPass() {
363373
property.init3ndPass();
364374
}
365375

376+
Set<String> toOneNames = new HashSet<String>();
366377
for (ToOne toOne : toOneRelations) {
367378
toOne.init3ndPass();
379+
if (!toOneNames.add(toOne.getName().toLowerCase())) {
380+
throw new RuntimeException("Duplicate name for " + toOne);
381+
}
368382
}
369383

384+
Set<String> toManyNames = new HashSet<String>();
370385
for (ToMany toMany : toManyRelations) {
371386
toMany.init3ndPass();
372387
Entity targetEntity = toMany.getTargetEntity();
@@ -375,6 +390,9 @@ void init3ndPass() {
375390
targetEntity.propertiesColumns.add(targetProperty);
376391
}
377392
}
393+
if (!toManyNames.add(toMany.getName().toLowerCase())) {
394+
throw new RuntimeException("Duplicate name for " + toMany);
395+
}
378396
}
379397

380398
}

DaoGenerator/src/de/greenrobot/daogenerator/ToMany.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919

2020
import java.util.List;
2121

22+
/** To-many relationship from a source entity to many target entitites. */
2223
public class ToMany {
24+
@SuppressWarnings("unused")
2325
private final Schema schema;
2426
private String name;
2527
private final Entity sourceEntity;
2628
private final Entity targetEntity;
2729
private Property[] sourceProperties;
2830
private final Property[] targetProperties;
31+
private StringBuilder orderBuilder;
2932

3033
public ToMany(Schema schema, Entity sourceEntity, Property[] sourceProperties, Entity targetEntity,
3134
Property[] targetProperties) {
@@ -64,6 +67,36 @@ public void setName(String name) {
6467
this.name = name;
6568
}
6669

70+
private void checkOrderBuilder() {
71+
if (orderBuilder == null) {
72+
orderBuilder = new StringBuilder();
73+
} else if (orderBuilder.length() > 0) {
74+
orderBuilder.append(",");
75+
}
76+
}
77+
78+
public void orderAsc(Property... properties) {
79+
for (Property property : properties) {
80+
checkOrderBuilder();
81+
orderBuilder.append(property.getColumnName()).append(" ASC");
82+
}
83+
}
84+
85+
public void orderDesc(Property... properties) {
86+
for (Property property : properties) {
87+
checkOrderBuilder();
88+
orderBuilder.append(property.getColumnName()).append(" DESC");
89+
}
90+
}
91+
92+
public String getOrder() {
93+
if (orderBuilder == null) {
94+
return null;
95+
} else {
96+
return orderBuilder.toString();
97+
}
98+
}
99+
67100
public void init2ndPass() {
68101
if (name == null) {
69102
char[] nameCharArray = targetEntity.getClassName().toCharArray();
@@ -101,27 +134,11 @@ public void init2ndPass() {
101134
public void init3ndPass() {
102135
}
103136

104-
protected boolean checkUseEquals(PropertyType propertyType) {
105-
boolean useEquals;
106-
switch (propertyType) {
107-
case Byte:
108-
case Short:
109-
case Int:
110-
case Long:
111-
case Boolean:
112-
case Float:
113-
useEquals = true;
114-
break;
115-
default:
116-
useEquals = false;
117-
break;
118-
}
119-
return useEquals;
120-
}
121-
122137
@Override
123138
public String toString() {
124-
return "ToOne '" + name + "' from " + sourceEntity + " to " + targetEntity;
139+
String sourceName = sourceEntity != null ? sourceEntity.getClassName() : null;
140+
String targetName = targetEntity != null ? targetEntity.getClassName() : null;
141+
return "ToMany '" + name + "' from " + sourceName + " to " + targetName;
125142
}
126143

127144
}

DaoGenerator/src/de/greenrobot/daogenerator/ToOne.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ protected boolean checkUseEquals(PropertyType propertyType) {
121121

122122
@Override
123123
public String toString() {
124-
return "ToOne '" + name + "' from " + sourceEntity + " to " + targetEntity;
124+
String sourceName = sourceEntity != null ? sourceEntity.getClassName() : null;
125+
String targetName = targetEntity != null ? targetEntity.getClassName() : null;
126+
return "ToOne '" + name + "' from " + sourceName + " to " + targetName;
125127
}
126128

127129
}

DaoTest/src-gen/de/greenrobot/daotest/ToManyEntity.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class ToManyEntity {
1616
private DaoSession daoSession;
1717

1818
private List<ToManyTargetEntity> toManyTargetEntity;
19+
private List<ToManyTargetEntity> ToManyDescList;
1920
public ToManyEntity() {
2021
}
2122

@@ -53,4 +54,21 @@ public synchronized void resetToManyTargetEntity() {
5354
toManyTargetEntity = null;
5455
}
5556

57+
/** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */
58+
public synchronized List<ToManyTargetEntity> getToManyDescList() {
59+
if (ToManyDescList == null) {
60+
if (daoSession == null) {
61+
throw new DaoException("Entity is detached from DAO context");
62+
}
63+
ToManyTargetEntityDao dao = daoSession.getToManyTargetEntityDao();
64+
ToManyDescList = dao._queryToManyEntity_ToManyDescList(id);
65+
}
66+
return ToManyDescList;
67+
}
68+
69+
/** Resets a to-many relationship, making the next get call to query for a fresh result. */
70+
public synchronized void resetToManyDescList() {
71+
ToManyDescList = null;
72+
}
73+
5674
}

DaoTest/src-gen/de/greenrobot/daotest/ToManyTargetEntity.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
public class ToManyTargetEntity {
88

99
private Long toManyId;
10+
private Long toManyIdDesc;
1011
private Long id;
1112

1213
public ToManyTargetEntity() {
@@ -16,8 +17,9 @@ public ToManyTargetEntity(Long id) {
1617
this.id = id;
1718
}
1819

19-
public ToManyTargetEntity(Long toManyId, Long id) {
20+
public ToManyTargetEntity(Long toManyId, Long toManyIdDesc, Long id) {
2021
this.toManyId = toManyId;
22+
this.toManyIdDesc = toManyIdDesc;
2123
this.id = id;
2224
}
2325

@@ -29,6 +31,14 @@ public void setToManyId(Long toManyId) {
2931
this.toManyId = toManyId;
3032
}
3133

34+
public Long getToManyIdDesc() {
35+
return toManyIdDesc;
36+
}
37+
38+
public void setToManyIdDesc(Long toManyIdDesc) {
39+
this.toManyIdDesc = toManyIdDesc;
40+
}
41+
3242
public Long getId() {
3343
return id;
3444
}

0 commit comments

Comments
 (0)