Skip to content

Commit

Permalink
Fixed issue #3169 (+ test cases)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Dec 11, 2014
1 parent 0dbaf5f commit 0396567
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
*/
package com.orientechnologies.orient.core.sql;

import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

import com.orientechnologies.orient.core.command.OCommandDistributedReplicateRequest;
import com.orientechnologies.orient.core.command.OCommandRequest;
import com.orientechnologies.orient.core.command.OCommandRequestText;
Expand All @@ -37,6 +34,15 @@
import com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField;
import com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;

/**
* SQL INSERT command.
*
Expand All @@ -48,6 +54,7 @@ public class OCommandExecutorSQLInsert extends OCommandExecutorSQLSetAware imple
public static final String KEYWORD_INSERT = "INSERT";
protected static final String KEYWORD_RETURN = "RETURN";
private static final String KEYWORD_VALUES = "VALUES";
private static final String KEYWORD_UNSAFE = "UNSAFE";
private String className = null;
private String clusterName = null;
private String indexName = null;
Expand All @@ -56,6 +63,7 @@ public class OCommandExecutorSQLInsert extends OCommandExecutorSQLSetAware imple
private AtomicLong saved = new AtomicLong(0);
private Object returnExpression = null;
private List<ODocument> queryResult = null;
private boolean unsafe = false;

@SuppressWarnings("unchecked")
public OCommandExecutorSQLInsert parse(final OCommandRequest iRequest) {
Expand All @@ -67,6 +75,12 @@ public OCommandExecutorSQLInsert parse(final OCommandRequest iRequest) {
newRecords = null;
content = null;

if (parserTextUpperCase.endsWith(KEYWORD_UNSAFE)) {
unsafe = true;
parserText = parserText.substring(0, parserText.length() - KEYWORD_UNSAFE.length() - 1);
parserTextUpperCase = parserTextUpperCase.substring(0, parserTextUpperCase.length() - KEYWORD_UNSAFE.length() - 1);
}

parserRequiredKeyword("INSERT");
parserRequiredKeyword("INTO");

Expand All @@ -88,6 +102,11 @@ else if (subjectName.startsWith(OCommandExecutorSQLAbstract.INDEX_PREFIX))
if (cls == null)
throwParsingException("Class " + subjectName + " not found in database");

if (!unsafe && cls.isSubClassOf("E"))
// FOUND EDGE
throw new OCommandExecutionException(
"'INSERT' command cannot create Edges. Use 'CREATE EDGE' command instead, or apply the 'UNSAFE' keyword to force it");

className = cls.getName();
}

Expand Down Expand Up @@ -173,7 +192,6 @@ public Object execute(final Map<Object, Object> iArgs) {
// RETURN LAST ENTRY
return prepareReturnItem(new ODocument(result));
} else {

// CREATE NEW DOCUMENTS
final List<ODocument> docs = new ArrayList<ODocument>();
if (newRecords != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,23 @@ public void testDeleteOfVerticesAndEdgesWithDeleteCommandAndUnsafe() {

}

public void testInsertOfEdgeWithInsertCommand() {
try {
database.command(new OCommandSQL("insert into E set a = 33")).execute();
Assert.assertTrue(false);
} catch (OCommandExecutionException e) {
Assert.assertTrue(true);
}

}

public void testInsertOfEdgeWithInsertCommandUnsafe() {

OrientEdge insertedEdge = database.command(new OCommandSQL("insert into E set in = #9:0, out = #9:1, a = 33 unsafe")).execute();
Assert.assertNotNull(insertedEdge);

Integer confirmDeleted = database.command(new OCommandSQL("delete from " + insertedEdge.getIdentity() + " unsafe")).execute();
Assert.assertEquals(confirmDeleted.intValue(), 1);
}

}

0 comments on commit 0396567

Please sign in to comment.