danieloneill / nodedbi

LibDBI addon for Node.js
GNU General Public License v2.0
1 stars 1 forks source link

NodeDBI

NodeDBI is a LibDBI interface for Node.js.

For the source code please visit the github repository.

In addition to providing a traditional interface for SQL database access, it also offers developers the ability of paging on results programmatically and storing result handles to a session as shown below.

It's somewhat complete, and of course contributions are much appreciated.

LibDBI and development headers are required.

On Debian or Ubuntu, apt-get install libdbi-dev.

Then run node-gyp configure build to build the module and node-gyp install to install it.

Example

I've put together a couple Gists that may be useful:

#!/usr/bin/nodejs

var mod = require('nodedbi');

var args = { 'host':'localhost', 'port':3306, 'username':'root', 'password':'', 'type':'mysql', 'dbname':'test' };

var obj = new mod.DBConnection( args );

var q = obj.query("SELECT * FROM users WHERE username=%1 OR id=%2", ['doneill', 6]);
if( !q )
{
        console.log("Query failed!");
        return;
}

// Simple:
var results = q.toArray();

// Doing the same thing manually:
var rc = q.count();
console.log("Row count: "+rc);

var fc = q.fieldCount();
console.log("Field count: "+q.fieldCount());

var fna = [];
for( var x=0; x < fc; x++ )
{
        var fn = q.fieldName(x+1);
        console.log("Field name("+(x+1)+"): "+fn);
        fna.push( fn );
}

for( var y=0; y < rc; y++ )
{
        q.seek(y+1);
        for( var x=0; x < fc; x++ )
        {
                var v = q.value(x+1);
                console.log( y+" ["+fna[x]+"] = ["+v+"]" );
        }
}

Methods

DBConnection( args )

Create a new database connection and connect.

Must be called with new qualifier, eg:

var NodeDBI = require('nodedbi');
var db = new NodeDBI.DBConnection(args);

A type parameter is required. Other parameters are database specific. For database specific driver options see the libdbi-drivers documentation.


DBConnection::query(queryString, [parameters])

Create and execute a database query.


DBConnection::lastError()

Retrieve the latest error as a string.


DBConnection::lastErrorCode()

Retrieve the latest error.


Connection::lastInsertId([name])

Fetch the row ID generated by the last INSERT command. The row ID is most commonly generated by an auto-incrementing column in the table.


Query()

This object is returned by DBConnection::query and cannot be instantiated on its own.


Query::count()

Row count of result set.


Query::fieldCount()

Column count of the result set.


Query::fieldName(column)

Fetch the field name of a specific column index. Column indexes in LibDBI begin at 1, not 0.


Query::fieldIndex(fieldName)

Fetch the column index of a specific field name. Column indexes in LibDBI begin at 1, not 0.


Query::seek(row)

Seek to a specified row in the resultset. Row indexes in LibDBI begin at 1, not 0.


Query::previousRow()

Seek the cursor to the previous row in the resultset.


Query::nextRow()

Seek the cursor to the next row in the resultset.


Query::currentRow()

Fetch the current row index of the resultset cursor. Row indexes in LibDBI begin at 1, not 0.


Query::value(field)

Fetch the value of the specified field on the current row index of the resultset cursor. Row and column indexes in LibDBI begin at 1, not 0.

field can be either a numeric column index or the field name.

Values are converted to the best Javascript type possible to avoid loss of precision. Binary data is returned as a Buffer object.


Query::begin()

Starts a transaction.


Query::commit()

Commits a transaction, i.e. writes all changes since the transaction was started to the database.


Query::rollback()

Rolls back a transaction, i.e. reverts all changes since the transaction started.