diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js index aabf21536..0f5635fde 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