Skip to content

Commit 17a7060

Browse files
Add multiple order by capability
1 parent 0a684f9 commit 17a7060

File tree

3 files changed

+97
-13
lines changed

3 files changed

+97
-13
lines changed

src/main/java/org/javawebstack/orm/query/Query.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public class Query<T extends Model> {
2424
private final QueryGroup<T> where;
2525
private Integer offset;
2626
private Integer limit;
27-
private QueryColumn order;
28-
private boolean desc = false;
27+
private QueryOrderBy order;
2928
private boolean withDeleted = false;
3029
private final List<QueryWith> withs = new ArrayList<>();
3130

@@ -37,6 +36,7 @@ public Query(Repo<T> repo, Class<T> model) {
3736
this.repo = repo;
3837
this.model = model;
3938
this.where = new QueryGroup<>();
39+
this.order = new QueryOrderBy();
4040
}
4141

4242
public boolean isWithDeleted() {
@@ -59,14 +59,10 @@ public Integer getOffset() {
5959
return offset;
6060
}
6161

62-
public QueryColumn getOrder() {
62+
public QueryOrderBy getOrder() {
6363
return order;
6464
}
6565

66-
public boolean isDescOrder() {
67-
return desc;
68-
}
69-
7066
public Repo<T> getRepo() {
7167
return repo;
7268
}
@@ -300,18 +296,25 @@ public Query<T> search(String search) {
300296
return this;
301297
}
302298

299+
public Query<T> order(String orderBy) {
300+
return order(orderBy, false);
301+
}
302+
303303
public Query<T> order(String orderBy, boolean desc) {
304304
return order(new QueryColumn(orderBy), desc);
305305
}
306306

307307
public Query<T> order(QueryColumn orderBy, boolean desc) {
308-
this.order = orderBy;
309-
this.desc = desc;
310-
return this;
311-
}
308+
boolean success = this.order.add(orderBy, desc);
309+
if(!success) {
310+
throw new ORMQueryException(String.format(
311+
"The column %s could not be ordered %s. This is probably caused by calling .order() on this column twice.",
312+
orderBy.toString(),
313+
desc ? "descendingly" : "ascendingly"
314+
));
315+
}
312316

313-
public Query<T> order(String orderBy) {
314-
return order(orderBy, false);
317+
return this;
315318
}
316319

317320
public Query<T> limit(int offset, int limit) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.javawebstack.orm.query;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
public class QueryOrderBy extends LinkedList<QueryOrderByElement>{
7+
8+
public boolean add(String columnName, boolean desc) {
9+
return this.add(new QueryColumn(columnName), desc);
10+
}
11+
12+
public boolean add(QueryColumn column, boolean desc) {
13+
return this.add(new QueryOrderByElement(column, desc));
14+
}
15+
16+
@Override
17+
public boolean add(QueryOrderByElement element) {
18+
boolean hasBeenAdded = false;
19+
if(!willOverwrite(element))
20+
hasBeenAdded = super.add(element);
21+
22+
return hasBeenAdded;
23+
}
24+
25+
private boolean willOverwrite(QueryOrderByElement element) {
26+
return this.stream().anyMatch(element::hasEqualColumn);
27+
}
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.javawebstack.orm.query;
2+
3+
import java.util.Objects;
4+
5+
public class QueryOrderByElement {
6+
private QueryColumn queryColumn;
7+
private boolean desc;
8+
9+
QueryOrderByElement(String columnName, boolean desc) {
10+
queryColumn = new QueryColumn(columnName);
11+
this.desc = desc;
12+
}
13+
14+
QueryOrderByElement(QueryColumn column, boolean desc) {
15+
this.queryColumn = column;
16+
this.desc = desc;
17+
}
18+
19+
public QueryColumn getQueryColumn() {
20+
return queryColumn;
21+
}
22+
23+
public boolean isDesc() {
24+
return desc;
25+
}
26+
27+
public boolean hasEqualColumn(Object o) {
28+
if (this == o) return true;
29+
if (o == null || getClass() != o.getClass()) return false;
30+
QueryOrderByElement that = (QueryOrderByElement) o;
31+
return getQueryColumn().equals(that.getQueryColumn());
32+
}
33+
34+
public boolean hasEqualOrderDirection(Object o) {
35+
if (this == o) return true;
36+
if (o == null || getClass() != o.getClass()) return false;
37+
QueryOrderByElement that = (QueryOrderByElement) o;
38+
return isDesc() == that.isDesc();
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) return true;
44+
if (o == null || getClass() != o.getClass()) return false;
45+
QueryOrderByElement that = (QueryOrderByElement) o;
46+
return isDesc() == that.isDesc() && getQueryColumn().equals(that.getQueryColumn());
47+
}
48+
49+
@Override
50+
public int hashCode() {
51+
return Objects.hash(getQueryColumn(), isDesc());
52+
}
53+
}

0 commit comments

Comments
 (0)