node-jdbc-firebird
Version:
This package is help to use jdbc connection
368 lines (367 loc) • 19.1 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectionType = void 0;
// Import interface
var IDatabase_1 = require("./IDatabase");
// Import all drivers
var drivers_1 = __importDefault(require("./drivers"));
// Import dependecies
var jdbc = require('jdbc');
var jinst = require('jdbc/lib/jinst');
var path = require('path');
// Connection Type
var ConnectionType;
(function (ConnectionType) {
ConnectionType["hive"] = "hive";
ConnectionType["firebirdsql"] = "firebirdsql";
ConnectionType["postgreSql"] = "postgresql";
ConnectionType["sqlite"] = "sqlite";
ConnectionType["tibero"] = "tibero";
ConnectionType["custom"] = "custom";
})(ConnectionType || (exports.ConnectionType = ConnectionType = {}));
var JdbcDriver = /** @class */ (function (_super) {
__extends(JdbcDriver, _super);
function JdbcDriver(type, config) {
var _this = _super.call(this, type, config) || this;
_this.get_version = function () { return _this.driver.version; };
_this.findAll = function (tableName) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.sql("SELECT * FROM ".concat(tableName))];
case 1: return [2 /*return*/, _a.sent()];
}
}); }); };
_this.count = function (tableName) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.sql("SELECT COUNT(*) as total from ".concat(tableName))];
case 1: return [2 /*return*/, _a.sent()];
}
}); }); };
_this.find = function (tableName, where) {
if (where === void 0) { where = 1; }
return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.sql("SELECT * FROM ".concat(tableName, " WHERE ").concat(where))];
case 1: return [2 /*return*/, _a.sent()];
}
}); });
};
_this.connection_count = function () { return JdbcDriver.connection.size; };
_this.connection_details = function () { return JdbcDriver.connection.entries(); };
_this.get_columns = function (tableName) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this.type === 'custom')) return [3 /*break*/, 1];
return [2 /*return*/, 'This method is not supported for the custom drivers'];
case 1: return [4 /*yield*/, this.sql(this.get_query(tableName, IDatabase_1.QueryType.columns))];
case 2: return [2 /*return*/, _a.sent()];
}
});
}); };
_this.get_table_properties = function (tableName) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this.type === 'custom')) return [3 /*break*/, 1];
return [2 /*return*/, 'This method is not supported for the custom drivers'];
case 1: return [4 /*yield*/, this.sql(this.get_query(tableName, IDatabase_1.QueryType.describe))];
case 2: return [2 /*return*/, _a.sent()];
}
});
}); };
_this.ddl = function (sql) { return __awaiter(_this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
try {
res = this.executeUpdate(sql);
return [2 /*return*/, res];
}
catch (err) {
console.error('Error in ddl method:', err);
throw err;
}
return [2 /*return*/];
});
}); };
_this.close = function (connObj) { return __awaiter(_this, void 0, void 0, function () {
var coon;
return __generator(this, function (_a) {
try {
coon = JdbcDriver.connection.get(this.type);
coon.release(connObj, function (err) {
if (err)
console.log('Connection relase issues::::');
else
console.log('Connection relase');
});
}
catch (err) {
console.error('Connection close error:::::', err);
throw err;
}
return [2 /*return*/];
});
}); };
_this.executeQuery = function (sql) { return __awaiter(_this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var stat;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.createStatement()];
case 1:
stat = _a.sent();
stat[0].executeQuery(sql, function (err, resultset) { return __awaiter(_this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!err) return [3 /*break*/, 1];
reject(err);
return [3 /*break*/, 3];
case 1: return [4 /*yield*/, resultset.toObjArray(function (err, rows) {
if (err)
reject(err);
else
resolve(rows);
stat[0].close(function (err) {
if (err)
console.log('Statement closing issues...', err);
else {
console.log('Statement closed.');
_this.close(stat[1]);
}
});
})];
case 2:
_a.sent();
_a.label = 3;
case 3: return [2 /*return*/];
}
});
}); });
return [2 /*return*/];
}
});
}); })];
});
}); };
_this.executeUpdate = function (sql) { return __awaiter(_this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var stat;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.createStatement()];
case 1:
stat = _a.sent();
stat[0].executeUpdate(sql, function (err, count) { return __awaiter(_this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
if (err)
reject(err);
else
resolve(count);
stat[0].close(function (err) {
if (err)
console.log('Statement closing issues::::');
else {
console.log('Statement closed');
_this.close(stat[1]);
}
});
return [2 /*return*/];
});
}); });
return [2 /*return*/];
}
});
}); })];
});
}); };
_this.createStatement = function () { return __awaiter(_this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var connObj, conn;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.open()];
case 1:
connObj = _a.sent();
if (connObj) {
console.log("Using connection: " + connObj.uuid);
conn = connObj.conn;
conn.createStatement(function (err, statement) {
if (err)
reject(err);
else
resolve([statement, connObj]);
});
}
else {
reject('Connection object not found');
}
return [2 /*return*/];
}
});
}); })];
});
}); };
_this.is_init = function (conn) {
return conn._reserved.length;
};
_this.type = type;
if (!jinst.isJvmCreated()) {
jinst.addOption('-Xrs');
jinst.setupClasspath([path.join(__dirname, _this.driverPath)]);
}
var connection = new jdbc(_this.get_config());
JdbcDriver.connection.set(_this.type, connection);
return _this;
}
JdbcDriver.prototype.sql = function (sql) {
return __awaiter(this, void 0, void 0, function () {
var res, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.executeQuery(sql)];
case 1:
res = _a.sent();
return [2 /*return*/, res];
case 2:
err_1 = _a.sent();
console.error('Error in sql method:', err_1);
throw err_1;
case 3: return [2 /*return*/];
}
});
});
};
JdbcDriver.prototype.open = function () {
return __awaiter(this, void 0, void 0, function () {
var connection, err_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 6, , 7]);
connection = JdbcDriver.connection.get(this.type);
if (!this.is_init(connection)) return [3 /*break*/, 1];
return [2 /*return*/, connection._reserved[0]];
case 1:
if (!!connection._pool.length) return [3 /*break*/, 3];
return [4 /*yield*/, this.init(connection)];
case 2:
_a.sent();
_a.label = 3;
case 3: return [4 /*yield*/, this.reserveConnection(connection)];
case 4: return [2 /*return*/, _a.sent()];
case 5: return [3 /*break*/, 7];
case 6:
err_2 = _a.sent();
console.error('Error opening connection:', err_2);
throw err_2;
case 7: return [2 /*return*/];
}
});
});
};
JdbcDriver.prototype.reserveConnection = function (connection) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) {
connection.reserve(function (err, connObj) {
if (err) {
reject(err);
}
else {
resolve(connObj);
}
});
})];
});
});
};
JdbcDriver.prototype.init = function (connection) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) {
connection.initialize(function (err) {
if (err) {
console.error('Error initializing connection:', err);
reject(err);
}
else {
JdbcDriver.connection.set(_this.type, connection);
resolve();
}
});
})];
});
});
};
JdbcDriver.connection = new Map();
return JdbcDriver;
}(drivers_1.default));
exports.default = JdbcDriver;