Saturday, May 16, 2009

Zotero Database Abstraction

It should be noted that the database abstraction layer for Zotero is still not complete. The authors noted that
            // Until the native dataset methods work (or at least exist),
            // we build a multi-dimensional associative array manually


It took me a while to understand how I can get query results from Zotero.DB. It was mentioned in one of the functions comments but was not clarified in a document file. The query returns varies depending on the query type as per the following comments on the Zotero.DB.query() function
  * 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
You should notice in the first comment that the SELECT Query would return an associative array that is built by Zotero coders and not a native multidimentional javascript array.

The easiest way to parse such an array is to use a For Each loop or a while loop.
            for (duplicateItems in potentialDuplicateItems)
                {
                //
                // Get each row now
                //
                var oneRow = potentialDuplicateItems[duplicateItems];
                idsArray[duplicateItems] = oneRow['ID'];
                }

Make sure you use the correct index to read from each row or you might get a lot of undefined errors. Errors are not bad but when you are developing for a firefox extention and you have to restart your browser for each test or correction, it might become a hassle.

No comments: