Skip to content

Commit

Permalink
Merge pull request #238 from jucapablanca/master
Browse files Browse the repository at this point in the history
Correct issues and new implementations
  • Loading branch information
mariuz authored Nov 27, 2020
2 parents 879a287 + 3f62863 commit a80139f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 64 deletions.
Empty file added git
Empty file.
8 changes: 6 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ declare module 'node-firebird' {
export interface Transaction {
query(query: string, params: any[], callback: QueryCallback): void;
execute(query: string, params: any[], callback: QueryCallback): void;
sequentially(query: string, params: any[], rowCallback: SequentialCallback, callback: SimpleCallback, asArray?: boolean): Database;
commit(callback?: SimpleCallback): void;
commitRetaining(callback?: SimpleCallback): void;
rollback(callback?: SimpleCallback): void;
rollbackRetaining(callback?: SimpleCallback): void;
}

export interface Options {
Expand All @@ -56,7 +59,7 @@ declare module 'node-firebird' {

export function attach(options: Options, callback: DatabaseCallback): void;
export function attach(options: SvcMgrOptions, callback: ServiceManagerCallback): void;
export function escape(value: any, protocolVersion: number = 0x800D /*PROTOCOL_VERSION13*/): string;
export function escape(value: any, protocolVersion: number /*PROTOCOL_VERSION13*/): string;
export function create(options: Options, callback: DatabaseCallback): void;
export function attachOrCreate(options: Options, callback: DatabaseCallback): void;
export function pool(max: number, options: Options): ConnectionPool;
Expand Down Expand Up @@ -191,6 +194,7 @@ declare module 'node-firebird' {
}

type ServiceManagerCallback = (err: any, svc: ServiceManager) => void;
// @ts-ignore
type ReadableCallback = (err: any, reader: NodeJS.ReadableStream) => void;
type InfoCallback = (err: any, info: ServerInfo) => void;
type LineCallback = (err: any, data: { result: number, line: string }) => void;
Expand Down Expand Up @@ -237,4 +241,4 @@ declare module 'node-firebird' {
readusers(options: ReadableOptions, callback: ReadableCallback): void;
readlimbo(options: ReadableOptions, callback: ReadableCallback): void;
}
}
}
121 changes: 76 additions & 45 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,29 @@ Transaction.prototype.execute = function(query, params, callback, custom) {
});
};

Transaction.prototype.sequentially = function (query, params, on, callback, asArray) {

if (params instanceof Function) {
asArray = callback;
callback = on;
on = params;
params = undefined;
}

if (on === undefined){
throw new Error('Expected "on" delegate.');
}

if (callback instanceof Boolean) {
asArray = callback;
callback = undefined;
}

var self = this;
self.execute(query, params, callback, { asObject: !asArray, asStream: true, on: on });
return self;
};

Transaction.prototype.query = function(query, params, callback) {

if (params instanceof Function) {
Expand Down Expand Up @@ -1616,19 +1639,25 @@ Database.prototype.execute = function(query, params, callback, custom) {

Database.prototype.sequentially = function(query, params, on, callback, asArray) {

if (params instanceof Function) {
asArray = callback;
callback = on;
on = params;
params = undefined;
}
if (params instanceof Function) {
asArray = callback;
callback = on;
on = params;
params = undefined;
}

if (on === undefined)
throw new Error('Expected "on" delegate.');
if (on === undefined){
throw new Error('Expected "on" delegate.');
}

var self = this;
self.execute(query, params, callback, { asObject: !asArray, asStream: true, on: on });
return self;
if (callback instanceof Boolean) {
asArray = callback;
callback = undefined;
}

var self = this;
self.execute(query, params, callback, { asObject: !asArray, asStream: true, on: on });
return self;
};

Database.prototype.query = function(query, params, callback) {
Expand Down Expand Up @@ -3183,14 +3212,7 @@ function decodeResponse(data, callback, cnx, lowercase_keys, cb) {

data.fcolumn = 0;
statement.connection.db.emit('row', data.frow, statement.nbrowsfetched, custom.asObject);

if (!custom.asStream) {
data.frows.push(data.frow);
}

if (custom.on) {
custom.on(data.frow, statement.nbrowsfetched);
}
data.frows.push(data.frow);
data.frow = custom.asObject ? {} : new Array(output.length);

try {
Expand Down Expand Up @@ -3446,7 +3468,10 @@ Connection.prototype.attach = function (options, callback, db) {

blr.addBytes([isc_dpb_process_id, 4]);
blr.addInt32(process.pid);
blr.addString(isc_dpb_process_name, process.title, DEFAULT_ENCODING);

let processName = process.title || "";
blr.addString(isc_dpb_process_name, processName.length > 255 ? processName.substring(processName.length - 255, processName.length) : processName, DEFAULT_ENCODING);

if (this.accept.authData) {
blr.addString(isc_dpb_specific_auth_data, this.accept.authData, DEFAULT_ENCODING);
}
Expand Down Expand Up @@ -3533,7 +3558,10 @@ Connection.prototype.createDatabase = function (options, callback) {

blr.addBytes([isc_dpb_process_id, 4]);
blr.addInt32(process.pid);
blr.addString(isc_dpb_process_name, process.title, DEFAULT_ENCODING);

let processName = process.title || "";
blr.addString(isc_dpb_process_name, processName.length > 255 ? processName.substring(processName.length - 255, processName.length) : processName, DEFAULT_ENCODING);

if (this.accept.authData) {
blr.addString(isc_dpb_specific_auth_data, this.accept.authData, DEFAULT_ENCODING);
}
Expand Down Expand Up @@ -4330,33 +4358,36 @@ Connection.prototype.fetch = function(statement, transaction, count, callback) {
this._queueEvent(callback);
};

Connection.prototype.fetchAll = function(statement, transaction, callback) {

var self = this;
var data;
var loop = function(err, ret) {

if (err) {
callback(err);
return;
}

if (!data) {
data = ret.data;
} else {
for (var i = 0, length = ret.data.length; i < length; i++)
data.push(ret.data[i]);
}

if (ret.fetched)
callback(undefined, data);
else
self.fetch(statement, transaction, DEFAULT_FETCHSIZE, loop);
}
Connection.prototype.fetchAll = function (statement, transaction, callback) {
var self = this, data = [];
var loop = function (err, ret) {
if (err) {
return callback(err);
} else if (ret && ret.data && ret.data.length) {
for (var i = 0; i < ret.data.length; i++) {
var pos = data.push(ret.data[i]);
if (statement.custom && statement.custom.asStream && statement.custom.on) {
statement.custom.on(ret.data[i], pos - 1);
}
if (i === ret.data.length - 1) {
if (ret.fetched) {
return callback(undefined, data);
} else {
self.fetch(statement, transaction, DEFAULT_FETCHSIZE, loop);
}
}
}
} else if (ret.fetched) {
callback(undefined, data);
} else {
self.fetch(statement, transaction, DEFAULT_FETCHSIZE, loop);
}
}

this.fetch(statement, transaction, DEFAULT_FETCHSIZE, loop);
this.fetch(statement, transaction, DEFAULT_FETCHSIZE, loop);
};


Connection.prototype.openBlob = function(blob, transaction, callback) {
var msg = this._msg;
msg.pos = 0;
Expand Down
27 changes: 10 additions & 17 deletions lib/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,18 @@ BitSet.prototype.scale = function(index) {
};

BitSet.prototype.set = function(index, value) {
this.scale(index);
let pos = index >>> 3;

for (let i = this.data.length; pos >= i; pos--) {
this.data.push(BIT_OFF);
}

pos = index >>> 3;

if (value === undefined || value) {
this.data[index >>> WORD_LOG] |= (1 << index);
this.data[pos] |= (1 << (index % BUFFER_BITS));
} else {
this.data[index >>> WORD_LOG] &= ~(1 << index);
this.data[pos] &= ~(1 << (index % BUFFER_BITS));
}
};

Expand All @@ -488,18 +494,5 @@ BitSet.prototype.get = function(index) {
};

BitSet.prototype.toBuffer = function() {
var buf = [], int32, tmp;

for (var i = 0; i < this.data.length; i += BUFFER_BITS) {
tmp = this.data.slice(i, i + BUFFER_BITS);
int32 = 0;

for (var k = 0; k < tmp.length; k++) {
int32 = (int32 * 2) + tmp[k];
}

buf.push(int32);
}

return Buffer.from(buf)
return Buffer.from(this.data);
};

0 comments on commit a80139f

Please sign in to comment.