|
40 | 40 | import java.sql.PreparedStatement;
|
41 | 41 | import java.sql.ResultSet;
|
42 | 42 | import java.sql.ResultSetMetaData;
|
| 43 | +import java.sql.SQLException; |
| 44 | +import java.sql.Types; |
43 | 45 | import java.util.ArrayList;
|
44 | 46 | import java.util.List;
|
45 | 47 |
|
@@ -140,48 +142,77 @@ public void poll(final @NotNull PollingInput pollingInput, final @NotNull Pollin
|
140 | 142 | }
|
141 | 143 |
|
142 | 144 | 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()) { |
144 | 147 | log.debug("Getting tag definition");
|
145 | 148 | /* Get the tag definition (Query, RowLimit and Split Lines)*/
|
146 | 149 | final PostgreSQLAdapterTagDefinition definition = tag.getDefinition();
|
147 | 150 |
|
148 | 151 | /* 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); |
170 | 163 | }
|
171 | 164 |
|
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); |
176 | 171 | }
|
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); |
179 | 178 | }
|
180 | 179 | } catch (final Exception e) {
|
181 | 180 | output.fail(e, null);
|
182 | 181 | }
|
183 | 182 | }
|
184 | 183 |
|
| 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 | + |
185 | 216 | @Override
|
186 | 217 | public int getPollingIntervalMillis() {
|
187 | 218 | return adapterConfig.getPollingIntervalMillis();
|
|
0 commit comments