Skip to content

Commit 7390a2f

Browse files
committed
address code review
1 parent 90557be commit 7390a2f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/main/java/org/tailormap/api/geotools/featuresources/AttachmentsHelper.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ public static AttachmentWithBinary getAttachment(TMFeatureType featureType, UUID
583583
/**
584584
* List attachments for multiple features grouped by their IDs. <br>
585585
* <strong>NOTE</strong>: the featurePKs list should contain {@link Comparable} objects (e.g. no {@code byte[]}), as
586-
* these are used as map keys. Eg. {@code byte[]} is converted to {@code ByteBuffer}.
586+
* these are used as map keys. E.g. {@code byte[]} is converted to {@code ByteBuffer}.
587+
*
588+
* <p><strong>TODO:</strong>{@link <a href="https://b3partners.atlassian.net/browse/HTM-1771">HTM-1771</a>}
587589
*
588590
* @param featureType the feature type
589591
* @param featurePKs the feature primary keys
@@ -621,15 +623,18 @@ public static AttachmentWithBinary getAttachment(TMFeatureType featureType, UUID
621623
try (Connection conn = ds.getDataSource().getConnection();
622624
PreparedStatement stmt = conn.prepareStatement(querySql)) {
623625

626+
Object firstPK = featurePKs.getFirst();
627+
boolean isUUID = firstPK instanceof UUID;
628+
boolean isByteBuffer = firstPK instanceof ByteBuffer;
629+
624630
switch (featureType.getFeatureSource().getJdbcConnection().getDbtype()) {
625631
case ORACLE -> {
626632
for (int i = 0; i < featurePKs.size(); i++) {
627-
if (featurePKs.getFirst() instanceof UUID) {
633+
if (isUUID) {
628634
// Oracle (RAW(16)): Comparisons are possible, but the values in the IN list must be
629635
// correctly formatted binary literals (hextoraw('...')).
630636
stmt.setBytes(i + 1, asBytes((UUID) featurePKs.get(i)));
631-
}
632-
if (featurePKs.getFirst() instanceof ByteBuffer) {
637+
} else if (isByteBuffer) {
633638
// unwrap ByteBuffer to byte[] for the query
634639
stmt.setBytes(i + 1, ((ByteBuffer) featurePKs.get(i)).array());
635640
} else {
@@ -639,7 +644,7 @@ public static AttachmentWithBinary getAttachment(TMFeatureType featureType, UUID
639644
}
640645
case SQLSERVER -> {
641646
for (int i = 0; i < featurePKs.size(); i++) {
642-
if (featurePKs.getFirst() instanceof UUID) {
647+
if (isUUID) {
643648
// use uppercase string representation for SQL Server UNIQUEIDENTIFIER
644649
stmt.setString(
645650
i + 1, featurePKs.get(i).toString().toUpperCase(Locale.ROOT));
@@ -665,7 +670,7 @@ public static AttachmentWithBinary getAttachment(TMFeatureType featureType, UUID
665670
while (rs.next()) {
666671
AttachmentMetadata a = getAttachmentMetadata(rs);
667672
Object keyObject = rs.getObject(1);
668-
if (featurePKs.getFirst() instanceof UUID
673+
if (isUUID
669674
&& featureType
670675
.getFeatureSource()
671676
.getJdbcConnection()
@@ -675,7 +680,7 @@ public static AttachmentWithBinary getAttachment(TMFeatureType featureType, UUID
675680
byte[] rawBytes = rs.getBytes(1);
676681
ByteBuffer bb = ByteBuffer.wrap(rawBytes);
677682
keyObject = new UUID(bb.getLong(), bb.getLong());
678-
} else if (featurePKs.getFirst() instanceof UUID
683+
} else if (isUUID
679684
&& featureType
680685
.getFeatureSource()
681686
.getJdbcConnection()
@@ -685,8 +690,9 @@ public static AttachmentWithBinary getAttachment(TMFeatureType featureType, UUID
685690
keyObject = UUID.fromString(rs.getString(1));
686691
}
687692

688-
if (featurePKs.getFirst() instanceof ByteBuffer) {
693+
if (isByteBuffer) {
689694
// we need to use a key that is comparable, so convert byte[] to ByteBuffer
695+
assert keyObject instanceof byte[];
690696
keyObject = ByteBuffer.wrap((byte[]) keyObject);
691697
}
692698
attachments.add(new AttachmentMetadataListItem(keyObject, a));

src/test/java/org/tailormap/api/geotools/featuresources/AttachmentsHelperIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ void deleteAttachmentsTableForFeatureType() throws SQLException, IOException {
322322

323323
try (Connection conn = ds.getDataSource().getConnection();
324324
Statement stmt = conn.createStatement();
325-
ResultSet rs = stmt.executeQuery("select count(*) from " + featureTypeName + "_attachments")) {
325+
ResultSet rs = stmt.executeQuery(
326+
"select count(*) from " + schemaPrefix + featureTypeName + "_attachments")) {
326327
if (rs.next()) {
327328
fail("Attachments table '%s_attachments' still exists.".formatted(featureTypeName));
328329
}

0 commit comments

Comments
 (0)