From fda1b7b91347b59a4364d16dac9680370796a175 Mon Sep 17 00:00:00 2001 From: Erik Brommers <1458944+eb1@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:57:16 -0700 Subject: [PATCH] More under-the-hood db changes Start on schema update change (v5), to include the 2 new tables (user and bookmark). Still need to include 1 user entry (current user) and 1 bookmark entry per project. --- www/js/models/json/project.js | 19 ------ www/js/models/json/user.js | 8 +-- www/js/models/sql/project.js | 36 ++++++++++- www/js/models/sql/user.js | 115 ++++++++++++++++++++++++++++++++-- 4 files changed, 148 insertions(+), 30 deletions(-) diff --git a/www/js/models/json/project.js b/www/js/models/json/project.js index fddaf535..de4c265c 100755 --- a/www/js/models/json/project.js +++ b/www/js/models/json/project.js @@ -257,16 +257,6 @@ define(function (require) { return "http://localhost:3042/projects"; }, - // parse: function (data) { - // if (_.isObject(data.results)) { - // console.log("Parsing project data results: " + data.results); - // return data.results; - // } else { - // console.log("Parsing project data: " + data); - // return data; - // } - // }, - fetch: function(options) { console.log("Fetching project"); @@ -275,15 +265,6 @@ define(function (require) { return Backbone.Collection.prototype.fetch.call(this, options); } - // sync: function (method, model, options) { - // if (method === "read") { - // // special case for sql - remove the blank name property for json - // if (options.data.hasOwnProperty('name') && options.data.name === "") { - // delete options.data.name; - // } - // } - // } - }); return { diff --git a/www/js/models/json/user.js b/www/js/models/json/user.js index f009e50b..7a8741f8 100644 --- a/www/js/models/json/user.js +++ b/www/js/models/json/user.js @@ -40,7 +40,7 @@ define(function (require) { UserCollection = Backbone.Collection.extend({ model: User, url: "/users" - }); + }), // Represents a placeholder in a project (book, chapter, and source phrase location). Can be more than 1 per user. Bookmark = Backbone.Model.extend({ @@ -52,13 +52,13 @@ define(function (require) { bookid: 0, chapterid: 0, spid: "" - }, - }); + } + }), BookmarkCollection = Backbone.Collection.extend({ model: Bookmark, url: "/bookmarks" - }) + }); return { User: User, diff --git a/www/js/models/sql/project.js b/www/js/models/sql/project.js index cd97f46d..2d693915 100755 --- a/www/js/models/sql/project.js +++ b/www/js/models/sql/project.js @@ -8,7 +8,7 @@ define(function (require) { Backbone = require('backbone'), i18n = require('i18n'), projects = [], - CURRSCHEMA = 4, + CURRSCHEMA = 5, // --- // STATIC METHODS @@ -344,6 +344,39 @@ define(function (require) { deferred.resolve(); }); } + if (fromVersion === 4) { + // AIM version 1.18 (user, bookmark tables) + window.Application.db.transaction(function (tx) { + var theSQL = ""; + // version table exists (see logic above), but is at version 4; update it here + tx.executeSql('UPDATE version SET schemaver=? WHERE id=?;', [CURRSCHEMA, 1], function (tx, res) { + console.log("version table updated -- schema version: " + CURRSCHEMA); + }, function (err) { + console.log("failed to set the version schema: " + err); + }); + // update changes for 1.18 + // 2 new tables -- user and bookmark + theSQL = 'CREATE TABLE user (id integer primary key, username text, userid text, roles text, CopySource integer, WrapUSFM integer, StopAtBoundaries integer, AllowEditBlankSP integer, ShowTranslationChecks integer, DefaultFTTarget integer, UILang integer, DarkMode integer, bookmarks Text, WordSpacing integer);'; + tx.executeSql(theSQL, [], function (tx, res) { + console.log("UpgradeSchema() - user table created"); + }, function (err) { + // exception thrown -- assume table doesn't exist + console.log("upgradeSchema: error: " + err.message); + }); + theSQL = 'CREATE TABLE IF NOT EXISTS bookmark (id integer primary key, bookmarkid text, projectid text, bookname text, bookid integer, chapterid integer, spid text);'; + tx.executeSql(theSQL, [], function (tx, res) { + console.log("UpgradeSchema() - bookmark table created"); + }, function (err) { + // exception thrown -- assume table doesn't exist + console.log("upgradeSchema: error: " + err.message); + }); + + }, function (e) { + console.log("upgradeSchema error: " + e.message); + }, function () { + deferred.resolve(); + }); + } return deferred.promise(); }, // checkSchema @@ -950,7 +983,6 @@ define(function (require) { break; } } - }), ProjectCollection = Backbone.Collection.extend({ diff --git a/www/js/models/sql/user.js b/www/js/models/sql/user.js index 519d523b..9ca3e003 100644 --- a/www/js/models/sql/user.js +++ b/www/js/models/sql/user.js @@ -12,6 +12,7 @@ define(function (require) { var $ = require('jquery'), Backbone = require('backbone'), users = [], + bookmarks = [], wordSpacingEnum = { NONE: 0, SMALL: 1, @@ -37,7 +38,16 @@ define(function (require) { deferred.resolve(results); return deferred.promise(); }, - + + findByBookmarkId = function (searchKey) { + var deferred = $.Deferred(); + var results = bookmarks.filter(function (element) { + return element.attributes.bookmarkid === searchKey; + }); + deferred.resolve(results); + return deferred.promise(); + }, + User = Backbone.Model.extend({ defaults: { username: "", @@ -118,7 +128,7 @@ define(function (require) { break; } } - }); + }), UserCollection = Backbone.Collection.extend({ model: User, @@ -229,7 +239,7 @@ define(function (require) { } } } - }); + }), // Represents a placeholder in a project (book, chapter, and source phrase location). Can be more than 1 per user. Bookmark = Backbone.Model.extend({ @@ -304,11 +314,106 @@ define(function (require) { break; } } - }); + }), BookmarkCollection = Backbone.Collection.extend({ model: Bookmark, - url: "/bookmarks" + + resetFromDB: function () { + var deferred = $.Deferred(), + i = 0, + len = 0; + + window.Application.db.transaction(function (tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS bookmark (id integer primary key, bookmarkid text, projectid text, bookname text, bookid integer, chapterid integer, spid text);'); + tx.executeSql("SELECT * from bookmark;", [], function (tx, res) { + for (i = 0, len = res.rows.length; i < len; ++i) { + // add the bookmark + var bookmark = new Bookmark(); + bookmark.off("change"); + bookmark.set(res.rows.item(i)); + bookmarks.push(bookmark); + bookmark.on("change", bookmark.save, bookmark); + } + console.log("SELECT ok: " + res.rows.length + " bookmark items"); + }); + }, function (e) { + deferred.reject(e); + }, function () { + deferred.resolve(); + }); + return deferred.promise(); + }, + + initialize: function () { + return this.resetFromDB(); + }, + + // Removes all bookmarks from the collection (and database) + clearAll: function () { + window.Application.db.transaction(function (tx) { + tx.executeSql('DELETE from bookmark;'); // clear out the table + bookmarks.length = 0; // delete local copy + }, function (err) { + console.log("DELETE error: " + err.message); + }); + }, + + sync: function (method, model, options) { + if (method === "read") { + if (options.data.hasOwnProperty('bookmarkid')) { + findByBookmarkId(options.data.bookmarkid).done(function (data) { + options.success(data); + }); + } else if (options.data.hasOwnProperty('projectid')) { + var deferred = $.Deferred(); + var username = options.data.projectid; + var len = 0; + var i = 0; + var retValue = null; + // special case -- empty name query ==> reset local copy so we force a retrieve + // from the database + if (projectid === "") { + bookmarks.length = 0; + } + var results = users.filter(function (element) { + return element.attributes.projectid === projectid; + }); + if (results.length === 0) { + // not in collection -- retrieve them from the db + window.Application.db.transaction(function (tx) { + tx.executeSql("SELECT * FROM bookmark;", [], function (tx, res) { + var tmpString = ""; + // populate the chapter collection with the query results + for (i = 0, len = res.rows.length; i < len; ++i) { + // add the book + var bookmark = new Bookmark(); + bookmark.off("change"); + bookmark.set(res.rows.item(i)); + bookmarks.push(bookmark); + bookmark.on("change", bookmark.save, bookmark); + } + // return the filtered results (now that we have them) + retValue = users.filter(function (element) { + return element.attributes.projectid === projectid; + }); + options.success(retValue); + deferred.resolve(retValue); + }); + }, function (e) { + options.error(); + deferred.reject(e); + }); + } else { + // results already in collection -- return them + options.success(results); + deferred.resolve(results); + } + // return the promise + return deferred.promise(); + } + } + } });