Comparing version 0.5.1 to 0.6.0
@@ -129,2 +129,9 @@ # Any-DB API | ||
### Query properties | ||
* `.text` - The string query submitted. If you are using MySQL this will | ||
contain interpolated values *after* the query has been enqueued by a | ||
connection. | ||
* `.values` - The parameter values submitted to the backend. | ||
### Query Events | ||
@@ -131,0 +138,0 @@ |
@@ -16,3 +16,3 @@ try { | ||
exports.createConnection = function createConnection (opts, callback) { | ||
exports.createConnection = function createConnection(opts, callback) { | ||
var conn = mysql.createConnection(opts) | ||
@@ -35,5 +35,10 @@ | ||
function wrapQueryMethod (realQuery) { | ||
return function query () { | ||
function wrapQueryMethod(realQuery) { | ||
return function query() { | ||
var q = realQuery.apply(this, arguments) | ||
if (!q.hasOwnProperty('text')) { | ||
Object.defineProperty(q, 'text', { | ||
get: function () { return this.sql } | ||
}); | ||
} | ||
q.on('result', q.emit.bind(q, 'row')) | ||
@@ -45,3 +50,3 @@ q._callback = wrapQueryCallback(q._callback) | ||
function wrapQueryCallback (callback) { | ||
function wrapQueryCallback(callback) { | ||
if (!callback) return | ||
@@ -53,3 +58,4 @@ return function (err, res) { | ||
rows: res, | ||
rowCount: res.length | ||
rowCount: res.length, | ||
lastInsertId: res.insertId | ||
}) | ||
@@ -56,0 +62,0 @@ } |
@@ -12,8 +12,8 @@ var sqlite3 = null | ||
inherits(SQLite3, EventEmitter) | ||
function SQLite3 (db) { | ||
function SQLite3(db) { | ||
EventEmitter.call(this) | ||
this._db = db | ||
var self = this | ||
this._db.on('open', function () { self.emit('open', self) }) | ||
this._db.on('close', function () { self.emit('end') }) | ||
this._db.on('open', function () { self.emit('open', self) }) | ||
this._db.on('close', function () { self.emit('end') }) | ||
this._db.on('error', function (err) { self.emit('error', err) }) | ||
@@ -23,5 +23,3 @@ } | ||
SQLite3.createConnection = function (opts, callback) { | ||
if (!sqlite3) { | ||
throw new Error("sqlite3 driver failed to load, please `npm install sqlite3`") | ||
} | ||
requireDriver(); | ||
@@ -47,3 +45,3 @@ var filename; | ||
var db = new sqlite3.Database(filename, mode) | ||
, adapter = new SQLite3(db) | ||
, adapter = new SQLite3(db) | ||
@@ -59,12 +57,10 @@ if (callback) { | ||
SQLite3.createQuery = function (stmt, params, callback) { | ||
if (!sqlite3) { | ||
throw new Error("sqlite3 driver failed to load, please `npm install sqlite3`") | ||
} | ||
if (stmt instanceof Query) return stmt | ||
return new Query(stmt, params, callback) | ||
SQLite3.createQuery = function (text, values, callback) { | ||
requireDriver(); | ||
if (text instanceof Query) return text | ||
return new Query(text, values, callback) | ||
} | ||
SQLite3.prototype.query = function (stmt, params, callback) { | ||
var query = stmt | ||
SQLite3.prototype.query = function (text, values, callback) { | ||
var query = text | ||
, rowError = false | ||
@@ -74,22 +70,34 @@ ; | ||
if (!(query instanceof Query)) { | ||
query = SQLite3.createQuery(stmt, params, callback); | ||
query = SQLite3.createQuery(text, values, callback); | ||
} | ||
this._db.each( | ||
query.stmt, | ||
query.params, | ||
function onRow(err, row) { | ||
if (rowError) return | ||
rowError = err | ||
query.handleRow(row) | ||
}, | ||
function onComplete(err, count) { | ||
if (err || rowError) query.handleError(err || rowError) | ||
else { | ||
var result = {rows: query._rows, rowCount: count} | ||
if (query._callback) query._callback(null, result) | ||
} | ||
query.emit('end', result) | ||
} | ||
) | ||
if (query.text.match(/^\s*insert\s+/i)) { | ||
this._db.run( | ||
query.text, | ||
query.values, | ||
function onComplete(err) { | ||
if (err) return query.handleError(err) | ||
var result = { rows: [], rowCount: 0, lastInsertId: this.lastID } | ||
if (query._callback) query._callback(null, result) | ||
query.emit('end', result) | ||
} | ||
) | ||
} | ||
else { | ||
this._db.each( | ||
query.text, | ||
query.values, | ||
function onRow(err, row) { | ||
if (rowError) return | ||
rowError = err | ||
query.handleRow(row) | ||
}, | ||
function onComplete(err, count) { | ||
if (err || rowError) return query.handleError(err || rowError) | ||
var result = {rows: query._rows, rowCount: count, lastInsertId: this.lastID} | ||
if (query._callback) query._callback(null, result) | ||
query.emit('end', result) | ||
} | ||
) | ||
} | ||
@@ -106,13 +114,18 @@ return query | ||
function requireDriver() { | ||
if (sqlite3) return; | ||
throw new Error("sqlite3 driver failed to load, please `npm install sqlite3`") | ||
} | ||
inherits(Query, EventEmitter) | ||
function Query(stmt, params, callback) { | ||
function Query(text, values, callback) { | ||
EventEmitter.call(this) | ||
this._rows = [] | ||
this._buffer = true | ||
this.stmt = stmt | ||
if (typeof params == 'function') { | ||
this._callback = params | ||
this.params = [] | ||
this.text = text | ||
if (typeof values == 'function') { | ||
this._callback = values | ||
this.values = [] | ||
} else { | ||
this.params = params | ||
this.values = values | ||
this._callback = callback | ||
@@ -119,0 +132,0 @@ } |
{ | ||
"name": "any-db", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Database-agnostic connection pooling, querying, and result sets", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
43601
29
874
3