Search in sources :

Example 1 with OCommandScript

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;
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OLiveQuery(com.orientechnologies.orient.core.sql.query.OLiveQuery) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream)

Example 2 with OCommandScript

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);
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) IOException(java.io.IOException) OException(com.orientechnologies.common.exception.OException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException)

Example 3 with OCommandScript

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);
}
Also used : OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 4 with OCommandScript

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"));
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) Collection(java.util.Collection) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 5 with OCommandScript

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();
}
Also used : OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)43 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)21 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)18 Test (org.junit.Test)13 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)11 ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)9 InputStream (java.io.InputStream)9 Test (org.testng.annotations.Test)9 Before (org.junit.Before)8 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)6 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)6 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)4 IOException (java.io.IOException)4 Collection (java.util.Collection)3 List (java.util.List)3 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)3 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)2 OException (com.orientechnologies.common.exception.OException)2 OCommandRequest (com.orientechnologies.orient.core.command.OCommandRequest)2