Search in sources :

Example 1 with OCommandSQL

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

the class AbstractServerClusterInsertTest method recreateIndexNode2.

protected void recreateIndexNode2() {
    // RE-CREATE INDEX ON NODE 1
    ServerRun server = serverInstance.get(1);
    ODatabaseDocumentTx database = poolFactory.get(getDatabaseURL(server), "admin", "admin").acquire();
    try {
        Object result = database.command(new OCommandSQL("create index Person.name on Person (name) unique")).execute();
        System.out.println("recreateIndexNode2: Node2 created index: " + result);
        Assert.assertEquals(expected, ((Number) result).intValue());
    } catch (ODistributedOperationException t) {
        for (ServerRun s : serverInstance) {
            final ODatabaseDocumentTx db = new ODatabaseDocumentTx(getDatabaseURL(s)).open("admin", "admin");
            try {
                List<ODocument> result = db.command(new OCommandSQL("select count(*) as count from Person where name is not null")).execute();
                Assert.assertEquals(expected, ((Number) result.get(0).field("count")).longValue());
                final OClass person = db.getMetadata().getSchema().getClass("Person");
                final int[] clIds = person.getPolymorphicClusterIds();
                long tot = 0;
                for (int clId : clIds) {
                    long count = db.countClusterElements(clId);
                    System.out.println("Cluster " + clId + " record: " + count);
                    tot += count;
                }
                Assert.assertEquals(expected, tot);
            } finally {
                db.close();
            }
        }
        database.activateOnCurrentThread();
        throw t;
    } finally {
        database.close();
    }
    // CHECK ON NODE 1
    server = serverInstance.get(0);
    database = poolFactory.get(getDatabaseURL(server), "admin", "admin").acquire();
    try {
        final long indexSize = database.getMetadata().getIndexManager().getIndex("Person.name").getSize();
        Assert.assertEquals(expected, indexSize);
        System.out.println("recreateIndexNode2: Node1 has the index too, ok");
    } finally {
        database.close();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODistributedOperationException(com.orientechnologies.orient.server.distributed.task.ODistributedOperationException)

Example 2 with OCommandSQL

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

the class OrientDatabase method increment.

@Override
public int increment(DatabaseObject object, String field, int value) throws DatabaseException {
    ODocument doc = ((DatabaseObjectImpl) object).document;
    String query = format(Lang.replace(IncrementQuery, Tokens.Value, String.valueOf(value)), doc.getClassName(), field);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(Fields.Id, doc.field(Fields.Id));
    OResultSet<ODocument> result = db.command(new OCommandSQL(query)).execute(params);
    ODocument document = (ODocument) result.get(0);
    return document.field(field);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with OCommandSQL

use of com.orientechnologies.orient.core.sql.OCommandSQL 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 OCommandSQL

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

the class OrientDatabase method delete.

@Override
public int delete(String eType, Object id) throws DatabaseException {
    checkNotNull(eType);
    if (id == null) {
        throw new DatabaseException("can't delete object (missing object id)");
    }
    if (!db.getMetadata().getSchema().existsClass(eType)) {
        return 0;
    }
    OCommandSQL command = new OCommandSQL(format(DeleteQuery, eType));
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(Fields.Id, id);
    return db.command(command).execute(params);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) DatabaseException(com.bluenimble.platform.db.DatabaseException)

Example 5 with OCommandSQL

use of com.orientechnologies.orient.core.sql.OCommandSQL in project divide by HiddenStage.

the class OrientDBDao method query.

@Override
public List<TransientObject> query(Query query) throws DAOException {
    checkDb();
    List<TransientObject> list = new ArrayList<TransientObject>();
    OTransaction transaction = db.getTransaction();
    transaction.begin();
    try {
        String q = query.getSQL();
        System.out.println("OrientDB_Query: " + q);
        if (query.getAction().equals(QueryBuilder.QueryAction.SELECT)) {
            List<ODocument> objects = db.query(new OSQLSynchQuery<ODocument>(q));
            for (ODocument w : objects) {
                list.add(new ODocumentWrapper(w).toObject(TransientObject.class));
            }
        }
        if (query.getAction().equals(QueryBuilder.QueryAction.DELETE)) {
            // List<ODocument> objects = db.command(new OCommandSQL("delete from io.divide.dao.TestObject1 RETURN BEFORE")).execute();
            Integer objects = db.command(new OCommandSQL(q)).execute();
            TransientObject o = new EmptyTO();
            o.put("count", objects);
            list.add(o);
            System.out.println("Delete: " + objects);
        }
        transaction.commit();
        transaction.close();
    } catch (Exception e) {
        transaction.rollback();
        transaction.close();
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OTransaction(com.orientechnologies.orient.core.tx.OTransaction) TransientObject(io.divide.shared.transitory.TransientObject) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)646 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)440 Test (org.junit.Test)109 Test (org.testng.annotations.Test)93 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)85 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)83 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)81 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)65 ORID (com.orientechnologies.orient.core.id.ORID)65 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)54 Set (java.util.Set)53 HashMap (java.util.HashMap)48 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)42 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)42 List (java.util.List)27 Before (org.junit.Before)25 OStorage (com.orientechnologies.orient.core.storage.OStorage)23 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)23 OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)22 ORecordId (com.orientechnologies.orient.core.id.ORecordId)21