forked from xdenser/node-firebird-libfbclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebird.js
More file actions
217 lines (179 loc) · 4.69 KB
/
Copy pathfirebird.js
File metadata and controls
217 lines (179 loc) · 4.69 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
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
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;
// 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(FBResult,"fetch");
MakeSafe(FBStatement,"exec");
MakeSafe(FBblob,"_read");
MakeSafe(FBblob,"_write");
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);
});
};
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,len){
if(err)
{
self.emit('error',err);
}
else
if(len>0)
{
self.emit('data', chunk, len);
if(res.length<=(cPos+len))
{
var nr = new Buffer(cPos+len);
res.copy(nr);
res = nr;
}
chunk.copy(res,cPos,0,len);
cPos = cPos + len;
self._read(chunk,receiver);
}
else
{
self._closeSync();
self.emit('end',res,cPos);
if(callback) callback(null, res, cPos);
}
});
}
exports.createConnection = function () {
var c = new Connection();
return c;
};
// Allows further extension
exports.binding = binding;
function Stream(blob){
var buf = null;
function allocBuf(){
buf = new Buffer(SchunkSize);
}
function ReadStream(strm) {
if(buf == null) allocBuf();
strm._blob._read(buf,function s_rcv(err, b, len){
if(err)
{
strm.emit('error',err);
}
else
if(len>0)
{
strm.emit('data', b, len);
if(!strm._paused) strm._blob._read(buf,s_rcv);
}
else
{
strm.emit('end');
}
});
};
if(!(blob instanceof binding.FBblob )) {
throw new Error('Expecting blob');
//blob = new binding.FBblob();
}
stream.Stream.call(this);
this._blob = blob;
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;
Stream.prototype.pause = function(){
this._paused = true;
};
Stream.prototype.resume = function(){
this._paused = false;
ReadStream(this);
};
Stream.prototype.destroy = function(){
this._blob._closeSync();
this.emit('close');
};
Stream.prototype.write = function(data, encoding, fd) {
if (typeof data != 'string') {
};
var self = this;
//this._blob._writeSync(data);
this._blob._write(data,function(err){
if(err) self.emit('error',err);
//self.emit('drain');
});
}
Stream.prototype.end = function(data, encoding, fd) {
var self = this;
if(data) this._blob._write(data,function(err){
console.log('in blob write callback');
if(err) self.emit('error',err);
self.destroy();
})
else self.destroy();
}