Return a Q promise from all async DB methods

With send() removed from Q, we can yield Q promises within
executeTransaction() instead of Mozilla promises.
This commit is contained in:
Dan Stillman 2013-03-28 19:20:34 -04:00
parent 957b649fa8
commit a2816a10ca

View file

@ -762,44 +762,44 @@ Zotero.DBConnection.prototype.getNextName = function (table, field, name)
// Async methods // Async methods
// //
// //
// Zotero.DB.executeTransaction(function (conn) { // Zotero.DB.executeTransaction(function (conn) {
// var created = yield Zotero.DB.queryAsync("CREATE TEMPORARY TABLE tmpFoo (foo TEXT, bar INT)"); // var created = yield Zotero.DB.queryAsync("CREATE TEMPORARY TABLE tmpFoo (foo TEXT, bar INT)");
// //
// // created == true // // created == true
// //
// var result = yield Zotero.DB.queryAsync("INSERT INTO tmpFoo VALUES ('a', ?)", 1); // var result = yield Zotero.DB.queryAsync("INSERT INTO tmpFoo VALUES ('a', ?)", 1);
// //
// // result == 1 // // result == 1
// //
// yield Zotero.DB.queryAsync("INSERT INTO tmpFoo VALUES ('b', 2)"); // yield Zotero.DB.queryAsync("INSERT INTO tmpFoo VALUES ('b', 2)");
// yield Zotero.DB.queryAsync("INSERT INTO tmpFoo VALUES ('c', 3)"); // yield Zotero.DB.queryAsync("INSERT INTO tmpFoo VALUES ('c', 3)");
// yield Zotero.DB.queryAsync("INSERT INTO tmpFoo VALUES ('d', 4)"); // yield Zotero.DB.queryAsync("INSERT INTO tmpFoo VALUES ('d', 4)");
// //
// var value = yield Zotero.DB.valueQueryAsync("SELECT foo FROM tmpFoo WHERE bar=?", 2); // var value = yield Zotero.DB.valueQueryAsync("SELECT foo FROM tmpFoo WHERE bar=?", 2);
// //
// // value == "b" // // value == "b"
// //
// var vals = yield Zotero.DB.columnQueryAsync("SELECT foo FROM tmpFoo"); // var vals = yield Zotero.DB.columnQueryAsync("SELECT foo FROM tmpFoo");
// //
// // '0' => "a" // // '0' => "a"
// // '1' => "b" // // '1' => "b"
// // '2' => "c" // // '2' => "c"
// // '3' => "d" // // '3' => "d"
// //
// let rows = yield Zotero.DB.queryAsync("SELECT * FROM tmpFoo"); // let rows = yield Zotero.DB.queryAsync("SELECT * FROM tmpFoo");
// for each(let row in rows) { // for each(let row in rows) {
// // row.foo == 'a', row.bar == 1 // // row.foo == 'a', row.bar == 1
// // row.foo == 'b', row.bar == 2 // // row.foo == 'b', row.bar == 2
// // row.foo == 'c', row.bar == 3 // // row.foo == 'c', row.bar == 3
// // row.foo == 'd', row.bar == 4 // // row.foo == 'd', row.bar == 4
// } // }
// //
// // Optional, but necessary to pass 'rows' on to the next handler // // Optional, but necessary to pass 'rows' on to the next handler
// Zotero.DB.asyncResult(rows); // Zotero.DB.asyncResult(rows);
// ) // })
// then(function (rows) { // .then(function (rows) {
// // rows == same as above // // rows == same as above
// ) // })
// .done(); // .done();
// //
/** /**
@ -809,12 +809,10 @@ Zotero.DBConnection.prototype.getNextName = function (table, field, name)
* pass a result by calling asyncResult(val) at the end * pass a result by calling asyncResult(val) at the end
*/ */
Zotero.DBConnection.prototype.executeTransaction = function (func) { Zotero.DBConnection.prototype.executeTransaction = function (func) {
return Q( return this._getConnectionAsync()
this._getConnectionAsync() .then(function (conn) {
.then(function (conn) { return conn.executeTransaction(func);
return conn.executeTransaction(func); });
})
);
}; };
@ -952,7 +950,7 @@ Zotero.DBConnection.prototype.asyncResult = function (val) {
*/ */
Zotero.DBConnection.prototype._getConnectionAsync = function () { Zotero.DBConnection.prototype._getConnectionAsync = function () {
if (this._connectionAsync) { if (this._connectionAsync) {
return this.Promise.resolve(this._connectionAsync); return Q(this._connectionAsync);
} }
var db = this._getDBConnection(); var db = this._getDBConnection();
@ -961,11 +959,11 @@ Zotero.DBConnection.prototype._getConnectionAsync = function () {
}; };
var self = this; var self = this;
Zotero.debug("Asynchronously opening DB connection"); Zotero.debug("Asynchronously opening DB connection");
return this.Sqlite.openConnection(options) return Q(this.Sqlite.openConnection(options)
.then(function(conn) { .then(function(conn) {
self._connectionAsync = conn; self._connectionAsync = conn;
return conn; return conn;
}); }));
}; };