31 March 2014
31 March 2014,
 0

Hi all,

OrientDB allowed to execute arbitrary script written in Javascript or any scripting language installed in the JVM. Well, starting from now we created a minimal SQL engine to allow batch of commands. Batch of commands are very useful when you have to execute multiple things at the server side avoiding the network roundtrip for each command. Example to create a new vertex in a transaction and attach it to an existent vertex by creating a new edge between them:

 

begin
let account = create vertex Account set name = ‘Luke’
let city = select from City where name = ‘London’
let edge = create edge Lives from $account to $city retry 100
commit
return $edge

 

It’s plain OrientDB SQL, but with a few news:
  • begin -> begins a transaction
  • rollback -> rollbacks an active transaction
  • commit -> commits an active transaction
  • let <variable> = <command> -> executes a command and assign it in the context as . That variable can be used in further commands by prefixing it with $
  • return <variable>|<value>|null -> returns a value instead of last command result (default)
Note also the usage of $account and $city on further SQL commands.

 

Java API

 

This can be used by Java API with:
database.open("admin", "admin");

String cmd = "begin\n";
cmd += "let a = create vertex set script = true\n";
cmd += "let b = select from v limit 1\n";
cmd += "let e = create edge from $a to $b retry 100\n";
cmd += "commit\n";
cmd += "return $e";

OIdentifiable edge = database.command(new OCommandScript("sql", cmd)).execute();

 

Remember to put one command per line (postfix it with \n).

 

HTTP REST API

 

And via HTTP REST interface (https://github.com/orientechnologies/orientdb/issues/2056). Execute a POST against /batch URL by sending this payload:

 

{ "transaction" : true, 
  "operations" : [ 
    {
      "type" : "script",
      "language" : "sql",
      "script" : [ "let account = create vertex Account set name = 'Luke'",
                   "let city =select from City where name = 'London'",
                   "create edge Lives from $account to $city retry 100" ]
    }
  ]
}

 

Hope this new feature will simplify your development improving performance.

 

What about having more complex constructs like IF, FOR, etc? If you need more complexity, I suggest you to use Javascript as language that already support all these concepts.

 

Luca Garulli
CEO at Orient Technologies
the Company behind OrientDB

http://about.me/luca.garulli

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>