|
31 | 31 | import java.time.LocalTime;
|
32 | 32 | import java.time.OffsetDateTime;
|
33 | 33 | import java.time.OffsetTime;
|
| 34 | +import java.time.format.DateTimeParseException; |
34 | 35 | import java.util.Collections;
|
35 | 36 | import org.postgresql.jdbc.PgResultSetMetaData;
|
36 | 37 | import org.slf4j.Logger;
|
@@ -108,22 +109,42 @@ public void setStatementField(final PreparedStatement preparedStatement,
|
108 | 109 | }
|
109 | 110 | }
|
110 | 111 |
|
111 |
| - private void setTimeWithTimezone(PreparedStatement preparedStatement, int parameterIndex, String value) throws SQLException { |
112 |
| - preparedStatement.setObject(parameterIndex, OffsetTime.parse(value)); |
| 112 | + private void setTimeWithTimezone(final PreparedStatement preparedStatement, final int parameterIndex, final String value) throws SQLException { |
| 113 | + try { |
| 114 | + preparedStatement.setObject(parameterIndex, OffsetTime.parse(value)); |
| 115 | + } catch (final DateTimeParseException e) { |
| 116 | + //attempt to parse the time w/o timezone. This can be caused by schema created with a different version of the connector |
| 117 | + preparedStatement.setObject(parameterIndex, LocalTime.parse(value)); |
| 118 | + } |
113 | 119 | }
|
114 | 120 |
|
115 |
| - private void setTimestampWithTimezone(PreparedStatement preparedStatement, int parameterIndex, String value) throws SQLException { |
116 |
| - preparedStatement.setObject(parameterIndex, OffsetDateTime.parse(value)); |
| 121 | + private void setTimestampWithTimezone(final PreparedStatement preparedStatement, final int parameterIndex, final String value) throws SQLException { |
| 122 | + try { |
| 123 | + preparedStatement.setObject(parameterIndex, OffsetDateTime.parse(value)); |
| 124 | + } catch (final DateTimeParseException e) { |
| 125 | + //attempt to parse the datetime w/o timezone. This can be caused by schema created with a different version of the connector |
| 126 | + preparedStatement.setObject(parameterIndex, LocalDateTime.parse(value)); |
| 127 | + } |
117 | 128 | }
|
118 | 129 |
|
119 | 130 | @Override
|
120 |
| - protected void setTimestamp(PreparedStatement preparedStatement, int parameterIndex, String value) throws SQLException { |
121 |
| - preparedStatement.setObject(parameterIndex, LocalDateTime.parse(value)); |
| 131 | + protected void setTimestamp(final PreparedStatement preparedStatement, final int parameterIndex, final String value) throws SQLException { |
| 132 | + try { |
| 133 | + preparedStatement.setObject(parameterIndex, LocalDateTime.parse(value)); |
| 134 | + } catch (final DateTimeParseException e) { |
| 135 | + //attempt to parse the datetime with timezone. This can be caused by schema created with an older version of the connector |
| 136 | + preparedStatement.setObject(parameterIndex, OffsetDateTime.parse(value)); |
| 137 | + } |
122 | 138 | }
|
123 | 139 |
|
124 | 140 | @Override
|
125 |
| - protected void setTime(PreparedStatement preparedStatement, int parameterIndex, String value) throws SQLException { |
126 |
| - preparedStatement.setObject(parameterIndex, LocalTime.parse(value)); |
| 141 | + protected void setTime(final PreparedStatement preparedStatement, final int parameterIndex, final String value) throws SQLException { |
| 142 | + try { |
| 143 | + preparedStatement.setObject(parameterIndex, LocalTime.parse(value)); |
| 144 | + } catch (final DateTimeParseException e) { |
| 145 | + //attempt to parse the datetime with timezone. This can be caused by schema created with an older version of the connector |
| 146 | + preparedStatement.setObject(parameterIndex, OffsetTime.parse(value)); |
| 147 | + } |
127 | 148 | }
|
128 | 149 |
|
129 | 150 | @Override
|
@@ -170,21 +191,21 @@ public void setJsonField(final ResultSet resultSet, final int colIndex, final Ob
|
170 | 191 | }
|
171 | 192 |
|
172 | 193 | @Override
|
173 |
| - protected void putDate(ObjectNode node, String columnName, ResultSet resultSet, int index) throws SQLException { |
174 |
| - LocalDate date = getDateTimeObject(resultSet, index, LocalDate.class); |
| 194 | + protected void putDate(final ObjectNode node, final String columnName, final ResultSet resultSet, final int index) throws SQLException { |
| 195 | + final LocalDate date = getDateTimeObject(resultSet, index, LocalDate.class); |
175 | 196 | node.put(columnName, resolveEra(date, date.toString()));
|
176 | 197 | }
|
177 | 198 |
|
178 | 199 | @Override
|
179 |
| - protected void putTime(ObjectNode node, String columnName, ResultSet resultSet, int index) throws SQLException { |
180 |
| - LocalTime time = getDateTimeObject(resultSet, index, LocalTime.class); |
| 200 | + protected void putTime(final ObjectNode node, final String columnName, final ResultSet resultSet, final int index) throws SQLException { |
| 201 | + final LocalTime time = getDateTimeObject(resultSet, index, LocalTime.class); |
181 | 202 | node.put(columnName, time.toString());
|
182 | 203 | }
|
183 | 204 |
|
184 | 205 | @Override
|
185 |
| - protected void putTimestamp(ObjectNode node, String columnName, ResultSet resultSet, int index) throws SQLException { |
186 |
| - LocalDateTime timestamp = getDateTimeObject(resultSet, index, LocalDateTime.class); |
187 |
| - LocalDate date = timestamp.toLocalDate(); |
| 206 | + protected void putTimestamp(final ObjectNode node, final String columnName, final ResultSet resultSet, final int index) throws SQLException { |
| 207 | + final LocalDateTime timestamp = getDateTimeObject(resultSet, index, LocalDateTime.class); |
| 208 | + final LocalDate date = timestamp.toLocalDate(); |
188 | 209 | node.put(columnName, resolveEra(date, timestamp.toString()));
|
189 | 210 | }
|
190 | 211 |
|
@@ -214,7 +235,7 @@ public JDBCType getFieldType(final JsonNode field) {
|
214 | 235 | }
|
215 | 236 |
|
216 | 237 | @Override
|
217 |
| - public JsonSchemaType getJsonType(JDBCType jdbcType) { |
| 238 | + public JsonSchemaType getJsonType(final JDBCType jdbcType) { |
218 | 239 | return switch (jdbcType) {
|
219 | 240 | case BOOLEAN -> JsonSchemaType.BOOLEAN;
|
220 | 241 | case TINYINT, SMALLINT, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, NUMERIC, DECIMAL -> JsonSchemaType.NUMBER;
|
@@ -264,7 +285,7 @@ private void putHstoreAsJson(final ObjectNode node, final String columnName, fin
|
264 | 285 | final var data = resultSet.getObject(index);
|
265 | 286 | try {
|
266 | 287 | node.put(columnName, OBJECT_MAPPER.writeValueAsString(data));
|
267 |
| - } catch (JsonProcessingException e) { |
| 288 | + } catch (final JsonProcessingException e) { |
268 | 289 | throw new RuntimeException("Could not parse 'hstore' value:" + e);
|
269 | 290 | }
|
270 | 291 | }
|
|
0 commit comments