Skip to content

Commit

Permalink
Fix CREATE EDGE to take into account useClassForEdgeLabel and useVert…
Browse files Browse the repository at this point in the history
…exFieldsForEdgeLabels flags
  • Loading branch information
luigidellaquila committed Oct 9, 2015
1 parent fce93cf commit 24b3065
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 20 deletions.
Expand Up @@ -30,23 +30,11 @@
import com.orientechnologies.orient.core.exception.OConcurrentModificationException; import com.orientechnologies.orient.core.exception.OConcurrentModificationException;
import com.orientechnologies.orient.core.metadata.OMetadataInternal; import com.orientechnologies.orient.core.metadata.OMetadataInternal;
import com.orientechnologies.orient.core.metadata.schema.OClass; import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.sql.OCommandExecutorSQLRetryAbstract; import com.orientechnologies.orient.core.sql.*;
import com.orientechnologies.orient.core.sql.OCommandParameters;
import com.orientechnologies.orient.core.sql.OCommandSQLParsingException;
import com.orientechnologies.orient.core.sql.OSQLEngine;
import com.orientechnologies.orient.core.sql.OSQLHelper;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime; import com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime;
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph; import com.tinkerpop.blueprints.impls.orient.*;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType; import java.util.*;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;


/** /**
* SQL CREATE EDGE command. * SQL CREATE EDGE command.
Expand All @@ -60,6 +48,7 @@ public class OCommandExecutorSQLCreateEdge extends OCommandExecutorSQLRetryAbstr
private String from; private String from;
private String to; private String to;
private OClass clazz; private OClass clazz;
private String edgeLabel;
private String clusterName; private String clusterName;
private List<OPair<String, Object>> fields; private List<OPair<String, Object>> fields;
private int batch = 100; private int batch = 100;
Expand All @@ -85,7 +74,8 @@ public OCommandExecutorSQLCreateEdge parse(final OCommandRequest iRequest) {


String className = null; String className = null;


String temp = parseOptionalWord(true); String tempLower = parseOptionalWord(false);
String temp = tempLower == null ? null : tempLower.toUpperCase();


while (temp != null) { while (temp != null) {
if (temp.equals("CLUSTER")) { if (temp.equals("CLUSTER")) {
Expand Down Expand Up @@ -113,8 +103,13 @@ public OCommandExecutorSQLCreateEdge parse(final OCommandRequest iRequest) {
batch = Integer.parseInt(temp); batch = Integer.parseInt(temp);


} else if (className == null && temp.length() > 0) { } else if (className == null && temp.length() > 0) {
className = temp; className = tempLower;
clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(className); OrientBaseGraph graph = OrientBaseGraph.getActiveGraph();
if (graph != null && graph.isUseClassForEdgeLabel()) {
clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass(temp);
} else {
clazz = ((OMetadataInternal) database.getMetadata()).getImmutableSchemaSnapshot().getClass("E");
}
} }


temp = parseOptionalWord(true); temp = parseOptionalWord(true);
Expand All @@ -132,6 +127,7 @@ public OCommandExecutorSQLCreateEdge parse(final OCommandRequest iRequest) {
if (clazz == null) if (clazz == null)
throw new OCommandSQLParsingException("Class '" + className + "' was not found"); throw new OCommandSQLParsingException("Class '" + className + "' was not found");


edgeLabel = className;
} finally { } finally {
textRequest.setText(originalQuery); textRequest.setText(originalQuery);
} }
Expand Down Expand Up @@ -186,7 +182,7 @@ public List<Object> call(OrientBaseGraph graph) {
fields = OPair.convertFromMap(content.toMap()); fields = OPair.convertFromMap(content.toMap());
} }


edge = fromVertex.addEdge(null, toVertex, clsName, clusterName, fields); edge = fromVertex.addEdge(null, toVertex, edgeLabel, clusterName, fields);


if (fields != null && !fields.isEmpty()) { if (fields != null && !fields.isEmpty()) {
if (edge.isLightweight()) if (edge.isLightweight())
Expand Down
@@ -0,0 +1,63 @@
package com.orientechnologies.orient.graph.sql;

import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.impls.orient.OrientElement;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
import org.junit.Test;

import static org.junit.Assert.*;

public class TestGraphCreateEdgeWithoutClass {

@Test
public void testCreateEdgeWithoutClass() {
OrientGraph graph = new OrientGraph("memory:" + TestGraphCreateEdgeWithoutClass.class.getSimpleName());
graph.setUseClassForEdgeLabel(false);
graph.setUseVertexFieldsForEdgeLabels(true);
try {



OrientVertex test = graph.addVertex("class:V");
test.setProperty("name", "foo");
test.save();
OrientVertex test1 = graph.addVertex("class:V");
test1.setProperty("name", "bar");
test1.save();
graph.commit();
graph
.command(new OCommandSQL(
"create edge FooBar from (select from V where name='foo') to (select from V where name = 'bar')"))
.execute();
graph.commit();

Iterable<OrientElement> res = graph
.command(
new OSQLSynchQuery("select out as f1, out_ as f2, out_edgetestedge as f3, out_FooBar as f4, outE() as f5 from v where name = 'foo'"))
.execute();


boolean found = false;
for (OrientElement oDocument : res) {
assertNull(oDocument.getRecord().field("f1"));
assertNull(oDocument.getRecord().field("f2"));
assertNull(oDocument.getRecord().field("f3"));
assertNotNull(oDocument.getRecord().field("f4"));
Iterable<ODocument> f5 = oDocument.getRecord().field("f5");
assertNotNull(f5);
for(ODocument e:f5) {
assertEquals(e.field("label"), "FooBar");
found = true;
}
}
assertTrue(found);

} finally {
graph.drop();
}
}

}

0 comments on commit 24b3065

Please sign in to comment.