/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/** @module vertx-jdbc-js/jdbc_connection */
var utils = require('vertx-js/util/utils');
var io = Packages.io;
var JsonObject = io.vertx.core.json.JsonObject;
var JJdbcConnection = io.vertx.ext.jdbc.JdbcConnection;
/**
Represents the <code>JdbcConnection</code> which is obtained from the <code>JdbcService</code>.
@class
*/
var JdbcConnection = function(j_val) {
var j_jdbcConnection = j_val;
var that = this;
/**
Sets the auto commit flag for this connection. True by default. Set to false if you want
@public
@param autoCommit {boolean}
@param resultHandler {function}
@return {JdbcConnection}
*/
this.setAutoCommit = function(autoCommit, resultHandler) {
var __args = arguments;
if (__args.length === 2 && typeof __args[0] ==='boolean' && typeof __args[1] === 'function') {
j_jdbcConnection.setAutoCommit(autoCommit, function(ar) {
if (ar.succeeded()) {
resultHandler(null, null);
} else {
resultHandler(null, ar.cause());
}
});
return that;
} else utils.invalidArgs();
};
/**
Executes the given SQL statement
@public
@param sql {string}
@param resultHandler {function}
@return {JdbcConnection}
*/
this.execute = function(sql, resultHandler) {
var __args = arguments;
if (__args.length === 2 && typeof __args[0] === 'string' && typeof __args[1] === 'function') {
j_jdbcConnection.execute(sql, function(ar) {
if (ar.succeeded()) {
resultHandler(null, null);
} else {
resultHandler(null, ar.cause());
}
});
return that;
} else utils.invalidArgs();
};
/**
Executes the given SQL <code>SELECT</code> statement which returns the results of the query.
@public
@param sql {string}
@param params {todo}
@param resultHandler {function}
@return {JdbcConnection}
*/
this.query = function(sql, params, resultHandler) {
var __args = arguments;
if (__args.length === 3 && typeof __args[0] === 'string' && typeof __args[1] === 'object' && __args[1] instanceof Array && typeof __args[2] === 'function') {
j_jdbcConnection.query(sql, utils.convParamJsonArray(params), function(ar) {
if (ar.succeeded()) {
resultHandler(utils.convReturnJson(ar.result().toJson()), null);
} else {
resultHandler(null, ar.cause());
}
});
return that;
} else utils.invalidArgs();
};
/**
Executes the given SQL statement which may be an <code>INSERT</code>, <code>UPDATE</code>, or <code>DELETE</code>
statement.
@public
@param sql {string}
@param params {todo}
@param resultHandler {function}
@return {JdbcConnection}
*/
this.update = function(sql, params, resultHandler) {
var __args = arguments;
if (__args.length === 3 && typeof __args[0] === 'string' && typeof __args[1] === 'object' && __args[1] instanceof Array && typeof __args[2] === 'function') {
j_jdbcConnection.update(sql, utils.convParamJsonArray(params), function(ar) {
if (ar.succeeded()) {
resultHandler(utils.convReturnJson(ar.result().toJson()), null);
} else {
resultHandler(null, ar.cause());
}
});
return that;
} else utils.invalidArgs();
};
/**
Closes the connection. Important to always close the connection when you are done so it's returned to the pool.
@public
@param handler {function}
*/
this.close = function(handler) {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] === 'function') {
j_jdbcConnection.close(function(ar) {
if (ar.succeeded()) {
handler(null, null);
} else {
handler(null, ar.cause());
}
});
} else utils.invalidArgs();
};
/**
Commits all changes made since the previous commit/rollback.
@public
@param handler {function}
@return {JdbcConnection}
*/
this.commit = function(handler) {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] === 'function') {
j_jdbcConnection.commit(function(ar) {
if (ar.succeeded()) {
handler(null, null);
} else {
handler(null, ar.cause());
}
});
return that;
} else utils.invalidArgs();
};
/**
Rolls back all changes made since the previous commit/rollback.
@public
@param handler {function}
@return {JdbcConnection}
*/
this.rollback = function(handler) {
var __args = arguments;
if (__args.length === 1 && typeof __args[0] === 'function') {
j_jdbcConnection.rollback(function(ar) {
if (ar.succeeded()) {
handler(null, null);
} else {
handler(null, ar.cause());
}
});
return that;
} else utils.invalidArgs();
};
// A reference to the underlying Java delegate
// NOTE! This is an internal API and must not be used in user code.
// If you rely on this property your code is likely to break if we change it / remove it without warning.
this._jdel = j_jdbcConnection;
};
// We export the Constructor function
module.exports = JdbcConnection;