use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class PreparedStatementTest method testFunction.
@Test
public void testFunction() {
Map<String, Object> params = new HashMap<String, Object>();
params.put("one", 1);
params.put("three", 3);
Iterable<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select max(:one, :three) as maximo")).execute(params);
boolean found = false;
for (ODocument doc : result) {
found = true;
Assert.assertEquals(doc.field("maximo"), 3);
}
Assert.assertTrue(found);
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class PreparedStatementTest method testNamedParamTarget.
@Test
public void testNamedParamTarget() {
Map<String, Object> params = new HashMap<String, Object>();
params.put("className", "PreparedStatementTest1");
Iterable<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select from :className")).execute(params);
Set<String> expected = new HashSet<String>();
expected.add("foo1");
expected.add("foo2");
boolean found = false;
for (ODocument doc : result) {
found = true;
Assert.assertTrue(expected.contains(doc.field("name")));
}
Assert.assertTrue(found);
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class PreparedStatementTest method testNamedParamTargetDocument.
@Test
public void testNamedParamTargetDocument() {
Iterable<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select from PreparedStatementTest1 limit 1")).execute();
ODocument record = result.iterator().next();
Map<String, Object> params = new HashMap<String, Object>();
params.put("inputRid", record);
result = database.command(new OSQLSynchQuery<ODocument>("select from :inputRid")).execute(params);
boolean found = false;
for (ODocument doc : result) {
found = true;
Assert.assertEquals(doc.getIdentity(), record.getIdentity());
Assert.assertEquals(doc.field("name"), record.field("name"));
}
Assert.assertTrue(found);
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class OSQLEngine method parseRIDTarget.
public Set<OIdentifiable> parseRIDTarget(final ODatabaseDocument database, String iTarget, final OCommandContext iContext, Map<Object, Object> iArgs) {
final Set<OIdentifiable> ids;
if (iTarget.startsWith("(")) {
// SUB-QUERY
final OSQLSynchQuery<Object> query = new OSQLSynchQuery<Object>(iTarget.substring(1, iTarget.length() - 1));
query.setContext(iContext);
final List<OIdentifiable> result = database.query(query, iArgs);
if (result == null || result.isEmpty())
ids = Collections.emptySet();
else {
ids = new HashSet<OIdentifiable>((int) (result.size() * 1.3));
for (OIdentifiable aResult : result) ids.add(aResult.getIdentity());
}
} else if (iTarget.startsWith("[")) {
// COLLECTION OF RIDS
final String[] idsAsStrings = iTarget.substring(1, iTarget.length() - 1).split(",");
ids = new HashSet<OIdentifiable>((int) (idsAsStrings.length * 1.3));
for (String idsAsString : idsAsStrings) {
if (idsAsString.startsWith("$")) {
Object r = iContext.getVariable(idsAsString);
if (r instanceof OIdentifiable)
ids.add((OIdentifiable) r);
else
OMultiValue.add(ids, r);
} else
ids.add(new ORecordId(idsAsString));
}
} else {
// SINGLE RID
if (iTarget.startsWith("$")) {
Object r = iContext.getVariable(iTarget);
if (r instanceof OIdentifiable)
ids = Collections.<OIdentifiable>singleton((OIdentifiable) r);
else
ids = (Set<OIdentifiable>) OMultiValue.add(new HashSet<OIdentifiable>(OMultiValue.getSize(r)), r);
} else
ids = Collections.<OIdentifiable>singleton(new ORecordId(iTarget));
}
return ids;
}
use of com.orientechnologies.orient.core.sql.query.OSQLSynchQuery in project orientdb by orientechnologies.
the class OStreamSerializerAnyStreamable method fromStream.
/**
* Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
*/
public Object fromStream(final byte[] iStream) throws IOException {
if (iStream == null || iStream.length == 0)
// NULL VALUE
return null;
final int classNameSize = OBinaryProtocol.bytes2int(iStream);
if (classNameSize <= 0) {
final String message = "Class signature not found in ANY element: " + Arrays.toString(iStream);
OLogManager.instance().error(this, message);
throw new OSerializationException(message);
}
final String className = new String(iStream, 4, classNameSize, "UTF-8");
try {
final OSerializableStream stream;
// CHECK FOR ALIASES
if (className.equalsIgnoreCase("q"))
// QUERY
stream = new OSQLSynchQuery<Object>();
else if (className.equalsIgnoreCase("c"))
// SQL COMMAND
stream = new OCommandSQL();
else if (className.equalsIgnoreCase("s"))
// SCRIPT COMMAND
stream = new OCommandScript();
else
// CREATE THE OBJECT BY INVOKING THE EMPTY CONSTRUCTOR
stream = (OSerializableStream) Class.forName(className).newInstance();
return stream.fromStream(OArrays.copyOfRange(iStream, 4 + classNameSize, iStream.length));
} catch (Exception e) {
final String message = "Error on unmarshalling content. Class: " + className;
OLogManager.instance().error(this, message, e);
throw OException.wrapException(new OSerializationException(message), e);
}
}
Aggregations