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);
}
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"));
}
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"));
}
}
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"));
}
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());
}
Aggregations