@@ -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,20 +296,50 @@ public Query<T> search(String search) {
300296 return this ;
301297 }
302298
303- public Query <T > order (String orderBy , boolean desc ) {
304- return order (new QueryColumn (orderBy ), desc );
305- }
299+ /**
300+ * Sorts the results by the given column name ascendingly.
301+ *
302+ * @param columnName The name of the column to sort ascendingly by.
303+ * @return The Query object with the given order by information added.
304+ * @throws ORMQueryException if the order operation is called twice on a column specification with the same name.
305+ */
306+ public Query <T > order (String columnName ) throws ORMQueryException {
307+ return order (columnName , false );
308+ }
309+
310+ /**
311+ * Sorts the results by the given column name with the given order direction.
312+ *
313+ * @param columnName The name of the column to sort ascendingly by.
314+ * @param desc If true it will order descendingly, if false it will order ascendingly.
315+ * @return The Query object with the given order by information added.
316+ * @throws ORMQueryException if the order operation is called twice on a column specification with the same name.
317+ */
318+ public Query <T > order (String columnName , boolean desc ) throws ORMQueryException {
319+ return order (new QueryColumn (columnName ), desc );
320+ }
321+
322+ /**
323+ * Sorts the results by the given column with the given order direction.
324+ *
325+ * @param column The column encoded as QueryColumn object.
326+ * @param desc If true it will order descendingly, if false it will order ascendingly.
327+ * @return The Query object with the given order by information added.
328+ * @throws ORMQueryException if the order operation is called twice on a column specification with the same name.
329+ */
330+ public Query <T > order (QueryColumn column , boolean desc ) throws ORMQueryException {
331+ boolean success = this .order .add (column , desc );
332+ if (!success ) {
333+ throw new ORMQueryException (String .format (
334+ "The column %s could not be ordered %s. This is probably caused by calling .order() on this column twice." ,
335+ column .toString (),
336+ desc ? "descendingly" : "ascendingly"
337+ ));
338+ }
306339
307- public Query <T > order (QueryColumn orderBy , boolean desc ) {
308- this .order = orderBy ;
309- this .desc = desc ;
310340 return this ;
311341 }
312342
313- public Query <T > order (String orderBy ) {
314- return order (orderBy , false );
315- }
316-
317343 public Query <T > limit (int offset , int limit ) {
318344 return offset (offset ).limit (limit );
319345 }
0 commit comments