olivierphi / Node-DBI

A SQL database abstraction layer strongly inspired by the PHP Zend Framework Zend_Db API, with support of multiple Node.js database engines
https://github.com/DrBenton/Node-DBI
93 stars 22 forks source link

please DBWrapper.close() method to close db connection #1

Closed flyingsky closed 13 years ago

flyingsky commented 13 years ago

To use DBWrapper to fetch result from database, it requires dbwrapper.connect(), then execute sql statements. But DBWrapper doesn't have close method, therefore we cannot close db connection. If you run test cases in vows without close db connection, the vows process won't be over.

In my local code, I add below methods. ===========dbWrapper.js========== DBWrapper.prototype.close = function() { var me = this; if(me._connected){ me._adapter.close(function(){ me._connected = false; }); } };

==========adapter--mysql.js========= Adapter.prototype.close = function(callback){ if(this._debug){ console.log('Adapter.prototype.close (' + JSON.stringify(this._connectionParams) + ')'); } if(this._dbClient){ this._dbClient.end(); callback(0); }else{ callback(1); console.log('cannot close connection, since it is not connected'); } };

=============adapter--sqlite.js================ Adapter.prototype.close = function(callback){ if(this._debug){ console.log('Adapter.prototype.close (' + JSON.stringify(this._connectionParams) + ')'); } if(this._dbClient){ this._dbClient.close(callback); }else{ callback(1); console.log('cannot close connection, since it is not connected'); } };

============dbAdapterAbstract.js=============

DBAdapterAbstract.prototype.close = function(callback) { throw new Error('DB Adapter implementations must override DBAdapterAbstract.prototype.close() !'); };

===========How to use it========== var DBWrapper = require('node-dbi').DBWrapper; var dbwrapper = new DBWrapper('mysql', yourConnectionParams); dbwrapper.connect(); dbwrapper.fetchAll('select * from user', null, function(err, result){ //your code to process result dbwrapper.close(); });

olivierphi commented 13 years ago

Thanks for pointing to this lack ! :-) It's fixed now : I've added the "close()" method in the DBWrapper and in its DB adapters !

I've pushed this new version to the NPM repository, too.