Class: Connection

$.hdb. Connection

HANA database connection.

new Connection()

Methods

close()

Closes the connection
Throws:
Throws an error if the operation fails.
Type
$.hdb.SQLException

commit()

Commits the changes and ends the current transaction. By default autocommit mode is disabled, which means all database changes must be explicitly commited.
Throws:
Throws an error if the operation fails.
Type
$.hdb.SQLException
Example
var connection = $.hdb.getConnection();
connection.executeUpdate('UPDATE "DB_EXAMPLE"."ICECREAM" SET QUANTITY=? WHERE FLAVOR=?', 9, 'CHOCOLATE');
connection.commit();

executeQuery(query, arguments) → {$.hdb.ResultSet}

Executes a database query. The query string is prepared and the additional arguments are applied as statement parameters, before the statement is executed.
Parameters:
Name Type Argument Description
query string The query string to be executed.
arguments any <optional>
<repeatable>
Variable number of arguments to be bound to the query parameters.
Throws:
Throws an error if the statement cannot be executed.
Type
$.hdb.SQLException
Returns:
Type
$.hdb.ResultSet
Example
var connection = $.hdb.getConnection();
connection.executeQuery('SELECT * FROM "DB_EXAMPLE"."ICECREAM"');
connection.executeQuery('SELECT * FROM "DB_EXAMPLE"."ICECREAM" WHERE FLAVOR = ?', 'CHOCOLATE');
connection.executeQuery('SELECT * FROM "DB_EXAMPLE"."ICECREAM" WHERE FLAVOR = ? AND PRICE < ?', 'STRAWBERRY', 2.50);

executeUpdate(statement, arguments) → {number|array}

Executes a SQL statement, which changes the database state. SELECT and CALL statements are not allowed here. The query string is prepared and the additional arguments are applied as statement parameters, before the statement is executed.
Parameters:
Name Type Argument Description
statement string The statement to be executed.
arguments any <optional>
<repeatable>
Variable number of arguments to be bound to the query parameters.
Throws:
Throws an error if the statement cannot be executed.
Type
$.hdb.SQLException
Returns:
Number of affected rows | Array of numbers in case of batch update.
Type
number | array
Example
var connection = $.hdb.getConnection();
connection.executeUpdate('INSERT INTO "DB_EXAMPLE"."ICECREAM" VALUES (?,?,?)','STRAWBERRY', 2.50, 64.18);
connection.executeUpdate('UPDATE "DB_EXAMPLE"."CASH" SET INCOME=?', 64.18);
connection.executeUpdate('DELETE FROM "DB_EXAMPLE"."ICECREAM" WHERE FLAVOR = ?', 'STRAWBERRY');

//Batch Insert
var argsArray = [["MINT", 3.50, 34.5], ["VANILLA", 2.50, 23.6], ["CHERRY", 4.50, 67.9]];
connection.executeUpdate('INSERT INTO "DB_EXAMPLE"."ICECREAM" VALUES (?,?,?)', argsArray)
connection.commit();

getLastWarning() → {Object|null}

Returns the warning of the most recently executed statement.
Throws:
Throws an error if the operation fails.
Type
$.hdb.SQLException
Returns:
The return value has two properties: 'code' and 'message'. 'null' is returned if the most recent statement didn't issue a warning.
Type
Object | null

loadProcedure(schema, procedure) → {function}

Returns a JavaScript function representing the stored procedure being loaded.
Parameters:
Name Type Description
schema string The schema to which the procedure belongs.
procedure string The name of the procedure.
Returns:
Type
function
Examples
var connection = $.hdb.getConnection();
// the procedure has signature 'DB_EXAMPLE'.'icecream.shop::sell'(IN flavor VARCHAR, IN quantity INTEGER, IN payment DECIMAL, OUT change DECIMAL)
var fnSell = connection.loadProcedure('DB_EXAMPLE', 'icecream.shop::sell');
// call the procedure just like calling a javascript function
// sell three ice cream cups with chocolate flavor for 20 bucks each
var result = fnSell('CHOCOLATE', 3, 20.00);
// alternatively use named parameters
var result = fnSell({flavor: 'CHOCOLATE', quantity: 3, payment: 20.00});
// result is a $.hdb.ProcedureResult object
Table Parameter Support For Stored Procedures

// the procedure has signature 'DB_EXAMPLE'.'icecream.shop::lower_price_by'(IN ice_cream_prices prices_table_type, IN lower_price_by DOUBLE, OUT new_ice_cream_prices prices_table_type)
var fnlowerPrice = connection.loadProcedure('DB_EXAMPLE', 'icecream.shop::lower_price_by');

// passing a $.hdb.ResultSet object
var price_list = connection.executeQuery('SELECT * FROM "DB_EXAMPLE"."SOLD_FLAVORS_PRICES"');
var result = fnlowerPrice(price_list, 1.50);

// passing a string containing the name of existing table in the database
var result = fnlowerPrice('"DB_EXAMPLE"."SOLD_FLAVORS_PRICES"', 1.50);

// passing an array of JSON objects representing the rows in the table
var price_list = [{flavor: 'STRAWBERRY', price: 4.50}, {flavor : 'VANILLA', price: 3.50}, {flavor: 'CHOCOLATE', price: 5.50}];
var result = fnlowerPrice(price_list, 1.50);

// We can get the table output parameter "new_ice_cream_prices" like any other output parameter:
var new_price_list = result.new_ice_cream_prices;
// new_price_list is a $.hdb.ResultSet object

rollback()

Reverts the changes and ends the current transaction.
Throws:
Throws an error if the operation fails.
Type
$.hdb.SQLException
Example
var connection = $.hdb.getConnection();
connection.executeUpdate('UPDATE "DB_EXAMPLE"."ICECREAM" SET QUANTITY=? WHERE FLAVOR=?', 9, 'CHOCOLATE');
connection.rollback();

setAutoCommit(enable)

Changes the auto-commit flag of the connection.
Parameters:
Name Type Description
enable number A number value, which can be either 0 (false) or 1 (true)