Automatic initialisation of embedded collections on SQL UPDATE
Resolves:
@@ -117,10 +117,36 @@ public class OUpdateItem extends SimpleNode {
applyOperation(doc, left, rightValue, ctx);
} else {
Object val = doc.getProperty(left.getStringValue());
+ if (val == null) {
+ val = initSchemafullCollections(doc, left.getStringValue());
+ }
leftModifier.setValue(doc, val, rightValue, ctx);
}
}
+ private Object initSchemafullCollections(OResultInternal doc, String propName) {
+ OClass oClass = doc.getElement().flatMap(x -> x.getSchemaType()).orElse(null);
+ if (oClass == null) {
+ return null;
+ }
+ OProperty prop = oClass.getProperty(propName);
+ if (prop == null) {
+ return null;
+ }
+ Object result = null;
+ if (prop.getType() == OType.EMBEDDEDMAP || prop.getType() == OType.LINKMAP) {
+ result = new HashMap<>();
+ doc.setProperty(propName, result);
+ } else if (prop.getType() == OType.EMBEDDEDLIST || prop.getType() == OType.LINKLIST) {
+ result = new ArrayList<>();
+ doc.setProperty(propName, result);
+ } else if (prop.getType() == OType.EMBEDDEDSET || prop.getType() == OType.LINKSET) {
+ result = new HashSet<>();
+ doc.setProperty(propName, result);
+ }
+ return result;
+ }
+
private OClass calculateLinkedTypeForThisItem(OResultInternal doc, OCommandContext ctx) {
return null; // TODO
}
