Search in sources :

Example 31 with OSQLSynchQuery

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

the class JSONTest method testToJSONWithNoLazyLoadAndClosedDatabase.

@Test
public void testToJSONWithNoLazyLoadAndClosedDatabase() {
    List<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select * from Profile where name = 'Barack' and surname = 'Obama'")).execute();
    for (ODocument doc : result) {
        doc.reload("*:0");
        String jsonFull = doc.toJSON();
        ORID rid = doc.getIdentity();
        database.close();
        database.open("admin", "admin");
        doc = database.load(rid);
        doc.setLazyLoad(false);
        doc.reload("*:0");
        database.close();
        String jsonLoaded = doc.toJSON();
        Assert.assertEquals(jsonLoaded, jsonFull);
        database.open("admin", "admin");
        doc = database.load(rid);
        doc.setLazyLoad(false);
        doc.load("*:0");
        database.close();
        jsonLoaded = doc.toJSON();
        Assert.assertEquals(jsonLoaded, jsonFull);
    }
    if (database.isClosed())
        database.open("admin", "admin");
    for (ODocument doc : result) {
        doc.reload("*:1");
        String jsonFull = doc.toJSON();
        ORID rid = doc.getIdentity();
        database.close();
        database.open("admin", "admin");
        doc = database.load(rid);
        doc.setLazyLoad(false);
        doc.reload("*:1");
        database.close();
        String jsonLoaded = doc.toJSON();
        Assert.assertEquals(jsonFull, jsonLoaded);
        database.open("admin", "admin");
        doc = database.load(rid);
        doc.setLazyLoad(false);
        doc.load("*:1");
        database.close();
        jsonLoaded = doc.toJSON();
        Assert.assertEquals(jsonFull, jsonLoaded);
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 32 with OSQLSynchQuery

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

the class IndexTest method testUseOfIndex.

@Test(dependsOnMethods = "testDuplicatedIndexOnUnique")
public void testUseOfIndex() {
    final List<Profile> result = database.command(new OSQLSynchQuery<Profile>("select * from Profile where nick = 'Jay'")).execute();
    Assert.assertFalse(result.isEmpty());
    Profile record;
    for (int i = 0; i < result.size(); ++i) {
        record = result.get(i);
        OrientTest.printRecord(i, record);
        Assert.assertTrue(record.getName().toString().equalsIgnoreCase("Jay"));
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) Profile(com.orientechnologies.orient.test.domain.whiz.Profile) Test(org.testng.annotations.Test) DatabaseAbstractTest(com.orientechnologies.DatabaseAbstractTest) OrientTest(com.orientechnologies.orient.test.database.base.OrientTest)

Example 33 with OSQLSynchQuery

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

the class IndexTest method testIndexReturnOnlySpecifiedClass.

@Test(dependsOnMethods = "createInheritanceIndex")
public void testIndexReturnOnlySpecifiedClass() throws Exception {
    List<ODocument> result;
    ODatabaseDocument db = database.getUnderlying();
    result = db.command(new OSQLSynchQuery("select * from ChildTestClass where testParentProperty = 10")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(10L, result.get(0).field("testParentProperty"));
    result = db.command(new OCommandSQL("select * from AnotherChildTestClass where testParentProperty = 11")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(11L, result.get(0).field("testParentProperty"));
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test) DatabaseAbstractTest(com.orientechnologies.DatabaseAbstractTest) OrientTest(com.orientechnologies.orient.test.database.base.OrientTest)

Example 34 with OSQLSynchQuery

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

the class IndexTest method testNullIndexKeysSupport.

public void testNullIndexKeysSupport() {
    final ODatabaseDocumentTx databaseDocumentTx = (ODatabaseDocumentTx) database.getUnderlying();
    final OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    final OClass clazz = schema.createClass("NullIndexKeysSupport", 1, null);
    clazz.createProperty("nullField", OType.STRING);
    ODocument metadata = new ODocument();
    metadata.field("ignoreNullValues", false);
    clazz.createIndex("NullIndexKeysSupportIndex", INDEX_TYPE.NOTUNIQUE.toString(), null, metadata, new String[] { "nullField" });
    for (int i = 0; i < 20; i++) {
        if (i % 5 == 0) {
            ODocument document = new ODocument("NullIndexKeysSupport");
            document.field("nullField", (Object) null);
            document.save();
        } else {
            ODocument document = new ODocument("NullIndexKeysSupport");
            document.field("nullField", "val" + i);
            document.save();
        }
    }
    List<ODocument> result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupport where nullField = 'val3'"));
    Assert.assertEquals(result.size(), 1);
    Assert.assertEquals(result.get(0).field("nullField"), "val3");
    final String query = "select from NullIndexKeysSupport where nullField is null";
    result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from NullIndexKeysSupport where nullField is null"));
    Assert.assertEquals(result.size(), 4);
    for (ODocument document : result) Assert.assertNull(document.field("nullField"));
    final ODocument explain = databaseDocumentTx.command(new OCommandSQL("explain " + query)).execute();
    Assert.assertTrue(explain.<Set<String>>field("involvedIndexes").contains("NullIndexKeysSupportIndex"));
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 35 with OSQLSynchQuery

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

the class IndexTest method testIndexPaginationTest.

public void testIndexPaginationTest() {
    ODatabaseDocumentTx databaseDocumentTx = (ODatabaseDocumentTx) database.getUnderlying();
    final OSchema schema = databaseDocumentTx.getMetadata().getSchema();
    final OClass indexPaginationTest = schema.createClass("IndexPaginationTestClass", 1, null);
    indexPaginationTest.createProperty("prop", OType.INTEGER);
    indexPaginationTest.createIndex("IndexPaginationTest", INDEX_TYPE.UNIQUE, "prop", "@rid");
    List<ORID> rids = new ArrayList<ORID>();
    for (int i = 99; i >= 0; i--) {
        final ODocument document = new ODocument("IndexPaginationTestClass");
        document.field("prop", i / 2);
        document.save();
        rids.add(document.getIdentity());
    }
    List<ODocument> result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from index:IndexPaginationTest order by key limit 5"));
    Assert.assertEquals(result.size(), 5);
    int lastKey = -1;
    ORID lastRid = null;
    for (ODocument document : result) {
        document.setLazyLoad(false);
        if (lastKey > -1)
            Assert.assertTrue(lastKey <= (Integer) document.<OCompositeKey>field("key").getKeys().get(0));
        lastKey = (Integer) document.<OCompositeKey>field("key").getKeys().get(0);
        lastRid = document.field("rid");
        Assert.assertTrue(rids.remove(document.<OIdentifiable>field("rid").getIdentity()));
    }
    while (true) {
        result = databaseDocumentTx.query(new OSQLSynchQuery<ODocument>("select from index:IndexPaginationTest where key > ? order by key limit 5"), new OCompositeKey(lastKey, lastRid));
        if (result.isEmpty())
            break;
        Assert.assertEquals(result.size(), 5);
        for (ODocument document : result) {
            document.setLazyLoad(false);
            if (lastKey > -1)
                Assert.assertTrue(lastKey <= (Integer) document.<OCompositeKey>field("key").getKeys().get(0));
            lastKey = (Integer) document.<OCompositeKey>field("key").getKeys().get(0);
            lastRid = document.field("rid", OType.LINK);
            Assert.assertTrue(rids.remove(document.<ORID>field("rid", OType.LINK)));
        }
    }
    Assert.assertTrue(rids.isEmpty());
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORID(com.orientechnologies.orient.core.id.ORID) 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