Search in sources :

Example 1 with OResultSet

use of com.orientechnologies.orient.core.sql.executor.OResultSet in project wicket-orientdb by OrienteerBAP.

the class OrientDBSettingsTest method testInstallingHooks.

@Test
public void testInstallingHooks() {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    OClass clazz = db.getMetadata().getSchema().getClass("TestHooks");
    assertNotNull(clazz);
    try (OResultSet result = db.query("select from TestHooks")) {
        assertTrue(result.hasNext());
        ODocument doc = (ODocument) result.next().getElement().orElse(null);
        assertNotNull(doc);
        assertEquals("HOOK", doc.field("name"));
    }
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.executor.OResultSet) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 2 with OResultSet

use of com.orientechnologies.orient.core.sql.executor.OResultSet in project wicket-orientdb by OrienteerBAP.

the class OSchemaHelper method oDocument.

/**
 * Create an {@link ODocument} if required of an current class.
 * Existance of an document checked by specified primary key field name and required value
 * @param pkField primary key field name
 * @param pkValue required primary key value
 * @return this helper
 */
public OSchemaHelper oDocument(String pkField, Object pkValue) {
    checkOClass();
    try (OResultSet result = db.query("select from " + lastClass.getName() + " where " + pkField + " = ? limit 1", pkValue)) {
        if (result.hasNext()) {
            lastDocument = (ODocument) result.next().toElement();
        } else {
            lastDocument = new ODocument(lastClass);
            lastDocument.field(pkField, pkValue);
        }
    }
    return this;
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.executor.OResultSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with OResultSet

use of com.orientechnologies.orient.core.sql.executor.OResultSet in project wicket-orientdb by OrienteerBAP.

the class TestInAppOrientDBCompatibility method testInHook.

@Test
public void testInHook() throws Exception {
    ODatabaseSession db = wicket.getTester().getDatabaseSession();
    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();
            try (OResultSet result = database.query(script)) {
                if (result.hasNext()) {
                    iDocument.field("c", (Object) result.next().getProperty("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();
    doc.reload();
    assertEquals(3, (Object) doc.field("a"));
    assertEquals(3, (Object) doc.field("b"));
    assertEquals(6, (Object) doc.field("c"));
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.executor.OResultSet) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ODocumentHookAbstract(com.orientechnologies.orient.core.hook.ODocumentHookAbstract) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 4 with OResultSet

use of com.orientechnologies.orient.core.sql.executor.OResultSet 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) {
        ODatabaseSession db = OrientDbWebSession.get().getDatabaseSession();
        try (OResultSet result = db.query(queryManager.getCountSql(), prepareParams())) {
            if (result.hasNext()) {
                Number sizeNumber = result.next().getProperty("count");
                size = sizeNumber != null ? sizeNumber.longValue() : 0;
            } else {
                size = 0L;
            }
        }
    }
    return size;
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.executor.OResultSet) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession)

Example 5 with OResultSet

use of com.orientechnologies.orient.core.sql.executor.OResultSet in project wicket-orientdb by OrienteerBAP.

the class OQueryModel method load.

@SuppressWarnings("unchecked")
protected List<K> load() {
    ODatabaseSession db = OrientDbWebSession.get().getDatabaseSession();
    String sql = prepareSql(null, null);
    try (OResultSet result = db.query(sql, prepareParams())) {
        if (transformer != null) {
            return result.elementStream().map(e -> transformer.apply(e)).collect(Collectors.toCollection(LinkedList::new));
        }
        return result.elementStream().map(e -> (K) e).collect(Collectors.toCollection(LinkedList::new));
    }
}
Also used : java.util(java.util) ConvertToODocumentFunction(ru.ydn.wicket.wicketorientdb.utils.ConvertToODocumentFunction) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) DocumentWrapperTransformer(ru.ydn.wicket.wicketorientdb.utils.DocumentWrapperTransformer) StringQueryManager(ru.ydn.wicket.wicketorientdb.utils.query.StringQueryManager) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) IFilterCriteriaManager(ru.ydn.wicket.wicketorientdb.utils.query.filter.IFilterCriteriaManager) IModel(org.apache.wicket.model.IModel) FilterCriteriaType(ru.ydn.wicket.wicketorientdb.utils.query.filter.FilterCriteriaType) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Function(com.google.common.base.Function) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) OrientDbWebSession(ru.ydn.wicket.wicketorientdb.OrientDbWebSession) Model(org.apache.wicket.model.Model) LoadableDetachableModel(org.apache.wicket.model.LoadableDetachableModel) IQueryManager(ru.ydn.wicket.wicketorientdb.utils.query.IQueryManager) IFilterCriteria(ru.ydn.wicket.wicketorientdb.utils.query.filter.IFilterCriteria) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) OSchemaUtils(ru.ydn.wicket.wicketorientdb.utils.OSchemaUtils) GetObjectFunction(ru.ydn.wicket.wicketorientdb.utils.GetObjectFunction) ORecord(com.orientechnologies.orient.core.record.ORecord) OResultSet(com.orientechnologies.orient.core.sql.executor.OResultSet) SortOrder(org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder) OElement(com.orientechnologies.orient.core.record.OElement) OResultSet(com.orientechnologies.orient.core.sql.executor.OResultSet) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession)

Aggregations

OResultSet (com.orientechnologies.orient.core.sql.executor.OResultSet)5 ODatabaseSession (com.orientechnologies.orient.core.db.ODatabaseSession)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)4 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 Test (org.junit.Test)2 Function (com.google.common.base.Function)1 Maps (com.google.common.collect.Maps)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ODocumentHookAbstract (com.orientechnologies.orient.core.hook.ODocumentHookAbstract)1 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)1 OElement (com.orientechnologies.orient.core.record.OElement)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 Serializable (java.io.Serializable)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 SortOrder (org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder)1 IModel (org.apache.wicket.model.IModel)1 LoadableDetachableModel (org.apache.wicket.model.LoadableDetachableModel)1 Model (org.apache.wicket.model.Model)1