Search in sources :

Example 26 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class LuceneTransactionQueryTest method txRemoveTest.

@Test
public void txRemoveTest() {
    db.begin();
    ODocument doc = new ODocument("c1");
    doc.field("p1", "abc");
    OIndex<?> index = db.getMetadata().getIndexManager().getIndex("C1.p1");
    db.save(doc);
    String query = "select from C1 where p1 lucene \"abc\" ";
    List<ODocument> vertices = ODatabaseRecordThreadLocal.INSTANCE.get().command(new OSQLSynchQuery<ODocument>(query)).execute();
    Assert.assertEquals(vertices.size(), 1);
    Assert.assertEquals(index.getSize(), 1);
    db.commit();
    query = "select from C1 where p1 lucene \"abc\" ";
    vertices = db.command(new OSQLSynchQuery<ODocument>(query)).execute();
    Assert.assertEquals(vertices.size(), 1);
    Assert.assertEquals(index.getSize(), 1);
    db.begin();
    doc = new ODocument("c1");
    doc.field("p1", "abc");
    db.delete(vertices.get(0));
    query = "select from C1 where p1 lucene \"abc\" ";
    vertices = db.command(new OSQLSynchQuery<ODocument>(query)).execute();
    Collection coll = (Collection) index.get("abc");
    Assert.assertEquals(vertices.size(), 0);
    Assert.assertEquals(coll.size(), 0);
    Iterator iterator = coll.iterator();
    int i = 0;
    while (iterator.hasNext()) {
        iterator.next();
        i++;
    }
    Assert.assertEquals(i, 0);
    Assert.assertEquals(index.getSize(), 0);
    db.rollback();
    query = "select from C1 where p1 lucene \"abc\" ";
    vertices = db.command(new OSQLSynchQuery<ODocument>(query)).execute();
    Assert.assertEquals(vertices.size(), 1);
    Assert.assertEquals(index.getSize(), 1);
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) Iterator(java.util.Iterator) Collection(java.util.Collection) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 27 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class GraphDatabaseTest method nestedQuery.

@SuppressWarnings("unchecked")
public void nestedQuery() {
    Vertex countryVertex1 = database.addVertex(null, "name", "UK", "area", "Europe", "code", "2");
    Vertex cityVertex1 = database.addVertex(null, "name", "leicester", "lat", "52.64640", "long", "-1.13159");
    Vertex cityVertex2 = database.addVertex(null, "name", "manchester", "lat", "53.47497", "long", "-2.25769");
    database.addEdge(null, countryVertex1, cityVertex1, "owns");
    database.addEdge(null, countryVertex1, cityVertex2, "owns");
    database.commit();
    String subquery = "select out('owns') from V where name = 'UK'";
    List<OIdentifiable> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(subquery));
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(((Collection) ((ODocument) result.get(0)).field("out")).size(), 2);
    subquery = "select expand(out('owns')) from V where name = 'UK'";
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(subquery));
    Assert.assertEquals(result.size(), 2);
    for (int i = 0; i < result.size(); i++) {
        // System.out.println("uno: " + result.get(i));
        Assert.assertTrue(((ODocument) result.get(i).getRecord()).containsField("lat"));
    }
    String query = "select name, lat, long, distance(lat,long,51.5,0.08) as distance from (select expand(out('owns')) from V where name = 'UK') order by distance";
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query));
    Assert.assertEquals(result.size(), 2);
    for (int i = 0; i < result.size(); i++) {
        // System.out.println("dos: " + result.get(i));
        Assert.assertTrue(((ODocument) result.get(i).getRecord()).containsField("lat"));
        Assert.assertTrue(((ODocument) result.get(i).getRecord()).containsField("distance"));
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 28 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class GraphDatabaseTest method sqlNestedQueries.

@Test
public void sqlNestedQueries() {
    Vertex vertex1 = database.addVertex(null, "driver", "John");
    Vertex vertex2 = database.addVertex(null, "car", "ford");
    Vertex targetVertex = database.addVertex(null, "car", "audi");
    Edge edge = database.addEdge(null, vertex1, vertex2, "E");
    edge.setProperty("color", "red");
    edge.setProperty("action", "owns");
    edge = database.addEdge(null, vertex1, targetVertex, "E");
    edge.setProperty("color", "red");
    edge.setProperty("action", "wants");
    database.commit();
    String query1 = "select driver from V where out().car contains 'ford'";
    List<ODocument> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query1));
    Assert.assertEquals(result.size(), 1);
    String query2 = "select driver from V where outE()[color='red'].inV().car contains 'ford'";
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query2));
    Assert.assertEquals(result.size(), 1);
    // TODO these tests are broken, they should test "contains" instead of "="
    String query3 = "select driver from V where outE()[action='owns'].inV().car = 'ford'";
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query3));
    Assert.assertEquals(result.size(), 1);
    String query4 = "select driver from V where outE()[color='red'][action='owns'].inV().car = 'ford'";
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>(query4));
    Assert.assertEquals(result.size(), 1);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Edge(com.tinkerpop.blueprints.Edge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 29 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class GraphDatabaseTest method populate.

@Test
public void populate() {
    OClass vehicleClass = database.createVertexType("GraphVehicle");
    database.createVertexType("GraphCar", vehicleClass);
    database.createVertexType("GraphMotocycle", "GraphVehicle");
    ODocument carNode = database.addVertex("class:GraphCar", "brand", "Hyundai", "model", "Coupe", "year", 2003).getRecord();
    ODocument motoNode = database.addVertex("class:GraphMotocycle", "brand", "Yamaha", "model", "X-City 250", "year", 2009).getRecord();
    database.commit();
    database.addEdge(null, database.getVertex(carNode), database.getVertex(motoNode), "E").save();
    List<ODocument> result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>("select from GraphVehicle"));
    Assert.assertEquals(result.size(), 2);
    for (ODocument v : result) {
        Assert.assertTrue(v.getSchemaClass().isSubClassOf(vehicleClass));
    }
    database.shutdown();
    database = new OrientGraph(url);
    database.setUseLightweightEdges(false);
    database.getRawGraph().getMetadata().getSchema().reload();
    result = database.getRawGraph().query(new OSQLSynchQuery<ODocument>("select from GraphVehicle"));
    Assert.assertEquals(result.size(), 2);
    Edge edge1 = null;
    Edge edge2 = null;
    for (ODocument v : result) {
        Assert.assertTrue(v.getSchemaClass().isSubClassOf("GraphVehicle"));
        if (v.getClassName().equals("GraphCar")) {
            Assert.assertEquals(database.getVertex(v).countEdges(Direction.OUT), 1);
            edge1 = database.getVertex(v).getEdges(Direction.OUT).iterator().next();
        } else {
            Assert.assertEquals(database.getVertex(v).countEdges(Direction.IN), 1);
            edge2 = database.getVertex(v).getEdges(Direction.IN).iterator().next();
        }
    }
    Assert.assertEquals(edge1, edge2);
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Edge(com.tinkerpop.blueprints.Edge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 30 with OSQLSynchQuery

use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.

the class JSONTest method testInnerDocCreation.

public void testInnerDocCreation() {
    ODocument adamDoc = new ODocument("InnerDocCreation");
    adamDoc.fromJSON("{\"name\":\"adam\"}");
    adamDoc.save();
    ODocument eveDoc = new ODocument("InnerDocCreation");
    eveDoc.fromJSON("{\"@type\":\"d\",\"name\":\"eve\",\"friends\":[" + adamDoc.toJSON() + "]}");
    eveDoc.save();
    Map<ORID, ODocument> contentMap = new HashMap<ORID, ODocument>();
    ODocument adam = new ODocument("InnerDocCreation");
    adam.field("name", "adam");
    contentMap.put(adamDoc.getIdentity(), adam);
    ODocument eve = new ODocument("InnerDocCreation");
    eve.field("name", "eve");
    List<ORID> friends = new ArrayList<ORID>();
    friends.add(adamDoc.getIdentity());
    eve.field("friends", friends);
    contentMap.put(eveDoc.getIdentity(), eve);
    Map<ORID, List<ORID>> traverseMap = new HashMap<ORID, List<ORID>>();
    List<ORID> adamTraverse = new ArrayList<ORID>();
    adamTraverse.add(adamDoc.getIdentity());
    traverseMap.put(adamDoc.getIdentity(), adamTraverse);
    List<ORID> eveTraverse = new ArrayList<ORID>();
    eveTraverse.add(eveDoc.getIdentity());
    eveTraverse.add(adamDoc.getIdentity());
    traverseMap.put(eveDoc.getIdentity(), eveTraverse);
    for (ODocument o : database.browseClass("InnerDocCreation")) {
        ODocument content = contentMap.get(o.getIdentity());
        Assert.assertTrue(content.hasSameContentOf(o));
    }
    for (ODocument o : database.browseClass("InnerDocCreation")) {
        List<ORID> traverse = traverseMap.remove(o.getIdentity());
        for (OIdentifiable id : new OSQLSynchQuery<ODocument>("traverse * from " + o.getIdentity().toString())) {
            Assert.assertTrue(traverse.remove(id.getIdentity()));
        }
        Assert.assertTrue(traverse.isEmpty());
    }
    Assert.assertTrue(traverseMap.isEmpty());
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORID(com.orientechnologies.orient.core.id.ORID) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)530 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)445 Test (org.testng.annotations.Test)287 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)83 Test (org.junit.Test)73 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)59 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)53 ORID (com.orientechnologies.orient.core.id.ORID)36 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)32 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)23 HashMap (java.util.HashMap)23 List (java.util.List)22 ORecordId (com.orientechnologies.orient.core.id.ORecordId)19 Profile (com.orientechnologies.orient.test.domain.whiz.Profile)19 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)16 OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)15 Collection (java.util.Collection)15 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)14 OrientTest (com.orientechnologies.orient.test.database.base.OrientTest)13 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)12