Skip to main content
Added improved code version
Source Link
kurunve
  • 4.4k
  • 2
  • 21
  • 29

As far as I get the question, there are no such structure as WHERE 1 in apex. Based on your question, next structure may be used:

List<String> and_conditions = new List<String>();
//add some condition like/
//  and_conditions.add( ' test__c = true AND test_data__c != null ');
//  and_conditions.add(' test__c = false AND test_data__c = \'Data\' ');
String where_clause = ' ';
if (!and_conditions.isEmpty()){
    for(Integer i = 0; i < and_conditions.size(); i++){
         if(i == 0){
             where_clause = ' WHERE (' + and_conditions.get(i) + ') ';
         }else{
             where_clause +=' AND (' + and_conditions.get(i) + ') ';
         }
    }    
}

That would support only AND clause as a root clause, but that is simple enough to understand pattern, and improve it on demand.

P.S. there are some statements which are always true. Like WHERE Id != null

P.S.S. With 1.5 year more experience, code written above can be improved

List<String> and_conditions = new List<String>();
//add some condition like/
//  and_conditions.add( ' test__c = true AND test_data__c != null ');
//  and_conditions.add(' test__c = false AND test_data__c = \'Data\' ');
String where_clause = ' ';
if (!and_conditions.isEmpty()){
    where_clause = 'WHERE (' + String.join(and_conditions, ' ) AND ( ') + ')';
}

As far as I get the question, there are no such structure as WHERE 1 in apex. Based on your question, next structure may be used:

List<String> and_conditions = new List<String>();
//add some condition like/
//  and_conditions.add( ' test__c = true AND test_data__c != null ');
//  and_conditions.add(' test__c = false AND test_data__c = \'Data\' ');
String where_clause = ' ';
if (!and_conditions.isEmpty()){
    for(Integer i = 0; i < and_conditions.size(); i++){
         if(i == 0){
             where_clause = ' WHERE (' + and_conditions.get(i) + ') ';
         }else{
             where_clause +=' AND (' + and_conditions.get(i) + ') ';
         }
    }    
}

That would support only AND clause as a root clause, but that is simple enough to understand pattern, and improve it on demand.

P.S. there are some statements which are always true. Like WHERE Id != null

As far as I get the question, there are no such structure as WHERE 1 in apex. Based on your question, next structure may be used:

List<String> and_conditions = new List<String>();
//add some condition like/
//  and_conditions.add( ' test__c = true AND test_data__c != null ');
//  and_conditions.add(' test__c = false AND test_data__c = \'Data\' ');
String where_clause = ' ';
if (!and_conditions.isEmpty()){
    for(Integer i = 0; i < and_conditions.size(); i++){
         if(i == 0){
             where_clause = ' WHERE (' + and_conditions.get(i) + ') ';
         }else{
             where_clause +=' AND (' + and_conditions.get(i) + ') ';
         }
    }    
}

That would support only AND clause as a root clause, but that is simple enough to understand pattern, and improve it on demand.

P.S. there are some statements which are always true. Like WHERE Id != null

P.S.S. With 1.5 year more experience, code written above can be improved

List<String> and_conditions = new List<String>();
//add some condition like/
//  and_conditions.add( ' test__c = true AND test_data__c != null ');
//  and_conditions.add(' test__c = false AND test_data__c = \'Data\' ');
String where_clause = ' ';
if (!and_conditions.isEmpty()){
    where_clause = 'WHERE (' + String.join(and_conditions, ' ) AND ( ') + ')';
}
Source Link
kurunve
  • 4.4k
  • 2
  • 21
  • 29

As far as I get the question, there are no such structure as WHERE 1 in apex. Based on your question, next structure may be used:

List<String> and_conditions = new List<String>();
//add some condition like/
//  and_conditions.add( ' test__c = true AND test_data__c != null ');
//  and_conditions.add(' test__c = false AND test_data__c = \'Data\' ');
String where_clause = ' ';
if (!and_conditions.isEmpty()){
    for(Integer i = 0; i < and_conditions.size(); i++){
         if(i == 0){
             where_clause = ' WHERE (' + and_conditions.get(i) + ') ';
         }else{
             where_clause +=' AND (' + and_conditions.get(i) + ') ';
         }
    }    
}

That would support only AND clause as a root clause, but that is simple enough to understand pattern, and improve it on demand.

P.S. there are some statements which are always true. Like WHERE Id != null