-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
318 lines (252 loc) · 8.2 KB
/
index.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
*this service will perform common tasks for oracle-db via 'oracledb' [official node module from oracle]
*@ayanb
*@111215
*/
//instantiate required module
var oracledb = require('oracledb');
/**
* this function will not take any parameters.
* static parameters should be changed from here directly if required
*/
//var o = {};
var o = {
dbName:null,
tableName:null,
fromString: null,
whereString: null,
selectString: null,
intoString: null,
insertString: null,
limitString: null,
orderByString: null,
insertValue:[],
placeholderString:null,
connect: function(cb) {
console.log("connecting to database");
oracledb.getConnection({
user: "user",
password: "pass",
connectString: "CONN_STRING"
},
function(err, con) {
if (err) {
console.error(err.message);
cb(null);
}
cb(con);
});
return this;
},
releaseConnection: function(connection) {
console.log("releasing database connection");
connection.release(
function(err) {
if (err) {
console.error(err.message);
}
});
return this;
},
clearData:function(){
this.dbName= null;
this.tableName= null;
this.fromString = null;
this.whereString = null;
this.selectString = null;
this.intoString = null;
this.insertString = null;
this.limitString = null;
this.orderByString = null;
this.insertValue = [];
this.placeholderString = null;
},
from: function(dbName, tableName) {
//if already value is set, clear it
if (this.fromString) {
this.fromString = null;
}
if (dbName && tableName) {
this.dbName = dbName;
this.tableName = tableName;
this.fromString = "FROM " + dbName + "." + tableName + " ";
}
return this;
},
into: function(dbName, tableName) {
//if already value is set, clear it
if (this.intoString) {
this.intoString = null;
}
if (dbName && tableName) {
this.dbName = dbName;
this.tableName = tableName;
this.intoString = "INSERT INTO " + dbName + "." + tableName + " ";
}
//console.log(this.intoString+this.insertString);
//console.log(this.insertValue);
return this;
},
where: function(whereObject) {
if (typeof whereObject !== 'object') {
return;
}
var index = 0;
var GAP = " "; //specifying single gap or space as variable
var where_string = "WHERE";
where_string += GAP; //adding space
for (var i in whereObject) {
//if this is not the first iteration then append "ADD"
if (index !== 0) {
where_string += "AND";
where_string += GAP; //adding space
}
where_string += i; //adding key
where_string += GAP; //adding space
where_string += "="; //adding equal
where_string += "\'" + whereObject[i] + "\'"; //adding val
where_string += GAP; //adding space
//we have to apend "AND" for an extra where clause, but only if current iteration is not the last one of
//the where object
//increment the index variable
index++;
}
console.log(where_string);
this.whereString = where_string;
return this;
},
limit: function(limit_to_num) {
if (limit_to_num) {
this.limitString = this.whereString ? "AND rownum <=" + limit_to_num : "WHERE rownum <=" + limit_to_num;
}
return this;
},
order: function(colName, orderWith) {
if (!colName) {
return;
}
if (!orderWith) {
orderWith = 'ASC';
}
this.orderByString = " ORDER BY " + colName + " " + orderWith;
return this;
},
fetch: function(columns) {
var columnsFinal;
if (typeof columns !== 'object' || columns === null) {
columnsFinal = '*';
} else {
//blank string
var column_string = "";
for (var i in columns) {
//concating with each column name from the coloumns array
column_string += columns[i];
column_string += ",";
//now we need a comma, but not on the last iteration
}
//remove the last comma
var columnsFinal = column_string.slice(0, -1);
}
var GAP = " ";
this.selectString = "SELECT" + GAP + columnsFinal + GAP;
return this;
},
insert:function(rowColumn){
//var insert_string = "";
var field_string = ""; //(id,fname,lname)
var placeholder_string = ""; //(:1,:2,:3)
//var value_string = []; //[null,'ayan','banerjee']
var index = 0;
var GAP = " "; //space
field_string += "(";
placeholder_string += "(";
for (var i in rowColumn) {
if (index !== 0) {
field_string += ",";
placeholder_string += ",";
}
field_string += i; //concating field string
this.insertValue.push(rowColumn[i]); //adding value to array
placeholder_string += ":" + (index + 1); //concating placeholder string
//increment the index variable, this is for manually tracking
index++;
}
field_string += ")";
placeholder_string += ")";
this.insertString = field_string + GAP + "VALUES" + GAP + placeholder_string;
return this;
},
//this is the final function of the chain with callback support, now this
//function should be same for all chains like 'fetch', 'insert', 'update'
exec: function(cb) {
var query = "";
var valueString = this.insertValue ? this.insertValue : [];
var options = this.insertValue.length > 0 ? { autoCommit: true }:{outFormat: oracledb.OBJECT,maxRows: 50000}
if (this.intoString) {
query += this.intoString;
}
if (this.insertString) {
query += this.insertString;
}
if (this.selectString) {
query += this.selectString;
}
if (this.fromString) {
query += this.fromString;
}
if (this.whereString) {
query += this.whereString;
}
if (this.limitString) {
query += this.limitString;
}
if (this.orderByString) {
query += this.orderByString;
}
console.log(query);
console.log(valueString);
console.log(options);
this.connect(function(con) {
con.execute(
query, valueString,options,// bind value for :id
function(err, result) {
if (err) {console.error(err.message); cb(null); }
if(o.intoString){
//now insert query is successfull, but we need to return the lastinsert id, we have to do it manually in oracle
//this function will only execute if this is an insert query, not required for fetch query
//we assume that the sequence name will be like this 'table_name_seq'
//console.log("select "+o.dbName+"."+o.tableName+"_id_seq.nextval");
//con.execute("select "+o.dbName+"."+o.tableName+"_seq.nextval from dual", [], {}, function(err, nextval) {
//if (err) {console.error(err.message); cb(null); }
//no error here
con.execute("select "+o.dbName+"."+o.tableName+"_id_seq.currval from dual", [], {}, function(err, currval) {
if (err) {console.error(err.message); cb(null); }
//no error here
var last_insert_id = currval.rows[0][0] ;
//console.log("select * from "+o.dbName+"."+o.tableName+" where id="+last_insert_id);
con.execute("select * from "+o.dbName+"."+o.tableName+" where country_id="+last_insert_id, [], {outFormat: oracledb.OBJECT,maxRows: 50000}, function(err, lastRow) {
if (err) {console.error(err.message); cb(null); }
//send responce
//console.log('sendddddddddddddddddddding responce');
cb(lastRow.rows);
//reset variables
o.clearData();
//release oracle connection
o.releaseConnection(con);
});
});
}else{
//send responce
cb(result.rows);
//reset variables
o.clearData();
//release oracle connection
o.releaseConnection(con);
}
//});
});
});
//cb(this.query);
}
}
module.exports = o;