Commit 1c3e2b72 by Eric Fredricksen Committed by Eric Fredricksen

Improved semantics a bit

parent e2cdbf1e
...@@ -6,7 +6,8 @@ Functions ...@@ -6,7 +6,8 @@ Functions
### `sqlite.openDatabaseSync(filename)` ### `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])` ### `DatabaseSync.query(sql [,bindings] [,callback])`
...@@ -16,20 +17,22 @@ case `bindings` should be an array of values, or the form `$VVV` where ...@@ -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 `VVV` is an identifier, in which canse `bindings` should be an object
with keys matching the variable names. with keys matching the variable names.
If provided the `callback` is called with an argument for each If provided the `callback` is called with a number of arguments equal
statement in the query. Each argument is an array of objects mapping to the number of statements in the query. Each argument is a result
column names to values. 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. - **`r.rowsAffected`** is the number of rows affected by an `UPDATE` query.
- **`rows.rowid`** is the `ROWID` of the last `INSERT` command - **`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 The return value is the first (often only) result set.
`count` property giving the total number of rows affected. That same
`this` object is returned by `query`.
### `sqlite.Db.close()` ### `DatabaseSync.close()`
Closes the database. Closes the database.
......
...@@ -22,7 +22,8 @@ while True: ...@@ -22,7 +22,8 @@ while True:
m = os.stat(mdname).st_mtime m = os.stat(mdname).st_mtime
if mdtime != m: 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 mdtime = m
time.sleep(1) time.sleep(1)
......
...@@ -33,7 +33,6 @@ exports.openDatabaseSync = function (name, version, displayName, ...@@ -33,7 +33,6 @@ exports.openDatabaseSync = function (name, version, displayName,
} }
// This is an extension of the HTML5 API
DatabaseSync.prototype.query = function (sql, bindings, callback) { DatabaseSync.prototype.query = function (sql, bindings, callback) {
// TODO: error callback // TODO: error callback
if (typeof(bindings) == "function") { if (typeof(bindings) == "function") {
...@@ -41,9 +40,20 @@ DatabaseSync.prototype.query = function (sql, bindings, callback) { ...@@ -41,9 +40,20 @@ DatabaseSync.prototype.query = function (sql, bindings, callback) {
bindings = callback; bindings = callback;
callback = tmp; 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") { if (typeof(callback) == "function") {
callback.apply(result, result); callback.apply(result, all);
} }
return result; return result;
} }
...@@ -56,10 +66,13 @@ DatabaseSync.prototype.query = function (sql, bindings, callback) { ...@@ -56,10 +66,13 @@ DatabaseSync.prototype.query = function (sql, bindings, callback) {
function SQLTransactionSync(db, txCallback, errCallback, successCallback) { function SQLTransactionSync(db, txCallback, errCallback, successCallback) {
this.database = db; this.database = db;
this.executeSql = function(sqlStatement, arguments, callback) { this.executeSql = function(sqlStatement, arguments, callback) {
var result = db.query(sqlStatement, arguments, callback)[0]; var result = db.query(sqlStatement, arguments);
result.rows = {item: function (index) { return result[index]; }, if (callback) {
length: result.length}; var tx = this;
callback.apply(result, [tx].concat(result.all));
}
return result; return result;
} }
...@@ -72,7 +85,8 @@ function SQLTransactionSync(db, txCallback, errCallback, successCallback) { ...@@ -72,7 +85,8 @@ function SQLTransactionSync(db, txCallback, errCallback, successCallback) {
DatabaseSync.prototype.transaction = function (txCallback, errCallback, DatabaseSync.prototype.transaction = function (txCallback, errCallback,
successCallback) { successCallback) {
var tx = new SQLTransactionSync(this, txCallback, errCallback, successCallback); var tx = new SQLTransactionSync(this, txCallback,
errCallback, successCallback);
} }
// TODO: readTransaction() // TODO: readTransaction()
......
...@@ -163,6 +163,8 @@ protected: ...@@ -163,6 +163,8 @@ protected:
rosult->Set(String::New("insertId"), rosult->Set(String::New("insertId"),
Integer::New(sqlite3_last_insert_rowid(*db))); Integer::New(sqlite3_last_insert_rowid(*db)));
result->Set(Integer::New(result->Length()), rosult); result->Set(Integer::New(result->Length()), rosult);
sqlite3_finalize(stmt);
} }
result->Set(String::New("rowsAffected"), Integer::New(changes)); result->Set(String::New("rowsAffected"), Integer::New(changes));
......
...@@ -39,9 +39,10 @@ db.query("SELECT a FROM egg; SELECT y FROM egg", function (as, ys) { ...@@ -39,9 +39,10 @@ db.query("SELECT a FROM egg; SELECT y FROM egg", function (as, ys) {
process.assert(ys.length == 4); process.assert(ys.length == 4);
}); });
db.query("SELECT e FROM egg WHERE a = ?", [5], function (rows) { db.query("SELECT e FROM egg WHERE a = ?", [5], function (es) {
process.assert(rows.length == 1); asserteq(es.length, 1);
process.assert(rows[0].e == "E"); 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 () { ...@@ -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("INSERT INTO test (x,y,z) VALUES ($x, $y, $z)", {$x:1, $y:2, $z:3});
}); });
db.query("SELECT * FROM test WHERE rowid < ?;", [1], db.query("SELECT * FROM test WHERE rowid < ?;", [1]);
function (stmt) {
sys.puts("rose:");
sys.puts(stmt);
});
db.query("UPDATE test SET y = 10;", [], function () { db.query("UPDATE test SET y = 10;", [], function () {
sys.puts(this.rowsAffected + " rows affected"); sys.puts(this.rowsAffected + " rows affected");
process.assert(this.rowsAffected == 2); 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(); db.close();
sys.puts("OK\n"); sys.puts("OK\n");
process.exit()
// Perhaps do this, one day // Perhaps do this, one day
//var q = db.prepare("SELECT * FROM t WHERE rowid=?"); //var q = db.prepare("SELECT * FROM t WHERE rowid=?");
//var rows = q.execute([1]); //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();
});
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