From e06928f293d837262e5a805feb070e4fcabba587 Mon Sep 17 00:00:00 2001 From: Etienne LESOT Date: Thu, 17 Oct 2024 16:03:43 +0200 Subject: [PATCH] fix Signed-off-by: Etienne LESOT --- .../migration/ModificationOrderMigration.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java b/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java index e237cde66..e5c890a86 100644 --- a/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java +++ b/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java @@ -12,6 +12,7 @@ import liquibase.statement.core.UpdateStatement; import org.apache.commons.lang3.tuple.Pair; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -28,8 +29,8 @@ public class ModificationOrderMigration implements CustomSqlChange { public SqlStatement[] generateStatements(Database database) throws CustomChangeException { JdbcConnection connection = (JdbcConnection) database.getConnection(); List statements = new ArrayList<>(); - try { - ResultSet groupIds = connection.createStatement().executeQuery("select distinct group_id from modification where group_id is not null"); + try (PreparedStatement stmt = connection.prepareStatement("select distinct group_id from modification where group_id is not null")) { + ResultSet groupIds = stmt.executeQuery(); while (groupIds.next()) { UUID groupId = UUID.fromString(groupIds.getString(1)); reorderNetworkModifications(groupId, true, connection, statements, database); @@ -41,7 +42,7 @@ public SqlStatement[] generateStatements(Database database) throws CustomChangeE return statements.toArray(new SqlStatement[0]); } - private void reorderNetworkModifications(UUID groupId, boolean stashed, JdbcConnection connection, List statements, Database database) throws SQLException, DatabaseException { + private void reorderNetworkModifications(UUID groupId, boolean stashed, JdbcConnection connection, List statements, Database database) throws CustomChangeException { List entities = findAllByGroupId(groupId, stashed, connection); List> entitiesToUpdate = new ArrayList<>(); if (!entities.isEmpty()) { @@ -56,13 +57,19 @@ private void reorderNetworkModifications(UUID groupId, boolean stashed, JdbcConn createMigrationRequests(entitiesToUpdate, statements, database); } - private List findAllByGroupId(UUID groupId, boolean stashed, JdbcConnection connection) throws DatabaseException, SQLException { - ResultSet resultSet = connection.createStatement().executeQuery(String.format("SELECT id FROM modification m WHERE m.group_id = '%s' AND m.stashed = %b order by modifications_order", groupId, stashed)); - List entities = new ArrayList<>(); - while (resultSet.next()) { - entities.add(UUID.fromString(resultSet.getString(1))); + private List findAllByGroupId(UUID groupId, boolean stashed, JdbcConnection connection) throws CustomChangeException { + try (PreparedStatement stmt = connection.prepareStatement("SELECT id FROM modification m WHERE m.group_id = '?' AND m.stashed = ? order by modifications_order")) { + stmt.setString(1, groupId.toString()); + stmt.setBoolean(2, stashed); + ResultSet resultSet = stmt.executeQuery(); + List entities = new ArrayList<>(); + while (resultSet.next()) { + entities.add(UUID.fromString(resultSet.getString(1))); + } + return entities; + } catch (SQLException | DatabaseException e) { + throw new CustomChangeException(e); } - return entities; } private void createMigrationRequests(List> entities, List statements, Database database) { @@ -73,7 +80,7 @@ private void createMigrationRequests(List> entities, List