From 52fec7c82b062a6d76e6c29e17aeb529aaa84e9b Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Wed, 18 Dec 2024 16:43:56 +0200 Subject: [PATCH] Non static native query prefix (#2152) Signed-off-by: Avgustin Marinov --- .../java/org/eclipse/hawkbit/repository/jpa/Jpa.java | 4 +++- .../java/org/eclipse/hawkbit/repository/jpa/Jpa.java | 6 ++++-- .../jpa/management/JpaControllerManagement.java | 4 ++-- .../jpa/management/JpaDeploymentManagement.java | 12 ++++++------ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/hawkbit-repository/hawkbit-repository-jpa-eclipselink/src/main/java/org/eclipse/hawkbit/repository/jpa/Jpa.java b/hawkbit-repository/hawkbit-repository-jpa-eclipselink/src/main/java/org/eclipse/hawkbit/repository/jpa/Jpa.java index 4f8a8ac0a2..da4909827a 100644 --- a/hawkbit-repository/hawkbit-repository-jpa-eclipselink/src/main/java/org/eclipse/hawkbit/repository/jpa/Jpa.java +++ b/hawkbit-repository/hawkbit-repository-jpa-eclipselink/src/main/java/org/eclipse/hawkbit/repository/jpa/Jpa.java @@ -33,7 +33,9 @@ public enum JpaVendor { log.info("JPA vendor: {}", JPA_VENDOR); } - public static final char NATIVE_QUERY_PARAMETER_PREFIX = '?'; + public static char nativeQueryParamPrefix() { + return '?'; + } public static String formatNativeQueryInClause(final String name, final Collection collection) { return formatEclipseLinkNativeQueryInClause(IntStream.range(0, collection.size()).mapToObj(i -> name + "_" + i).toList()); diff --git a/hawkbit-repository/hawkbit-repository-jpa-hibernate/src/main/java/org/eclipse/hawkbit/repository/jpa/Jpa.java b/hawkbit-repository/hawkbit-repository-jpa-hibernate/src/main/java/org/eclipse/hawkbit/repository/jpa/Jpa.java index 133ec77d53..f6f4138040 100644 --- a/hawkbit-repository/hawkbit-repository-jpa-hibernate/src/main/java/org/eclipse/hawkbit/repository/jpa/Jpa.java +++ b/hawkbit-repository/hawkbit-repository-jpa-hibernate/src/main/java/org/eclipse/hawkbit/repository/jpa/Jpa.java @@ -28,10 +28,12 @@ public enum JpaVendor { public static final JpaVendor JPA_VENDOR = JpaVendor.HIBERNATE; static { - log.info("JPA vendor: {}", JPA_VENDOR); + log.info("JPA Vendor: {}", JPA_VENDOR); } - public static final char NATIVE_QUERY_PARAMETER_PREFIX = ':'; + public static char nativeQueryParamPrefix() { + return ':'; + } public static String formatNativeQueryInClause(final String name, final Collection collection) { return ":" + name; diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaControllerManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaControllerManagement.java index cc05fdd4fd..15434c60ff 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaControllerManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaControllerManagement.java @@ -746,9 +746,9 @@ private Void updateLastTargetQueries(final String tenant, final List */ private void setLastTargetQuery(final String tenant, final long currentTimeMillis, final List chunk) { final Query updateQuery = entityManager.createNativeQuery( - "UPDATE sp_target SET last_target_query = " + Jpa.NATIVE_QUERY_PARAMETER_PREFIX + "last_target_query " + + "UPDATE sp_target SET last_target_query = " + Jpa.nativeQueryParamPrefix() + "last_target_query " + "WHERE controller_id IN (" + Jpa.formatNativeQueryInClause("cid", chunk) + ")" + - " AND tenant = " + Jpa.NATIVE_QUERY_PARAMETER_PREFIX + "tenant"); + " AND tenant = " + Jpa.nativeQueryParamPrefix() + "tenant"); updateQuery.setParameter("last_target_query", currentTimeMillis); Jpa.setNativeQueryInParameter(updateQuery, "cid", chunk); diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaDeploymentManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaDeploymentManagement.java index 801a7b33e9..e3bdbdde9b 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaDeploymentManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaDeploymentManagement.java @@ -124,9 +124,9 @@ public class JpaDeploymentManagement extends JpaActionManagement implements Depl private static final int ACTION_PAGE_LIMIT = 1000; private static final String QUERY_DELETE_ACTIONS_BY_STATE_AND_LAST_MODIFIED_DEFAULT = "DELETE FROM sp_action " + - "WHERE tenant=" + Jpa.NATIVE_QUERY_PARAMETER_PREFIX + "tenant" + + "WHERE tenant=" + Jpa.nativeQueryParamPrefix() + "tenant" + " AND status IN (%s)" + - " AND last_modified_at<" + Jpa.NATIVE_QUERY_PARAMETER_PREFIX + "last_modified_at LIMIT " + ACTION_PAGE_LIMIT; + " AND last_modified_at<" + Jpa.nativeQueryParamPrefix() + "last_modified_at LIMIT " + ACTION_PAGE_LIMIT; private static final EnumMap QUERY_DELETE_ACTIONS_BY_STATE_AND_LAST_MODIFIED; static { @@ -134,16 +134,16 @@ public class JpaDeploymentManagement extends JpaActionManagement implements Depl QUERY_DELETE_ACTIONS_BY_STATE_AND_LAST_MODIFIED.put( Database.SQL_SERVER, "DELETE TOP (" + ACTION_PAGE_LIMIT + ") FROM sp_action " + - "WHERE tenant=" + Jpa.NATIVE_QUERY_PARAMETER_PREFIX + "tenant" + + "WHERE tenant=" + Jpa.nativeQueryParamPrefix() + "tenant" + " AND status IN (%s)" + - " AND last_modified_at<" + Jpa.NATIVE_QUERY_PARAMETER_PREFIX + "last_modified_at "); + " AND last_modified_at<" + Jpa.nativeQueryParamPrefix() + "last_modified_at "); QUERY_DELETE_ACTIONS_BY_STATE_AND_LAST_MODIFIED.put( Database.POSTGRESQL, "DELETE FROM sp_action " + "WHERE id IN (SELECT id FROM sp_action " + - "WHERE tenant=" + Jpa.NATIVE_QUERY_PARAMETER_PREFIX + "tenant" + + "WHERE tenant=" + Jpa.nativeQueryParamPrefix() + "tenant" + " AND status IN (%s)" + - " AND last_modified_at<" + Jpa.NATIVE_QUERY_PARAMETER_PREFIX + "last_modified_at LIMIT " + ACTION_PAGE_LIMIT + ")"); + " AND last_modified_at<" + Jpa.nativeQueryParamPrefix() + "last_modified_at LIMIT " + ACTION_PAGE_LIMIT + ")"); } private final EntityManager entityManager;