Connection Class
Represents the connection to the database.
Members
All members of the connection.
Methods| Type | Method | Description |
|---|---|---|
| abort([Function]) Method | Aborts the running database request that is being executed on the connection. | |
| clearPool([Function]) | Empties the connection pool. | |
| commit([Function]) | Performs a commit on the connection. | |
| connect({String | Object} [, Function]) | Creates a new connection to the database by using a connection string or a hash of connection parameters that is passed in as a parameter. | |
| connect(Number[, Function ]) | Connects to the database by using an existing connection. | |
| {disconnect | close}([Function]) | Closes the current connection. | |
| Result, Number, or Undefined. | exec[ute](String[, Array][,Object][, Function]) Method | Executes the specified SQL statement. |
| getClientInfo(String) | Returns the string of the specified property for the connection. | |
| getWarnings() Method | Returns an array of warnings for the connection. | |
| onTrace(traceOptions, callback) Method | Provides a callback to receive trace messages. | |
| Statement | prepare(String[, Function]) | Prepares the specified SQL statement and returns a Statement object. |
| rollback([Function]) | Performs a rollback on the connection. | |
| setAutoCommit(Boolean) | Changes the autocommit setting for the connection. | |
| setClientInfo(String, String) | Sets a client information property for the connection. | |
| setRowSetSize (Integer) Method |
Specifies the size of the row set. |
|
| setWarningCallback ([Function]) Method |
Deprecated, use the getWarnings() method instead. |
|
| state() Method |
Returns a string that indicates the state of the connection. |
Remarks
Once the connection is made, if there are network issues, any call using the connection fails and returns an error.
Example
The following example uses synchronous calls to create a new connection to the SAP HANA on-premise database server, issue a SQL query against the server, display the result set, and then disconnect from the server.
var hana=require('@sap/hana-client');
var client=hana.createConnection();
client.connect({serverNode:'myserver:30015',uid:'system',pwd:'manager'});
console.log('Connected');
result = client.exec("SELECT * FROM Customers");
console.log(result);
client.disconnect();
console.log('Disconnected');
The following on-premise example performs the same tasks as the previous example by using callbacks to perform asynchronous calls. Error checking is included.
var hana = require('@sap/hana-client');
var client = hana.createConnection();
client.connect("serverNode=myServer:30015;uid=MyUser;pwd=MyPassword",
function(err)
{
if(err) {
console.error("Connect error: ",err);
} else {
console.log("Connected");
client.exec("SELECT * FROM Customers",
function(err,rows)
{
if(err) {
console.error("Error: ",err);
} else {
console.log(rows);
}
}
);
client.disconnect(
function(err)
{
if(err) {
console.error("Disconnect error: ", err);
} else {
console.log("Disconnected");
}
}
);
}
}
);
The following on-premise example also uses callbacks, but the functions are not inlined.
var hana=require('@sap/hana-client');
var client=hana.createConnection();
client.connect("serverNode=myServer:30015;uid=myUser;pwd=myPassword", async_connect);
function async_connect(err)
{
if(err) {
console.error("Connect error: ", err);
} else {
console.log("Connected");
client.exec("SELECT FROM Customers",async_results);
}
}
function async_results(err,rows)
{
if(err) {
console.error("Error: ", err);
} else {
console.log(rows);
}
client.disconnect(async_disconnect);
}
function async_disconnect(err)
{
if(err) {
console.error("Disconnect error: ", err);
} else {
console.log("Disconnected");
}
}
You can also pass connection parameters into the createConnection function. Those connection parameters are combined with connection parameters in the connect() function call to get the connection string used for the connection. Use a hash of connection parameters or a connection string fragment in either call using the following on-premise example:
var hana=require('@sap/hana-client');
var client=hana.createConnection({uid: 'myUser'; pwd: 'myPassword'});
client.connect('serverNode=myServer:30015');
// the connection string that is used is
// "uid=myUser;pwd=myPassword;serverNode=myServer:30015"