Search in sources :

Example 26 with OCommandSQL

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

the class IndexTest method testTransactionUniqueIndexTestWithDotNameOne.

public void testTransactionUniqueIndexTestWithDotNameOne() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(database.getURL());
    db.open("admin", "admin");
    if (!db.getMetadata().getSchema().existsClass("TransactionUniqueIndexWithDotTest")) {
        final OClass termClass = db.getMetadata().getSchema().createClass("TransactionUniqueIndexWithDotTest", 1, null);
        termClass.createProperty("label", OType.STRING).createIndex(INDEX_TYPE.UNIQUE);
        db.getMetadata().getSchema().save();
    }
    ODocument docOne = new ODocument("TransactionUniqueIndexWithDotTest");
    docOne.field("label", "A");
    docOne.save();
    final List<ODocument> resultBeforeCommit = db.query(new OSQLSynchQuery<ODocument>("select from  index:TransactionUniqueIndexWithDotTest.label"));
    Assert.assertEquals(resultBeforeCommit.size(), 1);
    long countClassBefore = db.countClass("TransactionUniqueIndexWithDotTest");
    db.begin();
    try {
        ODocument docTwo = new ODocument("TransactionUniqueIndexWithDotTest");
        docTwo.field("label", "A");
        docTwo.save();
        db.commit();
        Assert.fail();
    } catch (ORecordDuplicatedException oie) {
    }
    Assert.assertEquals(((List<ODocument>) db.command(new OCommandSQL("select from TransactionUniqueIndexWithDotTest")).execute()).size(), countClassBefore);
    final List<ODocument> resultAfterCommit = db.query(new OSQLSynchQuery<ODocument>("select from  index:TransactionUniqueIndexWithDotTest.label"));
    Assert.assertEquals(resultAfterCommit.size(), 1);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) 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 27 with OCommandSQL

use of com.orientechnologies.orient.core.sql.OCommandSQL 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 28 with OCommandSQL

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

the class IndexTest method testIndexSQL.

@Test
public void testIndexSQL() {
    database.command(new OCommandSQL("create index idx unique METADATA { ignoreNullValues: false }")).execute();
    database.getMetadata().getIndexManager().reload();
    Assert.assertNotNull(database.getMetadata().getIndexManager().getIndex("idx"));
    final List<Long> positions = getValidPositions(3);
    database.command(new OCommandSQL("insert into index:IDX (key,rid) values (10,#3:" + positions.get(0) + ')')).execute();
    database.command(new OCommandSQL("insert into index:IDX (key,rid) values (20,#3:" + positions.get(1) + ')')).execute();
    List<ODocument> result = database.command(new OCommandSQL("select from index:IDX")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(result.size(), 2);
    for (ODocument d : result) {
        Assert.assertTrue(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
        if (d.field("key").equals(10))
            Assert.assertEquals(d.rawField("rid"), new ORecordId(3, positions.get(0)));
        else if (d.field("key").equals(20))
            Assert.assertEquals(d.rawField("rid"), new ORecordId(3, positions.get(1)));
        else
            Assert.assertTrue(false);
    }
    result = database.command(new OCommandSQL("select key, rid from index:IDX")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(result.size(), 2);
    for (ODocument d : result) {
        Assert.assertTrue(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
        if (d.field("key").equals(10))
            Assert.assertEquals(d.rawField("rid"), new ORecordId(3, positions.get(0)));
        else if (d.field("key").equals(20))
            Assert.assertEquals(d.rawField("rid"), new ORecordId(3, positions.get(1)));
        else
            Assert.assertTrue(false);
    }
    result = database.command(new OCommandSQL("select key from index:IDX")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(result.size(), 2);
    for (ODocument d : result) {
        Assert.assertTrue(d.containsField("key"));
        Assert.assertFalse(d.containsField("rid"));
    }
    result = database.command(new OCommandSQL("select rid from index:IDX")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(result.size(), 2);
    for (ODocument d : result) {
        Assert.assertFalse(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
    }
    result = database.command(new OCommandSQL("select rid from index:IDX where key = 10")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(result.size(), 1);
    for (ODocument d : result) {
        Assert.assertFalse(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ORecordId(com.orientechnologies.orient.core.id.ORecordId) 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 29 with OCommandSQL

use of com.orientechnologies.orient.core.sql.OCommandSQL 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 30 with OCommandSQL

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

the class IndexTest method testIndexRemoval.

@Test(dependsOnMethods = "linkedIndexedProperty")
public void testIndexRemoval() {
    List<ODocument> result = database.command(new OCommandSQL("select rid from index:Profile.nick")).execute();
    Assert.assertNotNull(result);
    ODocument firstProfile = null;
    for (ODocument d : result) {
        if (firstProfile == null)
            firstProfile = d.field("rid");
        Assert.assertFalse(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
    }
    result = database.command(new OCommandSQL("select rid from index:Profile.nick where key = ?")).execute(firstProfile.field("nick"));
    Assert.assertNotNull(result);
    Assert.assertEquals(result.get(0).field("rid"), firstProfile.getIdentity());
    firstProfile.delete();
    result = database.command(new OCommandSQL("select rid from index:Profile.nick where key = ?")).execute(firstProfile.field("nick"));
    Assert.assertTrue(result.isEmpty());
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) 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)

Aggregations

OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)646 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)440 Test (org.junit.Test)109 Test (org.testng.annotations.Test)93 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)85 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)83 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)81 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)65 ORID (com.orientechnologies.orient.core.id.ORID)65 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)54 Set (java.util.Set)53 HashMap (java.util.HashMap)48 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)42 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)42 List (java.util.List)27 Before (org.junit.Before)25 OStorage (com.orientechnologies.orient.core.storage.OStorage)23 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)23 OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)22 ORecordId (com.orientechnologies.orient.core.id.ORecordId)21