While most people think XS Engine is only based on OData entities (i.e. for reading data), it actually has a powerful JavaScript Database API that enables not only querying but executing any SQL statement (including inserting, updating and deleting records or running procedure calls).
This API is described in the "9.4 Server-Side JavaScript APIs" section of the HANA Developer Guide:http://help.sap.com/hana/hana_dev_en.pdf.
Below is a simple example of a server side JavaScript code (.xsjs file) for inserting data into HANA.
insert.xsjs
- var id = $.request.getParameter("id");
- if (id === null) {
- $.response.setContentType("text/plain");
- $.response.addBody("id is null!");
- }
- var val1 = $.request.getParameter("val1");
- if (val1 === null) {
- $.response.setContentType("text/plain");
- $.response.addBody("val1 is null!");
- }
- var output = {};
- output.data = [];
- var conn = $.db.getConnection();
- conn.prepareStatement("SET SCHEMA \"TEST\"").execute();
- var st = prepareStatement("INSERT INTO \"TABLE1\" values(?,?)");
- st.setString(1,id);
- st.setString(2,val1);
- st.execute();
- conn.commit();
- var record = [];
- record.push(id);
- record.push(val1);
- output.data.push(record);
- conn.close();
- $.response.setContentType("text/json");
- $.response.addBody(JSON.stringify(output));
In order to call it, just do a HTTP GET to the URL pointing to your .xsjs file, passing the URL parameters.
For testing purposes, it's possible to just open the URL in the browser directly.
If everything goes as expected, you should see the inserted record sent back as the response to the HTTP GET command (in JSON format).
Anything else should be interpreted as an error.
For example, calling http://<host>:<port>/test/logic/insert.xsjs?id=1&val1=10.5 in your browser, will insert this record in the "TABLE1" table:
And this is the output you should see in your browser:
In the above case, TABLE1 had "ID" as a Primary Key. Meaning, if I just call the same URL again, it will try to insert another record with the same key, and hence it will fail. But since I didn't handle the exception in my code, it will throw a HTTP 500 error. If you want to avoid that, you'll need to do some exception handling in your .xsjs file.
Finally, in order to properly test your .xsjs file, you'll need to define a XS application for the package that contains it.
Just make sure to create a .xsapp file like below in the root folder of that package in order to tell HANA that this is an existing XS Application, or else trying to call that URL above will just throw a HTTP 404 error.
test.xsapp
- {
- "auth_required": true
- }
Hi,
ReplyDeleteAwesome useful information provides to us sap hana training institute provides hana online training with real time experts also with provides online server access
sap hana online training
Hi,I used the same code but getting the following error....Prepare Statement was used before it is defined....What is the error ???
ReplyDelete
ReplyDeletenice course. thanks for sharing this post.
SAP HANA Training Institute in Delhi