Finish WebSQL implementation, all tests passing. Fixes #21.

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2013-11-29 10:29:04 -05:00
parent d5db4c51b9
commit 6ebbdc5915
5 changed files with 15 additions and 19 deletions

View File

@ -23,8 +23,7 @@ module.exports = function(grunt) {
'src/error.js', 'src/error.js',
'src/fs.js', 'src/fs.js',
'src/shared.js', 'src/shared.js',
'src/providers/**/*.js', 'src/providers/**/*.js'
'src/filesystems-manager.js'
] ]
}, },

View File

@ -7,7 +7,6 @@ define(function(require) {
window.webkitIndexedDB || window.webkitIndexedDB ||
window.msIndexedDB; window.msIndexedDB;
var IDB_RW = require('src/constants').IDB_RW; var IDB_RW = require('src/constants').IDB_RW;
var IDB_RO = require('src/constants').IDB_RO; var IDB_RO = require('src/constants').IDB_RO;

View File

@ -9,8 +9,8 @@ define(function(require) {
WebSQL: WebSQL, WebSQL: WebSQL,
Memory: Memory, Memory: Memory,
Default: IndexedDB, Default: IndexedDB,
// The Legacy provider does automatic fallback checks // The Fallback provider does automatic fallback checks
Legacy: (function() { Fallback: (function() {
if(IndexedDB.isSupported()) { if(IndexedDB.isSupported()) {
return IndexedDB; return IndexedDB;
} }

View File

@ -20,12 +20,12 @@ define(function(require) {
}; };
} }
WebSQLContext.prototype.clear = function(callback) { WebSQLContext.prototype.clear = function(callback) {
function onSuccess(transaction, result) {
callback(null);
}
function onError(transaction, error) { function onError(transaction, error) {
callback(error); callback(error);
} }
function onSuccess(transaction, result) {
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);
@ -33,18 +33,15 @@ define(function(require) {
}; };
WebSQLContext.prototype.get = function(key, callback) { WebSQLContext.prototype.get = function(key, callback) {
function onSuccess(transaction, result) { function onSuccess(transaction, result) {
if(result.rows.length === 0) { // If the key isn't found, return null
// Key not found, return null var value = result.rows.length === 0 ? null : result.rows.item(0).data;
callback(null, null); callback(null, value);
return;
}
callback(null, result.rows.item(0).data);
} }
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 = ? LIMIT 1", transaction.executeSql("SELECT data FROM " + FILE_STORE_NAME + " WHERE id = ?",
[key], onSuccess, onError); [key], onSuccess, onError);
}); });
}; };
@ -96,14 +93,15 @@ define(function(require) {
callback("[WebSQL] Unable to open database."); callback("[WebSQL] Unable to open database.");
return; return;
} }
that.db = db;
function onError(transaction, error) { function onError(transaction, error) {
callback(error); callback(error);
} }
function onSuccess(transaction, result) { function onSuccess(transaction, result) {
that.db = db;
function gotCount(transaction, result) { function gotCount(transaction, result) {
var firstAccess = result.rows.item(0).count > 0; var firstAccess = result.rows.item(0).count === 0;
callback(null, firstAccess); callback(null, firstAccess);
} }
function onError(transaction, error) { function onError(transaction, error) {

View File

@ -20,8 +20,8 @@ define(["IDBFS"], function(IDBFS) {
expect(typeof IDBFS.FileSystem.providers.Default).toEqual('function'); expect(typeof IDBFS.FileSystem.providers.Default).toEqual('function');
}); });
it("has Legacy constructor", function() { it("has Fallback constructor", function() {
expect(typeof IDBFS.FileSystem.providers.Legacy).toEqual('function'); expect(typeof IDBFS.FileSystem.providers.Fallback).toEqual('function');
}); });
}); });
}); });