Program: 143621
Project: 143621
Buggy source:
QuerySelect getPKSelectSqlSelect(IGlobalValueEntry provider,Table table,QuerySelect oldSQLQuery,List<IRecordInternal> findStates,boolean reduce,IDataSet omitPKs,List<SortColumn> orderByFields,boolean removeUnusedJoins) throws ServoyException {
  if (table == null) {
    throw new RepositoryException(ServoyException.InternalCodes.TABLE_NOT_FOUND);
  }
  QuerySelect retval;
  if (oldSQLQuery != null) {
    retval=AbstractBaseQuery.deepClone(oldSQLQuery);
    retval.setGroupBy(null);
    if (orderByFields != null)     retval.clearSorts();
    for (    String conditionName : retval.getConditionNames()) {
      if (conditionName.startsWith(SERVOY_CONDITION_PREFIX) && !(CONDITION_FILTER.equals(conditionName) || CONDITION_SEARCH.equals(conditionName) || CONDITION_RELATION.equals(conditionName))) {
        retval.setCondition(conditionName,null);
      }
    }
  }
 else {
    retval=new QuerySelect(new QueryTable(table.getSQLName(),table.getCatalog(),table.getSchema()));
  }
  ArrayList<IQuerySelectValue> pkQueryColumns=new ArrayList<IQuerySelectValue>(3);
  ArrayList<Column> pkColumns=new ArrayList<Column>(3);
  Iterator<Column> pks=table.getRowIdentColumns().iterator();
  if (!pks.hasNext()) {
    throw new RepositoryException(ServoyException.InternalCodes.PRIMARY_KEY_NOT_FOUND,new Object[]{table.getName()});
  }
  while (pks.hasNext()) {
    Column column=pks.next();
    pkColumns.add(column);
    pkQueryColumns.add(new QueryColumn(retval.getTable(),column.getID(),column.getSQLName(),column.getType(),column.getLength()));
  }
  retval.setColumns(pkQueryColumns);
  if (omitPKs != null && omitPKs.getRowCount() != 0) {
    retval.setCondition(CONDITION_OMIT,createSetConditionFromPKs(ISQLCondition.NOT_OPERATOR,pkQueryColumns.toArray(new QueryColumn[pkQueryColumns.size()]),pkColumns,omitPKs));
  }
 else   if (oldSQLQuery != null) {
    retval.setCondition(CONDITION_OMIT,oldSQLQuery.getConditionClone(CONDITION_OMIT));
  }
  if (findStates != null && findStates.size() != 0) {
    ISQLCondition moreWhere=null;
    for (    IRecordInternal obj : findStates) {
      if (obj instanceof FindState) {
        moreWhere=OrCondition.or(moreWhere,createConditionFromFindState((FindState)obj,retval,provider,pkQueryColumns));
      }
    }
    if (moreWhere != null) {
      if (reduce) {
        retval.addCondition(CONDITION_SEARCH,moreWhere);
      }
 else {
        retval.addConditionOr(CONDITION_SEARCH,moreWhere);
      }
      if (retval.getJoins() != null) {
        final boolean[] hasOr={false};
        retval.getCondition(CONDITION_SEARCH).acceptVisitor(new IVisitor(){
          public Object visit(          Object o){
            if (o instanceof OrCondition && ((OrCondition)o).getConditions().size() > 1) {
              hasOr[0]=true;
              return new VistorResult(o,false);
            }
            return o;
          }
        }
);
        if (hasOr[0]) {
          for (          ISQLJoin join : retval.getJoins()) {
            if (join instanceof QueryJoin && ((QueryJoin)join).getJoinType() == ISQLJoin.INNER_JOIN) {
              ((QueryJoin)join).setJoinType(ISQLJoin.LEFT_OUTER_JOIN);
            }
          }
        }
      }
    }
  }
  if (orderByFields != null || retval.getSorts() == null) {
    List<SortColumn> orderBy=orderByFields == null ? new ArrayList<SortColumn>(3) : orderByFields;
    if (orderBy.size() == 0) {
      for (int i=0; i < pkColumns.size(); i++) {
        orderBy.add(new SortColumn(pkColumns.get(i)));
      }
    }
    addSorts(retval,retval.getTable(),provider,table,orderBy,true);
  }
  if (removeUnusedJoins) {
    retval.removeUnusedJoins(false);
  }
  if (retval.getJoins() != null && retval.getColumns().size() == 1 && isDistinctAllowed(retval.getColumns(),retval.getSorts())) {
    retval.setDistinct(true);
  }
 else   if (retval.getJoins() == null && retval.getColumns().size() == pkColumns.size()) {
    retval.setPlainPKSelect(true);
  }
  return retval;
}

Fixed version (ground truth):
QuerySelect getPKSelectSqlSelect(IGlobalValueEntry provider,Table table,QuerySelect oldSQLQuery,List<IRecordInternal> findStates,boolean reduce,IDataSet omitPKs,List<SortColumn> orderByFields,boolean removeUnusedJoins) throws ServoyException {
  if (table == null) {
    throw new RepositoryException(ServoyException.InternalCodes.TABLE_NOT_FOUND);
  }
  QuerySelect retval;
  if (oldSQLQuery != null) {
    retval=AbstractBaseQuery.deepClone(oldSQLQuery);
    retval.setGroupBy(null);
    if (orderByFields != null)     retval.clearSorts();
    for (    String conditionName : retval.getConditionNames()) {
      if (conditionName.startsWith(SERVOY_CONDITION_PREFIX) && !(CONDITION_FILTER.equals(conditionName) || CONDITION_SEARCH.equals(conditionName) || CONDITION_RELATION.equals(conditionName))) {
        retval.setCondition(conditionName,null);
      }
    }
  }
 else {
    retval=new QuerySelect(new QueryTable(table.getSQLName(),table.getCatalog(),table.getSchema()));
  }
  ArrayList<IQuerySelectValue> pkQueryColumns=new ArrayList<IQuerySelectValue>(3);
  ArrayList<Column> pkColumns=new ArrayList<Column>(3);
  Iterator<Column> pks=table.getRowIdentColumns().iterator();
  if (!pks.hasNext()) {
    throw new RepositoryException(ServoyException.InternalCodes.PRIMARY_KEY_NOT_FOUND,new Object[]{table.getName()});
  }
  while (pks.hasNext()) {
    Column column=pks.next();
    pkColumns.add(column);
    pkQueryColumns.add(new QueryColumn(retval.getTable(),column.getID(),column.getSQLName(),column.getType(),column.getLength()));
  }
  retval.setColumns(pkQueryColumns);
  if (omitPKs != null && omitPKs.getRowCount() != 0) {
    retval.setCondition(CONDITION_OMIT,createSetConditionFromPKs(ISQLCondition.NOT_OPERATOR,pkQueryColumns.toArray(new QueryColumn[pkQueryColumns.size()]),pkColumns,omitPKs));
  }
 else   if (oldSQLQuery != null) {
    retval.setCondition(CONDITION_OMIT,oldSQLQuery.getConditionClone(CONDITION_OMIT));
  }
  if (findStates != null && findStates.size() != 0) {
    ISQLCondition moreWhere=null;
    for (    IRecordInternal obj : findStates) {
      if (obj instanceof FindState) {
        moreWhere=OrCondition.or(moreWhere,createConditionFromFindState((FindState)obj,retval,provider,pkQueryColumns));
      }
    }
    if (moreWhere != null) {
      if (reduce) {
        retval.addCondition(CONDITION_SEARCH,moreWhere);
      }
 else {
        retval.addConditionOr(CONDITION_SEARCH,moreWhere);
      }
      if (retval.getJoins() != null) {
        final boolean[] hasOr={false};
        retval.getCondition(CONDITION_SEARCH).acceptVisitor(new IVisitor(){
          public Object visit(          Object o){
            if (o instanceof OrCondition && ((OrCondition)o).getConditions().size() > 1) {
              hasOr[0]=true;
              return new VistorResult(o,false);
            }
            return o;
          }
        }
);
        if (hasOr[0]) {
          for (          ISQLJoin join : retval.getJoins()) {
            if (join instanceof QueryJoin && ((QueryJoin)join).getJoinType() == ISQLJoin.INNER_JOIN) {
              ((QueryJoin)join).setJoinType(ISQLJoin.LEFT_OUTER_JOIN);
            }
          }
        }
      }
    }
  }
  if (orderByFields != null || retval.getSorts() == null) {
    List<SortColumn> orderBy=orderByFields == null ? new ArrayList<SortColumn>(3) : orderByFields;
    if (orderBy.size() == 0) {
      for (int i=0; i < pkColumns.size(); i++) {
        orderBy.add(new SortColumn(pkColumns.get(i)));
      }
    }
    addSorts(retval,retval.getTable(),provider,table,orderBy,true);
  }
  if (removeUnusedJoins) {
    retval.removeUnusedJoins(false);
  }
  retval.setDistinct(false);
  retval.setPlainPKSelect(false);
  if (retval.getJoins() != null && retval.getColumns().size() == 1 && isDistinctAllowed(retval.getColumns(),retval.getSorts())) {
    retval.setDistinct(true);
  }
 else   if (retval.getJoins() == null && retval.getColumns().size() == pkColumns.size()) {
    retval.setPlainPKSelect(true);
  }
  return retval;
}