public class SoqlHelper {
|
|
public static string DistinctQueryFields(string sql)
|
{
|
string query_fields = sql;
|
if (query_fields == null) {
|
return null;
|
}
|
query_fields = query_fields.trim();
|
Integer sel_i = query_fields.indexOfIgnoreCase('select ');
|
string sel = 'select ';
|
|
if ( sel_i < 0) {
|
sel = '';
|
}
|
|
string whe = '';
|
Integer whe_i = query_fields.indexOfIgnoreCase(' from ');
|
if ( whe_i >= 0) {
|
whe = query_fields.substring(whe_i);
|
}
|
string query = query_fields.substring(sel_i < 0 ? 0 : sel.length(), whe_i < 0 ? query_fields.length() : whe_i);
|
Set<string> ss = new Set<string>();
|
for(string f : query.split(',')){
|
string s = f.trim().toLowerCase();
|
//System.debug('s='+s);
|
ss.add(s);
|
}
|
|
return sel + QueryFields(ss) + whe;
|
|
}
|
|
public static string WId(string wid){
|
return '\'' + wid + '\'';
|
}
|
|
public static string QueryFields(Set<string> lo){
|
return QueryFields(new List<string>(lo));
|
}
|
|
public static string QueryFields(List<object> lo){
|
return string.join(lo, ',');
|
}
|
|
public static string ToInCondition(List<AggregateResult> lar ,string key)
|
{
|
if(lar==null || lar.size() == 0)
|
{
|
return null;
|
}
|
|
string ss = ' ( ';
|
boolean b = true;
|
for(AggregateResult ar : lar)
|
{
|
if(ar.get(key)==null)continue;
|
if( b )
|
{
|
b = false;
|
ss += '\''+ar.get(key)+'\'';
|
}
|
else
|
{
|
ss += ',\''+ar.get(key)+'\'';
|
}
|
|
}
|
ss+=') ';
|
return ss;
|
}
|
|
public static string ToInCondition(Set<string> so){
|
return ToInCondition(new List<string>(so));
|
}
|
|
public static string ToInCondition(List<object> lo)
|
{
|
Set<object> so = new Set<object>(lo);
|
lo = new List<object>(so);
|
return '(\''+ string.join(lo, '\',\'')+'\')';
|
}
|
|
public static void InsertList(List<Sobject> so_list)
|
{
|
if(so_list == null || so_list.size() == 0)return;
|
insert so_list;
|
}
|
|
public static void UpdateList(List<Sobject> so_list)
|
{
|
if(so_list == null || so_list.size() == 0)return;
|
update so_list;
|
}
|
|
public static Map<string,Database.UpsertResult> UpsertList(List<Sobject> so_list, Schema.SObjectField field)
|
{
|
Map<string,Database.UpsertResult> mid = new Map<string,Database.UpsertResult>();
|
if(so_list == null || so_list.size() == 0)return mid;
|
List<Database.UpsertResult> urList2 = Database.upsert( so_list, field, false);
|
|
Set<String> accBasicRecordIdSet = new Set<String>();
|
integer index = 0;
|
string errorString = '';
|
string field_str = field.getDescribe().getName();
|
for(Database.UpsertResult ur : urList2){
|
if(!ur.IsSuccess()){
|
object fv = so_list[index].get(field_str);
|
|
mid.put(fv+'',ur);
|
errorString = '';
|
for(Database.Error errs: ur.getErrors()){
|
errorString += errs.getMessage() + ';';
|
}
|
//LogHelper.CreateRunTimeLog('runtime error', errorString, false, fv+'');
|
}
|
index++;
|
}
|
return mid;
|
}
|
|
public static void DeleteList(List<Sobject> so_list)
|
{
|
if(so_list == null || so_list.size() == 0)return;
|
delete so_list;
|
}
|
|
|
@future
|
public static void DeleteListAsync(List<Id> id_list)
|
{
|
if(id_list == null || id_list.size() == 0)return;
|
Database.delete(id_list);
|
}
|
|
}
|