Search in sources :

Example 41 with OCommandScript

use of com.orientechnologies.orient.core.command.script.OCommandScript in project orientdb by orientechnologies.

the class SQLInsertTest method insertWithReturn.

public void insertWithReturn() {
    if (!database.getMetadata().getSchema().existsClass("actor2")) {
        database.command(new OCommandSQL("CREATE CLASS Actor2")).execute();
        database.getMetadata().getSchema().reload();
    }
    // RETURN with $current.
    ODocument doc = database.command(new OCommandSQL("INSERT INTO Actor2 SET FirstName=\"FFFF\" RETURN $current")).execute();
    Assert.assertTrue(doc != null);
    Assert.assertEquals(doc.getClassName(), "Actor2");
    // RETURN with @rid
    Object res1 = database.command(new OCommandSQL("INSERT INTO Actor2 SET FirstName=\"Butch 1\" RETURN @rid")).execute();
    Assert.assertTrue(res1 instanceof ORecordId);
    Assert.assertTrue(((OIdentifiable) res1).getIdentity().isValid());
    // Create many records and return @rid
    Object res2 = database.command(new OCommandSQL("INSERT INTO Actor2(FirstName,LastName) VALUES ('Jay','Miner'),('Frank','Hermier'),('Emily','Saut')  RETURN @rid")).execute();
    Assert.assertTrue(res2 instanceof List<?>);
    Assert.assertTrue(((List) res2).get(0) instanceof ORecordId);
    // Create many records by INSERT INTO ...FROM and return wrapped field
    ORID another = ((OIdentifiable) res1).getIdentity();
    final String sql = "INSERT INTO Actor2 RETURN $current.FirstName  FROM SELECT * FROM [" + doc.getIdentity().toString() + "," + another.toString() + "]";
    List res3 = database.command(new OCommandSQL(sql)).execute();
    Assert.assertEquals(res3.size(), 2);
    Assert.assertTrue(((List) res3).get(0) instanceof ODocument);
    final ODocument res3doc = (ODocument) res3.get(0);
    Assert.assertTrue(res3doc.containsField("result"));
    Assert.assertTrue("FFFF".equalsIgnoreCase((String) res3doc.field("result")) || "Butch 1".equalsIgnoreCase((String) res3doc.field("result")));
    Assert.assertTrue(res3doc.containsField("rid"));
    Assert.assertTrue(res3doc.containsField("version"));
    // create record using content keyword and update it in sql batch passing recordID between commands
    final String sql2 = "let var1=INSERT INTO Actor2 CONTENT {Name:\"content\"} RETURN $current.@rid\n" + "let var2=UPDATE $var1 SET Bingo=1 RETURN AFTER @rid\n" + "return $var2";
    List<?> res_sql2 = database.command(new OCommandScript("sql", sql2)).execute();
    Assert.assertEquals(res_sql2.size(), 1);
    Assert.assertTrue(((List) res_sql2).get(0) instanceof ORecordId);
    // create record using content keyword and update it in sql batch passing recordID between commands
    final String sql3 = "let var1=INSERT INTO Actor2 CONTENT {Name:\"Bingo owner\"} RETURN @this\n" + "let var2=UPDATE $var1 SET Bingo=1 RETURN AFTER\n" + "return $var2";
    List<?> res_sql3 = database.command(new OCommandScript("sql", sql3)).execute();
    Assert.assertEquals(res_sql3.size(), 1);
    Assert.assertTrue(((List) res_sql3).get(0) instanceof ODocument);
    final ODocument sql3doc = (ODocument) (((List) res_sql3).get(0));
    Assert.assertEquals(sql3doc.field("Bingo"), 1);
    Assert.assertEquals(sql3doc.field("Name"), "Bingo owner");
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) ORID(com.orientechnologies.orient.core.id.ORID) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 42 with OCommandScript

use of com.orientechnologies.orient.core.command.script.OCommandScript in project orientdb by orientechnologies.

the class TransactionIsolationTest method testIsolationReadCommittedScript.

@Test
public void testIsolationReadCommittedScript() throws ExecutionException, InterruptedException {
    final ODatabaseDocumentTx db1 = new ODatabaseDocumentTx(url);
    db1.open("admin", "admin");
    final ODocument record1 = new ODocument();
    record1.field("name", "This is the first version").save();
    Future<List<OIdentifiable>> txFuture = Orient.instance().submit(new Callable<List<OIdentifiable>>() {

        @Override
        public List<OIdentifiable> call() throws Exception {
            String cmd = "";
            cmd += "begin isolation READ_COMMITTED;";
            cmd += "let r1 = select from " + record1.getIdentity() + ";";
            cmd += "sleep 2000;";
            cmd += "let r2 = select from " + record1.getIdentity() + " nocache;";
            cmd += "commit;";
            cmd += "return $r2;";
            db1.activateOnCurrentThread();
            return db1.command(new OCommandScript("sql", cmd)).execute();
        }
    });
    Thread.sleep(500);
    // CHANGE THE RECORD FROM DB2
    ODatabaseDocumentTx db2 = new ODatabaseDocumentTx(url);
    db2.open("admin", "admin");
    ODocument record2 = db2.load(record1.getIdentity());
    record2.field("name", "This is the second version").save();
    List<OIdentifiable> txRecord = txFuture.get();
    Assert.assertNotNull(txRecord);
    Assert.assertEquals(txRecord.size(), 1);
    Assert.assertEquals(((ODocument) txRecord.get(0).getRecord()).field("name"), "This is the second version");
    db2.close();
    db1.activateOnCurrentThread();
    db1.close();
}
Also used : OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) List(java.util.List) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 43 with OCommandScript

use of com.orientechnologies.orient.core.command.script.OCommandScript in project orientdb by orientechnologies.

the class OConsoleDatabaseApp method executeServerSideScript.

protected void executeServerSideScript(final String iLanguage, final String iText) {
    if (iText == null)
        return;
    resetResultSet();
    long start = System.currentTimeMillis();
    currentResult = currentDatabase.command(new OCommandScript(iLanguage, iText)).execute();
    float elapsedSeconds = getElapsedSecs(start);
    parseResult();
    if (currentResultSet != null) {
        dumpResultSet(-1);
        message("\nServer side script executed in %f sec(s). Returned %d records", elapsedSeconds, currentResultSet.size());
    } else {
        String lineFeed = currentResult instanceof Map<?, ?> ? "\n" : "";
        message("\nServer side script executed in %f sec(s). Value returned is: %s%s", elapsedSeconds, lineFeed, currentResult);
    }
}
Also used : OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript)

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