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");
}
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();
}
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);
}
}
Aggregations