Skip to content

Commit

Permalink
changed query to work with hib6
Browse files Browse the repository at this point in the history
  • Loading branch information
stalep committed Aug 18, 2023
1 parent 5e19312 commit 18b897b
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,30 +166,35 @@ void init() {
@Override
public DatasetService.DatasetList listByTest(int testId, String filter, Integer limit, Integer page, String sort, SortDirection direction, Integer viewId) {
StringBuilder sql = new StringBuilder("WITH schema_agg AS (")
.append(SCHEMAS_SELECT).append(" WHERE testid = ?1 GROUP BY dataset_id")
.append(SCHEMAS_SELECT).append(" WHERE testid = :testId GROUP BY dataset_id")
.append("), ").append(VALIDATION_SELECT);
JsonNode jsonFilter = null;
if (filter != null && !filter.isBlank()) {
sql.append(", all_labels AS (").append(ALL_LABELS_SELECT).append(" WHERE testid = ?1 GROUP BY dataset.id)");
sql.append(", all_labels AS (").append(ALL_LABELS_SELECT).append(" WHERE testid = :testId GROUP BY dataset.id)");
sql.append(DATASET_SUMMARY_SELECT);
addViewIdCondition(sql, viewId);
sql.append(" JOIN all_labels ON all_labels.dataset_id = ds.id WHERE testid = ?1 AND all_labels.values @> ?2");
sql.append(" JOIN all_labels ON all_labels.dataset_id = ds.id WHERE testid = :testId AND all_labels.values @> :jsonFilter");
jsonFilter = Util.parseFingerprint(filter);
} else {
sql.append(DATASET_SUMMARY_SELECT);
addViewIdCondition(sql, viewId);
sql.append(" WHERE testid = ?1 AND ?2 IS NULL");
// The line below worked for Hib5, but it causes Hib6 to complain about the param type (which is null)
// sql.append(" WHERE testid = ?1 AND ?2 IS NULL");
// This works for Hib6
sql.append(" WHERE testid = :testId");
}
addOrderAndPaging(limit, page, sort, direction, sql);
Query query = em.createNativeQuery(sql.toString())
.setParameter(1, testId);
.setParameter("testId", testId);
if (jsonFilter != null) {
query.unwrap(NativeQuery.class).setParameter(2, jsonFilter, new CustomType<>(new JsonBinaryType(), new TypeConfiguration()));
} else {
query.setParameter(2, null);
query.unwrap(NativeQuery.class).setParameter("jsonFilter", jsonFilter, new CustomType<>(new JsonBinaryType(), new TypeConfiguration()));
}
// It seems like setting a null value on a query param fails in Hib6
/*else {
query.setParameter("jsonFilter", null);
} */
if (viewId != null) {
query.setParameter(3, viewId);
query.setParameter("viewId", viewId);
}
initTypes(query);
DatasetService.DatasetList list = new DatasetService.DatasetList();
Expand All @@ -201,9 +206,9 @@ public DatasetService.DatasetList listByTest(int testId, String filter, Integer

private void addViewIdCondition(StringBuilder sql, Integer viewId) {
if (viewId == null) {
sql.append("(SELECT id FROM view WHERE test_id = ?1 AND name = 'Default')");
sql.append("(SELECT id FROM view WHERE test_id = :testId AND name = 'Default')");
} else {
sql.append("?3");
sql.append(":viewId");
}
}

Expand Down

0 comments on commit 18b897b

Please sign in to comment.