Commit cf923fa7 authored by lucas.parsy's avatar lucas.parsy

corrected bugs in websqlstorage breaking rsvp queues.

added sinon spy tests in websqlstorage test file.
parent b864cc1c
...@@ -37,7 +37,10 @@ ...@@ -37,7 +37,10 @@
}); });
} }
function createDatabase(db) { function initDatabase(that) {
var db = that._database;
if (that._base_created === true) {return; }
that._base_created = true;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return sqlExec(db, "CREATE TABLE IF NOT EXISTS documents" + return sqlExec(db, "CREATE TABLE IF NOT EXISTS documents" +
...@@ -89,7 +92,7 @@ ...@@ -89,7 +92,7 @@
throw new TypeError("blob_len parameter must be a number >= 20"); throw new TypeError("blob_len parameter must be a number >= 20");
} }
this._blob_length = spec.blob_length || 2000000; this._blob_length = spec.blob_length || 2000000;
createDatabase(this._database); this._base_created = false;
} }
function addProperty(db, id, prop, value) { function addProperty(db, id, prop, value) {
...@@ -109,11 +112,15 @@ ...@@ -109,11 +112,15 @@
websqlStorage.prototype.put = function (id, param) { websqlStorage.prototype.put = function (id, param) {
var db = this._database, var db = this._database,
that = this,
dataString = JSON.stringify(param), dataString = JSON.stringify(param),
i, i,
arrayLen; arrayLen;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return initDatabase(that);
})
.push(function () { .push(function () {
return sqlExec(db, "INSERT OR REPLACE INTO " + return sqlExec(db, "INSERT OR REPLACE INTO " +
"documents(id, data) VALUES(?,?)", "documents(id, data) VALUES(?,?)",
...@@ -139,9 +146,13 @@ ...@@ -139,9 +146,13 @@
}; };
websqlStorage.prototype.remove = function (id) { websqlStorage.prototype.remove = function (id) {
var db = this._database; var db = this._database,
that = this;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return initDatabase(that);
})
.push(function () { .push(function () {
return sqlExec(db, "DELETE FROM documents WHERE id = ?", [id]); return sqlExec(db, "DELETE FROM documents WHERE id = ?", [id]);
}) })
...@@ -155,9 +166,13 @@ ...@@ -155,9 +166,13 @@
}; };
websqlStorage.prototype.get = function (id) { websqlStorage.prototype.get = function (id) {
var db = this._database; var db = this._database,
that = this;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return initDatabase(that);
})
.push(function () { .push(function () {
return sqlExec(db, "SELECT data FROM documents WHERE id = ?", [id]); return sqlExec(db, "SELECT data FROM documents WHERE id = ?", [id]);
}) })
...@@ -170,9 +185,13 @@ ...@@ -170,9 +185,13 @@
}; };
websqlStorage.prototype.allAttachments = function (id) { websqlStorage.prototype.allAttachments = function (id) {
var db = this._database; var db = this._database,
that = this;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return initDatabase(that);
})
.push(function () { .push(function () {
return sqlExec(db, "SELECT COUNT(*) FROM documents WHERE id = ?", [id]); return sqlExec(db, "SELECT COUNT(*) FROM documents WHERE id = ?", [id]);
}) })
...@@ -195,11 +214,10 @@ ...@@ -195,11 +214,10 @@
}); });
}; };
function sendBlobPart(db, id, name, blob, nbSlice) { function sendBlobPart(db, id, name, blob, nbSlice, queue) {
return new RSVP.Queue() queue.push(function () {
.push(function () { return jIO.util.readBlobAsDataURL(blob);
return jIO.util.readBlobAsDataURL(blob); })
})
.push(function (strBlob) { .push(function (strBlob) {
strBlob = strBlob.currentTarget.result; strBlob = strBlob.currentTarget.result;
return sqlExec(db, "INSERT INTO blob(id, attachment, part, blob)" + return sqlExec(db, "INSERT INTO blob(id, attachment, part, blob)" +
...@@ -209,9 +227,13 @@ ...@@ -209,9 +227,13 @@
websqlStorage.prototype.putAttachment = function (id, name, blob) { websqlStorage.prototype.putAttachment = function (id, name, blob) {
var db = this._database, var db = this._database,
that = this,
partSize = this._blob_length; partSize = this._blob_length;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return initDatabase(that);
})
.push(function () { .push(function () {
return sqlExec(db, "SELECT COUNT(*) FROM documents WHERE id = ?", [id]); return sqlExec(db, "SELECT COUNT(*) FROM documents WHERE id = ?", [id]);
}) })
...@@ -233,17 +255,20 @@ ...@@ -233,17 +255,20 @@
}) })
.push(function () { .push(function () {
var blobSize = blob.size, var blobSize = blob.size,
queue = new RSVP.Queue(),
i, i,
index; index;
for (i = 0, index = 0; i < blobSize; i += partSize, index += 1) { for (i = 0, index = 0; i < blobSize; i += partSize, index += 1) {
sendBlobPart(db, id, name, blob.slice(i, i + partSize), index); sendBlobPart(db, id, name, blob.slice(i, i + partSize), index, queue);
} }
return queue;
}); });
}; };
websqlStorage.prototype.getAttachment = function (id, name, options) { websqlStorage.prototype.getAttachment = function (id, name, options) {
var db = this._database, var db = this._database,
that = this,
partSize = this._blob_length, partSize = this._blob_length,
start, start,
end, end,
...@@ -271,6 +296,9 @@ ...@@ -271,6 +296,9 @@
} }
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return initDatabase(that);
})
.push(function () { .push(function () {
var command = "SELECT part, blob FROM blob WHERE id = ? AND " + var command = "SELECT part, blob FROM blob WHERE id = ? AND " +
"attachment = ? AND part >= ?", "attachment = ? AND part >= ?",
...@@ -312,9 +340,12 @@ ...@@ -312,9 +340,12 @@
}; };
websqlStorage.prototype.removeAttachment = function (id, name) { websqlStorage.prototype.removeAttachment = function (id, name) {
var db = this._database; var db = this._database,
that = this;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return initDatabase(that);
})
.push(function () { .push(function () {
return sqlExec(db, "DELETE FROM attachment WHERE " + return sqlExec(db, "DELETE FROM attachment WHERE " +
"id = ? AND attachment = ?", [id, name]); "id = ? AND attachment = ?", [id, name]);
...@@ -333,6 +364,7 @@ ...@@ -333,6 +364,7 @@
websqlStorage.prototype.buildQuery = function (options) { websqlStorage.prototype.buildQuery = function (options) {
var db = this._database, var db = this._database,
that = this,
query = "SELECT id"; query = "SELECT id";
if (options === undefined) { options = {}; } if (options === undefined) { options = {}; }
...@@ -342,6 +374,9 @@ ...@@ -342,6 +374,9 @@
query += " FROM documents ORDER BY id"; query += " FROM documents ORDER BY id";
return new RSVP.Queue() return new RSVP.Queue()
.push(function () {
return initDatabase(that);
})
.push(function () { .push(function () {
return sqlExec(db, query, []); return sqlExec(db, query, []);
}) })
......
This diff is collapsed.
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