Skip to Content

/wp-content/uploads/2015/10/8_850644.png

According to the snippet above, node.js server act as a client to the HANA server with announce “client” object and call connect function, we will modify the code to run SQL after connected successfully.

First is CREATE TABLE statement:

/wp-content/uploads/2015/10/10_850645.png

Here we need to stop for a while and take a look at the snippet above, maybe it’s little strange to the developers who have no experience on JavaScript, that’s the one of the features of JavaScript compared with Java, it’s callback based. As we mentioned above node.js is event-driven, callback is the implementation of that, callback is a function which means “you can call the function when you finished”, OK let’s take the above snippet as example, and take a look at the first line we can know that the whole snippet can be regarded as

/wp-content/uploads/2015/10/11_850646.png

We call connect function on client object, and the connect function has a input parameter, it’s a function, yes, in JavaScript, function can be assigned to a variable and send to function as parameter, that another difference with Java, function is “First Class Citizens” in JavaScript. Let’s go on, the blue function here, is callback function. In a word the code want to say to computer, “Hey, let’s connect to HANA, and then do as the function defined”. The callback function has a parameter err, parameter is the definition of function. Maybe you will ask “how can we know the parameter of the callback function?” You need to check the documentation, to get the expected parameter list of callback function.

Let’s expand the callback function, we can know that the callback function do the following tasks:

  • Print out error info if error occurs and return.
  • Print out client state.
  • Run create table SQL.

You may have recognized that there is another callback function in the “client.exec()” statement, that true, and it defines what to do after the SQL runs on database, I think you have understand the basic concept of callback function, please note that callback functions may be nested, which may lead to “callback hell” if too much, we will see how to solve it later.

Run the applications will get the result as follow:

/wp-content/uploads/2015/10/12_850647.png

And let’s check whether this table is created in HANA:

/wp-content/uploads/2015/10/13_850648.png

Please note that the column name will be upper cased automatically.

Someone may argue that is that possible to write code like this:

/wp-content/uploads/2015/10/14_850649.png

I have to say yes–according to the test–but not recommended, this is totally blocking style code which is same as Java. The reason is that you cannot make sure that the order of function execution, as we said that node.js is non-blocking, this case happen to work just because connect() is a blocking function, you can try it by disconnect the network and run this program, it trying to connect until timeout, but remember not all functions are so blocking-friendly, actually most of functions in node.js is non-blocking.

/wp-content/uploads/2015/10/15_850650.png

Next let’s try INSERT statement:

/wp-content/uploads/2015/10/16_850651.png

The only difference is the parameter of callback function, a variable “affectedRows” to return the rows inserted successfully. And this operation also add a destroy function, the implementation is as follow:

/wp-content/uploads/2015/10/17_850652.png

Do remember to close the connection when terminate the program.

Let’s try query after insert:

/wp-content/uploads/2015/10/18_850653.png

Note that the callback parameter changed again, rows is the result set of this query, we can conclude the definition of callback function depends on the SQL statement, the output should be:

/wp-content/uploads/2015/10/19_850654.png

Finally is the UPDATE operation:

/wp-content/uploads/2015/10/20_850655.png

/wp-content/uploads/2015/10/21_850656.png

/wp-content/uploads/2015/10/22_850657.png

The parameter of callback function is the same as INSERT.

And finally the DELETE operation:

/wp-content/uploads/2015/10/23_850658.png

These is the basic SQL statement, we will see more complex cases in the next part.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply