From 18b897b7b247b34b7f0b3cd487d35f4f56b15a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A5le=20Pedersen?= Date: Fri, 18 Aug 2023 10:50:11 +0200 Subject: [PATCH] changed query to work with hib6 --- .../tools/horreum/svc/DatasetServiceImpl.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/horreum-backend/src/main/java/io/hyperfoil/tools/horreum/svc/DatasetServiceImpl.java b/horreum-backend/src/main/java/io/hyperfoil/tools/horreum/svc/DatasetServiceImpl.java index 935319ad7..680fe9f04 100644 --- a/horreum-backend/src/main/java/io/hyperfoil/tools/horreum/svc/DatasetServiceImpl.java +++ b/horreum-backend/src/main/java/io/hyperfoil/tools/horreum/svc/DatasetServiceImpl.java @@ -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(); @@ -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"); } }