node-firebird 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ /***************************************
2
+ *
3
+ * Statement
4
+ *
5
+ ***************************************/
6
+
7
+ function Statement(connection) {
8
+ this.connection = connection;
9
+ }
10
+
11
+ Statement.prototype.close = function(callback) {
12
+ this.connection.closeStatement(this, callback);
13
+ };
14
+
15
+ Statement.prototype.drop = function(callback) {
16
+ this.connection.dropStatement(this, callback);
17
+ };
18
+
19
+ Statement.prototype.release = function(callback) {
20
+ var cache_query = this.connection.getCachedQuery(this.query);
21
+ if (cache_query)
22
+ this.connection.closeStatement(this, callback);
23
+ else
24
+ this.connection.dropStatement(this, callback);
25
+ };
26
+
27
+ Statement.prototype.execute = function(transaction, params, callback, custom) {
28
+
29
+ if (params instanceof Function) {
30
+ custom = callback;
31
+ callback = params;
32
+ params = undefined;
33
+ }
34
+
35
+ this.custom = custom;
36
+ this.connection.executeStatement(transaction, this, params, callback, custom);
37
+ };
38
+
39
+ Statement.prototype.fetch = function(transaction, count, callback) {
40
+ this.connection.fetch(this, transaction, count, callback);
41
+ };
42
+
43
+ Statement.prototype.fetchAll = function(transaction, callback) {
44
+ this.connection.fetchAll(this, transaction, callback);
45
+ };
46
+
47
+ module.exports = Statement;
@@ -0,0 +1,160 @@
1
+ const {doCallback, doError} = require('../callback');
2
+ const Const = require('./const');
3
+
4
+ /***************************************
5
+ *
6
+ * Transaction
7
+ *
8
+ ***************************************/
9
+
10
+ function Transaction(connection) {
11
+ this.connection = connection;
12
+ this.db = connection.db;
13
+ }
14
+
15
+ Transaction.prototype.newStatement = function(query, callback) {
16
+ var cnx = this.connection;
17
+ var self = this;
18
+ var query_cache = cnx.getCachedQuery(query);
19
+
20
+ if (query_cache) {
21
+ callback(null, query_cache);
22
+ } else {
23
+ cnx.prepare(self, query, false, callback);
24
+ }
25
+ };
26
+
27
+ Transaction.prototype.execute = function(query, params, callback, custom) {
28
+
29
+ if (params instanceof Function) {
30
+ custom = callback;
31
+ callback = params;
32
+ params = undefined;
33
+ }
34
+
35
+ var self = this;
36
+ this.newStatement(query, function(err, statement) {
37
+
38
+ if (err) {
39
+ doError(err, callback);
40
+ return;
41
+ }
42
+
43
+ function dropError(err) {
44
+ statement.release();
45
+ doCallback(err, callback);
46
+ }
47
+
48
+ statement.execute(self, params, function(err, ret) {
49
+ if (err) {
50
+ dropError(err);
51
+ return;
52
+ }
53
+
54
+ switch (statement.type) {
55
+ case Const.isc_info_sql_stmt_select:
56
+ statement.fetchAll(self, function(err, r) {
57
+ if (err) {
58
+ dropError(err);
59
+ return;
60
+ }
61
+
62
+ statement.release();
63
+
64
+ if (callback)
65
+ callback(undefined, r, statement.output, true);
66
+
67
+ });
68
+
69
+ break;
70
+
71
+ case Const.isc_info_sql_stmt_exec_procedure:
72
+ if (ret && ret.data && ret.data.length > 0) {
73
+ statement.release();
74
+
75
+ if (callback)
76
+ callback(undefined, ret.data[0], statement.output, true);
77
+
78
+ break;
79
+ } else if (statement.output.length) {
80
+ statement.fetch(self, 1, function(err, ret) {
81
+ if (err) {
82
+ dropError(err);
83
+ return;
84
+ }
85
+
86
+ statement.release();
87
+
88
+ if (callback)
89
+ callback(undefined, ret.data[0], statement.output, false);
90
+ });
91
+
92
+ break;
93
+ }
94
+
95
+ // Fall through is normal
96
+ default:
97
+ statement.release();
98
+ if (callback)
99
+ callback()
100
+ break;
101
+ }
102
+
103
+ }, custom);
104
+ });
105
+ };
106
+
107
+ Transaction.prototype.sequentially = function (query, params, on, callback, asArray) {
108
+
109
+ if (params instanceof Function) {
110
+ asArray = callback;
111
+ callback = on;
112
+ on = params;
113
+ params = undefined;
114
+ }
115
+
116
+ if (on === undefined){
117
+ throw new Error('Expected "on" delegate.');
118
+ }
119
+
120
+ if (callback instanceof Boolean) {
121
+ asArray = callback;
122
+ callback = undefined;
123
+ }
124
+
125
+ var self = this;
126
+ self.execute(query, params, callback, { asObject: !asArray, asStream: true, on: on });
127
+ return self;
128
+ };
129
+
130
+ Transaction.prototype.query = function(query, params, callback) {
131
+
132
+ if (params instanceof Function) {
133
+ callback = params;
134
+ params = undefined;
135
+ }
136
+
137
+ if (callback === undefined)
138
+ callback = noop;
139
+
140
+ this.execute(query, params, callback, { asObject: true, asStream: callback === undefined || callback === null });
141
+
142
+ };
143
+
144
+ Transaction.prototype.commit = function(callback) {
145
+ this.connection.commit(this, callback);
146
+ };
147
+
148
+ Transaction.prototype.rollback = function(callback) {
149
+ this.connection.rollback(this, callback);
150
+ };
151
+
152
+ Transaction.prototype.commitRetaining = function(callback) {
153
+ this.connection.commitRetaining(this, callback);
154
+ };
155
+
156
+ Transaction.prototype.rollbackRetaining = function(callback) {
157
+ this.connection.rollbackRetaining(this, callback);
158
+ };
159
+
160
+ module.exports = Transaction;
@@ -0,0 +1,518 @@
1
+ const Const= require('./const');
2
+
3
+ /***************************************
4
+ *
5
+ * SQLVar
6
+ *
7
+ ***************************************/
8
+
9
+ const
10
+ ScaleDivisor = [1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000, 100000000000,1000000000000,10000000000000,100000000000000,1000000000000000];
11
+ const
12
+ DateOffset = 40587,
13
+ TimeCoeff = 86400000,
14
+ MsPerMinute = 60000;
15
+
16
+ //------------------------------------------------------
17
+
18
+ function SQLVarText() {}
19
+
20
+ SQLVarText.prototype.decode = function(data, lowerV13) {
21
+ let ret;
22
+ if (this.subType > 1) {
23
+ // ToDo: with column charset
24
+ ret = data.readText(this.length, Const.DEFAULT_ENCODING);
25
+ } else if (this.subType === 0) {
26
+ // without charset definition
27
+ ret = data.readText(this.length, Const.DEFAULT_ENCODING);
28
+ } else {
29
+ ret = data.readBuffer(this.length);
30
+ }
31
+
32
+ if (!lowerV13 || !data.readInt()) {
33
+ return ret;
34
+ }
35
+
36
+ return null;
37
+ };
38
+
39
+ SQLVarText.prototype.calcBlr = function(blr) {
40
+ blr.addByte(Const.blr_text);
41
+ blr.addWord(this.length);
42
+ };
43
+
44
+ //------------------------------------------------------
45
+
46
+ function SQLVarNull() {}
47
+ SQLVarNull.prototype = new SQLVarText();
48
+ SQLVarNull.prototype.constructor = SQLVarNull;
49
+
50
+ //------------------------------------------------------
51
+
52
+ function SQLVarString() {}
53
+
54
+ SQLVarString.prototype.decode = function(data, lowerV13) {
55
+ let ret;
56
+ if (this.subType > 1) {
57
+ // ToDo: with column charset
58
+ ret = data.readString(Const.DEFAULT_ENCODING);
59
+ } else if (this.subType === 0) {
60
+ // without charset definition
61
+ ret = data.readString(Const.DEFAULT_ENCODING);
62
+ } else {
63
+ ret = data.readBuffer();
64
+ }
65
+
66
+ if (!lowerV13 || !data.readInt()) {
67
+ return ret;
68
+ }
69
+
70
+ return null;
71
+ };
72
+
73
+ SQLVarString.prototype.calcBlr = function(blr) {
74
+ blr.addByte(Const.blr_varying);
75
+ blr.addWord(this.length);
76
+ };
77
+
78
+ //------------------------------------------------------
79
+
80
+ function SQLVarQuad() {}
81
+
82
+ SQLVarQuad.prototype.decode = function(data, lowerV13) {
83
+ var ret = data.readQuad();
84
+
85
+ if (!lowerV13 || !data.readInt()) {
86
+ return ret;
87
+ }
88
+ return null;
89
+ };
90
+
91
+ SQLVarQuad.prototype.calcBlr = function(blr) {
92
+ blr.addByte(Const.blr_quad);
93
+ blr.addShort(this.scale);
94
+ };
95
+
96
+ //------------------------------------------------------
97
+
98
+ function SQLVarBlob() {}
99
+ SQLVarBlob.prototype = new SQLVarQuad();
100
+ SQLVarBlob.prototype.constructor = SQLVarBlob;
101
+
102
+ SQLVarBlob.prototype.calcBlr = function(blr) {
103
+ blr.addByte(Const.blr_quad);
104
+ blr.addShort(0);
105
+ };
106
+
107
+ //------------------------------------------------------
108
+
109
+ function SQLVarArray() {}
110
+ SQLVarArray.prototype = new SQLVarQuad();
111
+ SQLVarArray.prototype.constructor = SQLVarArray;
112
+
113
+ SQLVarArray.prototype.calcBlr = function(blr) {
114
+ blr.addByte(Const.blr_quad);
115
+ blr.addShort(0);
116
+ };
117
+
118
+ //------------------------------------------------------
119
+
120
+ function SQLVarInt() {}
121
+
122
+ SQLVarInt.prototype.decode = function(data, lowerV13) {
123
+ var ret = data.readInt();
124
+
125
+ if (this.scale) {
126
+ ret = ret / ScaleDivisor[Math.abs(this.scale)];
127
+ }
128
+
129
+ if (!lowerV13 || !data.readInt()) {
130
+ return ret;
131
+ }
132
+
133
+ return null;
134
+ };
135
+
136
+ SQLVarInt.prototype.calcBlr = function(blr) {
137
+ blr.addByte(Const.blr_long);
138
+ blr.addShort(this.scale);
139
+ };
140
+
141
+ //------------------------------------------------------
142
+
143
+ function SQLVarShort() {}
144
+ SQLVarShort.prototype = new SQLVarInt();
145
+ SQLVarShort.prototype.constructor = SQLVarShort;
146
+
147
+ SQLVarShort.prototype.calcBlr = function(blr) {
148
+ blr.addByte(Const.blr_short);
149
+ blr.addShort(this.scale);
150
+ };
151
+
152
+ //------------------------------------------------------
153
+
154
+ function SQLVarInt64() {}
155
+
156
+ SQLVarInt64.prototype.decode = function(data, lowerV13) {
157
+ var ret = data.readInt64();
158
+
159
+ if (this.scale) {
160
+ ret = ret / ScaleDivisor[Math.abs(this.scale)];
161
+ }
162
+
163
+ if (!lowerV13 || !data.readInt()) {
164
+ return ret;
165
+ }
166
+ return null;
167
+ };
168
+
169
+ SQLVarInt64.prototype.calcBlr = function(blr) {
170
+ blr.addByte(Const.blr_int64);
171
+ blr.addShort(this.scale);
172
+ };
173
+
174
+ //------------------------------------------------------
175
+
176
+ function SQLVarInt128() {}
177
+
178
+ SQLVarInt128.prototype.decode = function (data, lowerV13) {
179
+ var retBigInt = BigInt(data.readInt128())
180
+
181
+ if (retBigInt > BigInt(Number.MAX_SAFE_INTEGER)) {
182
+ var ret = retBigInt.toString();
183
+
184
+ var integerPart = ret.slice(0, Math.abs(this.scale) * -1)
185
+ var decimalPart = ret.slice(Math.abs(this.scale) * -1)
186
+
187
+ if (integerPart === '') integerPart = '0'
188
+
189
+ ret = `${integerPart}.${decimalPart}`
190
+ } else {
191
+ var ret = Number(retBigInt);
192
+ ret = ret / ScaleDivisor[Math.abs(this.scale)];
193
+ }
194
+
195
+ if (!lowerV13 || !data.readInt()) {
196
+ return ret;
197
+ }
198
+
199
+ return null;
200
+ };
201
+
202
+ SQLVarInt128.prototype.calcBlr = function (blr) {
203
+ blr.addByte(Const.blr_int128);
204
+ blr.addShort(this.scale);
205
+ };
206
+
207
+ //------------------------------------------------------
208
+
209
+ function SQLVarFloat() { }
210
+
211
+ SQLVarFloat.prototype.decode = function(data, lowerV13) {
212
+ var ret = data.readFloat();
213
+
214
+ if (!lowerV13 || !data.readInt()) {
215
+ return ret;
216
+ }
217
+
218
+ return null;
219
+ };
220
+
221
+ SQLVarFloat.prototype.calcBlr = function(blr) {
222
+ blr.addByte(Const.blr_float);
223
+ };
224
+
225
+ //------------------------------------------------------
226
+
227
+ function SQLVarDouble() {}
228
+
229
+ SQLVarDouble.prototype.decode = function(data, lowerV13) {
230
+ var ret = data.readDouble();
231
+
232
+ if (!lowerV13 || !data.readInt()) {
233
+ return ret;
234
+ }
235
+
236
+ return null;
237
+ };
238
+
239
+ SQLVarDouble.prototype.calcBlr = function(blr) {
240
+ blr.addByte(Const.blr_double);
241
+ };
242
+
243
+ //------------------------------------------------------
244
+
245
+ function SQLVarDate() {}
246
+
247
+ SQLVarDate.prototype.decode = function(data, lowerV13) {
248
+ var ret = data.readInt();
249
+
250
+ if (!lowerV13 || !data.readInt()) {
251
+ var d = new Date(0);
252
+ d.setMilliseconds((ret - DateOffset) * TimeCoeff + d.getTimezoneOffset() * MsPerMinute);
253
+ return d;
254
+ }
255
+
256
+ return null;
257
+ };
258
+
259
+ SQLVarDate.prototype.calcBlr = function(blr) {
260
+ blr.addByte(Const.blr_sql_date);
261
+ };
262
+
263
+ //------------------------------------------------------
264
+
265
+ function SQLVarTime() {}
266
+
267
+ SQLVarTime.prototype.decode = function(data, lowerV13) {
268
+ var ret = data.readUInt();
269
+
270
+ if (!lowerV13 || !data.readInt()) {
271
+ var d = new Date(0);
272
+ d.setMilliseconds(Math.floor(ret / 10) + d.getTimezoneOffset() * MsPerMinute);
273
+ return d;
274
+ }
275
+ return null;
276
+ };
277
+
278
+ SQLVarTime.prototype.calcBlr = function(blr) {
279
+ blr.addByte(Const.blr_sql_time);
280
+ };
281
+
282
+ //------------------------------------------------------
283
+
284
+ function SQLVarTimeStamp() {}
285
+
286
+ SQLVarTimeStamp.prototype.decode = function(data, lowerV13) {
287
+ var date = data.readInt();
288
+ var time = data.readUInt();
289
+
290
+ if (!lowerV13 || !data.readInt()) {
291
+ var d = new Date(0);
292
+ d.setMilliseconds((date - DateOffset) * TimeCoeff + Math.floor(time / 10) + d.getTimezoneOffset() * MsPerMinute);
293
+ return d;
294
+ }
295
+
296
+ return null;
297
+ };
298
+
299
+ SQLVarTimeStamp.prototype.calcBlr = function(blr) {
300
+ blr.addByte(Const.blr_timestamp);
301
+ };
302
+
303
+ //------------------------------------------------------
304
+
305
+ function SQLVarBoolean() {}
306
+
307
+ SQLVarBoolean.prototype.decode = function(data, lowerV13) {
308
+ var ret = data.readInt();
309
+
310
+ if (!lowerV13 || !data.readInt()) {
311
+ return Boolean(ret);
312
+ }
313
+ return null;
314
+ };
315
+
316
+ SQLVarBoolean.prototype.calcBlr = function(blr) {
317
+ blr.addByte(Const.blr_bool);
318
+ };
319
+
320
+ //------------------------------------------------------
321
+
322
+ function SQLParamInt(value){
323
+ this.value = value;
324
+ }
325
+
326
+ SQLParamInt.prototype.calcBlr = function(blr) {
327
+ blr.addByte(Const.blr_long);
328
+ blr.addShort(0);
329
+ };
330
+
331
+ SQLParamInt.prototype.encode = function(data) {
332
+ if (this.value != null) {
333
+ data.addInt(this.value);
334
+ } else {
335
+ data.addInt(0);
336
+ data.addInt(1);
337
+ }
338
+ };
339
+
340
+ //------------------------------------------------------
341
+
342
+ function SQLParamInt64(value){
343
+ this.value = value;
344
+ }
345
+
346
+ SQLParamInt64.prototype.calcBlr = function(blr) {
347
+ blr.addByte(Const.blr_int64);
348
+ blr.addShort(0);
349
+ };
350
+
351
+ SQLParamInt64.prototype.encode = function(data) {
352
+ if (this.value != null) {
353
+ data.addInt64(this.value);
354
+ } else {
355
+ data.addInt64(0);
356
+ data.addInt(1);
357
+ }
358
+ };
359
+
360
+ //------------------------------------------------------
361
+
362
+ function SQLParamInt128(value) {
363
+ this.value = value;
364
+ }
365
+
366
+ SQLParamInt128.prototype.calcBlr = function (blr) {
367
+ blr.addByte(Const.blr_int128);
368
+ blr.addShort(0);
369
+ };
370
+
371
+ SQLParamInt128.prototype.encode = function (data) {
372
+ if (this.value != null) {
373
+ data.addInt128(this.value);
374
+ } else {
375
+ data.addInt128(0);
376
+ data.addInt(1);
377
+ }
378
+ };
379
+
380
+ //------------------------------------------------------
381
+
382
+ function SQLParamDouble(value) {
383
+ this.value = value;
384
+ }
385
+
386
+ SQLParamDouble.prototype.encode = function(data) {
387
+ if (this.value != null) {
388
+ data.addDouble(this.value);
389
+ } else {
390
+ data.addDouble(0);
391
+ data.addInt(1);
392
+ }
393
+ };
394
+
395
+ SQLParamDouble.prototype.calcBlr = function(blr) {
396
+ blr.addByte(Const.blr_double);
397
+ };
398
+
399
+ //------------------------------------------------------
400
+
401
+ function SQLParamString(value) {
402
+ this.value = value;
403
+ }
404
+
405
+ SQLParamString.prototype.encode = function(data) {
406
+ if (this.value != null) {
407
+ data.addText(this.value, Const.DEFAULT_ENCODING);
408
+ } else {
409
+ data.addInt(1);
410
+ }
411
+ };
412
+
413
+ SQLParamString.prototype.calcBlr = function(blr) {
414
+ blr.addByte(Const.blr_text);
415
+ var len = this.value ? Buffer.byteLength(this.value, Const.DEFAULT_ENCODING) : 0;
416
+ blr.addWord(len);
417
+ };
418
+
419
+ //------------------------------------------------------
420
+
421
+ function SQLParamQuad(value) {
422
+ this.value = value;
423
+ }
424
+
425
+ SQLParamQuad.prototype.encode = function(data) {
426
+ if (this.value != null) {
427
+ data.addInt(this.value.high);
428
+ data.addInt(this.value.low);
429
+ } else {
430
+ data.addInt(0);
431
+ data.addInt(0);
432
+ data.addInt(1);
433
+ }
434
+ };
435
+
436
+ SQLParamQuad.prototype.calcBlr = function(blr) {
437
+ blr.addByte(Const.blr_quad);
438
+ blr.addShort(0);
439
+ };
440
+
441
+ //------------------------------------------------------
442
+
443
+ function SQLParamDate(value) {
444
+ this.value = value;
445
+ }
446
+
447
+ SQLParamDate.prototype.encode = function(data) {
448
+ if (this.value != null) {
449
+
450
+ var value = this.value.getTime() - this.value.getTimezoneOffset() * MsPerMinute;
451
+ var time = value % TimeCoeff;
452
+ var date = (value - time) / TimeCoeff + DateOffset;
453
+ time *= 10;
454
+
455
+ // check overflow
456
+ if (time < 0) {
457
+ date--;
458
+ time = TimeCoeff*10 + time;
459
+ }
460
+
461
+ data.addInt(date);
462
+ data.addUInt(time);
463
+ } else {
464
+ data.addInt(0);
465
+ data.addUInt(0);
466
+ data.addInt(1);
467
+ }
468
+ };
469
+
470
+ SQLParamDate.prototype.calcBlr = function(blr) {
471
+ blr.addByte(Const.blr_timestamp);
472
+ };
473
+
474
+ //------------------------------------------------------
475
+
476
+ function SQLParamBool(value) {
477
+ this.value = value;
478
+ }
479
+
480
+ SQLParamBool.prototype.encode = function(data) {
481
+ if (this.value != null) {
482
+ data.addInt(this.value ? 1 : 0);
483
+ } else {
484
+ data.addInt(0);
485
+ data.addInt(1);
486
+ }
487
+ };
488
+
489
+ SQLParamBool.prototype.calcBlr = function(blr) {
490
+ blr.addByte(Const.blr_short);
491
+ blr.addShort(0);
492
+ };
493
+
494
+ module.exports = {
495
+ SQLVarArray,
496
+ SQLVarDate,
497
+ SQLVarBlob,
498
+ SQLVarBoolean,
499
+ SQLVarDouble,
500
+ SQLVarInt,
501
+ SQLVarInt64,
502
+ SQLVarInt128,
503
+ SQLVarFloat,
504
+ SQLVarNull,
505
+ SQLVarShort,
506
+ SQLVarString,
507
+ SQLVarText,
508
+ SQLVarTime,
509
+ SQLVarTimeStamp,
510
+ SQLParamBool,
511
+ SQLParamDate,
512
+ SQLParamDouble,
513
+ SQLParamInt,
514
+ SQLParamInt64,
515
+ SQLParamInt128,
516
+ SQLParamQuad,
517
+ SQLParamString,
518
+ };