Merge pull request #130 from humphd/websql-fixes

Fix WebSQL provider to properly deal with typed arrays, 100% of tests passing now on WebKit with WebSQL
This commit is contained in:
Alan K 2014-03-04 14:34:22 -05:00
commit 503a2d2aca
2 changed files with 47 additions and 7 deletions

View File

@ -4,6 +4,7 @@ define(function(require) {
var WSQL_VERSION = require('src/constants').WSQL_VERSION; var WSQL_VERSION = require('src/constants').WSQL_VERSION;
var WSQL_SIZE = require('src/constants').WSQL_SIZE; var WSQL_SIZE = require('src/constants').WSQL_SIZE;
var WSQL_DESC = require('src/constants').WSQL_DESC; var WSQL_DESC = require('src/constants').WSQL_DESC;
var u8toArray = require('src/shared').u8toArray;
function WebSQLContext(db, isReadOnly) { function WebSQLContext(db, isReadOnly) {
var that = this; var that = this;
@ -27,7 +28,7 @@ define(function(require) {
callback(null); callback(null);
} }
this.getTransaction(function(transaction) { this.getTransaction(function(transaction) {
transaction.executeSql("DELETE FROM " + FILE_STORE_NAME, transaction.executeSql("DELETE FROM " + FILE_STORE_NAME + ";",
[], onSuccess, onError); [], onSuccess, onError);
}); });
}; };
@ -35,17 +36,37 @@ define(function(require) {
function onSuccess(transaction, result) { function onSuccess(transaction, result) {
// If the key isn't found, return null // If the key isn't found, return null
var value = result.rows.length === 0 ? null : result.rows.item(0).data; var value = result.rows.length === 0 ? null : result.rows.item(0).data;
try {
if(value) {
value = JSON.parse(value);
// Deal with special-cased flattened typed arrays in WebSQL (see put() below)
if(value.__isUint8Array) {
value = new Uint8Array(value.__array);
}
}
callback(null, value); callback(null, value);
} catch(e) {
callback(e);
}
} }
function onError(transaction, error) { function onError(transaction, error) {
callback(error); callback(error);
} }
this.getTransaction(function(transaction) { this.getTransaction(function(transaction) {
transaction.executeSql("SELECT data FROM " + FILE_STORE_NAME + " WHERE id = ?", transaction.executeSql("SELECT data FROM " + FILE_STORE_NAME + " WHERE id = ?;",
[key], onSuccess, onError); [key], onSuccess, onError);
}); });
}; };
WebSQLContext.prototype.put = function(key, value, callback) { WebSQLContext.prototype.put = function(key, value, callback) {
// We do extra work to make sure typed arrays survive
// being stored in the db and still get the right prototype later.
if(Object.prototype.toString.call(value) === "[object Uint8Array]") {
value = {
__isUint8Array: true,
__array: u8toArray(value)
};
}
value = JSON.stringify(value);
function onSuccess(transaction, result) { function onSuccess(transaction, result) {
callback(null); callback(null);
} }
@ -53,7 +74,7 @@ define(function(require) {
callback(error); callback(error);
} }
this.getTransaction(function(transaction) { this.getTransaction(function(transaction) {
transaction.executeSql("INSERT OR REPLACE INTO " + FILE_STORE_NAME + " (id, data) VALUES (?, ?)", transaction.executeSql("INSERT OR REPLACE INTO " + FILE_STORE_NAME + " (id, data) VALUES (?, ?);",
[key, value], onSuccess, onError); [key, value], onSuccess, onError);
}); });
}; };
@ -65,7 +86,7 @@ define(function(require) {
callback(error); callback(error);
} }
this.getTransaction(function(transaction) { this.getTransaction(function(transaction) {
transaction.executeSql("DELETE FROM " + FILE_STORE_NAME + " WHERE id = ?", transaction.executeSql("DELETE FROM " + FILE_STORE_NAME + " WHERE id = ?;",
[key], onSuccess, onError); [key], onSuccess, onError);
}); });
}; };
@ -113,9 +134,15 @@ define(function(require) {
[], gotCount, onError); [], gotCount, onError);
} }
// Create the table and index we'll need to store the fs data.
db.transaction(function(transaction) { db.transaction(function(transaction) {
transaction.executeSql("CREATE TABLE IF NOT EXISTS " + FILE_STORE_NAME + " (id unique, data)", function createIndex(transaction) {
transaction.executeSql("CREATE INDEX IF NOT EXISTS idx_" + FILE_STORE_NAME + "_id" +
" on " + FILE_STORE_NAME + " (id);",
[], onSuccess, onError); [], onSuccess, onError);
}
transaction.executeSql("CREATE TABLE IF NOT EXISTS " + FILE_STORE_NAME + " (id unique, data TEXT);",
[], createIndex, onError);
}); });
}; };
WebSQL.prototype.getReadOnlyContext = function() { WebSQL.prototype.getReadOnlyContext = function() {

View File

@ -15,9 +15,22 @@ define(function(require) {
function nop() {} function nop() {}
/**
* Convert a Uint8Array to a regular array
*/
function u8toArray(u8) {
var array = [];
var len = u8.length;
for(var i = 0; i < len; i++) {
array[i] = u8[i];
}
return array;
}
return { return {
guid: guid, guid: guid,
hash: hash, hash: hash,
u8toArray: u8toArray,
nop: nop nop: nop
}; };