Commit 2cbaf25a by Orlando Vazquez

Add a Database#execute that, given some SQL, fetches all rows.

parent 82c2a674
...@@ -20,9 +20,17 @@ currently made. ...@@ -20,9 +20,17 @@ currently made.
db.open("aquateen.db", function (error) { db.open("aquateen.db", function (error) {
if (error) { if (error) {
console.log("Purple Alert! Aqua Teen Database unabled to be opened!")); console.log("Tonight. You."));
throw error; throw error;
} }
db.execute
( "INSERT INTO aqua_teens (name) VALUES (?)"
, ['meaty meaty moo']
, function (error, rows) {
if (error) throw error;
console.log("Aqua teen added.");
}
);
var sql = 'SELECT name FROM dudes WHERE type = ? AND age > ?'; var sql = 'SELECT name FROM dudes WHERE type = ? AND age > ?';
db.prepare(sql, function (error, statement) { db.prepare(sql, function (error, statement) {
...@@ -59,6 +67,12 @@ A filename of ":memory:" may be used to create an in-memory database. ...@@ -59,6 +67,12 @@ A filename of ":memory:" may be used to create an in-memory database.
Close the database handle. Close the database handle.
### database.execute(sql[, bindings], function (error, rows) {})
Execute a SQL query, `sql` with optional bindings `bindings` on the currently
opened database. The callback will be executed once with all the rows returned
for the query. This is much faster than `database.query` since there are less roundtrips into the thread-pool.
### database.query(sql, [bindings,] function (error, row) {}) ### database.query(sql, [bindings,] function (error, row) {})
Execute a SQL query, `sql`, with optional bindings `bindings` on the currently Execute a SQL query, `sql`, with optional bindings `bindings` on the currently
......
...@@ -82,6 +82,54 @@ function _doStep(db, statement, rowCallback) { ...@@ -82,6 +82,54 @@ function _doStep(db, statement, rowCallback) {
}); });
} }
// Execute a single SQL query with the given optional parameters. Calls
// `callback` with all rows or an error on query completion.
Database.prototype.execute = function (sql /* , bindings, callback */) {
var self = this;
var bindings, callback;
var n = arguments.length;
switch (n) {
case 3:
callback = arguments[2];
bindings = arguments[1];
break;
case 2:
callback = arguments[1];
break;
default: throw new Error("Invalid number of arguments ("+n+")");
}
self.prepare(sql, function (error, statement) {
if (error) {
return callback(error);
}
if (bindings) {
statement.bind(bindings, function (error) {
if (error) {
return callback(
new Error("Binding error: " + error.toString()));
}
fetchAll(statement);
});
}
else {
fetchAll(statement);
}
function fetchAll(statement) {
statement.fetchAll(function (error, rows) {
if (error) {
return callback(error);
}
statement.finalize(function () {
callback(undefined, rows);
});
});
}
});
}
// Execute SQL statements separated by semi-colons. // Execute SQL statements separated by semi-colons.
// SQL must contain no placeholders. Results are discarded. // SQL must contain no placeholders. Results are discarded.
Database.prototype.executeScript = function (script, callback) { Database.prototype.executeScript = function (script, callback) {
......
...@@ -820,18 +820,28 @@ int Statement::EIO_FetchAll(eio_req *req) { ...@@ -820,18 +820,28 @@ int Statement::EIO_FetchAll(eio_req *req) {
assert(stmt); assert(stmt);
int ret; int ret;
/* open the pool */
fetchall_req->pool = mpool_open(MPOOL_FLAG_USE_MAP_ANON int rc = sqlite3_step(stmt);
, 0
, NULL if (rc == SQLITE_ROW) {
, &ret); /* open the pool */
if (fetchall_req->pool == NULL) { fetchall_req->pool = mpool_open(MPOOL_FLAG_USE_MAP_ANON
req->result = -1; , 0
, NULL
, &ret);
if (fetchall_req->pool == NULL) {
req->result = -1;
fetchall_req->rows = NULL;
fetchall_req->error = (char *) mpool_strerror(ret);
return 0;
}
}
else {
fetchall_req->pool = NULL;
fetchall_req->rows = NULL; fetchall_req->rows = NULL;
fetchall_req->error = (char *) mpool_strerror(ret);
return 0;
} }
// We're going to be traversing a linked list in two dimensions. // We're going to be traversing a linked list in two dimensions.
struct row_node *cur = NULL struct row_node *cur = NULL
, *prev = NULL , *prev = NULL
......
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