Skip to content

Commit

Permalink
Added error log on generic SQL Request Util class. #2251
Browse files Browse the repository at this point in the history
  • Loading branch information
vertigo17 committed Dec 14, 2023
1 parent 8a991df commit 87d473a
Showing 1 changed file with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ public interface VoidSqlFunction<T> {
}

public static <T> T executeQuery(DatabaseSpring databaseSpring, String query, VoidSqlFunction<PreparedStatement> functionPrepareStatement,
SqlFunction<ResultSet, T> functionResultSet) throws CerberusException {
SqlFunction<ResultSet, T> functionResultSet) throws CerberusException {

LOG.debug(SQL_DEBUG, query);

try (Connection connection = databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
try (Connection connection = databaseSpring.connect(); PreparedStatement preStat = connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
functionPrepareStatement.apply(preStat);

try (ResultSet resultSet = preStat.executeQuery()) {
Expand All @@ -67,7 +66,7 @@ public static <T> T executeQuery(DatabaseSpring databaseSpring, String query, Vo
}
}
} catch (SQLException exception) {
LOG.debug(exception);
LOG.error("Unable to execute query : " + exception.toString());
throw new CerberusException(new MessageGeneral(MessageGeneralEnum.DATA_OPERATION_ERROR), exception);
}

Expand All @@ -78,8 +77,7 @@ public static <T> T executeUpdate(DatabaseSpring databaseSpring, String query, V

LOG.debug(SQL_DEBUG, query);

try (Connection connection = databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query)) {
try (Connection connection = databaseSpring.connect(); PreparedStatement preStat = connection.prepareStatement(query)) {
functionPrepareStatement.apply(preStat);
preStat.executeUpdate();
} catch (SQLException exception) {
Expand All @@ -88,6 +86,7 @@ public static <T> T executeUpdate(DatabaseSpring databaseSpring, String query, V
message.setDescription(message.getDescription().replace("%ITEM%", query).replace("%OPERATION%", "INSERT").replace("%REASON%", exception.toString()));
throw new CerberusException(new MessageGeneral(MessageGeneralEnum.DATA_OPERATION_ERROR_DUPLICATE), exception);
} else {
LOG.error("Unable to execute query : " + exception.toString());
throw new CerberusException(new MessageGeneral(MessageGeneralEnum.DATA_OPERATION_ERROR_WITH_REQUEST).resolveDescription("REQUEST", query), exception);
}
}
Expand All @@ -96,14 +95,13 @@ public static <T> T executeUpdate(DatabaseSpring databaseSpring, String query, V
}

public static <T> List<T> executeQueryList(DatabaseSpring databaseSpring, String query, VoidSqlFunction<PreparedStatement> functionPrepareStatement,
SqlFunction<ResultSet, T> functionResultSet) throws CerberusException {
SqlFunction<ResultSet, T> functionResultSet) throws CerberusException {

LOG.debug(SQL_DEBUG, query);

List<T> res = new LinkedList<>();

try (Connection connection = databaseSpring.connect();
PreparedStatement preStat = connection.prepareStatement(query)) {
try (Connection connection = databaseSpring.connect(); PreparedStatement preStat = connection.prepareStatement(query)) {
functionPrepareStatement.apply(preStat);

try (ResultSet resultSet = preStat.executeQuery()) {
Expand All @@ -112,6 +110,7 @@ public static <T> List<T> executeQueryList(DatabaseSpring databaseSpring, String
}
}
} catch (SQLException exception) {
LOG.error("Unable to execute query : " + exception.toString());
throw new CerberusException(new MessageGeneral(MessageGeneralEnum.DATA_OPERATION_ERROR_WITH_REQUEST).resolveDescription("REQUEST", query), exception);
}

Expand Down

0 comments on commit 87d473a

Please sign in to comment.