Search in sources :

Example 41 with OSQLSynchQuery

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

the class JSONTest method testNestedLinkCreationFieldTypes.

public void testNestedLinkCreationFieldTypes() {
    ODocument jaimeDoc = new ODocument("NestedLinkCreationFieldTypes");
    jaimeDoc.field("name", "jaime");
    jaimeDoc.save();
    // The link between jaime and cersei is saved properly - the #2263 test case
    ODocument cerseiDoc = new ODocument("NestedLinkCreationFieldTypes");
    cerseiDoc.fromJSON("{\"@type\":\"d\",\"@fieldTypes\":\"valonqar=x\",\"name\":\"cersei\",\"valonqar\":" + jaimeDoc.getIdentity() + "}");
    cerseiDoc.save();
    // The link between jamie and tyrion is not saved properly
    ODocument tyrionDoc = new ODocument("NestedLinkCreationFieldTypes");
    tyrionDoc.fromJSON("{\"@type\":\"d\",\"name\":\"tyrion\",\"emergency_contact\":{\"@type\":\"d\", \"@fieldTypes\":\"contact=x\",\"relationship\":\"brother\",\"contact\":" + jaimeDoc.getIdentity() + "}}");
    tyrionDoc.save();
    final Map<ORID, ODocument> contentMap = new HashMap<ORID, ODocument>();
    ODocument jaime = new ODocument("NestedLinkCreationFieldTypes");
    jaime.field("name", "jaime");
    contentMap.put(jaimeDoc.getIdentity(), jaime);
    ODocument cersei = new ODocument("NestedLinkCreationFieldTypes");
    cersei.field("name", "cersei");
    cersei.field("valonqar", jaimeDoc.getIdentity());
    contentMap.put(cerseiDoc.getIdentity(), cersei);
    ODocument tyrion = new ODocument("NestedLinkCreationFieldTypes");
    tyrion.field("name", "tyrion");
    ODocument embeddedDoc = new ODocument();
    embeddedDoc.field("relationship", "brother");
    embeddedDoc.field("contact", jaimeDoc.getIdentity());
    tyrion.field("emergency_contact", embeddedDoc);
    contentMap.put(tyrionDoc.getIdentity(), tyrion);
    final Map<ORID, List<ORID>> traverseMap = new HashMap<ORID, List<ORID>>();
    List<ORID> jaimeTraverse = new ArrayList<ORID>();
    jaimeTraverse.add(jaimeDoc.getIdentity());
    traverseMap.put(jaimeDoc.getIdentity(), jaimeTraverse);
    List<ORID> cerseiTraverse = new ArrayList<ORID>();
    cerseiTraverse.add(cerseiDoc.getIdentity());
    cerseiTraverse.add(jaimeDoc.getIdentity());
    traverseMap.put(cerseiDoc.getIdentity(), cerseiTraverse);
    List<ORID> tyrionTraverse = new ArrayList<ORID>();
    tyrionTraverse.add(tyrionDoc.getIdentity());
    tyrionTraverse.add(jaimeDoc.getIdentity());
    traverseMap.put(tyrionDoc.getIdentity(), tyrionTraverse);
    for (ODocument o : database.browseClass("NestedLinkCreationFieldTypes")) {
        ODocument content = contentMap.get(o.getIdentity());
        Assert.assertTrue(content.hasSameContentOf(o));
        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)

Example 42 with OSQLSynchQuery

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

the class OSQLFunctionConvertTest method testSQLConversions.

@Test
public void testSQLConversions() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:testSQLConvert");
    try {
        db.create();
        db.command(new OCommandSQL("create class TestConversion")).execute();
        db.command(new OCommandSQL("insert into TestConversion set string = 'Jay', date = sysdate(), number = 33")).execute();
        ODocument doc = (ODocument) db.query(new OSQLSynchQuery<ODocument>("select from TestConversion limit 1")).get(0);
        db.command(new OCommandSQL("update TestConversion set selfrid = 'foo" + doc.getIdentity() + "'")).execute();
        List<ODocument> results = db.query(new OSQLSynchQuery<ODocument>("select string.asString() as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof String);
        results = db.query(new OSQLSynchQuery<ODocument>("select number.asDate() as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof Date);
        results = db.query(new OSQLSynchQuery<ODocument>("select number.asDateTime() as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof Date);
        results = db.query(new OSQLSynchQuery<ODocument>("select number.asInteger() as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof Integer);
        results = db.query(new OSQLSynchQuery<ODocument>("select number.asLong() as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof Long);
        results = db.query(new OSQLSynchQuery<ODocument>("select number.asFloat() as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof Float);
        results = db.query(new OSQLSynchQuery<ODocument>("select number.asDecimal() as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof BigDecimal);
        results = db.query(new OSQLSynchQuery<ODocument>("select number.convert('LONG') as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof Long);
        results = db.query(new OSQLSynchQuery<ODocument>("select number.convert('SHORT') as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof Short);
        results = db.query(new OSQLSynchQuery<ODocument>("select number.convert('DOUBLE') as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).field("convert") instanceof Double);
        results = db.query(new OSQLSynchQuery<ODocument>("select selfrid.substring(3).convert('LINK').string as convert from TestConversion"));
        assertNotNull(results);
        assertEquals(results.size(), 1);
        assertEquals(results.get(0).field("convert"), "Jay");
    } finally {
        db.drop();
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Date(java.util.Date) BigDecimal(java.math.BigDecimal) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 43 with OSQLSynchQuery

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

the class OCommandExecutorSQLSelectTest method testLimitOrdered.

@Test
public void testLimitOrdered() {
    OSQLSynchQuery sql = new OSQLSynchQuery("SELECT from alphabet ORDER BY letter LIMIT 9");
    List<ODocument> results = db.query(sql);
    assertEquals(9, results.size());
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 44 with OSQLSynchQuery

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

the class OCommandExecutorSQLSelectTest method testBacktick.

@Test
public void testBacktick() {
    OSQLSynchQuery sql = new OSQLSynchQuery("SELECT `foo-bar` as r from TestBacktick");
    List<ODocument> results = db.query(sql);
    assertEquals(results.size(), 1);
    ODocument doc = results.get(0);
    assertEquals(doc.field("r"), 10);
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 45 with OSQLSynchQuery

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

the class OCommandExecutorSQLSelectTest method testMatchesWithQuotes2.

@Test
public void testMatchesWithQuotes2() {
    // issue #5229
    OSQLSynchQuery sql = new OSQLSynchQuery("SELECT FROM matchesstuff WHERE (name matches '\\\\Qadm\\\\E.*' and not ( name matches '(.*)foo(.*)' ) )");
    List<ODocument> results = db.query(sql);
    assertEquals(results.size(), 1);
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

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