Comparing version 0.4.9 to 0.4.10
@@ -111,4 +111,13 @@ /* SQB | ||
options.autoCommit = !!options.autoCommit; | ||
options.resultSet = options.resultSet; | ||
options.debug = !!options.debug; | ||
let o = []; | ||
if (options.onfetchrow) o.push(options.onfetchrow); | ||
if (statement._onfetchrow && statement._onfetchrow.length) | ||
o = o.concat(statement._onfetchrow); | ||
options.onfetchrow = o; | ||
if (options.resultSet) { | ||
if (typeof options.resultSet !== 'object') | ||
options.resultSet = {}; | ||
options.extendedMetaData = true; | ||
@@ -118,2 +127,3 @@ options.objectRows = false; | ||
options.prefetchRows !== undefined ? options.prefetchRows : 100; | ||
options.resultSet.onfetchrow = options.onfetchrow; | ||
} else { | ||
@@ -125,6 +135,4 @@ options.maxRows = | ||
} | ||
options.debug = !!options.debug; | ||
options.onfetchrow = options.onfetchrow || statement._onfetchrow; | ||
out.options = options; | ||
return out; | ||
@@ -152,8 +160,12 @@ } | ||
options = o.options; | ||
self._execute(o.sql, o.params, options, (err, result) => { | ||
self.close(); // Release reference | ||
if (options && options.autoClose) self.close(); | ||
if (options.onfetchrow && result && result.rows) { | ||
if (options.onfetchrow && options.onfetchrow.length && | ||
result && result.rows) { | ||
result.rows.forEach((row, idx) => { | ||
options.onfetchrow(row, idx + 1); | ||
options.onfetchrow.forEach(fn => { | ||
fn(row, idx + 1); | ||
}); | ||
}); | ||
@@ -160,0 +172,0 @@ } |
@@ -66,6 +66,8 @@ /* SQB | ||
}); | ||
return this.meta.dbobj | ||
const st = this.meta.dbobj | ||
.select(...listFields) | ||
.from(subst) | ||
.onFetchRow(subst._onfetchrow); | ||
.from(subst); | ||
if (subst._onfetchrow && subst._onfetchrow.length) | ||
st.onFetchRow(...subst._onfetchrow); | ||
return st; | ||
} | ||
@@ -84,6 +86,8 @@ | ||
}); | ||
return this.meta.dbobj | ||
const st = this.meta.dbobj | ||
.select(...listFields) | ||
.from(subst) | ||
.onFetchRow(subst._onfetchrow); | ||
.from(subst); | ||
if (subst._onfetchrow && subst._onfetchrow.length) | ||
st.onFetchRow(...subst._onfetchrow); | ||
return st; | ||
} | ||
@@ -101,6 +105,8 @@ | ||
}); | ||
return this.meta.dbobj | ||
const st = this.meta.dbobj | ||
.select(...listFields) | ||
.from(subst) | ||
.onFetchRow(subst._onfetchrow); | ||
.from(subst); | ||
if (subst._onfetchrow && subst._onfetchrow.length) | ||
st.onFetchRow(...subst._onfetchrow); | ||
return st; | ||
} | ||
@@ -119,6 +125,8 @@ | ||
}); | ||
return this.meta.dbobj | ||
const st = this.meta.dbobj | ||
.select(...listFields) | ||
.from(subst) | ||
.onFetchRow(subst._onfetchrow); | ||
.from(subst); | ||
if (subst._onfetchrow && subst._onfetchrow.length) | ||
st.onFetchRow(...subst._onfetchrow); | ||
return st; | ||
} | ||
@@ -137,6 +145,8 @@ | ||
}); | ||
return this.meta.dbobj | ||
const st = this.meta.dbobj | ||
.select(...listFields) | ||
.from(subst) | ||
.onFetchRow(subst._onfetchrow); | ||
.from(subst); | ||
if (subst._onfetchrow && subst._onfetchrow.length) | ||
st.onFetchRow(...subst._onfetchrow); | ||
return st; | ||
} | ||
@@ -143,0 +153,0 @@ |
@@ -13,5 +13,7 @@ /* SQB | ||
/* External module dependencies. */ | ||
const {EventEmitter} = require('events'); | ||
const assert = require('assert'); | ||
const Promisify = require('putil-promisify'); | ||
const debug = require('debug')('sqb:ResultSet'); | ||
const TaskQueue = require('putil-taskqueue'); | ||
@@ -23,5 +25,6 @@ /** | ||
class ResultSet { | ||
class ResultSet extends EventEmitter { | ||
constructor(connection, options) { | ||
super(); | ||
this._connection = connection; | ||
@@ -35,4 +38,7 @@ connection.acquire(); | ||
this.autoClose = options && options.autoClose; | ||
this.onfetchrow = options && options.onfetchrow; | ||
this._taskQueue = []; | ||
this._taskQueue = new TaskQueue({cancelOnError: true}); | ||
if (options && options.onfetchrow && options.onfetchrow.length) | ||
options.onfetchrow.forEach((fn) => { | ||
this.on('fetchrow', fn); | ||
}); | ||
} | ||
@@ -52,3 +58,3 @@ | ||
get row() { | ||
get rowNum() { | ||
return this._rownum || 0; | ||
@@ -71,3 +77,2 @@ } | ||
/** | ||
* | ||
* @param {Function} [callback] | ||
@@ -81,22 +86,20 @@ * @return {Promise|undefined} | ||
const self = this; | ||
if (!self._connection) { | ||
if (self.closed) { | ||
callback(); | ||
return; | ||
} | ||
if (process.env.DEBUG) | ||
debug('Closing ResultSet'); | ||
self._taskQueue = []; | ||
self._addTask(() => { | ||
self._taskQueue.enqueue((nextTask) => { | ||
self._close((err) => { | ||
self._taskRunning = false; | ||
if (err) | ||
callback(err); | ||
else { | ||
if (!err) { | ||
const con = this._connection; | ||
this._connection = undefined; | ||
con.close(callback); | ||
con.close(); | ||
} | ||
callback(err); | ||
nextTask(); | ||
}); | ||
}); | ||
self._nextTask(); | ||
} | ||
@@ -116,10 +119,8 @@ | ||
const self = this; | ||
self._addTask(() => { | ||
self._taskQueue.enqueue((nextTask) => { | ||
self._first((...args) => { | ||
self._taskRunning = false; | ||
callback(...args); | ||
self._nextTask(); | ||
nextTask(); | ||
}); | ||
}); | ||
self._nextTask(); | ||
} | ||
@@ -156,10 +157,8 @@ | ||
const self = this; | ||
self._addTask(() => { | ||
self._taskQueue.enqueue((nextTask) => { | ||
self._fetchRows(rowStart, numRows, (...args) => { | ||
self._taskRunning = false; | ||
callback(...args); | ||
self._nextTask(); | ||
nextTask(); | ||
}); | ||
}); | ||
self._nextTask(); | ||
} | ||
@@ -178,22 +177,2 @@ | ||
_addTask(fn) { | ||
this._taskQueue.push(fn); | ||
this._nextTask(); | ||
} | ||
_nextTask() { | ||
const self = this; | ||
if (self._taskRunning) return; | ||
if (self.closed) { | ||
self._taskQueue = []; | ||
return; | ||
} | ||
if (self._taskQueue.length > 0) { | ||
const fn = self._taskQueue[0]; | ||
self._taskQueue.splice(0, 1); | ||
self._taskRunning = true; | ||
setImmediate(fn); | ||
} | ||
} | ||
_move(nRows, arrayResult, callback) { | ||
@@ -206,6 +185,7 @@ assert.ok(nRows, 'Insufficient arguments. nRows required'); | ||
function more() { | ||
self._addTask(() => { | ||
self._taskQueue.enqueue((nextTask) => { | ||
if (self.closed || !nRows || (nRows > 0 && self.eof) || | ||
(nRows < 0 && self.bof)) { | ||
callback(); | ||
nextTask(); | ||
return; | ||
@@ -215,3 +195,2 @@ } | ||
const rowStart = nRows > 0 ? rowNum + 1 : rowNum + nRows; | ||
self._fetchRows(rowStart, Math.abs(nRows), (err, rows) => { | ||
@@ -224,8 +203,8 @@ if (rows) { | ||
self._rownum = 0; | ||
callback(err, | ||
arrayResult ? rows : (rows ? rows[0] : undefined), | ||
() => setImmediate(() => more())); | ||
rows = arrayResult ? rows : | ||
(rows && rows.length ? rows[0] : undefined); | ||
callback(err, rows, more); | ||
nextTask(); | ||
}); | ||
}); | ||
self._nextTask(); | ||
} | ||
@@ -244,2 +223,3 @@ | ||
_fetchRows(rowStart, numRows, callback) { | ||
const self = this; | ||
@@ -251,11 +231,5 @@ if (rowStart < 1) { | ||
const doCallback = function(err, result) { | ||
self._taskRunning = false; | ||
callback(err, result); | ||
self._nextTask(); | ||
}; | ||
const out = []; | ||
if (numRows === 0) { | ||
doCallback(undefined, out); | ||
callback(undefined, out); | ||
return; | ||
@@ -270,3 +244,3 @@ } | ||
if (self.closed || self.eof) { | ||
doCallback(undefined, []); | ||
callback(undefined, []); | ||
return; | ||
@@ -277,3 +251,3 @@ } | ||
if (err) | ||
doCallback(err); | ||
callback(err); | ||
else { | ||
@@ -283,2 +257,9 @@ if (process.env.DEBUG) | ||
let k = self._fetchedRows; | ||
if (self.listenerCount('fetchrow') > 0) { | ||
for (const row of rows) { | ||
self.emit('fetchrow', row, k++, self.metaData); | ||
} | ||
} | ||
self._eof = rows.length < nrows; | ||
@@ -288,3 +269,3 @@ if (self.eof && self.autoClose) | ||
let k = self._fetchedRows; | ||
k = self._fetchedRows; | ||
self._fetchedRows += rows.length; | ||
@@ -303,3 +284,3 @@ // write rows to cache | ||
} | ||
doCallback(undefined, out); | ||
callback(undefined, out); | ||
} | ||
@@ -336,3 +317,3 @@ }); | ||
else | ||
doCallback(undefined, out); | ||
callback(undefined, out); | ||
@@ -401,6 +382,6 @@ }); | ||
const metaData = self.metaData; | ||
Object.getOwnPropertyNames(metaData).forEach(key => { | ||
const v = row[metaData[key].index]; | ||
metaData.fields.forEach((field, idx) => { | ||
const v = row[idx]; | ||
if (!(v === null && self.ignoreNulls)) | ||
out[key] = row[metaData[key].index]; | ||
out[field.name] = v; | ||
}); | ||
@@ -407,0 +388,0 @@ return out; |
@@ -26,2 +26,3 @@ /* SQB | ||
const ResultSet = require('./connect/resultset'); | ||
const TableMetaData = require('./connect/tablemetadata'); | ||
const {ResultCache} = require('./connect/resultcache'); | ||
@@ -31,3 +32,2 @@ | ||
//noinspection JSUnusedGlobalSymbols | ||
@@ -42,2 +42,3 @@ Object.assign(sqbexport, { | ||
ResultCache, | ||
TableMetaData, | ||
@@ -44,0 +45,0 @@ SqlObject, |
@@ -10,3 +10,2 @@ /* SQB | ||
/* Internal module dependencies. */ | ||
const Statement = require('./statement'); | ||
@@ -20,2 +19,5 @@ const SqlObject = require('../sqlobjects/sqlobject'); | ||
/* External module dependencies. */ | ||
const assert = require('assert'); | ||
/** | ||
@@ -231,3 +233,6 @@ * @class | ||
onFetchRow(callback) { | ||
this._onfetchrow = callback; | ||
if (!callback) return this; | ||
assert(typeof callback === 'function'); | ||
this._onfetchrow = this._onfetchrow = []; | ||
this._onfetchrow.push(callback); | ||
return this; | ||
@@ -234,0 +239,0 @@ } |
{ | ||
"name": "sqb", | ||
"description": "Plugin-driven, multi-dialect SQL query builder and Database connection framework for JavaScript", | ||
"version": "0.4.9", | ||
"version": "0.4.10", | ||
"author": "Panates Ltd.", | ||
@@ -25,3 +25,4 @@ "contributors": [ | ||
"putil-flattentext": "^1.0.1", | ||
"putil-promisify": "^1.0.3" | ||
"putil-promisify": "^1.0.3", | ||
"putil-taskqueue": "^1.0.0" | ||
}, | ||
@@ -28,0 +29,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82527
26
2803
4
+ Addedputil-taskqueue@^1.0.0
+ Addeddoublylinked@1.0.7(transitive)
+ Addedputil-taskqueue@1.3.1(transitive)