Skip to content

Commit 5d0a594

Browse files
author
Johannes Bao
committed
Fixing sonarqube
1 parent f62a10d commit 5d0a594

File tree

5 files changed

+33
-37
lines changed

5 files changed

+33
-37
lines changed

src/main/java/edu/ie3/datamodel/io/SqlUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class SqlUtils {
1313

1414
protected static final Logger log = LoggerFactory.getLogger(SqlUtils.class);
1515
private static final String endQueryCreateTable =
16-
")\n" + "\t WITHOUT OIDS\n" + "\t TABLESPACE pg_default;";
16+
")\n \t WITHOUT OIDS\n \t TABLESPACE pg_default;";
1717

1818
private SqlUtils() {
1919
throw new IllegalStateException("Utility classes cannot be instantiated");
@@ -36,7 +36,7 @@ public static String queryCreateGridTable(String schemaName) {
3636
* @return input with quoteSymbol
3737
*/
3838
public static String quote(String input, String quoteSymbol) {
39-
if (Objects.equals(input, "")) {
39+
if (Objects.equals(input, "") || Objects.equals(input, "null")) {
4040
return "NULL";
4141
} else {
4242
return input.matches("^\".*\"$") ? input : quoteSymbol + input + quoteSymbol;

src/main/java/edu/ie3/datamodel/io/connectors/SqlConnector.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,16 @@ public ResultSet executeQuery(Statement stmt, String query) throws SQLException
6161
/**
6262
* Executes an update query
6363
*
64-
* @param updateQuery the query to execute
64+
* @param query the query to execute
6565
* @return The number of updates or a negative number if the execution failed
6666
*/
67-
public int executeUpdate(String updateQuery) throws SQLException {
68-
return getConnection().createStatement().executeUpdate(updateQuery);
67+
public int executeUpdate(String query) throws SQLException {
68+
try (Statement statement = getConnection().createStatement()) {
69+
return statement.executeUpdate(query);
70+
} catch (SQLException e) {
71+
throw new SQLException(
72+
String.format("Error at execution of query, SQLReason: '%s'", e.getMessage()), e);
73+
}
6974
}
7075

7176
/**
@@ -162,17 +167,21 @@ public Map<String, String> extractFieldMap(ResultSet rs) {
162167
* @param tableName Name of the table, that should be checked
163168
* @return True, if the table exists
164169
*/
165-
public boolean tableExistsSQL(String tableName) throws SQLException {
166-
PreparedStatement preparedStatement =
167-
connection.prepareStatement(
168-
"SELECT count(*) "
169-
+ "FROM information_schema.tables "
170-
+ "WHERE table_name = ?"
171-
+ "LIMIT 1;");
172-
preparedStatement.setString(1, tableName);
170+
public boolean tableExistsSQL(String tableName) {
171+
String query =
172+
"SELECT count(*) "
173+
+ "FROM information_schema.tables "
174+
+ "WHERE table_name = ?"
175+
+ "LIMIT 1;";
176+
try (PreparedStatement ps = getConnection().prepareStatement(query)) {
177+
ps.setString(1, tableName);
173178

174-
ResultSet resultSet = preparedStatement.executeQuery();
175-
resultSet.next();
176-
return resultSet.getInt(1) != 0;
179+
ResultSet resultSet = ps.executeQuery();
180+
resultSet.next();
181+
return resultSet.getInt(1) != 0;
182+
} catch (SQLException e) {
183+
log.error("Error during execution of query {}", query, e);
184+
}
185+
return false;
177186
}
178187
}

src/main/java/edu/ie3/datamodel/io/sink/SqlSink.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import edu.ie3.datamodel.models.timeseries.TimeSeriesEntry;
3434
import edu.ie3.datamodel.models.value.Value;
3535
import edu.ie3.util.StringUtils;
36-
import java.io.IOException;
3736
import java.sql.SQLException;
3837
import java.util.*;
3938
import java.util.stream.Collectors;
@@ -105,9 +104,7 @@ public <C extends Entity> void persistAll(Collection<C> entities, DbGridMetadata
105104
// Persist the entities in hierarchic order to avoid failure because of foreign keys
106105
for (Class<?> cls : hierarchicInsert()) {
107106
persistMixedList(
108-
entitiesToAdd.stream()
109-
.filter(ent -> cls.isAssignableFrom(ent.getClass()))
110-
.collect(Collectors.toList()),
107+
entitiesToAdd.stream().filter(ent -> cls.isAssignableFrom(ent.getClass())).toList(),
111108
identifier);
112109
entitiesToAdd.removeIf(
113110
ent ->
@@ -202,15 +199,7 @@ private <C extends Entity, E extends TimeSeriesEntry<V>, V extends Value> void p
202199
} else if (TimeSeries.class.isAssignableFrom(cls)) {
203200
entities.forEach(
204201
ts -> {
205-
try {
206-
persistTimeSeries((TimeSeries<E, V>) ts, identifier);
207-
} catch (SQLException e) {
208-
throw new RuntimeException(
209-
String.format(
210-
"An error occurred during extraction of entity '%s', SQLReason: '%s'",
211-
cls.getSimpleName(), e.getMessage()),
212-
e);
213-
}
202+
persistTimeSeries((TimeSeries<E, V>) ts, identifier);
214203
});
215204
} else {
216205
log.error("I don't know how to handle an entity of class {}", cls.getSimpleName());
@@ -244,22 +233,20 @@ private <C extends Entity> void insertListIgnoreNested(
244233

245234
/** Persist one time series. */
246235
protected <E extends TimeSeriesEntry<V>, V extends Value> void persistTimeSeries(
247-
TimeSeries<E, V> timeSeries, DbGridMetadata identifier) throws SQLException {
236+
TimeSeries<E, V> timeSeries, DbGridMetadata identifier) {
248237
try {
249238
TimeSeriesProcessorKey key = new TimeSeriesProcessorKey(timeSeries);
250239
String[] headerElements = processorProvider.getHeaderElements(key);
251240
persistTimeSeries(timeSeries, headerElements, identifier);
252241
} catch (ProcessorProviderException e) {
253242
log.error(
254243
"Exception occurred during receiving of header elements. Cannot write this element.", e);
255-
} catch (IOException e) {
256-
log.error("Exception occurred during closing of writer.", e);
257244
}
258245
}
259246

260247
private <E extends TimeSeriesEntry<V>, V extends Value> void persistTimeSeries(
261248
TimeSeries<E, V> timeSeries, String[] headerElements, DbGridMetadata identifier)
262-
throws ProcessorProviderException, IOException {
249+
throws ProcessorProviderException {
263250
try {
264251
String query =
265252
basicInsertQueryValuesITS(

src/test/groovy/edu/ie3/datamodel/io/sink/SqlSinkTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class SqlSinkTest extends Specification implements TestContainerHelper, TimeSeri
277277

278278
then:
279279
def exception = thrown(SQLException)
280-
exception.message.contains("ERROR: invalid input syntax for type uuid: \"null\"\n")
280+
exception.message.contains("ERROR: null value in column \"node\" of relation \"pv_input\" violates not-null constraint")
281281

282282
cleanup:
283283
sink.shutdown()
@@ -317,8 +317,8 @@ class SqlSinkTest extends Specification implements TestContainerHelper, TimeSeri
317317

318318
then:
319319
def exception = thrown(SQLException)
320-
exception.message == "ERROR: insert or update on table \"load_input\" violates foreign key constraint \"load_input_node_fkey\"\n" +
321-
" Detail: Key (node)=(4ca90220-74c2-4369-9afa-a18bf068840d) is not present in table \"node_input\"."
320+
exception.message.contains("ERROR: insert or update on table \"load_input\" violates foreign key constraint \"load_input_node_fkey\"\n" +
321+
" Detail: Key (node)=(4ca90220-74c2-4369-9afa-a18bf068840d) is not present in table \"node_input\".")
322322
}
323323

324324
def "A valid SqlSink throws an exception if a grid does not exist."() {

src/test/resources/log4j2-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</Appenders>
2626
<Loggers>
2727
<asyncRoot level="debug">
28-
<AppenderRef ref="Console" level="error"/>
28+
<AppenderRef ref="Console" level="off"/>
2929
<AppenderRef ref="RF" level="debug"/>
3030
</asyncRoot>
3131
</Loggers>

0 commit comments

Comments
 (0)