Skip to content

Commit 682035e

Browse files
committed
Bind arrays correctly in PG updates
1 parent daf5ccb commit 682035e

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

document-store/src/main/java/org/hypertrace/core/documentstore/postgres/FlatPostgresCollection.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,9 +1122,7 @@ private void executeUpdate(
11221122
for (Object param : params) {
11231123
ps.setObject(idx++, param);
11241124
}
1125-
for (Object param : filterParams.getObjectParams().values()) {
1126-
ps.setObject(idx++, param);
1127-
}
1125+
filterParams.bindTo(connection, ps, idx);
11281126
int rowsUpdated = ps.executeUpdate();
11291127
LOGGER.debug("Rows updated: {}", rowsUpdated);
11301128
} catch (SQLException e) {

document-store/src/main/java/org/hypertrace/core/documentstore/postgres/Params.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.hypertrace.core.documentstore.postgres;
22

3+
import java.sql.Array;
4+
import java.sql.Connection;
5+
import java.sql.PreparedStatement;
6+
import java.sql.SQLException;
37
import java.util.HashMap;
48
import java.util.Map;
59
import lombok.Getter;
@@ -26,6 +30,21 @@ public Map<Integer, Object> getObjectParams() {
2630
return objectParams;
2731
}
2832

33+
public int bindTo(Connection connection, PreparedStatement ps, int startIndex)
34+
throws SQLException {
35+
int idx = startIndex;
36+
for (Object value : objectParams.values()) {
37+
if (value instanceof ArrayParam) {
38+
ArrayParam arrayParam = (ArrayParam) value;
39+
Array sqlArray = connection.createArrayOf(arrayParam.getSqlType(), arrayParam.getValues());
40+
ps.setArray(idx++, sqlArray);
41+
} else {
42+
ps.setObject(idx++, value);
43+
}
44+
}
45+
return idx;
46+
}
47+
2948
public static Builder newBuilder() {
3049
return new Builder();
3150
}

0 commit comments

Comments
 (0)