Skip to content

Commit

Permalink
Fixed issue #4911
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Sep 3, 2015
1 parent 93ffa8e commit 0190a83
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Expand Up @@ -72,7 +72,7 @@ public class OCommandExecutorSQLUpdate extends OCommandExecutorSQLRetryAbstract
private List<OPair<String, Object>> addEntries = new ArrayList<OPair<String, Object>>();
private List<OTriple<String, String, Object>> putEntries = new ArrayList<OTriple<String, String, Object>>();
private List<OPair<String, Object>> removeEntries = new ArrayList<OPair<String, Object>>();
private List<OPair<String, Number>> incrementEntries = new ArrayList<OPair<String, Number>>();
private List<OPair<String, Object>> incrementEntries = new ArrayList<OPair<String, Object>>();
private ODocument merge = null;
private String lockStrategy = "NONE";
private OReturnHandler returnHandler = new ORecordCountHandler();
Expand Down Expand Up @@ -276,7 +276,7 @@ public boolean result(final Object iRecord) {
boolean updated = handleContent(record);
updated |= handleMerge(record);
updated |= handleSetEntries(record);
updated |= handleIncrementEnries(record);
updated |= handleIncrementEntries(record);
updated |= handleAddEntries(record);
updated |= handlePutEntries(record);
updated |= handleRemoveEntries(record);
Expand Down Expand Up @@ -440,19 +440,27 @@ private boolean handleSetEntries(final ODocument record) {
return updated;
}

private boolean handleIncrementEnries(ODocument record) {
private boolean handleIncrementEntries(final ODocument record) {
boolean updated = false;
// BIND VALUES TO INCREMENT
if (!incrementEntries.isEmpty()) {
for (OPair<String, Number> entry : incrementEntries) {
for (OPair<String, Object> entry : incrementEntries) {
final Number prevValue = record.field(entry.getKey());

Number current;
if (entry.getValue() instanceof OSQLFilterItem)
current = (Number) ((OSQLFilterItem) entry.getValue()).getValue(record, null, context);
else if (entry.getValue() instanceof Number)
current = (Number) entry.getValue();
else
throw new OCommandExecutionException("Increment value is not a number (" + entry.getValue() + ")");

if (prevValue == null)
// NO PREVIOUS VALUE: CONSIDER AS 0
record.field(entry.getKey(), entry.getValue());
record.field(entry.getKey(), current);
else
// COMPUTING INCREMENT
record.field(entry.getKey(), OType.increment(prevValue, entry.getValue()));
record.field(entry.getKey(), OType.increment(prevValue, current));
}
updated = true;
}
Expand Down Expand Up @@ -740,7 +748,7 @@ private void parseIncrementFields() {
fieldValue = getBlock(parserRequiredWord(false, "Value expected"));

// INSERT TRANSFORMED FIELD VALUE
incrementEntries.add(new OPair(fieldName, (Number) getFieldValueCountingParameters(fieldValue)));
incrementEntries.add(new OPair(fieldName, getFieldValueCountingParameters(fieldValue)));
parserSkipWhiteSpaces();

firstLap = false;
Expand Down
@@ -1,16 +1,17 @@
package com.orientechnologies.orient.core.sql;

import com.orientechnologies.orient.core.command.script.OCommandScript;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.orientechnologies.orient.core.command.script.OCommandScript;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;

@Test
public class OCommandExecutorSQLScriptTest {
Expand Down Expand Up @@ -72,7 +73,6 @@ public void testReturnExpanded() throws Exception {
script.append("let $a = insert into V set test = 'sql script test'\n");
script.append("return $a.toJSON()\n");
String qResult = db.command(new OCommandScript("sql", script.toString())).execute();

Assert.assertNotNull(qResult);

new ODocument().fromJSON(qResult);
Expand Down Expand Up @@ -135,4 +135,17 @@ public void testReturnObject() throws Exception {

Assert.assertTrue(result.iterator().next() instanceof Map);
}

@Test
public void testIncrementAndLet() throws Exception {

StringBuilder script = new StringBuilder();
script.append("CREATE CLASS TestCounter;\n");
script.append("INSERT INTO TestCounter set weight = 3;\n");
script.append("LET counter = SELECT count(*) FROM TestCounter;\n");
script.append("UPDATE TestCounter INCREMENT weight = $counter[0].count RETURN AfTER @this;\n");
List<ODocument> qResult = db.command(new OCommandScript("sql", script.toString())).execute();

Assert.assertEquals(qResult.get(0).field("weight"), 4l);
}
}

0 comments on commit 0190a83

Please sign in to comment.