forked from xdenser/node-firebird-libfbclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebird.js
239 lines (197 loc) · 5.58 KB
/
firebird.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
var binding = require("./build/Release/binding");
var stream = require("stream");
var util = require("util");
var events = require('events');
var SchunkSize = 4*1024;
var Connection = binding.Connection;
var FBEventEmitter = binding.FBEventEmitter;
var FBResult = binding.FBResult;
var FBStatement = binding.FBStatement;
var FBblob = binding.FBblob;
var Transaction = binding.Transaction;
// inherit FBEventEmitter (and all descendants ) from events.EventEmitter
FBEventEmitter.prototype.__proto__ = events.EventEmitter.prototype;
function MakeSafe(obj,meth){
var superm = obj.prototype[meth];
obj.prototype[meth] = function safe(){
if(this.inAsyncCall){
var self = this;
var args = arguments;
this.once("fbStopAsync",function(){
safe.apply(self,args);
//superm.apply(self,args);
});
}
else {
superm.apply(this,arguments);
}
}
}
MakeSafe(Connection,"query");
MakeSafe(Connection,"commit");
MakeSafe(Connection, "rollback");
MakeSafe(Connection, "startTransaction");
//MakeSafe(Connection, "startNewTransaction");
MakeSafe(FBResult,"fetch");
MakeSafe(FBStatement, "exec");
MakeSafe(FBStatement, "execInTrans");
MakeSafe(FBblob,"_read");
MakeSafe(FBblob, "_write");
MakeSafe(Transaction, "start");
MakeSafe(Transaction, "query");
MakeSafe(Transaction, "commit");
MakeSafe(Transaction, "rollback");
var superConnect = Connection.prototype.connect;
Connection.prototype.connect = function(db,user,pass,role,cb){
var obj = this;
superConnect.call(this,db,user,pass,role,function (err){
if(err && (!cb || obj.listeners('error').length)) obj.emit('error',err);
else obj.emit('connected');
if(cb) cb(err);
});
};
var superQuery = Connection.prototype.query;
Connection.prototype.query = function(sql,cb){
var obj = this;
superQuery.call(this,sql,function(err,res){
if(err && (!cb || obj.listeners('error').length)) obj.emit('error',err);
else obj.emit('result',res);
if(cb) cb(err,res);
});
};
var superStartNewTransaction = Connection.prototype.startNewTransaction;
Connection.prototype.startNewTransaction = function (cb) {
var obj = this;
var tr = superStartNewTransaction.call(this, function (err) {
if (err && (!cb || obj.listeners('error').length)) obj.emit('error', err);
if (cb) cb(err, tr);
});
}
binding.FBblob.prototype._readAll = function(initialSize, chunkSize, callback){
if(initialSize instanceof Function)
{
callback = initialSize;
chunkSize = null;
initialSize = null;
}
else
if(chunkSize instanceof Function)
{
callback = chunkSize;
chunkSize = null;
}
if(!chunkSize) chunkSize = 1024;
if(!initialSize) initialSize = 0;
var chunk = new Buffer(chunkSize);
var res = new Buffer(initialSize);
var cPos = 0;
this._openSync();
var self = this;
this._read(chunk, function receiver(err, b, isLast) {
if(err)
{
self.emit('error',err);
}
else
if(!isLast)
{
self.emit('data', b);
if(res.length<=(cPos+b.length))
{
var nr = new Buffer(cPos + b.length);
res.copy(nr);
res = nr;
}
b.copy(res,cPos);
cPos = cPos + b.length;
self._read(chunk,receiver);
}
else
{
self._closeSync();
self.emit('end',res,cPos);
if(callback) callback(null, res, cPos);
}
});
}
exports.createConnection = function (options) {
var c = new Connection(options);
return c;
};
// Allows further extension
exports.binding = binding;
function Stream(blob){
if(!(blob instanceof binding.FBblob )) {
throw new Error('Expecting blob');
//blob = new binding.FBblob();
}
stream.Stream.call(this);
this._blob = blob;
this._pendingWrites = 0;
this.readable = false;
this.writeable = false;
if(blob.isReadable)
{
this._blob._openSync();
this.readable = true;
this._paused = true;
}
else
this.writable = true;
};
util.inherits(Stream, stream.Stream);
exports.Stream = Stream;
function ReadStream(strm) {
if (strm._buf == null) {
strm._buf = new Buffer(SchunkSize);
}
strm._blob._read(strm._buf, function s_rcv(err, b, isLast) {
if (err) {
return strm.emit('error', err);
}
strm.emit('data', b);
if (!strm._paused && !isLast) strm._blob._read(strm._buf, s_rcv);
if (isLast) strm.emit('end');
});
};
Stream.prototype.pause = function(){
this._paused = true;
};
Stream.prototype.resume = function(){
this._paused = false;
ReadStream(this);
};
Stream.prototype.destroy = function () {
this._destroyed = true;
this.check_destroyed();
};
Stream.prototype.check_destroyed = function () {
if (this._destroyed && !this._pendingWrites) {
this._blob._closeSync();
this.emit('close');
}
}
Stream.prototype.write = function (data, encoding, fd) {
if (this._destroyed) {
throw new Error("Stream closed");
}
var self = this;
this._pendingWrites++;
this._blob._write(data, function (err) {
self._pendingWrites--;
if (err) self.emit('error', err);
self.check_destroyed();
});
}
Stream.prototype.end = function(data, encoding, fd) {
var self = this;
if (data) {
this._pendingWrites++;
this._blob._write(data, function (err) {
self._pendingWrites--;
if (err) self.emit('error', err);
self.destroy();
})
}
else this.destroy();
}