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