import javax.annotation.Nullable;
-import java.sql.*;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
import java.util.Comparator;
+import java.util.Date;
import java.util.List;
-import static org.sonar.api.utils.DateUtils.dateToLong;
-
class MeasureFilterSql {
private final Database database;
private final MeasureFilter filter;
private final MeasureFilterContext context;
private final String sql;
- private final List<Date> dateParameters = Lists.newArrayList();
+ private final List<Long> dateParameters = Lists.newArrayList();
MeasureFilterSql(Database database, MeasureFilter filter, MeasureFilterContext context) {
this.database = database;
this.sql = generateSql();
}
+ private static void appendInStatement(List<String> values, StringBuilder to) {
+ to.append(" (");
+ for (int i = 0; i < values.size(); i++) {
+ if (i > 0) {
+ to.append(",");
+ }
+ to.append("'");
+ to.append(StringEscapeUtils.escapeSql(values.get(i)));
+ to.append("'");
+ }
+ to.append(") ");
+ }
+
+ private static Ordering newObjectOrdering(boolean ascending) {
+ if (ascending) {
+ return Ordering.from(new Comparator<Comparable>() {
+ @Override
+ public int compare(@Nullable Comparable left, @Nullable Comparable right) {
+ if (left == null) {
+ return 1;
+ }
+ if (right == null) {
+ return -1;
+ }
+
+ return left.compareTo(right);
+ }
+ });
+ }
+ return Ordering.from(new Comparator<Comparable>() {
+ @Override
+ public int compare(@Nullable Comparable left, @Nullable Comparable right) {
+ if (left == null) {
+ return 1;
+ }
+ if (right == null) {
+ return -1;
+ }
+
+ return -left.compareTo(right);
+ }
+ });
+ }
+
List<MeasureFilterRow> execute(Connection connection) throws SQLException {
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet rs = null;
try {
for (int index = 0; index < dateParameters.size(); index++) {
- statement.setLong(index + 1, dateToLong(dateParameters.get(index)));
+ statement.setLong(index + 1, dateParameters.get(index));
}
rs = statement.executeQuery();
return process(rs);
}
private void appendDateConditions(StringBuilder sb) {
- java.util.Date fromDate = filter.getFromDate();
+ Date fromDate = filter.getFromDate();
if (fromDate != null) {
sb.append(" AND s.created_at >= ? ");
- dateParameters.add(new Date(fromDate.getTime()));
+ dateParameters.add(fromDate.getTime());
}
- java.util.Date toDate = filter.getToDate();
+ Date toDate = filter.getToDate();
if (toDate != null) {
sb.append(" AND s.created_at <= ? ");
- dateParameters.add(new Date(toDate.getTime()));
+ dateParameters.add(toDate.getTime());
}
}
return rowProcessor.sort(rows, filter.sort().isAsc());
}
- private static void appendInStatement(List<String> values, StringBuilder to) {
- to.append(" (");
- for (int i = 0; i < values.size(); i++) {
- if (i > 0) {
- to.append(",");
- }
- to.append("'");
- to.append(StringEscapeUtils.escapeSql(values.get(i)));
- to.append("'");
- }
- to.append(") ");
- }
-
/**
* Replace escape percent and underscore by adding a slash just before
*/
return newObjectOrdering(ascending);
}
}
-
- private static Ordering newObjectOrdering(boolean ascending) {
- if (ascending) {
- return Ordering.from(new Comparator<Comparable>() {
- @Override
- public int compare(@Nullable Comparable left, @Nullable Comparable right) {
- if (left == null) {
- return 1;
- }
- if (right == null) {
- return -1;
- }
-
- return left.compareTo(right);
- }
- });
- }
- return Ordering.from(new Comparator<Comparable>() {
- @Override
- public int compare(@Nullable Comparable left, @Nullable Comparable right) {
- if (left == null) {
- return 1;
- }
- if (right == null) {
- return -1;
- }
-
- return -left.compareTo(right);
- }
- });
- }
}