Add sqlite3 adapter, and change initialization slightly

This commit is contained in:
Timothy Warren 2014-11-03 10:59:18 -05:00
parent df9dfcdebc
commit c43e902525
9 changed files with 130 additions and 8 deletions

View File

@ -9,6 +9,8 @@ A node query builder for various SQL databases, based on CodeIgniter's query bui
* mysql
* mysql2
* pg
* dblite
* sqlite3
### Installation
@ -21,7 +23,10 @@ A node query builder for various SQL databases, based on CodeIgniter's query bui
var connection = ... // Database module connection
// Three arguments: database type, database connection, database connection library
var query = nodeQuery('mysql', connection, 'mysql2');
var query = nodeQuery.init('mysql', connection, 'mysql2');
// You can also retrieve the instance later
query = nodeQuery.getQuery();
query.select('foo')
.from('bar')

29
lib/adapters/sqlite3.js Normal file
View File

@ -0,0 +1,29 @@
'use strict';
var adapter = require('../adapter'),
getArgs = require('getargs');
/** @module adapters/dblite */
var Sqlite3 = function(instance) {
// That 'new' keyword is annoying
if ( ! (this instanceof Sqlite3)) return new Sqlite3(instance);
/**
* Run the sql query as a prepared statement
*
* @param {String} sql - The sql with placeholders
* @param {Array} params - The values to insert into the query
* @param {Function} callback - Callback to run when a response is recieved
* @return void
*/
adapter.execute = function(/*sql, params, callback*/) {
var args = getArgs('sql:string, [params]:array, callback:function', arguments);
instance.all(args.sql, args.params, args.callback);
};
return adapter;
}
module.exports = Sqlite3;

View File

@ -3,6 +3,8 @@
/** @module node-query */
var nodeQuery = {};
var instance = null;
/**
* Create a query builder object
*
@ -29,8 +31,22 @@ nodeQuery.init = function (driverType, connObject, connLib) {
}
});
return qb(require(paths.driver), require(paths.adapter)(connObject));
instance = qb(require(paths.driver), require(paths.adapter)(connObject));
return instance;
};
/**
* Return an existing query builder instance
*
* @return {queryBuilder}
*/
nodeQuery.getQuery = function () {
if ( ! instance) {
throw new Error("No Query Builder instance to return");
}
module.exports = nodeQuery.init;
return instance;
};
module.exports = nodeQuery;

View File

@ -25,7 +25,7 @@ if (connection)
{
// Set up the query builder object
var nodeQuery = require('../../lib/node-query');
var qb = nodeQuery('sqlite', connection, adapterName);
var qb = nodeQuery.init('sqlite', connection, adapterName);
// Set up the sqlite database
var sql = 'CREATE TABLE IF NOT EXISTS "create_test" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);' +

View File

@ -17,7 +17,7 @@ var connection = mysql2.createConnection(config.conn);
// Set up the query builder object
var nodeQuery = require('../../lib/node-query');
var qb = nodeQuery('mysql', connection, adapterName);
var qb = nodeQuery.init('mysql', connection, adapterName);
// Set up the test base
testBase._setUp(qb, function(test, err, rows) {

View File

@ -17,7 +17,7 @@ var connection = mysql.createConnection(config.conn);
// Set up the query builder object
var nodeQuery = require('../../lib/node-query');
var qb = nodeQuery('mysql', connection, adapterName);
var qb = nodeQuery.init('mysql', connection, adapterName);
// Set up the test base
testBase._setUp(qb, function(test, err, rows) {

View File

@ -22,7 +22,7 @@ connection.connect(function(err) {
// Set up the query builder object
var nodeQuery = require('../../lib/node-query');
var qb = nodeQuery('pg', connection, adapterName);
var qb = nodeQuery.init('pg', connection, adapterName);
// Set up the test base

View File

@ -0,0 +1,72 @@
'use strict';
// Load the test base
delete require.cache[require.resolve('../query-builder-base')];
var testBase = require('../query-builder-base');
var tests = testBase.tests;
// Load the test config file
var adapterName = 'sqlite3';
var sqlite = null;
var connection = null;
// Set up the connection
try {
sqlite = require(adapterName).verbose();
connection = new sqlite.Database(':memory:');
} catch (e) {
// Export an empty testsuite if module not loaded
console.log(e);
console.log("Database adapter sqlite3 not found");
return {};
}
if (connection)
{
// Set up the query builder object
var nodeQuery = require('../../lib/node-query');
var qb = nodeQuery.init('sqlite', connection, adapterName);
// Set up the sqlite database
var sql = 'CREATE TABLE IF NOT EXISTS "create_test" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);';
var sql2 = 'CREATE TABLE IF NOT EXISTS "create_join" ("id" INTEGER PRIMARY KEY, "key" TEXT, "val" TEXT);';
connection.serialize(function() {
connection.run(sql);
connection.run(sql2);
});
// Set up the test base
testBase._setUp(qb, function(test, err, result) {
if (err != null) {
test.done();
throw new Error(err);
}
// Insert/Update/Delete queries return undefined
if (result === undefined) {
result = {};
}
test.ok(result, 'sqlite3: Valid result for generated query');
test.done();
});
tests["sqlite3 adapter with query builder"] = function(test) {
test.expect(1);
test.ok(testBase.qb);
// Close the db connection
connection.close();
test.done();
};
// Export the final test object
module.exports = tests;
}
else
{
module.exports = {};
}

View File

@ -27,7 +27,7 @@ module.exports = {
'Invalid driver type': function(test) {
test.expect(1);
test.throws(function() {
modules['node-query']('foo', {}, 'bar');
modules['node-query'].init('foo', {}, 'bar');
}, Error, "Bad driver throws exception");
test.done();
}