Permalink
Browse files

Improved semantics a bit

  • Loading branch information...
1 parent e2cdbf1 commit 1c3e2b7244cc08488e0d55bfd966746c42293fa1 @grumdrig committed with Eric Fredricksen Dec 8, 2009
Showing with 51 additions and 53 deletions.
  1. +14 −11 README
  2. +2 −1 compiloop.py
  3. +21 −7 sqlite.js
  4. +2 −0 sqlite3_bindings.cc
  5. +12 −34 test.js
View
25 README
@@ -6,7 +6,8 @@ Functions
### `sqlite.openDatabaseSync(filename)`
-Returns an object representing the sqlite3 database with given filename.
+Returns a `DatabaseSync` object representing the sqlite3 database with
+given filename.
### `DatabaseSync.query(sql [,bindings] [,callback])`
@@ -16,20 +17,22 @@ case `bindings` should be an array of values, or the form `$VVV` where
`VVV` is an identifier, in which canse `bindings` should be an object
with keys matching the variable names.
-If provided the `callback` is called with an argument for each
-statement in the query. Each argument is an array of objects mapping
-column names to values.
+If provided the `callback` is called with a number of arguments equal
+to the number of statements in the query. Each argument is a result
+set which is an array of objects mapping column names to values.
-Each callback argument `rows` also has these properties
+Each result set `r` also has these accessors:
-- **`rows.count`** is the number of rows affected by the query.
-- **`rows.rowid`** is the `ROWID` of the last `INSERT` command
+- **`r.rowsAffected`** is the number of rows affected by an `UPDATE` query.
+- **`r.insertId`** is the `ROWID` of the an `INSERT` query
+- **`r.rows.length`** (also just `r.length`) is the number of rows in a
+ `SELECT` result
+- **`r.all`** is an array of result sets, one for each statement in the
+ query
-Within the callback, `this` is an array of all such arrays, with a
-`count` property giving the total number of rows affected. That same
-`this` object is returned by `query`.
+The return value is the first (often only) result set.
-### `sqlite.Db.close()`
+### `DatabaseSync.close()`
Closes the database.
View
@@ -22,7 +22,8 @@ def handler():
m = os.stat(mdname).st_mtime
if mdtime != m:
- os.system("Markdown.pl < %s > %s.html" % (mdname, mdname))
+ os.system("Markdown.pl -v < %s > %s.html" % (mdname, mdname))
+ print mdname + ".html updated"
mdtime = m
time.sleep(1)
View
@@ -33,17 +33,27 @@ exports.openDatabaseSync = function (name, version, displayName,
}
-// This is an extension of the HTML5 API
DatabaseSync.prototype.query = function (sql, bindings, callback) {
// TODO: error callback
if (typeof(bindings) == "function") {
var tmp = bindings;
bindings = callback;
callback = tmp;
}
- var result = this.performQuery(sql, bindings);
+ var all = this.performQuery(sql, bindings);
+ if (all.length == 0) {
+ var result = None;
+ } else {
+ for (var i = 0; i < all.length; ++i) {
+ var resultset = all[i];
+ resultset.all = all;
+ resultset.rows = {item: function (index) { return resultset[index]; },
+ length: resultset.length};
+ }
+ var result = all[0];
+ }
if (typeof(callback) == "function") {
- callback.apply(result, result);
+ callback.apply(result, all);
}
return result;
}
@@ -56,10 +66,13 @@ DatabaseSync.prototype.query = function (sql, bindings, callback) {
function SQLTransactionSync(db, txCallback, errCallback, successCallback) {
this.database = db;
+
this.executeSql = function(sqlStatement, arguments, callback) {
- var result = db.query(sqlStatement, arguments, callback)[0];
- result.rows = {item: function (index) { return result[index]; },
- length: result.length};
+ var result = db.query(sqlStatement, arguments);
+ if (callback) {
+ var tx = this;
+ callback.apply(result, [tx].concat(result.all));
+ }
return result;
}
@@ -72,7 +85,8 @@ function SQLTransactionSync(db, txCallback, errCallback, successCallback) {
DatabaseSync.prototype.transaction = function (txCallback, errCallback,
successCallback) {
- var tx = new SQLTransactionSync(this, txCallback, errCallback, successCallback);
+ var tx = new SQLTransactionSync(this, txCallback,
+ errCallback, successCallback);
}
// TODO: readTransaction()
View
@@ -163,6 +163,8 @@ class Sqlite3Db : public EventEmitter
rosult->Set(String::New("insertId"),
Integer::New(sqlite3_last_insert_rowid(*db)));
result->Set(Integer::New(result->Length()), rosult);
+
+ sqlite3_finalize(stmt);
}
result->Set(String::New("rowsAffected"), Integer::New(changes));
View
46 test.js
@@ -39,9 +39,10 @@ db.query("SELECT a FROM egg; SELECT y FROM egg", function (as, ys) {
process.assert(ys.length == 4);
});
-db.query("SELECT e FROM egg WHERE a = ?", [5], function (rows) {
- process.assert(rows.length == 1);
- process.assert(rows[0].e == "E");
+db.query("SELECT e FROM egg WHERE a = ?", [5], function (es) {
+ asserteq(es.length, 1);
+ asserteq(es[0].e, es.rows.item(0).e);
+ asserteq(es[0].e, "E");
});
@@ -64,46 +65,23 @@ db.query("CREATE TABLE test (x,y,z)", function () {
db.query("INSERT INTO test (x,y,z) VALUES ($x, $y, $z)", {$x:1, $y:2, $z:3});
});
-db.query("SELECT * FROM test WHERE rowid < ?;", [1],
- function (stmt) {
- sys.puts("rose:");
- sys.puts(stmt);
- });
+db.query("SELECT * FROM test WHERE rowid < ?;", [1]);
db.query("UPDATE test SET y = 10;", [], function () {
sys.puts(this.rowsAffected + " rows affected");
process.assert(this.rowsAffected == 2);
});
-
+db.transaction(function(tx) {
+ tx.executeSql("SELECT * FROM test WHERE x = ?", [1], function (tx,records) {
+ for (var i = 0; records.rows.item(i); ++i)
+ asserteq(records.rows.item(i).z, 3);
+ });
+});
+
db.close();
sys.puts("OK\n");
-process.exit()
// Perhaps do this, one day
//var q = db.prepare("SELECT * FROM t WHERE rowid=?");
//var rows = q.execute([1]);
-
-// Perhaps use this syntax if query starts returning a promise:
-
-c.query("select * from test;").addCallback(function (rows) {
- puts("result1:");
- p(rows);
-});
-
-c.query("select * from test limit 1;").addCallback(function (rows) {
- puts("result2:");
- p(rows);
-});
-
-c.query("select ____ from test limit 1;").addCallback(function (rows) {
- puts("result3:");
- p(rows);
-}).addErrback(function (e) {
- puts("error! "+ e.message);
- puts("full: "+ e.full);
- puts("severity: "+ e.severity);
- c.close();
-});
-
-

0 comments on commit 1c3e2b7

Please sign in to comment.