db.js
1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"use strict";
var oracledb = require('oracledb');
var config;
var oraCon;
function init(options) {
if (!options.config) {
console.trace('Undefined options.config');
process.exit(1);
}
config = options.config;
connectToOracle();
}
function connectToOracle(callback) {
oracledb.getConnection(config.oracle, function(err, connection) {
if (err) {
console.trace('Can not connect to oracle db: ' + err);
process.exit(1);
}
oraCon = connection;
console.log("Oracle db connected")
if (callback) {
callback(null, oraCon);
}
});
}
function connection() {
return oraCon;
}
function createResultObject(res) {
let metadata = res.metaData;
let metadataCount = metadata.length;
let retval = [];
let resultCount = res.rows.length;
for (let i=0; i < resultCount; i++) {
let row = res.rows[i];
let data = {};
for (let j=0; j<metadataCount; j++) {
let colName = metadata[j].name;
data[colName] = row[j];
}
retval.push(data);
}
return retval;
}
function composeQueryWithFilter(query, filter) {
let newQuery = query;
let values = [];
if (!filter) {
return {query: newQuery, values: values};
}
let i;
for (let key in filter) {
newQuery += " AND " + key + " = :A" + i++;
values.push(filter[key]);
}
return {query: newQuery, values: values};
}
exports.init = init;
exports.connection = connection;
exports.composeQueryWithFilter = composeQueryWithFilter;
exports.createResultObject = createResultObject;