node-firebird 1.1.8 → 1.1.10

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,100 @@
1
+ const net = require("net");
2
+ const zlib = require("zlib");
3
+
4
+ /**
5
+ * Socket proxy.
6
+ */
7
+ class Socket {
8
+ constructor(port, host) {
9
+ this._socket = net.createConnection(port, host);
10
+ this._socket.setNoDelay(true);
11
+ this.compressor = null;
12
+ this.compressorBuffer = [];
13
+ this.decompressor = null;
14
+ this.decompressorBuffer = [];
15
+ this.buffer = null;
16
+
17
+ return new Proxy(this._socket, this);
18
+ }
19
+
20
+ /**
21
+ * Decompress data when receive it if compression is enabled.
22
+ * Override on data event.
23
+ */
24
+ on(event, cb) {
25
+ if (event === 'data') {
26
+ const mainCb = cb;
27
+ cb = (data) => {
28
+ if (this.compress) {
29
+ this.decompressor.write(data, () => {
30
+ mainCb(Buffer.concat(this.decompressorBuffer));
31
+ this.decompressorBuffer = []; // Reset buffer
32
+ });
33
+ } else {
34
+ mainCb(data);
35
+ }
36
+ };
37
+ }
38
+
39
+ this._socket.on(event, cb);
40
+ }
41
+
42
+ /**
43
+ * Compress data before sending to socket if compression is enabled.
44
+ */
45
+ write(data, defer = false) {
46
+ if (defer) {
47
+ this.buffer = Buffer.from(data);
48
+ return;
49
+ }
50
+
51
+ if (!defer && this.buffer) {
52
+ data = Buffer.concat([this.buffer, data]);
53
+ this.buffer = null;
54
+ }
55
+
56
+ if (this.compress) {
57
+ this.compressor.write(data, () => {
58
+ this._socket.write(Buffer.concat(this.compressorBuffer));
59
+ this.compressorBuffer = []; // Reset buffer
60
+ });
61
+ } else {
62
+ this._socket.write(data);
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Enable compression/decompression on the fly.
68
+ */
69
+ enableCompression() {
70
+ this.compress = true;
71
+
72
+ // Create decompressor instance
73
+ this.decompressor = zlib.createInflate();
74
+ this.decompressor.on('data', (inflate) => {
75
+ this.decompressorBuffer.push(inflate);
76
+ });
77
+
78
+ // Create compressor instance
79
+ this.compressor = zlib.createDeflate({
80
+ flush: zlib.constants.Z_FULL_FLUSH,
81
+ finishFlush: zlib.constants.Z_SYNC_FLUSH
82
+ });
83
+ this.compressor.on('data', (deflate) => {
84
+ this.compressorBuffer.push(deflate);
85
+ });
86
+ }
87
+
88
+ /**
89
+ * Proxy trap.
90
+ */
91
+ get(target, field) {
92
+ if (field in this) {
93
+ return this[field].bind(this);
94
+ }
95
+
96
+ return target[field];
97
+ }
98
+ }
99
+
100
+ module.exports = Socket;
@@ -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;