// filters on measures
private List<MeasureCriterion> measureCriteria = Lists.newLinkedList();
+ private int periodIndex = 0;
// sorting
private Integer sortedMetricId;
- private int sortedVariationIndex = 0; // >0 only when sortedMetricId not null
+ private boolean sortedByMeasureVariation = false;
private boolean sortedByLanguage;
private boolean sortedByName;
private boolean sortedByDate;
}
public Filter setSortedMetricId(Integer id) {
- return setSortedMetricId(id, true);
+ return setSortedMetricId(id, true, false);
}
- public Filter setSortedMetricId(Integer id, boolean useValue) {
+ public Filter setSortedMetricId(Integer id, boolean isNumericValue, boolean isVariation) {
unsetSorts();
this.sortedMetricId = id;
- this.useMeasureValueToSort = useValue;
+ this.useMeasureValueToSort = isNumericValue;
+ this.sortedByMeasureVariation = isVariation;
return this;
}
return this;
}
- public int getSortedVariationIndex() {
- return sortedVariationIndex;
+ public int getPeriodIndex() {
+ return periodIndex;
}
- public Filter setSortedVariationIndex(int i) {
- this.sortedVariationIndex = i;
- return this;
+ public void setPeriodIndex(int i) {
+ this.periodIndex = i;
+ }
+
+ public boolean isOnPeriod() {
+ return periodIndex>0;
+ }
+
+ public void setSortedByMeasureVariation(boolean b) {
+ this.sortedByMeasureVariation = b;
}
- static String getVariationColumn(int variationIndex) {
- switch (variationIndex) {
+ static String getVariationColumn(int periodIndex) {
+ switch (periodIndex) {
case 1:
return "variation_value_1";
case 2:
String getColumnToSort() {
String col = "text_value";
if (useMeasureValueToSort()) {
- col = (sortedVariationIndex>0 ? getVariationColumn (sortedVariationIndex) : "value");
+ col = (sortedByMeasureVariation ? getVariationColumn (periodIndex) : "value");
}
return col;
}
+ public boolean mustReturnEmptyResult() {
+ boolean hasCriterionOnVariation = false;
+ for (MeasureCriterion criterion : measureCriteria) {
+ if (criterion.isVariation()) {
+ hasCriterionOnVariation = true;
+ }
+ }
+ return (hasCriterionOnVariation && !isOnPeriod());
+ }
+
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
import org.sonar.api.utils.TimeProfiler;
import javax.persistence.Query;
+import java.util.Collections;
public class FilterExecutor implements ServerComponent {
private static final Logger LOG = LoggerFactory.getLogger(FilterExecutor.class);
}
public FilterResult execute(Filter filter) {
+ if (filter.mustReturnEmptyResult()) {
+ return new FilterResult(filter, Collections.emptyList());
+ }
+
String sql = null;
try {
TimeProfiler profiler = new TimeProfiler(FilterExecutor.class).start("Build/execute SQL query");
}
for (int index = 0; index < filter.getMeasureCriteria().size(); index++) {
MeasureCriterion criterion = filter.getMeasureCriteria().get(index);
+ String column = (criterion.isVariation() ? Filter.getVariationColumn(filter.getPeriodIndex()) : "value");
sql.append(", MAX(CASE WHEN pm.metric_id=");
sql.append(criterion.getMetricId());
- sql.append(" AND pm.value");
+ sql.append(" AND pm.");
+ sql.append(column);
sql.append(criterion.getOperator());
sql.append(criterion.getValue());
- sql.append(" THEN value ELSE NULL END) AS crit_");
+ sql.append(" THEN ");
+ sql.append(column);
+ sql.append(" ELSE NULL END) AS crit_");
sql.append(index);
sql.append(" ");
}
if (index > 0) {
sql.append(" OR ");
}
- MeasureCriterion criteria = filter.getMeasureCriteria().get(index);
- sql.append("(pm.metric_id=").append(criteria.getMetricId()).append(" and pm.value")
- .append(criteria.getOperator()).append(criteria.getValue()).append(")");
+ MeasureCriterion criterion = filter.getMeasureCriteria().get(index);
+ String column = (criterion.isVariation() ? Filter.getVariationColumn(filter.getPeriodIndex()) : "value");
+ sql.append("(pm.metric_id=").append(criterion.getMetricId()).append(" and pm.").append(column)
+ .append(criterion.getOperator()).append(criterion.getValue()).append(")");
index++;
}