Permalink
Browse files

Fix events.

  • Loading branch information...
1 parent f384e87 commit e3e2d829c38b414db4c9eff8aad313fcee8e7caa @grumdrig committed with Eric Fredricksen Dec 13, 2009
Showing with 80 additions and 53 deletions.
  1. +4 −0 sqlite.js
  2. +51 −52 sqlite3_bindings.cc
  3. +25 −1 test.js
View
@@ -24,6 +24,10 @@ process.mixin(exports, bindings);
// Conform somewhat to http://dev.w3.org/html5/webdatabase/#sql
+exports.SQLITE_DELETE = 9;
+exports.SQLITE_INSERT = 18;
+exports.SQLITE_UPDATE = 23;
+
exports.openDatabaseSync = function (name, version, displayName,
estimatedSize, creationCallback) {
View
@@ -21,6 +21,45 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
using namespace v8;
using namespace node;
+
+#define CHECK(rc) { if ((rc) != SQLITE_OK) \
+ return ThrowException(Exception::Error(String::New( \
+ sqlite3_errmsg(*db)))); }
+
+#define SCHECK(rc) { if ((rc) != SQLITE_OK) \
+ return ThrowException(Exception::Error(String::New( \
+ sqlite3_errmsg(sqlite3_db_handle(*stmt))))); }
+
+#define REQ_ARGS(N) \
+ if (args.Length() < (N)) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Expected " #N "arguments")));
+
+#define REQ_STR_ARG(I, VAR) \
+ if (args.Length() <= (I) || !args[I]->IsString()) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " must be a string"))); \
+ String::Utf8Value VAR(args[I]->ToString());
+
+#define REQ_EXT_ARG(I, VAR) \
+ if (args.Length() <= (I) || !args[I]->IsExternal()) \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " invalid"))); \
+ Local<External> VAR = Local<External>::Cast(args[I]);
+
+#define OPT_INT_ARG(I, VAR, DEFAULT) \
+ int VAR; \
+ if (args.Length() <= (I)) { \
+ VAR = (DEFAULT); \
+ } else if (args[I]->IsInt32()) { \
+ VAR = args[I]->Int32Value(); \
+ } else { \
+ return ThrowException(Exception::TypeError( \
+ String::New("Argument " #I " must be an integer"))); \
+ }
+
+
+
class Sqlite3Db : public EventEmitter
{
public:
@@ -44,9 +83,6 @@ class Sqlite3Db : public EventEmitter
}
protected:
- Sqlite3Db() : db_(NULL) {
- }
-
Sqlite3Db(sqlite3* db) : db_(db) {
}
@@ -61,61 +97,22 @@ class Sqlite3Db : public EventEmitter
protected:
static Handle<Value> New(const Arguments& args) {
HandleScope scope;
-
- if (args.Length() == 0 || !args[0]->IsString()) {
- return ThrowException(Exception::TypeError(
- String::New("First argument must be a string")));
- }
-
- String::Utf8Value filename(args[0]->ToString());
+ REQ_STR_ARG(0, filename);
sqlite3* db;
int rc = sqlite3_open(*filename, &db);
- if (rc) {
- Local<String> err = v8::String::New(sqlite3_errmsg(db));
- sqlite3_close(db);
- return ThrowException(Exception::Error(err));
- }
- (new Sqlite3Db(db))->Wrap(args.This());
- return args.This();
- }
-
-
-#define CHECK(rc) { if ((rc) != SQLITE_OK) \
- return ThrowException(Exception::Error(String::New( \
- sqlite3_errmsg(*db)))); }
-
-#define SCHECK(rc) { if ((rc) != SQLITE_OK) \
- return ThrowException(Exception::Error(String::New( \
- sqlite3_errmsg(sqlite3_db_handle(*stmt))))); }
+ if (rc) return ThrowException(Exception::Error(
+ String::New("Error opening database")));
+ Sqlite3Db* dbo = new Sqlite3Db(db);
+ dbo->Wrap(args.This());
-#define REQ_ARGS(N) \
- if (args.Length() < (N)) \
- return ThrowException(Exception::TypeError( \
- String::New("Expected " #N "arguments"))); \
+ sqlite3_commit_hook(db, CommitHook, dbo);
+ sqlite3_rollback_hook(db, RollbackHook, dbo);
+ sqlite3_update_hook(db, UpdateHook, dbo);
-#define REQ_STR_ARG(I, VAR) \
- if (args.Length() <= (I) || !args[I]->IsString()) \
- return ThrowException(Exception::TypeError( \
- String::New("Argument " #I " must be a string"))); \
- String::Utf8Value VAR(args[I]->ToString());
-
-#define REQ_EXT_ARG(I, VAR) \
- if (args.Length() <= (I) || !args[I]->IsExternal()) \
- return ThrowException(Exception::TypeError( \
- String::New("Argument " #I " invalid"))); \
- Local<External> VAR = Local<External>::Cast(args[I]);
-
-#define OPT_INT_ARG(I, VAR, DEFAULT) \
- int VAR; \
- if (args.Length() <= (I)) { \
- VAR = (DEFAULT); \
- } else if (args[I]->IsInt32()) { \
- VAR = args[I]->Int32Value(); \
- } else { \
- return ThrowException(Exception::TypeError( \
- String::New("Argument " #I " must be an integer"))); \
+ return args.This();
}
+
//
// JS DatabaseSync bindings
//
@@ -165,6 +162,7 @@ class Sqlite3Db : public EventEmitter
db->Emit("update", 4, args);
}
+ /*
static Handle<Value> Open(const Arguments& args) {
HandleScope scope;
Sqlite3Db* db = ObjectWrap::Unwrap<Sqlite3Db>(args.This());
@@ -178,6 +176,7 @@ class Sqlite3Db : public EventEmitter
return args.This();
}
+ */
static Handle<Value> Prepare(const Arguments& args) {
HandleScope scope;
View
@@ -16,6 +16,26 @@ function asserteq(v1, v2) {
var db = sqlite.openDatabaseSync('test.db');
+
+var commits = 0;
+var rollbacks = 0;
+var updates = 0;
+db.addListener("commit", function () {
+ commits++;
+});
+
+db.addListener("rollback", function () {
+ sys.puts("ROLLBACK");
+ rollbacks++;
+});
+
+db.addListener("update", function (operation, database, table, rowid) {
+ //sys.puts("update " + operation + " " + database + "." + table + " " + rowid);
+ updates++;
+});
+
+
+
db.query("CREATE TABLE egg (a,y,e)");
db.query("INSERT INTO egg (a) VALUES (1)", function () {
process.assert(this.insertId == 1);
@@ -46,7 +66,6 @@ db.query("SELECT e FROM egg WHERE a = ?", [5], function (es) {
});
-
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE tex (t,e,x)");
var i = tx.executeSql("INSERT INTO tex (t,e,x) VALUES (?,?,?)",
@@ -89,6 +108,11 @@ try {
}
+sys.puts("commits: " + commits);
+sys.puts("rollbacks: " + rollbacks);
+sys.puts("updates: " + updates);
+
+
db.close();
sys.puts("OK\n");

0 comments on commit e3e2d82

Please sign in to comment.