Calling OrientDB function from SQL command

735 views
Skip to first unread message

Gaurav Dhiman

unread,
Jul 28, 2014, 5:17:23 PM7/28/14
to orient-...@googlegroups.com
Hi,

I tried calling a JS function in OrientDB using below SQl command, but the result is different from what I get when I run the function directly in OrientDB Studio.

query -- SELECT getAllCountries FETCHPLAN *:-1
result -- { '@rid': { cluster: -2, position: 0 } }

What is wrong in above quer, why it is not returning the actual data ?

If I run the same query in OientDB Studio, it returns the actual data.

Regards,
Gaurav

Enrico Risa

unread,
Jul 28, 2014, 6:03:43 PM7/28/14
to orient-...@googlegroups.com
Hi Gaurav

try :

SELECT getAllCountries() FETCHPLAN *:-1

Enrico


--

---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gaurav Dhiman

unread,
Jul 29, 2014, 1:53:47 AM7/29/14
to orient-...@googlegroups.com
On Tue, Jul 29, 2014 at 3:33 AM, Enrico Risa <enric...@gmail.com> wrote:
Hi Gaurav

try :

SELECT getAllCountries() FETCHPLAN *:-1

It throws below error:
com.orientechnologies.orient.core.command.script.OCommandScriptException: Error on parsing script at position #0: Error on execution of the script Script: getAllCountries ------^ --> javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "request" is not defined. (#69) in at line number 69 --> sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "request" is not defined. (#69)

 Regards,
Gaurav

--

---
You received this message because you are subscribed to a topic in the Google Groups "OrientDB" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/orient-database/GEUke7zon34/unsubscribe.
To unsubscribe from this group and all its topics, send an email to orient-databa...@googlegroups.com.

Luigi Dell'Aquila

unread,
Jul 29, 2014, 2:33:08 AM7/29/14
to orient-...@googlegroups.com
Hi Gaurav,

I'd like to see your function code to be sure, but I guess you are trying to access the "request" object (eg. trying to retrieve a request parameter).
Request object is not present when invoking a function inside a query. 
Let me explain in depth, you can invoke JS functions in many ways:

1) as web services, like this:
http://localhost:2480/function/<dbname>/getAllCountries/

2) from inside a query, like this:
SELECT getAllCountries()

3) from Java API

In the first case you are executing an HTTP request to the OrientDB REST API, so "request" and "response" objects will be created by the server and bound to the function context.
In the second (and other) case you are not in an HTTP request, so no "request" and "response" objects will be available.

Your stack trace suggests that you are trying to access a "request" object that does not exist in the context. I suggest you to change your function code checking if the "request" exists before using it.

Regards

Luigi
 





Gaurav Dhiman

unread,
Jul 29, 2014, 2:58:29 AM7/29/14
to orient-...@googlegroups.com
Hi Luigi,

Thanks for inputs. Your response make sense. I was accessing request object in function body, hence the error.
I altered the function and now below is the current code of getAllCountries() function.

  var mc = db.query("SELECT FROM MasterCountry");
  return mc;

In this case, now when I try to invoke function using SQL << SELECT getAllCountries(); >>, below is the response
{ '@rid': { cluster: -2, position: 0 } }

Although now I am not getting error as I removed the access to request object in function body, but still the response is not what I was expecting. When I call the same function over REST/HTTP, response returns all the countries data as shown below.

{"result":[{"@type":"d","@rid":"#14:0","@version":15,"@class":"MasterCountry","name":"Afghanistan","code":"AF","resetFlag":true}, {"@type":"d","@rid":"#14:1","@version":2,"@class":"MasterCountry","name":"\u00c5land Islands","code":"AX","resetFlag":true}, {"@type":"d","@rid":"#14:2","@version":2,"@class":"MasterCountry","name":"Albania","code":"AL","resetFlag":true}, {"@type":"d","@rid":"#14:3","@version":2,"@class":"MasterCountry","name":"Algeria","code":"DZ","resetFlag":true}, {"@type":"d","@rid":"#14:4","@version":2,"@class":"MasterCountry","name":"American Samoa","code":"AS","resetFlag":true}, {"@type":"d","@rid":"#14:5","@version":2,"@class":"MasterCountry","name":"AndorrA","code":"AD","resetFlag":true}, {"@type":"d","@rid":"#14:6","@version":2,"@class":"MasterCountry","name":"Angola","code":"AO","resetFlag":true}, {"@type":"d","@rid":"#14:7","@version":2,"@class":"MasterCountry","name":"Anguilla","code":"AI","resetFlag":true}, {"@type":"d","@rid":"#14:8","@version":2,"@class":"MasterCountry","name":"Antarctica","code":"AQ","resetFlag":true}, {"@type":"d","@rid":"#14:9","@version":2,"@class":"MasterCountry","name":"Antigua and Barbuda","code":"AG","resetFlag":true},
....
....
....
....
]}

--
Best Regards,
Gaurav


Luigi Dell'Aquila

unread,
Jul 29, 2014, 3:36:03 AM7/29/14
to orient-...@googlegroups.com
Hi Gaurav,

First of all you have to remove the semicolon at the end of the query:

SELECT getAllCountries()

instead of 

SELECT getAllCountries();  < -


Then two simple questions: 
1) are you trying to execute the query from Studio or from a client? You should obtain a single record containing a collection as a result, so maybe in Studio you will not be able to see it properly.
2) can you try this in Studio and tell me what you see?

SELECT expand(getAllCountries) from  (SELECT getAllCountries())

Regards

Luigi



Gaurav Dhiman

unread,
Jul 29, 2014, 4:11:42 AM7/29/14
to orient-...@googlegroups.com
Luigi,

Thanks. Below SQL shared by you worked and gave me the same results that REST/HTTP is returning.
SELECT expand(getAllCountries) from  (SELECT getAllCountries())

Now in case I need to send parameters to functions, how to do that ??

--
Best Regards,
Gaurav

Reply all
Reply to author
Forward
0 new messages