Skip to content

Commit 95e92f7

Browse files
committed
bugfix for postgres
1 parent 347e984 commit 95e92f7

2 files changed

Lines changed: 39 additions & 14 deletions

File tree

src/main/java/picoded/dstack/jsql_json/JsonbUtils.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ public static MutablePair<String, byte[]> serializeDataMap(Map<String, Object> i
103103
continue;
104104
}
105105
} else if (v instanceof byte[]) {
106-
// Handling of binary data
107-
binMap.put(k, v);
106+
// Skip binary data in keySet loop; processed below from fullSet
108107
} else {
109108
// In all other cases, treat it as JSON data
110109
// we add it to the jsonMap, if its within the keyset
@@ -114,6 +113,17 @@ public static MutablePair<String, byte[]> serializeDataMap(Map<String, Object> i
114113
}
115114
}
116115

116+
// Build complete binary map from fullSet to prevent losing existing binary properties during partial updates
117+
for (String k : fullSet) {
118+
if (k.equalsIgnoreCase("_otm") || k.length() > 64) {
119+
continue;
120+
}
121+
Object v = inMap.get(k);
122+
if (v instanceof byte[] && v != null && v != ObjectToken.NULL) {
123+
binMap.put(k, v);
124+
}
125+
}
126+
117127
// Lets do the required conversions
118128
String json = ConvertJSON.fromMap(jsonMap);
119129
byte[] bin = null;

src/main/java/picoded/dstack/jsql_json/PostgresJsonb_DataObjectMap.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,32 @@ public void DataObjectRemoteDataMap_update(String _oid, Map<String, Object> full
214214
// Curent timestamp
215215
long now = JSql_DataObjectMapUtil.getCurrentTimestamp();
216216

217-
// // Ensure GUID is registered
218-
// sqlObj.upsert( //
219-
// dataStorageTable, //
220-
// new String[] { "oID" }, //
221-
// new Object[] { _oid }, //
222-
// new String[] { "uTm", "data", "bData" }, //
223-
// new Object[] { now, dataPair.getLeft(), dataPair.getRight() }, //
224-
// new String[] { "cTm", "eTm" }, //
225-
// new Object[] { now, 0 }, //
226-
// null // The only misc col, is pKy, which is being handled by DB
227-
// );
217+
// Determine which keys are being deleted/removed (explicitly null or ObjectToken.NULL)
218+
String updateDataSql = dataStorageTable + ".data||EXCLUDED.data";
219+
Set<String> keysToProcess = keys;
220+
if (keysToProcess == null) {
221+
keysToProcess = fullMap.keySet();
222+
}
223+
224+
java.util.List<String> deletedKeys = new java.util.ArrayList<>();
225+
for (String k : keysToProcess) {
226+
if (k.equalsIgnoreCase("oid") || k.equalsIgnoreCase("_oid") || k.equalsIgnoreCase("_otm")) {
227+
continue;
228+
}
229+
Object v = fullMap.get(k);
230+
if (v == null || v == picoded.core.common.ObjectToken.NULL) {
231+
deletedKeys.add(k);
232+
}
233+
}
234+
235+
if (!deletedKeys.isEmpty()) {
236+
StringBuilder sb = new StringBuilder();
237+
sb.append("(").append(updateDataSql).append(")");
238+
for (String dk : deletedKeys) {
239+
sb.append(" - '").append(dk.replace("'", "''")).append("'");
240+
}
241+
updateDataSql = sb.toString();
242+
}
228243

229244
// Perform the upsert command
230245
sqlObj.update_raw( //
@@ -233,7 +248,7 @@ public void DataObjectRemoteDataMap_update(String _oid, Map<String, Object> full
233248
"VALUES ( ?, ?, ?, ?, ?::jsonb, ? ) " + //
234249
"ON CONFLICT ( oID ) DO UPDATE SET " + //
235250
"uTm=EXCLUDED.uTm, " + //
236-
"data=" + dataStorageTable + ".data||EXCLUDED.data, " + //
251+
"data=" + updateDataSql + ", " + //
237252
"bData=EXCLUDED.bData", new Object[] { //
238253
_oid, now, now, 0, dataPair.getLeft(), dataPair.getRight() //
239254
});

0 commit comments

Comments
 (0)