-
Notifications
You must be signed in to change notification settings - Fork 18
/
OrderBy.cls
53 lines (44 loc) · 1.58 KB
/
OrderBy.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
global class OrderBy implements Soqlable{
global String field {get;set;}
global Boolean ascending {get;set;}
global Boolean descending {get{return !ascending;}}
global Boolean nullsFirst {get;set;}
global Boolean nullsLast {get{return !nullsFirst;}}
private Boolean sortSet = false;
private Boolean nullsSet = false;
global OrderBy(String field){
if(ApexLangUtils.isBlank(field)){
throw new IllegalArgumentException('field is empty');
}
this.field = field;
this.ascending();
this.sortSet = false;
}
global OrderBy ascending (){ return setAscending(true); }
global OrderBy descending(){ return setAscending(false);}
global OrderBy nullsFirst(){ return setNullsFirst(true);}
global OrderBy nullsLast (){ return setNullsFirst(false);}
private OrderBy setAscending(Boolean ascending){
this.ascending = ascending;
this.sortSet = true;
if(!this.nullsSet){
this.nullsFirst = this.descending;
}
return this;
}
private OrderBy setNullsFirst(Boolean nullsFirst){
this.nullsFirst = nullsFirst;
this.nullsSet = true;
return this;
}
global String toSoql(){ return this.toSoql(null); }
global String toSoql(SoqlOptions options){
if(options == null){
options = SoqlOptions.DEFAULT_OPTIONS;
}
return this.field
+ (sortSet ? (ascending ? ' ASC' : ' DESC'): '')
+ (nullsSet ? (nullsFirst ? ' NULLS FIRST' : ' NULLS LAST') : '')
;
}
}