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();
}
}
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);
}
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);
}
}
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);
}
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;
}
Aggregations