Commit 6d53991a by Orlando Vazquez

Add Database#executeScript and clean up sqlite.js

parent fa95fda3
...@@ -56,7 +56,7 @@ The low-level bindings directly interface with the SQLite C API. The API ...@@ -56,7 +56,7 @@ The low-level bindings directly interface with the SQLite C API. The API
approximately matches the SQLite3 API when it makes sense. approximately matches the SQLite3 API when it makes sense.
var sys = require('sys'), var sys = require('sys'),
sqlite = require('sqlite/sqlite3_bindings'); sqlite = require('sqlite');
var db = new sqlite.Database(); var db = new sqlite.Database();
...@@ -99,7 +99,7 @@ approximately matches the SQLite3 API when it makes sense. ...@@ -99,7 +99,7 @@ approximately matches the SQLite3 API when it makes sense.
To create a new database object: To create a new database object:
var db = sqlite_bindings.Database(); var db = sqlite.Database();
### database.open(filename, function (error) {}) ### database.open(filename, function (error) {})
...@@ -113,6 +113,23 @@ A filename of ":memory:" may be used to create an in-memory database. ...@@ -113,6 +113,23 @@ A filename of ":memory:" may be used to create an in-memory database.
Close the database handle. Close the database handle.
### database.executeScript(SQL, function (error) {});
db.executeScript
( "CREATE TABLE table1 (id, name);"
+ "CREATE TABLE table2 (id, age);"
+ "INSERT INTO table1 (1, 'Mister Shake');"
+ "INSER INTO table2 (1, 34);"
, function (error) {
if (error) throw error;
// ...
});
Execute multiple semi-colon separated SQL statements. Statements must take no
placeholders. Each statements will be executed with a single step() and then
reset. This is ideally suited to executing DDL statements that take no
arguments and return no results.
### database.prepare(SQL, [options,] function (error, statement) {}) ### database.prepare(SQL, [options,] function (error, statement) {})
Create a prepared statement from an SQL string. Prepared statements can be Create a prepared statement from an SQL string. Prepared statements can be
......
...@@ -18,14 +18,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -18,14 +18,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
var sys = require("sys"); var sys = require("sys");
var sqlite = require("./sqlite3_bindings"); var sqlite = require("./sqlite3_bindings");
// load numeric constants from sqlite3_bindings
for (prop in sqlite) {
var obj = sqlite[prop];
if ((obj === +obj) || (toString.call(obj) === '[object Number]') ) {
exports[prop] = sqlite[prop];
}
}
var Database = exports.Database = function () { var Database = exports.Database = function () {
var self = this; var self = this;
...@@ -40,28 +32,48 @@ Database.prototype = { ...@@ -40,28 +32,48 @@ Database.prototype = {
constructor: Database, constructor: Database,
}; };
function _queryDone(db, statement) { Database.prototype.query = function(sql, bindings, rowCallback) {
if (statement.tail) { var self = this;
statement.finalize(function () {
db.prepareAndStep(statement.tail, onPrepare); if (typeof(bindings) == "function") {
}); rowCallback = bindings;
return; bindings = undefined;
} }
statement.finalize(function () { this.prepare(sql, function(error, statement) {
// if there are any queries queued, let them know it's safe to go function next() {
db.emit("ready"); _doStep(self, statement, rowCallback);
}
if (error) {
return rowCallback (error);
}
if (statement) {
if (Array.isArray(bindings)) {
statement.bindArray(bindings, next);
}
else if (typeof(bindings) === 'object') {
statement.bindObject(bindings, next);
}
else {
next();
}
}
else {
rowCallback();
}
}); });
} }
function _doStep(db, statement, rowCallback) { function _doStep(db, statement, rowCallback) {
statement.step(function (error, row) { statement.step(function (error, row) {
if (error) if (error) {
return rowCallback (error); return rowCallback(error);
}
if (!row) { if (!row) {
rowCallback(); rowCallback();
_queryDone(db, statement); statement.finalize(function(){});
return; return;
} }
rowCallback(undefined, row); rowCallback(undefined, row);
...@@ -70,48 +82,38 @@ function _doStep(db, statement, rowCallback) { ...@@ -70,48 +82,38 @@ function _doStep(db, statement, rowCallback) {
}); });
} }
function _onPrepare(db, statement, bindings, rowCallback) { // Execute SQL statements separated by semi-colons.
function next() { // SQL must contain no placeholders. Results are discarded.
_doStep(db, statement, rowCallback); Database.prototype.executeScript = function (script, callback) {
}
if (Array.isArray(bindings)) {
statement.bindArray(bindings, next);
}
else if (typeof(bindings) === 'object') {
statement.bindObject(bindings, next);
}
}
Database.prototype.query = function(sql, bindings, rowCallback, prepareMode) {
var self = this;
if (typeof(bindings) == "function") {
prepareMode = rowCallback || sqlite.EXEC_EMPTY;
rowCallback = bindings;
bindings = [];
}
if (typeof(prepareMode) == "undefined")
prepareMode = sqlite.EXEC_EMPTY;
this.prepareAndStep(sql, function(error, statement) {
if (error)
return rowCallback (error);
if (statement) {
_onPrepare(self, statement, bindings, rowCallback);
} else {
rowCallback();
}
}, prepareMode);
}
Database.prototype.insert = function(sql, insertCallback) {
var self = this; var self = this;
this.prepareAndStep(sql, function(error, info) { (function stepOverSQL(sql) {
if (error) self.prepare(sql, function(error, statement) {
return insertCallback (error); if (error) {
return callback(error);
insertCallback (undefined, (info ? info.last_inserted_id : 0)); }
}, sqlite.EXEC_LAST_INSERT_ID);
statement.step(function (error, row) {
var tail;
if (error) {
callback(error);
return;
}
if (!row) {
statement.finalize(function(){});
tail = statement.tail;
if (typeof tail == "string") {
tail = tail.trim();
}
if (tail) {
stepOverSQL(tail);
}
else {
callback();
}
}
});
});
})(script);
} }
...@@ -34,6 +34,7 @@ exports.insertMany = function (db, table, fields, rows, callback) { ...@@ -34,6 +34,7 @@ exports.insertMany = function (db, table, fields, rows, callback) {
} }
db.prepare(sql, function (error, stmt) { db.prepare(sql, function (error, stmt) {
if (error) return callback(error);
statement = stmt; statement = stmt;
doStep(--i); doStep(--i);
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment