Skip to content

Commit 1c45ff1

Browse files
improve handling of native types
1 parent 1e6b366 commit 1c45ff1

File tree

1 file changed

+59
-28
lines changed

1 file changed

+59
-28
lines changed

modules/hivemq-edge-module-postgresql/src/main/java/com/hivemq/edge/adapters/postgresql/PostgreSQLPollingProtocolAdapter.java

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import java.sql.PreparedStatement;
4141
import java.sql.ResultSet;
4242
import java.sql.ResultSetMetaData;
43+
import java.sql.SQLException;
44+
import java.sql.Types;
4345
import java.util.ArrayList;
4446
import java.util.List;
4547

@@ -140,48 +142,77 @@ public void poll(final @NotNull PollingInput pollingInput, final @NotNull Pollin
140142
}
141143

142144
private void loadDataFromDB(final @NotNull PollingOutput output, final @NotNull PostgreSQLAdapterTag tag) {
143-
try {
145+
// ARM to ensure the connection is closed afterwards
146+
try (final Connection connection = databaseConnection.getConnection()) {
144147
log.debug("Getting tag definition");
145148
/* Get the tag definition (Query, RowLimit and Split Lines)*/
146149
final PostgreSQLAdapterTagDefinition definition = tag.getDefinition();
147150

148151
/* Execute query and handle result */
149-
try (final Connection connection = databaseConnection.getConnection()) {
150-
final PreparedStatement preparedStatement = connection.prepareStatement(tag.getDefinition().getQuery());
151-
final ResultSet result = preparedStatement.executeQuery();
152-
assert result != null;
153-
final ArrayList<ObjectNode> resultObject = new ArrayList<>();
154-
final ResultSetMetaData resultSetMD = result.getMetaData();
155-
while (result.next()) {
156-
final int numColumns = resultSetMD.getColumnCount();
157-
final ObjectNode node = OBJECT_MAPPER.createObjectNode();
158-
for (int i = 1; i <= numColumns; i++) {
159-
final String column_name = resultSetMD.getColumnName(i);
160-
node.put(column_name, result.getString(column_name));
161-
}
162-
163-
/* Publish datapoint with a single line if split is required */
164-
if (definition.getSpiltLinesInIndividualMessages()) {
165-
log.debug("Splitting lines in multiple messages");
166-
output.addDataPoint("queryResult", node);
167-
} else {
168-
resultObject.add(node);
169-
}
152+
153+
final PreparedStatement preparedStatement = connection.prepareStatement(tag.getDefinition().getQuery());
154+
final ResultSet result = preparedStatement.executeQuery();
155+
assert result != null;
156+
final ArrayList<ObjectNode> resultObject = new ArrayList<>();
157+
final ResultSetMetaData resultSetMD = result.getMetaData();
158+
while (result.next()) {
159+
final int numColumns = resultSetMD.getColumnCount();
160+
final ObjectNode node = OBJECT_MAPPER.createObjectNode();
161+
for (int i = 1; i <= numColumns; i++) {
162+
parseAndAddValue(i, result, resultSetMD, node);
170163
}
171164

172-
/* Publish datapoint with all lines if no split is required */
173-
if (!definition.getSpiltLinesInIndividualMessages()) {
174-
log.debug("Publishing all lines in a single message");
175-
output.addDataPoint("queryResult", resultObject);
165+
/* Publish datapoint with a single line if split is required */
166+
if (definition.getSpiltLinesInIndividualMessages()) {
167+
log.debug("Splitting lines in multiple messages");
168+
output.addDataPoint("queryResult", node);
169+
} else {
170+
resultObject.add(node);
176171
}
177-
} catch (final Exception e) {
178-
output.fail(e, null);
172+
}
173+
174+
/* Publish datapoint with all lines if no split is required */
175+
if (!definition.getSpiltLinesInIndividualMessages()) {
176+
log.debug("Publishing all lines in a single message");
177+
output.addDataPoint("queryResult", resultObject);
179178
}
180179
} catch (final Exception e) {
181180
output.fail(e, null);
182181
}
183182
}
184183

184+
// according to https://www.ibm.com/docs/en/db2/11.1?topic=djr-sql-data-type-representation
185+
private void parseAndAddValue(
186+
final int index,
187+
final @NotNull ResultSet result,
188+
final @NotNull ResultSetMetaData resultSetMD,
189+
final @NotNull ObjectNode node) throws SQLException {
190+
final String columnName = resultSetMD.getColumnName(index);
191+
final int columnType = resultSetMD.getColumnType(index);
192+
switch (columnType) {
193+
case Types.BIT:
194+
case Types.TINYINT:
195+
case Types.SMALLINT:
196+
case Types.INTEGER:
197+
node.put(columnName, result.getInt(index));
198+
return;
199+
case Types.BIGINT:
200+
node.put(columnName, result.getLong(index));
201+
return;
202+
case Types.DECIMAL:
203+
node.put(columnName, result.getBigDecimal(index));
204+
return;
205+
case Types.REAL:
206+
case Types.FLOAT:
207+
case Types.DOUBLE:
208+
case Types.NUMERIC:
209+
node.put(columnName, result.getDouble(index));
210+
return;
211+
default:
212+
node.put(columnName, result.getString(index));
213+
}
214+
}
215+
185216
@Override
186217
public int getPollingIntervalMillis() {
187218
return adapterConfig.getPollingIntervalMillis();

0 commit comments

Comments
 (0)