title: Dynamically building conditions in JOOQ
body: <p>I have an endpoint that receives a string with all the columns and value they want to filter by. Here's an example of what the filters param looks like: <code>?filter=active=true,type=ADMIN</code>, basically each filter is separated by a <code>,</code> and then the column name and filter value are separated by an <code>=</code> So I made this method to dynamically build a condition statement with all the different filters for any table:</p>
  
  <pre><code>public Condition mapFilterToCondition(String tableName, String filters) {
      Condition condition = DSL.trueCondition();
      String[] filterFields = filters.split(",");
      Table&lt;?&gt; table = PUBLIC.getTable(tableName);
      Field&lt;?&gt;[] fields = table.fields();
      for(String filterField : filterFields) {
          String[] filter = filterField.split("=");
          for(Field&lt;?&gt; field : fields) {
              String fieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, filter[0]);
              if(field.getName().equals(fieldName)) {
                  if(field.getType() == String.class) {
                      condition.and(table.field(field).like(filter[1]));
                  } else if (Boolean.valueOf(filter[1])) {
                      condition.and(table.field(field).isTrue());
                  } else if (!Boolean.valueOf(filter[1])) {
                      condition.and(table.field(field).isFalse());
                  }
              }
          }
      }
      return condition;
  }
  </code></pre>
  
  <p>I then use the condition built by this method to call a repository that makes the sql request:</p>
  
  <pre><code>public List&lt;UserAccount&gt; findAllByFilter(Condition condition, Pageable pageable) {
      return dsl.select()
              .from(USER_ACCOUNT)
              .where(condition)
              .limit(pageable.getPageSize())
              .offset((int) pageable.getOffset())
              .fetchInto(UserAccount.class);
  }
  </code></pre>
  
  <p>As I understood, the condition should have something like this:</p>
  
  <p><code>.where(USER_ACCOUNT.TYPE.like("ADMIN"))</code>
  <code>.where(USER_ACCOUNT.ACTIVE.isTrue())</code></p>
  
  <p>based on the condition I built, but whenever I make the call, it returns every single user, not taking into account the filters. While debugging I found out the condition is never changing from <code>1 = 1</code>, which is the value it has from <code>DSL.trueCondition();</code>, so my question is how I should be handling the construction of this condition? Am I using <code>condition.and(...)</code> wrong, or should I be using something else instead? </p>
  
  <p>Any help is appreciated! </p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>@GetMapping(value = "/admin", produces = "application/json")
  public ResponseEntity listAll(String filters, Pageable pageable) {
      Condition condition = filterMapService.mapFilterToCondition("user_account", filters);
      List&lt;UserAccount&gt; adminAccounts = userAccountRepository.findAllByFilter(condition, pageable);
      if (adminAccounts.isEmpty()) {
          return new ResponseEntity(HttpStatus.NO_CONTENT);
      }
      List&lt;AdminDTO&gt; accounts = adminAccounts.stream()
              .map(account -&gt; modelMapper.map(account, AdminDTO.class)).collect(Collectors.toList());
      return new ResponseEntity&lt;&gt;(accounts, HttpStatus.OK);
  }
  </code></pre>