Search in sources :

Example 1 with OSQLSynchQuery

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

the class TestInAppOrientDBCompatibility method testInHook.

@Test
public void testInHook() throws Exception {
    ODatabaseDocument db = wicket.getTester().getDatabase();
    OSchema schema = db.getMetadata().getSchema();
    OClass oClass = schema.createClass("TestInHook");
    oClass.createProperty("a", OType.INTEGER);
    oClass.createProperty("b", OType.INTEGER);
    oClass.createProperty("c", OType.INTEGER);
    ODocument doc = new ODocument(oClass);
    doc.field("a", 2);
    doc.field("b", 2);
    doc.save();
    doc.reload();
    assertEquals(2, (Object) doc.field("a"));
    assertEquals(2, (Object) doc.field("b"));
    assertNull(doc.field("c"));
    db.registerHook(new ODocumentHookAbstract(db) {

        {
            setIncludeClasses("TestInHook");
        }

        @Override
        public void onRecordAfterCreate(ODocument iDocument) {
            onRecordAfterRead(iDocument);
        }

        @Override
        public void onRecordAfterRead(ODocument iDocument) {
            String script = "select sum(a, b) as value from " + iDocument.getIdentity();
            List<ODocument> calculated = database.query(new OSQLSynchQuery<Object>(script));
            if (calculated != null && !calculated.isEmpty()) {
                iDocument.field("c", (Object) calculated.get(0).field("value"));
            }
        }

        @Override
        public DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
            return DISTRIBUTED_EXECUTION_MODE.SOURCE_NODE;
        }
    });
    doc.reload();
    assertEquals(2, (Object) doc.field("a"));
    assertEquals(2, (Object) doc.field("b"));
    assertEquals(4, (Object) doc.field("c"));
    doc = new ODocument(oClass);
    doc.field("a", 3);
    doc.field("b", 3);
    doc.save();
    assertEquals(3, (Object) doc.field("a"));
    assertEquals(3, (Object) doc.field("b"));
    assertEquals(6, (Object) doc.field("c"));
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) DISTRIBUTED_EXECUTION_MODE(com.orientechnologies.orient.core.hook.ORecordHook.DISTRIBUTED_EXECUTION_MODE) Test(org.junit.Test)

Example 2 with OSQLSynchQuery

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

the class OQueryModel method size.

/**
 * Get the size of the data
 * @return results size
 */
public long size() {
    if (size == null) {
        ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
        OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>(queryManager.getCountSql());
        List<ODocument> ret = db.query(enhanceContextByVariables(query), prepareParams());
        if (ret != null && ret.size() > 0) {
            Number sizeNumber = ret.get(0).field("count");
            size = sizeNumber != null ? sizeNumber.longValue() : 0;
        } else {
            size = 0L;
        }
    }
    return size;
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with OSQLSynchQuery

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

the class OrientDatabase method _query.

private Object _query(String type, Query.Construct construct, final Query query, boolean returnBefore) throws DatabaseException {
    if (query == null) {
        return null;
    }
    if (Query.Construct.select.equals(construct)) {
        returnBefore = false;
    }
    boolean queryHasEntity = true;
    String entity = query.entity();
    if (Lang.isNullOrEmpty(entity)) {
        queryHasEntity = false;
        entity = type;
    }
    entity = checkNotNull(entity);
    tracer.log(Tracer.Level.Debug, "Query Entity {0}", entity);
    if (!db.getMetadata().getSchema().existsClass(entity)) {
        tracer.log(Tracer.Level.Debug, "Entity {0} not found", entity);
        return null;
    }
    String cacheKey = construct.name() + query.name();
    String sQuery = null;
    Map<String, Object> bindings = query.bindings();
    if (queryHasEntity && query.caching().cache(Target.meta) && !Lang.isNullOrEmpty(query.name())) {
        sQuery = (String) QueriesCache.get(cacheKey);
        tracer.log(Tracer.Level.Debug, "Query meta loaded from cache {0}", sQuery);
    }
    if (sQuery == null) {
        CompiledQuery cQuery = compile(entity, construct, query, returnBefore);
        sQuery = (String) cQuery.query();
        bindings = cQuery.bindings();
        if (queryHasEntity && query.caching().cache(Target.meta) && !Lang.isNullOrEmpty(query.name())) {
            QueriesCache.put(cacheKey, sQuery);
            tracer.log(Tracer.Level.Debug, "Query meta stored in cache {0}", sQuery);
        }
    }
    tracer.log(Tracer.Level.Debug, "\tQuery {0}", sQuery);
    tracer.log(Tracer.Level.Debug, "\tBindings: {0}", bindings);
    if (Query.Construct.select.equals(construct)) {
        OSQLSynchQuery<ODocument> q = new OSQLSynchQuery<ODocument>(sQuery);
        List<ODocument> result = db.command(q).execute(bindings);
        if (result == null || result.isEmpty()) {
            return null;
        }
        return result;
    } else {
        return db.command(new OCommandSQL(sQuery)).execute(bindings);
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) CompiledQuery(com.bluenimble.platform.db.query.CompiledQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with OSQLSynchQuery

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

the class OCommandExecutorSQLResultsetAbstract method parse.

/**
 * Compile the filter conditions only the first time.
 */
public OCommandExecutorSQLResultsetAbstract parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
    init(textRequest);
    if (iRequest instanceof OSQLSynchQuery) {
        request = (OSQLSynchQuery<ODocument>) iRequest;
    } else if (iRequest instanceof OSQLAsynchQuery)
        request = (OSQLAsynchQuery<ODocument>) iRequest;
    else {
        // BUILD A QUERY OBJECT FROM THE COMMAND REQUEST
        request = new OSQLSynchQuery<ODocument>(textRequest.getText());
        if (textRequest.getResultListener() != null)
            request.setResultListener(textRequest.getResultListener());
    }
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 5 with OSQLSynchQuery

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

the class OCommandExecutorSQLUpdateTest method testUpdateMergeWithIndex.

@Test
public void testUpdateMergeWithIndex() {
    final ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:OCommandExecutorSQLUpdateTestMergeWithIndex");
    db.create();
    try {
        db.command(new OCommandSQL("CREATE CLASS i_have_a_list ")).execute();
        db.command(new OCommandSQL("CREATE PROPERTY i_have_a_list.id STRING")).execute();
        db.command(new OCommandSQL("CREATE INDEX i_have_a_list.id ON i_have_a_list (id) UNIQUE")).execute();
        db.command(new OCommandSQL("CREATE PROPERTY i_have_a_list.types EMBEDDEDLIST STRING")).execute();
        db.command(new OCommandSQL("CREATE INDEX i_have_a_list.types ON i_have_a_list (types) NOTUNIQUE")).execute();
        db.command(new OCommandSQL("INSERT INTO i_have_a_list CONTENT {\"id\": \"the_id\", \"types\": [\"aaa\", \"bbb\"]}")).execute();
        Iterable result = db.query(new OSQLSynchQuery<Object>("SELECT * FROM i_have_a_list WHERE types = 'aaa'"));
        assertTrue(result.iterator().hasNext());
        db.command(new OCommandSQL("UPDATE i_have_a_list CONTENT {\"id\": \"the_id\", \"types\": [\"ccc\", \"bbb\"]} WHERE id = 'the_id'")).execute();
        result = db.query(new OSQLSynchQuery<Object>("SELECT * FROM i_have_a_list WHERE types = 'ccc'"));
        assertTrue(result.iterator().hasNext());
        result = db.query(new OSQLSynchQuery<Object>("SELECT * FROM i_have_a_list WHERE types = 'aaa'"));
        assertFalse(result.iterator().hasNext());
    } finally {
        db.close();
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) 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