Skip to content

Commit

Permalink
Fix automatic conversion of embedded sets with INSERT CONTENT statemets
Browse files Browse the repository at this point in the history
Resolves: #6670
  • Loading branch information
luigidellaquila committed Oct 3, 2016
1 parent f3ef57b commit 8ad0088
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2522,31 +2522,39 @@ protected void autoConvertValues() {
OType linkedType = prop.getLinkedType();
if (linkedType == null)
continue;
Object value = field(prop.getName());
final ODocumentEntry entry = _fields.get(prop.getName());
if (entry == null)
continue;
if (!entry.created && !entry.changed)
continue;
Object value = entry.value;
if (value == null)
continue;
try {
if (type == OType.EMBEDDEDLIST && !(value instanceof OTrackedList)) {
List<Object> list = new OTrackedList<Object>(this);
if (type == OType.EMBEDDEDLIST) {
OTrackedList<Object> list = new OTrackedList<Object>(this);
Collection<Object> values = (Collection<Object>) value;
for (Object object : values) {
list.add(OType.convert(object, linkedType.getDefaultJavaType()));
}
field(prop.getName(), list);
} else if (type == OType.EMBEDDEDMAP && !(value instanceof OTrackedMap)) {
entry.value = list;
replaceListenerOnAutoconvert(entry, value);
} else if (type == OType.EMBEDDEDMAP) {
Map<Object, Object> map = new OTrackedMap<Object>(this);
Map<Object, Object> values = (Map<Object, Object>) value;
for (Entry<Object, Object> object : values.entrySet()) {
map.put(object.getKey(), OType.convert(object.getValue(), linkedType.getDefaultJavaType()));
}
field(prop.getName(), map);
} else if (type == OType.EMBEDDEDSET && !(value instanceof OTrackedSet)) {
Set<Object> list = new OTrackedSet<Object>(this);
entry.value = map;
replaceListenerOnAutoconvert(entry, value);
} else if (type == OType.EMBEDDEDSET) {
Set<Object> set = new OTrackedSet<Object>(this);
Collection<Object> values = (Collection<Object>) value;
for (Object object : values) {
list.add(OType.convert(object, linkedType.getDefaultJavaType()));
set.add(OType.convert(object, linkedType.getDefaultJavaType()));
}
field(prop.getName(), list);
entry.value = set;
replaceListenerOnAutoconvert(entry, value);
}
} catch (Exception e) {
throw OException
Expand All @@ -2556,6 +2564,21 @@ protected void autoConvertValues() {
}
}

private void replaceListenerOnAutoconvert(final ODocumentEntry entry, Object oldValue) {
if (entry.changeListener != null) {
// A listener was there, remove on the old value add it to the new value.
final OTrackedMultiValue<Object, Object> oldMultiValue = (OTrackedMultiValue<Object, Object>) oldValue;
oldMultiValue.removeRecordChangeListener(entry.changeListener);
((OTrackedMultiValue<Object, Object>) entry.value).addChangeListener(entry.changeListener);
} else {
// no listener was there add it only to the new value
final OSimpleMultiValueChangeListener<Object, Object> listener = new OSimpleMultiValueChangeListener<Object, Object>(this,
entry);
((OTrackedMultiValue<Object, Object>) entry.value).addChangeListener(listener);
entry.changeListener = listener;
}
}

protected byte[] toStream(final boolean iOnlyDelta) {
STATUS prev = _status;
_status = STATUS.MARSHALLING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,8 @@
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.math.BigDecimal;
import java.util.*;

/**
* If some of the tests start to fail then check cluster number in queries, e.g #7:1. It can be because the order of clusters could
Expand Down Expand Up @@ -575,6 +569,19 @@ public void testInsertWithClusterAsFieldName() {
Assert.assertEquals(result.get(0).field("cluster"), "foo");
}

public void testInsertEmbeddedBigDecimal() {
// issue #6670
database.getMetadata().getSchema().getOrCreateClass("TestInsertEmbeddedBigDecimal");
database.command(new OCommandSQL("create property TestInsertEmbeddedBigDecimal.ed embeddedlist decimal")).execute();
database.command(new OCommandSQL("INSERT INTO TestInsertEmbeddedBigDecimal CONTENT {\"ed\": [5,null,5]}")).execute();
List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>("SELECT FROM TestInsertEmbeddedBigDecimal"));
Assert.assertEquals(result.size(), 1);
Iterable ed = result.get(0).field("ed");
Object o = ed.iterator().next();
Assert.assertEquals(o.getClass(), BigDecimal.class);
Assert.assertEquals(((BigDecimal)o).intValue(), 5);
}

private List<Long> getValidPositions(int clusterId) {
final List<Long> positions = new ArrayList<Long>();

Expand Down

0 comments on commit 8ad0088

Please sign in to comment.