Commit 87e12892 by Konstantin Käfer

add Statement#lastID and Statement#changes on .run() queries

parent 9f6496b9
...@@ -434,7 +434,7 @@ Handle<Value> Statement::Run(const Arguments& args) { ...@@ -434,7 +434,7 @@ Handle<Value> Statement::Run(const Arguments& args) {
HandleScope scope; HandleScope scope;
Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This()); Statement* stmt = ObjectWrap::Unwrap<Statement>(args.This());
Baton* baton = stmt->Bind<Baton>(args); Baton* baton = stmt->Bind<RunBaton>(args);
if (baton == NULL) { if (baton == NULL) {
return ThrowException(Exception::Error(String::New("Data type is not supported"))); return ThrowException(Exception::Error(String::New("Data type is not supported")));
} }
...@@ -449,7 +449,7 @@ void Statement::EIO_BeginRun(Baton* baton) { ...@@ -449,7 +449,7 @@ void Statement::EIO_BeginRun(Baton* baton) {
} }
int Statement::EIO_Run(eio_req *req) { int Statement::EIO_Run(eio_req *req) {
STATEMENT_INIT(Baton); STATEMENT_INIT(RunBaton);
sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->handle); sqlite3_mutex* mtx = sqlite3_db_mutex(stmt->db->handle);
sqlite3_mutex_enter(mtx); sqlite3_mutex_enter(mtx);
...@@ -465,6 +465,10 @@ int Statement::EIO_Run(eio_req *req) { ...@@ -465,6 +465,10 @@ int Statement::EIO_Run(eio_req *req) {
if (!(stmt->status == SQLITE_ROW || stmt->status == SQLITE_DONE)) { if (!(stmt->status == SQLITE_ROW || stmt->status == SQLITE_DONE)) {
stmt->message = std::string(sqlite3_errmsg(stmt->db->handle)); stmt->message = std::string(sqlite3_errmsg(stmt->db->handle));
} }
else {
baton->inserted_id = sqlite3_last_insert_rowid(stmt->db->handle);
baton->changes = sqlite3_changes(stmt->db->handle);
}
} }
sqlite3_mutex_leave(mtx); sqlite3_mutex_leave(mtx);
...@@ -474,7 +478,7 @@ int Statement::EIO_Run(eio_req *req) { ...@@ -474,7 +478,7 @@ int Statement::EIO_Run(eio_req *req) {
int Statement::EIO_AfterRun(eio_req *req) { int Statement::EIO_AfterRun(eio_req *req) {
HandleScope scope; HandleScope scope;
STATEMENT_INIT(Baton); STATEMENT_INIT(RunBaton);
if (stmt->status != SQLITE_ROW && stmt->status != SQLITE_DONE) { if (stmt->status != SQLITE_ROW && stmt->status != SQLITE_DONE) {
Error(baton); Error(baton);
...@@ -482,6 +486,9 @@ int Statement::EIO_AfterRun(eio_req *req) { ...@@ -482,6 +486,9 @@ int Statement::EIO_AfterRun(eio_req *req) {
else { else {
// Fire callbacks. // Fire callbacks.
if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) { if (!baton->callback.IsEmpty() && baton->callback->IsFunction()) {
stmt->handle_->Set(String::NewSymbol("lastID"), Local<Integer>(Integer::New(baton->inserted_id)));
stmt->handle_->Set(String::NewSymbol("changes"), Local<Integer>(Integer::New(baton->changes)));
Local<Value> argv[] = { Local<Value>::New(Null()) }; Local<Value> argv[] = { Local<Value>::New(Null()) };
TRY_CATCH_CALL(stmt->handle_, baton->callback, 1, argv); TRY_CATCH_CALL(stmt->handle_, baton->callback, 1, argv);
} }
......
...@@ -101,6 +101,13 @@ public: ...@@ -101,6 +101,13 @@ public:
Data::Row row; Data::Row row;
}; };
static struct RunBaton : Baton {
RunBaton(Statement* stmt_, Handle<Function> cb_) :
Baton(stmt_, cb_), inserted_id(0), changes(0) {}
sqlite3_int64 inserted_id;
int changes;
};
static struct RowsBaton : Baton { static struct RowsBaton : Baton {
RowsBaton(Statement* stmt_, Handle<Function> cb_) : RowsBaton(Statement* stmt_, Handle<Function> cb_) :
Baton(stmt_, cb_) {} Baton(stmt_, cb_) {}
......
var sqlite3 = require('sqlite3');
var assert = require('assert');
exports['test row changes and lastID'] = function(beforeExit) {
var db = new sqlite3.Database(':memory:');
var finished = false;
db.serialize(function() {
db.run("CREATE TABLE foo (id INT, txt TEXT)");
var stmt = db.prepare("INSERT INTO foo VALUES(?, ?)");
var j = 1;
for (var i = 0; i < 1000; i++) {
stmt.run(i, "demo", function(err) {
if (err) throw err;
// Relies on SQLite's row numbering to be gapless and starting
// from 1.
assert.equal(j++, this.lastID);
});
}
db.run("UPDATE foo SET id = id + 1 WHERE id % 2 = 0", function(err) {
if (err) throw err;
assert.equal(500, this.changes);
finished = true;
});
});
beforeExit(function() {
assert.ok(finished);
})
}
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