-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
24 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,52 @@ | ||
public class QOrder { | ||
|
||
private static final String ASCENDING = 'ASC'; | ||
private static final String DESCENDING = 'DESC'; | ||
public enum SortOrder { ASCENDING, DESCENDING } | ||
public enum NullsOrder { FIRST, LAST } | ||
|
||
private String field; | ||
private String orderText = ASCENDING; | ||
private String nullsOrder = ''; | ||
private SortOrder sortValue; | ||
private NullsOrder nullsValue; | ||
|
||
public QOrder(String field) { | ||
this.field = field; | ||
this.sortValue = SortOrder.ASCENDING; | ||
} | ||
|
||
public QOrder ascending() { | ||
this.orderText = ASCENDING; | ||
this.sortValue = SortOrder.ASCENDING; | ||
return this; | ||
} | ||
|
||
public QOrder descending() { | ||
this.orderText = DESCENDING; | ||
this.sortValue = SortOrder.DESCENDING; | ||
return this; | ||
} | ||
|
||
public QOrder nullsFirst() { | ||
this.nullsOrder = ' NULLS FIRST'; | ||
this.nullsValue = NullsOrder.FIRST; | ||
return this; | ||
} | ||
|
||
public QOrder nullsLast() { | ||
this.nullsOrder = ' NULLS LAST'; | ||
this.nullsValue = NullsOrder.LAST; | ||
return this; | ||
} | ||
|
||
public String build() { | ||
return field + ' ' + orderText + nullsOrder; | ||
String orderString = field; | ||
|
||
if (sortValue == SortOrder.ASCENDING) { | ||
orderString += ' ASC'; | ||
} else { | ||
orderString += ' DESC'; | ||
} | ||
|
||
if (nullsValue != null && nullsValue == NullsOrder.FIRST) { | ||
orderString += ' NULLS FIRST'; | ||
} else if (nullsValue != null && nullsValue == NullsOrder.LAST) { | ||
orderString += ' NULLS LAST'; | ||
} | ||
|
||
return orderString; | ||
} | ||
} |