use of com.orientechnologies.orient.core.command.script.OCommandScript in project orientdb by orientechnologies.
the class OStreamSerializerAnyStreamable method toStream.
/**
* Serialize the class name size + class name + object content
*/
public byte[] toStream(final Object iObject) throws IOException {
if (iObject == null)
return null;
if (!(iObject instanceof OSerializableStream))
throw new OSerializationException("Cannot serialize the object [" + iObject.getClass() + ":" + iObject + "] since it does not implement the OSerializableStream interface");
OSerializableStream stream = (OSerializableStream) iObject;
// SERIALIZE THE CLASS NAME
final byte[] className;
if (iObject instanceof OLiveQuery<?>)
className = iObject.getClass().getName().getBytes("UTF-8");
else if (iObject instanceof OSQLSynchQuery<?>)
className = QUERY_COMMAND_CLASS_ASBYTES;
else if (iObject instanceof OCommandSQL)
className = SQL_COMMAND_CLASS_ASBYTES;
else if (iObject instanceof OCommandScript)
className = SCRIPT_COMMAND_CLASS_ASBYTES;
else {
if (iObject == null)
className = null;
else
className = iObject.getClass().getName().getBytes("UTF-8");
}
// SERIALIZE THE OBJECT CONTENT
byte[] objectContent = stream.toStream();
byte[] result = new byte[4 + className.length + objectContent.length];
// COPY THE CLASS NAME SIZE + CLASS NAME + OBJECT CONTENT
System.arraycopy(OBinaryProtocol.int2bytes(className.length), 0, result, 0, 4);
System.arraycopy(className, 0, result, 4, className.length);
System.arraycopy(objectContent, 0, result, 4 + className.length, objectContent.length);
return result;
}
use of com.orientechnologies.orient.core.command.script.OCommandScript 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);
}
}
use of com.orientechnologies.orient.core.command.script.OCommandScript in project orientdb by orientechnologies.
the class OCommandExecutorSQLScriptTest method testTx.
@Test
public void testTx() throws Exception {
StringBuilder script = new StringBuilder();
script.append("begin isolation REPEATABLE_READ\n");
script.append("let $a = insert into V set test = 'sql script test'\n");
script.append("commit retry 10\n");
script.append("return $a\n");
ODocument qResult = db.command(new OCommandScript("sql", script.toString())).execute();
Assert.assertNotNull(qResult);
}
use of com.orientechnologies.orient.core.command.script.OCommandScript in project orientdb by orientechnologies.
the class TestTypeGuessingWorkingWithSQLAndMultiValues method testLinkedValue.
@Test
public void testLinkedValue() {
Iterable<ODocument> result = db.command(new OCommandScript("sql", "insert into client set name = 'James Bond', phones = ['1234', '34567'], addresses = [{'city':'Shanghai', 'zip':'3999'}, {'city':'New York', 'street':'57th Ave'}]\n" + "update client add addresses = [{'@type':'d','@class':'Address','city':'London', 'zip':'67373'}] return after")).execute();
Assert.assertTrue(result.iterator().hasNext());
ODocument doc = result.iterator().next();
Collection<ODocument> addresses = ((Collection<ODocument>) doc.field("addresses"));
Assert.assertEquals(addresses.size(), 3);
for (ODocument a : addresses) Assert.assertTrue(a.getClassName().equals("Address"));
result = db.command(new OCommandSQL("update client add addresses = [{'city':'London', 'zip':'67373'}] return after")).execute();
Assert.assertTrue(result.iterator().hasNext());
doc = result.iterator().next();
addresses = ((Collection<ODocument>) doc.field("addresses"));
Assert.assertEquals(addresses.size(), 4);
for (ODocument a : addresses) Assert.assertTrue(a.getClassName().equals("Address"));
}
use of com.orientechnologies.orient.core.command.script.OCommandScript in project orientdb by orientechnologies.
the class TestTypeGuessingWorkingWithSQLAndMultiValues method create.
@BeforeMethod
public void create() {
db = new ODatabaseDocumentTx("memory:" + TestTypeGuessingWorkingWithSQLAndMultiValues.class.getSimpleName());
db.create();
db.command(new OCommandScript("sql", "create class Address\n" + "create property Address.street String\n" + "create property Address.city String\n" + "\n" + "create class Client\n" + "create property Client.name String\n" + "create property Client.phones embeddedSet String\n" + "create property Client.addresses embeddedList Address\n")).execute();
}
Aggregations