Commit e3e2d829 by Eric Fredricksen Committed by Eric Fredricksen

Fix events.

parent f384e879
...@@ -24,6 +24,10 @@ process.mixin(exports, bindings); ...@@ -24,6 +24,10 @@ process.mixin(exports, bindings);
// Conform somewhat to http://dev.w3.org/html5/webdatabase/#sql // 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, exports.openDatabaseSync = function (name, version, displayName,
estimatedSize, creationCallback) { estimatedSize, creationCallback) {
......
...@@ -21,6 +21,45 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -21,6 +21,45 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
using namespace v8; using namespace v8;
using namespace node; 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 class Sqlite3Db : public EventEmitter
{ {
public: public:
...@@ -44,9 +83,6 @@ public: ...@@ -44,9 +83,6 @@ public:
} }
protected: protected:
Sqlite3Db() : db_(NULL) {
}
Sqlite3Db(sqlite3* db) : db_(db) { Sqlite3Db(sqlite3* db) : db_(db) {
} }
...@@ -61,61 +97,22 @@ protected: ...@@ -61,61 +97,22 @@ protected:
protected: protected:
static Handle<Value> New(const Arguments& args) { static Handle<Value> New(const Arguments& args) {
HandleScope scope; HandleScope scope;
REQ_STR_ARG(0, filename);
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());
sqlite3* db; sqlite3* db;
int rc = sqlite3_open(*filename, &db); int rc = sqlite3_open(*filename, &db);
if (rc) { if (rc) return ThrowException(Exception::Error(
Local<String> err = v8::String::New(sqlite3_errmsg(db)); String::New("Error opening database")));
sqlite3_close(db); Sqlite3Db* dbo = new Sqlite3Db(db);
return ThrowException(Exception::Error(err)); dbo->Wrap(args.This());
}
(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))))); }
#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) \ sqlite3_commit_hook(db, CommitHook, dbo);
if (args.Length() <= (I) || !args[I]->IsExternal()) \ sqlite3_rollback_hook(db, RollbackHook, dbo);
return ThrowException(Exception::TypeError( \ sqlite3_update_hook(db, UpdateHook, dbo);
String::New("Argument " #I " invalid"))); \
Local<External> VAR = Local<External>::Cast(args[I]);
#define OPT_INT_ARG(I, VAR, DEFAULT) \ return args.This();
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"))); \
} }
// //
// JS DatabaseSync bindings // JS DatabaseSync bindings
// //
...@@ -165,6 +162,7 @@ protected: ...@@ -165,6 +162,7 @@ protected:
db->Emit("update", 4, args); db->Emit("update", 4, args);
} }
/*
static Handle<Value> Open(const Arguments& args) { static Handle<Value> Open(const Arguments& args) {
HandleScope scope; HandleScope scope;
Sqlite3Db* db = ObjectWrap::Unwrap<Sqlite3Db>(args.This()); Sqlite3Db* db = ObjectWrap::Unwrap<Sqlite3Db>(args.This());
...@@ -178,6 +176,7 @@ protected: ...@@ -178,6 +176,7 @@ protected:
return args.This(); return args.This();
} }
*/
static Handle<Value> Prepare(const Arguments& args) { static Handle<Value> Prepare(const Arguments& args) {
HandleScope scope; HandleScope scope;
......
...@@ -16,6 +16,26 @@ function asserteq(v1, v2) { ...@@ -16,6 +16,26 @@ function asserteq(v1, v2) {
var db = sqlite.openDatabaseSync('test.db'); 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("CREATE TABLE egg (a,y,e)");
db.query("INSERT INTO egg (a) VALUES (1)", function () { db.query("INSERT INTO egg (a) VALUES (1)", function () {
process.assert(this.insertId == 1); process.assert(this.insertId == 1);
...@@ -46,7 +66,6 @@ db.query("SELECT e FROM egg WHERE a = ?", [5], function (es) { ...@@ -46,7 +66,6 @@ db.query("SELECT e FROM egg WHERE a = ?", [5], function (es) {
}); });
db.transaction(function(tx) { db.transaction(function(tx) {
tx.executeSql("CREATE TABLE tex (t,e,x)"); tx.executeSql("CREATE TABLE tex (t,e,x)");
var i = tx.executeSql("INSERT INTO tex (t,e,x) VALUES (?,?,?)", var i = tx.executeSql("INSERT INTO tex (t,e,x) VALUES (?,?,?)",
...@@ -89,6 +108,11 @@ try { ...@@ -89,6 +108,11 @@ try {
} }
sys.puts("commits: " + commits);
sys.puts("rollbacks: " + rollbacks);
sys.puts("updates: " + updates);
db.close(); db.close();
sys.puts("OK\n"); sys.puts("OK\n");
......
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