Skip to content

Instantly share code, notes, and snippets.

@mmacfadden
Created December 30, 2015 06:47
Show Gist options
  • Save mmacfadden/54ab17d39743a2da8d22 to your computer and use it in GitHub Desktop.
Save mmacfadden/54ab17d39743a2da8d22 to your computer and use it in GitHub Desktop.
package com.orientechnologies.orient.core.sql;
import static org.testng.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
@Test
public class SelectWithFilterTest {
private static String DB_STORAGE = "memory";
private static String DB_NAME = "SelectWithFilterTest";
ODatabaseDocumentTx db;
@BeforeClass
public void beforeClass() throws Exception {
db = new ODatabaseDocumentTx(DB_STORAGE + ":" + DB_NAME);
db.create();
// Create the schema
db.command(new OCommandSQL("CREATE class sub")).execute();
db.command(new OCommandSQL("CREATE property sub.prop1 STRING")).execute();
db.command(new OCommandSQL("CREATE property sub.prop2 STRING")).execute();
db.command(new OCommandSQL("CREATE class parent")).execute();
db.command(new OCommandSQL("CREATE property parent.subs EMBEDDEDLIST sub ")).execute();
// Add some data
ODocument sub1 = new ODocument("sub");
sub1.field("prop1", "A");
sub1.field("prop2", "X");
ODocument sub2 = new ODocument("sub");
sub2.field("prop1", "B");
sub2.field("prop2", "X");
ArrayList<ODocument> subs = new ArrayList<ODocument>();
subs.add(sub1);
subs.add(sub2);
ODocument parent = new ODocument("parent");
parent.field("subs", subs);
db.save(parent);
}
@AfterClass
public void afterClass() throws Exception {
db.close();
}
@Test
public void selectWithFilterOnSubDocument() throws Exception {
// Filter matches 0 elements. The "subs" field will be an empty
// HashSet. The conversion from a HashSet to an EMBEDDEDLIST
// works.
String sql1 = "SELECT subs[prop1 = '1'] FROM parent";
OSQLSynchQuery<ODocument> query1 = new OSQLSynchQuery<ODocument>(sql1);
List<ODocument> result1 = db.command(query1).execute();
System.out.println(result1.get(0).field("subs").getClass());
List<Object> list1 = result1.get(0).field("subs", OType.EMBEDDEDLIST);
assertEquals(0, list1.size());
// Filter matches 2 elements. The "subs" field will be a HashSet with
// 2 ODocuments in it. The conversion from a HashSet to an EMBEDDEDLIST
// works.
String sql2 = "SELECT subs[prop2 = 'X'] FROM parent";
OSQLSynchQuery<ODocument> query2 = new OSQLSynchQuery<ODocument>(sql2);
List<ODocument> result2 = db.command(query2).execute();
System.out.println(result2.get(0).field("subs").getClass());
List<Object> list2 = result2.get(0).field("subs", OType.EMBEDDEDLIST);
assertEquals(2, list2.size());
// Filter matches 1 element. The "subs" field will be an ODocument. The
// conversion from a HashSet to an EMBEDDEDLIST does not work. There
// will be a ClassCastException.
String sql3 = "SELECT subs[prop1 = 'A'] FROM parent";
OSQLSynchQuery<ODocument> query3 = new OSQLSynchQuery<ODocument>(sql3);
List<ODocument> result3 = db.command(query3).execute();
System.out.println(result3.get(0).field("subs").getClass());
List<Object> list3 = result3.get(0).field("subs", OType.EMBEDDEDLIST);
assertEquals(1, list3.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment