Skip to content

Commit

Permalink
Non static native query prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <[email protected]>
  • Loading branch information
avgustinmm committed Dec 18, 2024
1 parent ed93d3f commit 6557e0b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> String formatNativeQueryInClause(final String name, final Collection<T> collection) {
return formatEclipseLinkNativeQueryInClause(IntStream.range(0, collection.size()).mapToObj(i -> name + "_" + i).toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> String formatNativeQueryInClause(final String name, final Collection<T> collection) {
return ":" + name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,9 @@ private Void updateLastTargetQueries(final String tenant, final List<TargetPoll>
*/
private void setLastTargetQuery(final String tenant, final long currentTimeMillis, final List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,26 @@ 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<Database, String> QUERY_DELETE_ACTIONS_BY_STATE_AND_LAST_MODIFIED;

static {
QUERY_DELETE_ACTIONS_BY_STATE_AND_LAST_MODIFIED = new EnumMap<>(Database.class);
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;
Expand Down

0 comments on commit 6557e0b

Please sign in to comment.