private QueryRunnerIntermediaryResult prepareResults( QueryRunnerIntermediaryResult previousThreadResults, List<List<String>> currentBatchResults) throws TechnicalException { QueryRunnerIntermediaryResult batchResult = new QueryRunnerIntermediaryResult( previousThreadResults, this.joinFieldIndexesWithinResults); // Tell next thread what field to use for the join int totalResultRows = 0; if (this.first) { for (List<String> list : currentBatchResults) { check(this.isLinkIndex || list.size() == this.totalFields); List<String> row = new ArrayList<String>(list); if (this.distinct && batchResult.containsRow(row)) { continue; } batchResult.addValueRow(row); totalResultRows++; } } else { List<Integer> previousJoinFieldIndexesWithinResults = previousThreadResults.getPreviousJoinFieldIndexesWithinResults(); boolean oneFieldJoin = previousJoinFieldIndexesWithinResults.size() == 1; int max = -1; if (QueryRunnerPrototypeConstants.CHECK) max = Collections.max(previousJoinFieldIndexesWithinResults); for (int rowNumber = 0; rowNumber < previousThreadResults.getTotalRows(); rowNumber++) { List<String> previousRow = previousThreadResults.getValueRow(rowNumber); check(previousRow.size() > max); String previousJoinFieldValue = null; List<String> previousJoinFieldValues = null; if (oneFieldJoin) { int index = previousJoinFieldIndexesWithinResults.get(0); previousJoinFieldValue = previousRow.get(index); } else { previousJoinFieldValues = new ArrayList<String>(); for (int index : previousJoinFieldIndexesWithinResults) { previousJoinFieldValues.add(previousRow.get(index)); } } for (List<String> list : currentBatchResults) { String leftJoinFieldValue = null; List<String> leftJoinFieldValues = null; if (oneFieldJoin) { leftJoinFieldValue = list.get(0); } else { leftJoinFieldValues = new ArrayList<String>(); for (int index = 0; index < this.leftJoinFieldFullNames.size(); index++) { // Since these are the first ones leftJoinFieldValues.add(list.get(index)); } } if ((oneFieldJoin && QueryRunnerPrototypeUtils.stringEquals( previousJoinFieldValue, leftJoinFieldValue)) || (!oneFieldJoin && QueryRunnerPrototypeUtils.stringListEquals( previousJoinFieldValues, leftJoinFieldValues))) { // nulls are considered (not a match if any of the string is null) // same size for the list is a input requirement (as well as order) List<String> row = new ArrayList<String>(previousRow); row.addAll(list); if (this.distinct && batchResult.containsRow(row)) { continue; } batchResult.addValueRow(row); totalResultRows++; } } } } return batchResult; }
private boolean customizeQuery( QueryRunnerIntermediaryResult previousThreadResults, StringBuffer currentQuery) { boolean potentialResults = true; List<Integer> previousJoinFieldIndexesWithinResults = previousThreadResults.getPreviousJoinFieldIndexesWithinResults(); check(previousJoinFieldIndexesWithinResults.size() == this.leftJoinFieldFullNames.size()); if (previousThreadResults.getTotalRows() > 0) { // Use an IN list if only 1 field (more efficient) StringBuffer stringBuffer = new StringBuffer(); boolean atLeastOne = false; if (this.leftJoinFieldFullNames.size() == 1) { // Just checked only 1 String column = this.aliasedLeftJoinFieldFullNames.get(0); int index = previousJoinFieldIndexesWithinResults.get(0); stringBuffer.append(" where " + column + " in ("); Set<String> setValues = new HashSet<String>(); for (int rowNumber = 0; rowNumber < previousThreadResults.getTotalRows(); rowNumber++) { List<String> row = previousThreadResults.getValueRow(rowNumber); String value = row.get(index); if (null != value && !setValues.contains( value)) { // Use of set to avoid doubles (useless in the IN list) setValues.add(value); stringBuffer.append((rowNumber == 0 ? "" : ",") + "'" + value + "'"); } } stringBuffer.append(")"); atLeastOne = !setValues.isEmpty(); // When only nulls } else { check(this.leftJoinFieldFullNames.size() == previousJoinFieldIndexesWithinResults.size()); for (int rowNumber = 0; rowNumber < previousThreadResults.getTotalRows(); rowNumber++) { List<String> row = previousThreadResults.getValueRow(rowNumber); StringBuffer stringBuffer2 = new StringBuffer(); for (int i = 0; i < this.aliasedLeftJoinFieldFullNames.size(); i++) { int index = previousJoinFieldIndexesWithinResults.get( i); // because we know both list are the same size String value = row.get(index); if (null != value) { stringBuffer2.append( (i == 0 ? "" : " and ") + this.aliasedLeftJoinFieldFullNames.get(i) + "=" + "'" + value + "'"); } else { // erase and break stringBuffer2 = null; break; } } if (null != stringBuffer2) { stringBuffer.append( (!atLeastOne ? " where " : " or ") + // first one is where "(" + stringBuffer2 + ")"); atLeastOne = true; } } } currentQuery.append(stringBuffer.toString()); if (!atLeastOne) { potentialResults = false; } } else { potentialResults = false; // if so, no results } return potentialResults; }