|
20 | 20 | import io.airbyte.cdk.integrations.source.relationaldb.models.InternalModels.StateType;
|
21 | 21 | import io.airbyte.cdk.integrations.source.relationaldb.state.StateManager;
|
22 | 22 | import io.airbyte.commons.json.Jsons;
|
23 |
| -import io.airbyte.protocol.models.AirbyteStreamNameNamespacePair; |
| 23 | +import io.airbyte.protocol.models.v0.AirbyteStreamNameNamespacePair; |
24 | 24 | import io.airbyte.protocol.models.v0.ConfiguredAirbyteCatalog;
|
25 | 25 | import io.airbyte.protocol.models.v0.ConfiguredAirbyteStream;
|
26 | 26 | import java.math.BigDecimal;
|
@@ -185,46 +185,46 @@ public static Map<io.airbyte.protocol.models.v0.AirbyteStreamNameNamespacePair,
|
185 | 185 |
|
186 | 186 | final Map<io.airbyte.protocol.models.v0.AirbyteStreamNameNamespacePair, CursorBasedStatus> cursorBasedStatusMap = new HashMap<>();
|
187 | 187 | streams.forEach(stream -> {
|
188 |
| - try { |
189 |
| - final String name = stream.getStream().getName(); |
190 |
| - final String namespace = stream.getStream().getNamespace(); |
191 |
| - final String fullTableName = |
192 |
| - getFullyQualifiedTableNameWithQuoting(namespace, name, quoteString); |
193 |
| - |
194 |
| - final Optional<CursorInfo> cursorInfoOptional = |
195 |
| - stateManager.getCursorInfo(new io.airbyte.protocol.models.v0.AirbyteStreamNameNamespacePair(name, namespace)); |
196 |
| - if (cursorInfoOptional.isEmpty()) { |
197 |
| - throw new RuntimeException(String.format("Stream %s was not provided with an appropriate cursor", stream.getStream().getName())); |
198 |
| - } |
| 188 | + final String name = stream.getStream().getName(); |
| 189 | + final String namespace = stream.getStream().getNamespace(); |
| 190 | + final String fullTableName = |
| 191 | + getFullyQualifiedTableNameWithQuoting(namespace, name, quoteString); |
199 | 192 |
|
200 |
| - LOGGER.info("Querying max cursor value for {}.{}", namespace, name); |
201 |
| - final String cursorField = cursorInfoOptional.get().getCursorField(); |
| 193 | + final Optional<CursorInfo> cursorInfoOptional = |
| 194 | + stateManager.getCursorInfo(new AirbyteStreamNameNamespacePair(name, namespace)); |
| 195 | + if (cursorInfoOptional.isEmpty()) { |
| 196 | + throw new RuntimeException(String.format("Stream %s was not provided with an appropriate cursor", stream.getStream().getName())); |
| 197 | + } |
| 198 | + final CursorBasedStatus cursorBasedStatus = new CursorBasedStatus(); |
| 199 | + final Optional<String> maybeCursorField = Optional.ofNullable(cursorInfoOptional.get().getCursorField()); |
| 200 | + maybeCursorField.ifPresent(cursorField -> { |
| 201 | + LOGGER.info("Cursor {}. Querying max cursor value for {}.{}", cursorField, namespace, name); |
202 | 202 | final String quotedCursorField = getIdentifierWithQuoting(cursorField, quoteString);
|
203 | 203 | final String cursorBasedSyncStatusQuery = String.format(MAX_CURSOR_VALUE_QUERY,
|
204 | 204 | quotedCursorField,
|
205 | 205 | fullTableName,
|
206 | 206 | quotedCursorField,
|
207 | 207 | quotedCursorField,
|
208 | 208 | fullTableName);
|
209 |
| - final List<JsonNode> jsonNodes = database.bufferedResultSetQuery(conn -> conn.prepareStatement(cursorBasedSyncStatusQuery).executeQuery(), |
210 |
| - resultSet -> JdbcUtils.getDefaultSourceOperations().rowToJson(resultSet)); |
211 |
| - final CursorBasedStatus cursorBasedStatus = new CursorBasedStatus(); |
212 |
| - cursorBasedStatus.setStateType(StateType.CURSOR_BASED); |
213 |
| - cursorBasedStatus.setVersion(2L); |
214 |
| - cursorBasedStatus.setStreamName(name); |
215 |
| - cursorBasedStatus.setStreamNamespace(namespace); |
| 209 | + final List<JsonNode> jsonNodes; |
| 210 | + try { |
| 211 | + jsonNodes = database.bufferedResultSetQuery(conn -> conn.prepareStatement(cursorBasedSyncStatusQuery).executeQuery(), |
| 212 | + resultSet -> JdbcUtils.getDefaultSourceOperations().rowToJson(resultSet)); |
| 213 | + } catch (SQLException e) { |
| 214 | + throw new RuntimeException("Failed to read max cursor value from %s.%s".formatted(namespace, name), e); |
| 215 | + } |
216 | 216 | cursorBasedStatus.setCursorField(ImmutableList.of(cursorField));
|
217 |
| - |
218 | 217 | if (!jsonNodes.isEmpty()) {
|
219 | 218 | final JsonNode result = jsonNodes.get(0);
|
220 | 219 | cursorBasedStatus.setCursor(result.get(cursorField).asText());
|
221 | 220 | cursorBasedStatus.setCursorRecordCount((long) jsonNodes.size());
|
222 | 221 | }
|
223 |
| - |
224 |
| - cursorBasedStatusMap.put(new io.airbyte.protocol.models.v0.AirbyteStreamNameNamespacePair(name, namespace), cursorBasedStatus); |
225 |
| - } catch (final SQLException e) { |
226 |
| - throw new RuntimeException(e); |
227 |
| - } |
| 222 | + cursorBasedStatus.setStateType(StateType.CURSOR_BASED); |
| 223 | + cursorBasedStatus.setVersion(2L); |
| 224 | + cursorBasedStatus.setStreamName(name); |
| 225 | + cursorBasedStatus.setStreamNamespace(namespace); |
| 226 | + cursorBasedStatusMap.put(new AirbyteStreamNameNamespacePair(name, namespace), cursorBasedStatus); |
| 227 | + }); |
228 | 228 | });
|
229 | 229 |
|
230 | 230 | return cursorBasedStatusMap;
|
|
0 commit comments