public without sharing class QueryWrapper {
|
private Map<String, Object> searchMap = new Map<String, Object>();
|
public String searchs = '';
|
private Schema.SObjectType type;
|
private List<SObjectField> sObjectFieldList = null;
|
public QueryWrapper(Schema.SObjectType type){
|
this.type = type;
|
}
|
public QueryWrapper(Schema.SObjectType type, List<SObjectField> sObjectFieldList){
|
this.type = type;
|
this.sObjectFieldList = sObjectFieldList;
|
}
|
|
|
public QueryWrapper() {
|
// more code here
|
}
|
//public QueryWrapper(String type){
|
// this.type = type;
|
//}
|
|
private String statmentType(Object o){
|
String whereO = '';
|
if(o instanceof Double) {
|
whereO = ((Double)o).format();
|
} else if(o instanceof Integer){
|
whereO = ((Integer)o).format();
|
} else if(o instanceof Long){
|
whereO = ((Long)o).format();
|
} else if(o instanceof String){
|
if(!''.equals(o)){
|
whereO = '\'' + String.escapeSingleQuotes((String)o) + '\'' ;
|
}else{
|
//报错
|
}
|
|
} else if(o instanceof Boolean){
|
if((Boolean)o){
|
whereO = 'true';
|
}else{
|
whereO = 'false';
|
}
|
} else if(o instanceof Decimal){
|
whereO = ((Decimal)o).format();
|
}else if(o instanceof ID){
|
// whereO = ((ID)o).format();
|
}else if(o instanceof Date){
|
whereO = ((Date)o).format();
|
}else if(o instanceof Datetime){
|
whereO = ((Datetime)o).format();
|
}
|
|
return whereO;
|
}
|
|
private void statment(String column, Object o, String wheres){
|
String whereO = '';
|
if(column != null && column != '' && o != null){
|
if('='.equals(wheres.trim()) || '!='.equals(wheres.trim()) ) {
|
whereO = statmentType(o);
|
} else if(' Like '.equals(wheres)) {
|
whereO = statmentType(o);
|
if(!''.equals(whereO)){
|
//之前Like的sql拼写有误重新拼接
|
whereO = whereO.substring(1,whereO.length()-1);
|
whereO = '%' + whereO + '%';
|
whereO = '\'' + whereO + '\'' ;
|
}
|
} else if(' rightLike '.equals(wheres)) {
|
whereO = statmentType(o);
|
if(!''.equals(whereO)){
|
//之前Like的sql拼写有误重新拼接
|
whereO = whereO.substring(1,whereO.length()-1);
|
whereO = whereO + '%';
|
whereO = '\'' + whereO + '\'' ;
|
|
}
|
} else if(' leftLike '.equals(wheres)) {
|
whereO = statmentType(o);
|
if(!''.equals(whereO)){
|
//之前Like的sql拼写有误重新拼接
|
whereO = whereO.substring(1,whereO.length()-1);
|
whereO = '%' + whereO;
|
whereO = '\'' + whereO + '\'' ;
|
|
}
|
}
|
|
if(searchs.equals('')){
|
searchs = searchs + column + wheres + whereO;
|
}else{
|
searchs = searchs + ' AND ' + column + wheres + whereO;
|
}
|
|
searchMap.put(column, o);
|
} else {
|
// throw
|
}
|
}
|
// in 和not in
|
private void statmentIn(String column, List<Object> o, String wheres){
|
String inList = '(';
|
if(o != null && o.size() > 0){
|
for(Object s : o){
|
if(s instanceof Double) {
|
s = ((Double)s).format();
|
} else if(s instanceof Integer){
|
s = ((Integer)s).format();
|
} else if(s instanceof Long){
|
s = ((Long)s).format();
|
} else if(s instanceof String){
|
if(!''.equals(s)){
|
s = '\'' + String.escapeSingleQuotes((String)s) + '\'' ;
|
}else{
|
//报错
|
}
|
}
|
inList += '' + s + ',';
|
}
|
inList = inList.substring(0,inList.length() - 1);
|
inList += ')';
|
|
if(searchs.equals('')){
|
searchs = searchs + column + wheres + inList;
|
}else{
|
searchs = searchs + ' AND ' + column + wheres + inList;
|
}
|
}else{
|
//报错
|
}
|
|
}
|
/**
|
* 等于
|
*/
|
public QueryWrapper eq(String column, Object o){
|
statment(column, o,' = ');
|
return this;
|
}
|
/**
|
* 不等于
|
*/
|
public QueryWrapper ne(String column, Object o){
|
statment(column, o,' != ');
|
return this;
|
}
|
/**
|
* IN
|
*/
|
public QueryWrapper inPlus(String column, List<Object> o){
|
statmentIn(column,o,' In ');
|
return this;
|
}
|
/**
|
* not in
|
*/
|
public QueryWrapper notIn(String column, List<Object> o){
|
statmentIn(column,o,' notIn ');
|
return this;
|
}
|
|
/**
|
* 大于
|
*/
|
public QueryWrapper gt(String column, Object o){
|
return null;
|
}
|
/**
|
* 大于等于
|
*/
|
public QueryWrapper ge(String column, Object o){
|
return null;
|
}
|
/**
|
* 小于等于
|
*/
|
public QueryWrapper lt(String column, Object o){
|
return null;
|
}
|
/**
|
* 小于等于
|
*/
|
public QueryWrapper le(String column, Object o){
|
return null;
|
}
|
/**
|
* Like
|
*/
|
public QueryWrapper likePlus(String column, Object o){
|
statment(column,o,' Like ');
|
return this;
|
}
|
/**
|
* right LIKE
|
*/
|
public QueryWrapper likeRight(String column, Object o){
|
statment(column,o,' rightLike ');
|
return this;
|
}
|
|
/**
|
* left LIKE
|
*/
|
public QueryWrapper likeLeft(String column, Object o){
|
statment(column,o,' leftLike ');
|
return this;
|
}
|
|
/**
|
* is null
|
*/
|
public QueryWrapper isNull(String column){
|
return this;
|
}
|
/**
|
* is not null
|
*/
|
public QueryWrapper isNotNull(String column){
|
return this;
|
}
|
|
/**
|
* 排序
|
*/
|
public QueryWrapper orderBy(String column, Object o){
|
searchMap.put('sort', column);
|
searchMap.put('sortOrder', o);
|
return this;
|
}
|
/**
|
* 正序
|
*/
|
public QueryWrapper orderByAsc(String column){
|
searchMap.put('sort', column);
|
searchMap.put('sortOrder', 'asc');
|
return this;
|
}
|
/**
|
* 倒序
|
*/
|
public QueryWrapper orderByDesc(String column){
|
searchMap.put('sort', column);
|
searchMap.put('sortOrder', ' desc');
|
return this;
|
}
|
|
//group by
|
public QueryWrapper groupBy(String column, String o){
|
searchMap.put('groupby', o);
|
return this;
|
}
|
|
|
public Schema.SObjectType getType(){
|
return this.type;
|
}
|
public void setType(Schema.SObjectType type){
|
this.type = type;
|
}
|
|
public List<SObjectField> getSObjectFieldList(){
|
return this.sObjectFieldList;
|
}
|
public void setSObjectFieldList(List<SObjectField> sObjectField){
|
this.sObjectFieldList = sObjectField;
|
}
|
|
|
public String build(){
|
String s = '';
|
if(!''.equals(searchs)){
|
s = ' where ' + searchs;
|
}
|
if(searchMap.containsKey('groupby') && !''.equals(searchMap.get('groupby'))){
|
s = s + ' group by ' + searchMap.get('groupby');
|
}
|
if(searchMap.containsKey('sort') && !''.equals(searchMap.get('sort')) && !''.equals('sortOrder')){
|
s = s + ' order by ' + searchMap.get('sort') + ' ' + searchMap.get('sortOrder');
|
}
|
return s;
|
}
|
|
}
|