return this;
}
@SuppressWarnings("unchecked")
public byte[] toStream() throws OSerializationException {
final OMemoryOutputStream buffer = new OMemoryOutputStream();
try {
buffer.add(text); // TEXT AS STRING
buffer.add(limit); // LIMIT AS INTEGER
buffer.addAsFixed(beginRange.toStream()); // THE RID WHERE TO BEGIN, OR -1:-1 to ignore it
buffer.addAsFixed(endRange.toStream()); // THE RID WHERE TO END, OR -1:-1 to ignore it
buffer.add(fetchPlan != null ? fetchPlan : ""); // FETCH PLAN IN FORM OF STRING (to know more goto:
// http://code.google.com/p/orient/wiki/FetchingStrategies)
if (parameters == null || parameters.size() == 0)
// NO PARAMETER, JUST SEND 0
buffer.add(new byte[0]);
else {
final Map<Object, Object> newParams = new HashMap<Object, Object>(parameters);
for (Entry<Object, Object> entry : newParams.entrySet()) {
final Object value = entry.getValue();
if (value instanceof Set<?> && ((Set<?>) value).iterator().next() instanceof ORecord<?>) {
// CONVERT RECORDS AS RIDS
final Set<ORID> newSet = new HashSet<ORID>();
for (ORecord<?> rec : (Set<ORecord<?>>) value) {
newSet.add(rec.getIdentity());
}
parameters.put(entry.getKey(), newSet);
} else if (value instanceof List<?> && ((List<?>) value).get(0) instanceof ORecord<?>) {
// CONVERT RECORDS AS RIDS
final List<ORID> newList = new ArrayList<ORID>();
for (ORecord<?> rec : (List<ORecord<?>>) value) {
newList.add(rec.getIdentity());
}
parameters.put(entry.getKey(), newList);
} else if (value instanceof Map<?, ?> && ((Map<?, ?>) value).values().iterator().next() instanceof ORecord<?>) {
// CONVERT RECORDS AS RIDS
final Map<Object, ORID> newMap = new HashMap<Object, ORID>();
for (Entry<?, ORecord<?>> mapEntry : ((Map<?, ORecord<?>>) value).entrySet()) {
newMap.put(mapEntry.getKey(), mapEntry.getValue().getIdentity());
}
parameters.put(entry.getKey(), newMap);
}
}
// PARAMETERS FOUND, SEND THEM AS DOCUMENT ITSELF
final ODocument param = new ODocument();
param.field("params", parameters);
buffer.add(param.toStream());
}
} catch (IOException e) {
throw new OSerializationException("Error while marshalling OSQLQuery", e);
}
return buffer.toByteArray();
}