node-firebird
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -136,2 +136,3 @@ var | ||
const | ||
@@ -903,6 +904,6 @@ DSQL_close = 1, | ||
case isc_info_sql_stmt_select: | ||
statement.fetchAll(self, function() { | ||
statement.fetchAll(self, function(ret) { | ||
statement.drop(); | ||
if (callback) { | ||
callback({meta: statement.output, data: statement.rows}) | ||
callback({meta: statement.output, data: ret}) | ||
} | ||
@@ -913,6 +914,6 @@ }, dropError); | ||
if (statement.output.length) { | ||
statement.fetch(self, 1, function() { | ||
statement.fetch(self, 1, function(ret) { | ||
statement.drop(); | ||
if (callback) { | ||
callback({meta: statement.output, data: statement.rows[0]}) | ||
callback({meta: statement.output, data: ret.data[0]}) | ||
} | ||
@@ -1118,7 +1119,3 @@ }, dropError); | ||
var output = statement.output; | ||
var rows = statement.rows; | ||
if (!rows) { | ||
rows = []; | ||
statement.rows = rows; | ||
} | ||
var rows = []; | ||
while (count && (status != 100)) { | ||
@@ -1135,5 +1132,15 @@ var row = new Array(output.length); | ||
} | ||
/* | ||
if (statement.rows) { | ||
for (var j = 0; j < rows.length; j++) { | ||
statement.rows.push(rows[j]) | ||
} | ||
} else { | ||
statement.rows = rows | ||
} | ||
if (status == 100) | ||
statement.fetched = true; | ||
return; | ||
*/ | ||
return {data: rows, fetched: Boolean(status == 100)}; | ||
case op_accept: | ||
@@ -1325,7 +1332,3 @@ if ( | ||
msg.addInt(DSQL_close); | ||
this._queueEvent(function(ret){ | ||
delete(statement.fetched); | ||
delete(statement.rows); | ||
if (callback) {callback(ret)} | ||
}, error); | ||
this._queueEvent(callback, error); | ||
}; | ||
@@ -1578,4 +1581,4 @@ | ||
function fetchBlobs(statement, transaction, from, callback, error) { | ||
if (statement.rows && statement.rows.length) { | ||
function fetchBlobs(statement, transaction, rows, callback, error) { | ||
if (rows.data && rows.data.length) { | ||
var indexes = []; | ||
@@ -1589,3 +1592,3 @@ for (var i = 0; i < statement.output.length; i++) { | ||
function fetch(row, col, callback, error) { | ||
var blobid = statement.rows[row][col]; | ||
var blobid = rows.data[row][col]; | ||
if (blobid) { | ||
@@ -1608,5 +1611,5 @@ statement.connection.openBlob(blobid, transaction, function(blob) { | ||
if (statement.output[col].subType == isc_blob_text) { | ||
statement.rows[row][col] = buffer.toString(DEFAULT_ENCODING); | ||
rows.data[row][col] = buffer.toString(DEFAULT_ENCODING); | ||
} else { | ||
statement.rows[row][col] = buffer | ||
rows.data[row][col] = buffer | ||
} | ||
@@ -1627,4 +1630,4 @@ callback(); | ||
var count = (statement.rows.length - from) * indexes.length; | ||
for (var r = from; r < statement.rows.length; r++) { | ||
var count = rows.data.length * indexes.length; | ||
for (var r = 0; r < rows.data.length; r++) { | ||
for (var c = 0; c < indexes.length; c++) { | ||
@@ -1636,3 +1639,3 @@ fetch(r, indexes[c], | ||
if (callback) { | ||
callback() | ||
callback(rows) | ||
} | ||
@@ -1652,3 +1655,3 @@ } | ||
if (callback) { | ||
callback() | ||
callback(rows) | ||
} | ||
@@ -1658,3 +1661,3 @@ } | ||
if (callback) { | ||
callback() | ||
callback(rows) | ||
} | ||
@@ -1683,8 +1686,8 @@ } | ||
if (transaction) { | ||
var len = statement.rows ? statement.rows.length: 0; | ||
var cb = function() { | ||
fetchBlobs(statement, transaction, len, callback, error) | ||
var cb = function(ret) { | ||
fetchBlobs(statement, transaction, ret, callback, error) | ||
} | ||
cb.statement = statement; | ||
this._queueEvent(cb, error); | ||
} else { | ||
@@ -1698,6 +1701,14 @@ callback.statement = statement; | ||
var self = this; | ||
var loop = function(){ | ||
if (statement.fetched) { | ||
callback() | ||
var data; | ||
var loop = function(ret){ | ||
if (!data) { | ||
data = ret.data; | ||
} else { | ||
for (var i = 0; i < ret.data.length; i++) { | ||
data.push(ret.data[i]); | ||
} | ||
} | ||
if (ret.fetched) { | ||
callback(data) | ||
} else { | ||
self.fetch(statement, transaction, DEFAULT_FETCHSIZE, loop, error) | ||
@@ -1704,0 +1715,0 @@ } |
{ | ||
"name": "node-firebird", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Firebird client - pure javascript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,2 +0,79 @@ | ||
node-firebird | ||
============= | ||
# node-firebird | ||
Pure javascript and asynchronous Firebird client for Node.js | ||
## Install | ||
npm install node-firebird | ||
## Examples | ||
### Connecting | ||
fb = require("node-firebird"); | ||
var database = new fb.Database('127.0.0.1', 3050, db, 'SYSDBA', 'masterkey', | ||
function(){ | ||
console.log("connected"); | ||
}, | ||
function(error){ | ||
console.log("can't connect"); | ||
} | ||
); | ||
### Querying | ||
#### Simple query | ||
database.execute("select cast(? as integer) from rdb$database", [123], | ||
function (result) { | ||
console.log(result.data) | ||
} | ||
); | ||
The transaction automatically started and commited/rollbacked. | ||
- query is a non optional string. | ||
- params is optional, can be a single value or an array. | ||
- callback & error are optional. | ||
### Using transaction | ||
var tr; | ||
function fail(err) { | ||
tr.rollback(); | ||
console.log(err.status); | ||
} | ||
database.startTransaction(function(transaction) { | ||
tr = transaction; | ||
tr.execute("select cast(? as integer) from rdb$database", 123, function(result1) { | ||
tr.execute("select cast(? as integer) from rdb$database", 456, function(result2) { | ||
tr.commit(function(ret) { | ||
console.log(result1.data[0]); | ||
console.log(result2.data[0]); | ||
}, fail) | ||
}, fail); | ||
}, fail); | ||
}) | ||
### Errors handling | ||
Most async methods can trigger a callback and an error event, they are optionnals. If an error occur the error event will be called, if no error event is provided, the error will be sent to the callback event and you will have to check if the result is an error. An error object have a status property. | ||
function CheckResult(obj) { | ||
if (obj.status) { | ||
throw new Error('oups') | ||
} | ||
} | ||
database.startTransaction(function(transaction) { | ||
transaction.execute("select cast(? as integer) from rdb$database", 123, function(result) { | ||
transaction.commit(function(ret) { // commit in all situations for a single query | ||
CheckResult(result); // error executing query ? | ||
CheckResult(ret); // error commiting ? | ||
console.log(result.data); | ||
}) | ||
}); | ||
}) |
@@ -5,3 +5,3 @@ fb = require("../lib"); | ||
macdb = '/fbdata/test.fdb'; | ||
windb = 'D:\\test\\NBDC.FDB'; | ||
windb = 'D:\\test\\test.fdb'; | ||
db = macdb; | ||
@@ -76,15 +76,73 @@ | ||
// concurrency | ||
test4 = function(count) { | ||
var n = Date.now(); | ||
var max = count; | ||
for (var i = 0; i < max; i++) { | ||
database.execute("select cast(? as integer) from rdb$database", 123, function(){ | ||
if (--count == 0) { | ||
console.log(max + " queries"); | ||
console.log((Date.now() - n)/max + 'ms / query'); | ||
function createPool(count, callback) { | ||
var pool = []; | ||
var done = count; | ||
while (count > 0) { | ||
pool[--count] = new fb.Database('127.0.0.1', 3050, db, 'SYSDBA', 'masterkey', function() { | ||
done--; | ||
if (done == 0) { | ||
callback(pool); | ||
} | ||
}); | ||
}, function(err) { | ||
callback(err) | ||
callback = null; | ||
}) | ||
} | ||
} | ||
test4 = function(count, poolsize) { | ||
createPool(poolsize, function(pool) { | ||
var n = Date.now(); | ||
var max = count; | ||
for (var i = 0; i < max; i++) { | ||
pool[i % poolsize].execute("select * from rdb$database", function(){ | ||
if (--count == 0) { | ||
console.log(max + " queries"); | ||
console.log((Date.now() - n)/max + 'ms / query'); | ||
for (var db in pool) {pool[db].detach()} | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
// more complex sample | ||
test5 = function() { | ||
var tr, st; | ||
function error(err) { | ||
if (tr) tr.rollback(); | ||
if (st) st.drop(); | ||
console.log(err); | ||
} | ||
function fetch(callback) { | ||
st.fetch(tr, function(ret) { | ||
console.log(ret.data); | ||
callback(ret.fetched); | ||
}, error) | ||
} | ||
database.startTransaction(function(transaction) { | ||
tr = transaction; | ||
tr.newStatement("select * from rdb$relations", function(statement) { | ||
st = statement; | ||
st.execute(tr, function() { | ||
var cb = function(fetched) { | ||
if (fetched) { | ||
st.drop(); | ||
tr.commit(); | ||
} else { | ||
fetch(cb); | ||
} | ||
}; | ||
fetch(cb); | ||
}, error) | ||
}, error); | ||
}, error) | ||
} | ||
connect = function(callback, error){ | ||
@@ -98,5 +156,5 @@ database = new fb.Database('127.0.0.1', 3050, db, 'SYSDBA', 'masterkey', callback, error) | ||
console.log('connected'); | ||
test2(); | ||
}, | ||
logerror | ||
); |
Sorry, the diff of this file is not supported yet
110096
2724
80
4