From 49b0f28f2680a32164a8f87427846f286e532661 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 2 Feb 2007 05:43:44 +0000 Subject: [PATCH] Abstracted the DB layer for use by Zotero utilities All the former Zotero.DB methods are now part of an instantiable Zotero.DBConnection object, and Zotero.DB is just one instance of it. Utilities can create and access a new SQLite database within the Zotero data folder by instantiating the DBConnection object: this.DB = new Zotero.DBConnection('myutility'); Utilities have access to everything the DB layer provides, including automatic backup and restore of databases. Utility writers are on their own for schema management, at least for now. Also: - Cleared non-English DB restore localized strings after change. - Disabled shutdown observer in Zotero object after moving DB backup code to DB layer --- chrome/content/zotero/xpcom/db.js | 1239 ++++++++++-------- chrome/content/zotero/xpcom/zotero.js | 77 +- chrome/locale/de-AT/zotero/zotero.properties | 4 - chrome/locale/de-CH/zotero/zotero.properties | 4 - chrome/locale/de-DE/zotero/zotero.properties | 4 - chrome/locale/en-US/zotero/zotero.properties | 6 +- chrome/locale/fr-FR/zotero/zotero.properties | 4 - chrome/locale/it-IT/zotero/zotero.properties | 4 - chrome/locale/ja-JP/zotero/zotero.properties | 4 - chrome/locale/ko-KR/zotero/zotero.properties | 4 - chrome/locale/nb-NO/zotero/zotero.properties | 4 - chrome/locale/nl-NL/zotero/zotero.properties | 4 - chrome/locale/sr-YU/zotero/zotero.properties | 4 - chrome/locale/zh-CN/zotero/zotero.properties | 4 - 14 files changed, 673 insertions(+), 693 deletions(-) diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js index aabf215361..0f5635fdef 100644 --- a/chrome/content/zotero/xpcom/db.js +++ b/chrome/content/zotero/xpcom/db.js @@ -20,657 +20,740 @@ ***** END LICENSE BLOCK ***** */ -Zotero.DB = new function(){ +Zotero.DBConnection = function(dbName) { + if (!dbName) { + throw ('DB name not provided in Zotero.DBConnection()'); + } + // Private members - var _connection; - var _transactionRollback; - var _transactionNestingLevel = 0; - var _callbacks = { begin: [], commit: [], rollback: [] }; + this._dbName = dbName; + this._shutdown = false; + this._connection = null; + this._transactionRollback = null; + this._transactionNestingLevel = 0; + this._callbacks = { begin: [], commit: [], rollback: [] }; + this._self = this; +} - this.query = query; - this.valueQuery = valueQuery; - this.rowQuery = rowQuery; - this.columnQuery = columnQuery; - this.getStatement = getStatement; - this.getLastInsertID = getLastInsertID; - this.getLastErrorString = getLastErrorString; - this.beginTransaction = beginTransaction; - this.commitTransaction = commitTransaction; - this.rollbackTransaction = rollbackTransaction; - this.addCallback = addCallback; - this.removeCallback = removeCallback; - this.transactionInProgress = transactionInProgress; - this.commitAllTransactions = commitAllTransactions; - this.tableExists = tableExists; - this.getColumns = getColumns; - this.getColumnHash = getColumnHash; - this.getNextID = getNextID; - this.getNextName = getNextName; +///////////////////////////////////////////////////////////////// +// +// Public methods +// +///////////////////////////////////////////////////////////////// + +/* + * Run an SQL query + * + * Optional _params_ is an array of bind parameters in the form + * [1,"hello",3] or [{'int':2},{'string':'foobar'}] + * + * Returns: + * - Associative array (similar to mysql_fetch_assoc) for SELECT's + * - lastInsertId for INSERT's + * - TRUE for other successful queries + * - FALSE on error + */ +Zotero.DBConnection.prototype.query = function (sql,params) { + var db = this._getDBConnection(); - ///////////////////////////////////////////////////////////////// - // - // Privileged methods - // - ///////////////////////////////////////////////////////////////// - - /* - * Run an SQL query - * - * Optional _params_ is an array of bind parameters in the form - * [1,"hello",3] or [{'int':2},{'string':'foobar'}] - * - * Returns: - * - Associative array (similar to mysql_fetch_assoc) for SELECT's - * - lastInsertId for INSERT's - * - TRUE for other successful queries - * - FALSE on error - */ - function query(sql,params){ - var db = _getDBConnection(); + try { + // Parse out the SQL command being used + var op = sql.match(/^[^a-z]*[^ ]+/i); + if (op) { + op = op.toString().toLowerCase(); + } - try { - // Parse out the SQL command being used - var op = sql.match(/^[^a-z]*[^ ]+/i); - if (op){ - op = op.toString().toLowerCase(); - } + // If SELECT statement, return result + if (op=='select') { + // Until the native dataset methods work (or at least exist), + // we build a multi-dimensional associative array manually - // If SELECT statement, return result - if (op=='select'){ - // Until the native dataset methods work (or at least exist), - // we build a multi-dimensional associative array manually + var statement = this.getStatement(sql, params); + + var dataset = new Array(); + while (statement.executeStep()) { + var row = new Array(); - var statement = getStatement(sql, params); - - var dataset = new Array(); - while (statement.executeStep()){ - var row = new Array(); - - for(var i=0; i0 in a table column +* +* Note: This retrieves all the rows of the column, so it's not really +* meant for particularly large tables. +**/ +Zotero.DBConnection.prototype.getNextID = function (table, column) { + var sql = 'SELECT ' + column + ' FROM ' + table + ' ORDER BY ' + column; + var vals = this.columnQuery(sql); + + if (!vals) { + return 1; + } + + if (vals[0] === '0') { + vals.shift(); + } + + for (var i=0, len=vals.length; i0 in a table column - * - * Note: This retrieves all the rows of the column, so it's not really - * meant for particularly large tables. - **/ - function getNextID(table, column){ - var sql = 'SELECT ' + column + ' FROM ' + table + ' ORDER BY ' + column; - var vals = Zotero.DB.columnQuery(sql); - - if (!vals){ - return 1; - } - - if (vals[0] === '0'){ - vals.shift(); - } - - for (var i=0, len=vals.length; i