Skip to content

Commit 0343640

Browse files
committed
Fixed the broken order Method
1 parent 188afa6 commit 0343640

File tree

4 files changed

+52
-157
lines changed

4 files changed

+52
-157
lines changed

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

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public class Query<T extends Model> {
2020
private final Repo<T> repo;
2121
private final Class<T> model;
2222
private List<String> select = new ArrayList<>();
23-
private final QueryGroup<T> where;
23+
private final QueryGroup<T> where = new QueryGroup<>();
2424
private Integer offset;
2525
private Integer limit;
26-
private QueryOrderBy order;
26+
private List<QueryOrderBy> order = new ArrayList<>();
2727
private boolean withDeleted = false;
2828
private final List<QueryColumn> groupBy = new ArrayList<>();
2929
private QueryGroup<T> having;
@@ -35,8 +35,6 @@ public Query(Class<T> model) {
3535
public Query(Repo<T> repo, Class<T> model) {
3636
this.repo = repo;
3737
this.model = model;
38-
this.where = new QueryGroup<>();
39-
this.order = new QueryOrderBy();
4038
}
4139

4240
public boolean isWithDeleted() {
@@ -67,7 +65,7 @@ public Integer getOffset() {
6765
return offset;
6866
}
6967

70-
public QueryOrderBy getOrder() {
68+
public List<QueryOrderBy> getOrder() {
7169
return order;
7270
}
7371

@@ -325,47 +323,16 @@ public Query<T> search(String search) {
325323
return this;
326324
}
327325

328-
/**
329-
* Sorts the results by the given column name ascendingly.
330-
*
331-
* @param columnName The name of the column to sort ascendingly by.
332-
* @return The Query object with the given order by information added.
333-
* @throws ORMQueryException if the order operation is called twice on a column specification with the same name.
334-
*/
335326
public Query<T> order(String columnName) throws ORMQueryException {
336327
return order(columnName, false);
337328
}
338329

339-
/**
340-
* Sorts the results by the given column name with the given order direction.
341-
*
342-
* @param columnName The name of the column to sort ascendingly by.
343-
* @param desc If true it will order descendingly, if false it will order ascendingly.
344-
* @return The Query object with the given order by information added.
345-
* @throws ORMQueryException if the order operation is called twice on a column specification with the same name.
346-
*/
347330
public Query<T> order(String columnName, boolean desc) throws ORMQueryException {
348331
return order(new QueryColumn(columnName), desc);
349332
}
350333

351-
/**
352-
* Sorts the results by the given column with the given order direction.
353-
*
354-
* @param column The column encoded as QueryColumn object.
355-
* @param desc If true it will order descendingly, if false it will order ascendingly.
356-
* @return The Query object with the given order by information added.
357-
* @throws ORMQueryException if the order operation is called twice on a column specification with the same name.
358-
*/
359334
public Query<T> order(QueryColumn column, boolean desc) throws ORMQueryException{
360-
boolean success = this.order.add(column, desc);
361-
if(!success) {
362-
throw new ORMQueryException(String.format(
363-
"The column %s could not be ordered %s. This is probably caused by calling .order() on this column twice.",
364-
column.toString(),
365-
desc ? "descendingly" : "ascendingly"
366-
));
367-
}
368-
335+
this.order.add(new QueryOrderBy(column, desc));
369336
return this;
370337
}
371338

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

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,77 @@
22

33
import org.javawebstack.orm.TableInfo;
44

5-
import java.util.LinkedList;
6-
import java.util.stream.Collectors;
5+
import java.util.Objects;
76

87
/**
9-
* The QueryOrderBy class serves as an aggregation of order by elements. It extends a list, because the order of the
10-
* order by statements is of relevance.
8+
* The QueryOrderBy class encodes an Order By Statement.
119
*/
12-
public class QueryOrderBy extends LinkedList<QueryOrderByElement>{
10+
public class QueryOrderBy {
11+
12+
private final QueryColumn queryColumn;
13+
private final boolean desc;
14+
15+
QueryOrderBy(String columnName, boolean desc) {
16+
queryColumn = new QueryColumn(columnName);
17+
this.desc = desc;
18+
}
19+
20+
QueryOrderBy(QueryColumn column, boolean desc) {
21+
this.queryColumn = column;
22+
this.desc = desc;
23+
}
1324

1425
/**
15-
* Add a new order by statement. If a statement with the same column name already exists it will not add the
16-
* statement.
26+
* Retrieves the QueryColumn of the statement which encodes the column name.
1727
*
18-
* @param columnName The column name to order by.
19-
* @param desc If the column should be order descendingly.
20-
* @return True if adding the statement was successful. False otherwise.
28+
* @return The encoding QueryColumn object.
2129
*/
22-
public boolean add(String columnName, boolean desc) {
23-
return this.add(new QueryColumn(columnName), desc);
30+
public QueryColumn getQueryColumn() {
31+
return queryColumn;
2432
}
2533

2634
/**
27-
* Add a new order by statement. If a statement with the same column name already exists it will not add the
28-
* statement.
35+
* Retrieves the information if this column is ordered ascendingly or descendingly.
2936
*
30-
* @param column The column to be ordered by. It will retrieve the name from the QueryColumn.
31-
* @param desc If the column should be order descendingly.
32-
* @return True if adding the statement was successful. False otherwise.
37+
* @return false if ascending, true if descending.
3338
*/
34-
public boolean add(QueryColumn column, boolean desc) {
35-
return this.add(new QueryOrderByElement(column, desc));
39+
public boolean isDesc() {
40+
return desc;
3641
}
3742

38-
@Override
3943
/**
40-
* Add a new order by statement. If a statement with the same column name already exists it will not add the
41-
* statement.
44+
* Compares the encoded column name.
4245
*
43-
* @param element The direct QueryOrderByElement which encodes the order by statement.
44-
* @return True if adding the statement was successful. False otherwise.
46+
* @param o An object to compare to.
47+
* @return True if the object is a QueryOrderBy with a QueryColumn with generates the same identifier.
4548
*/
46-
public boolean add(QueryOrderByElement element) {
47-
boolean hasBeenAdded = false;
48-
if(!willOverwrite(element))
49-
hasBeenAdded = super.add(element);
50-
51-
return hasBeenAdded;
49+
public boolean hasEqualColumn(Object o) {
50+
if (this == o) return true;
51+
if (o == null || getClass() != o.getClass()) return false;
52+
QueryOrderBy that = (QueryOrderBy) o;
53+
return getQueryColumn().equals(that.getQueryColumn());
5254
}
5355

54-
private boolean willOverwrite(QueryOrderByElement element) {
55-
return this.stream().anyMatch(element::hasEqualColumn);
56+
public boolean equals(Object o) {
57+
if (this == o) return true;
58+
if (o == null || getClass() != o.getClass()) return false;
59+
QueryOrderBy that = (QueryOrderBy) o;
60+
return isDesc() == that.isDesc() && getQueryColumn().equals(that.getQueryColumn());
5661
}
5762

63+
public int hashCode() {
64+
return Objects.hash(getQueryColumn(), isDesc());
65+
}
5866

59-
// The toString methods are specific to MySQL so they might have to be replaced later on.
60-
@Override
6167
public String toString() {
62-
return toString(null);
68+
return this.toString(null);
6369
}
6470

6571
public String toString(TableInfo info) {
66-
return this.stream()
67-
.map(singleOrderByElement -> singleOrderByElement.toString(info))
68-
.collect(Collectors.joining(","));
72+
String stringifiedOrderBy = getQueryColumn().toString(info);
73+
if (isDesc())
74+
stringifiedOrderBy += " DESC";
75+
return stringifiedOrderBy;
6976
}
77+
7078
}

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

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/main/java/org/javawebstack/orm/wrapper/builder/MySQLQueryStringBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public SQLQueryString buildQuery(Query<?> query) {
6666
parameters.addAll(group.getParameters());
6767
}
6868

69-
QueryOrderBy orderBy = query.getOrder();
69+
List<QueryOrderBy> orderBy = query.getOrder();
7070
if (!orderBy.isEmpty()) {
7171
sb.append(" ORDER BY ")
72-
.append(orderBy.toString(repo.getInfo()));
72+
.append(orderBy.stream().map(ob -> ob.toString(repo.getInfo())).collect(Collectors.joining(",")));
7373
}
7474

7575
Integer offset = query.getOffset();

0 commit comments

Comments
 (0)