From fdc78e1b088301681cc4c4b92a04a21981c44cc8 Mon Sep 17 00:00:00 2001 From: Jos Shepherd Date: Fri, 5 Nov 2010 17:03:05 +0000 Subject: [PATCH 1/2] Fix for troublesome values being returned from DroidDB stores. Stick to one level of JSON serialization. --- framework/assets/js/storage.js | 3 +-- framework/src/com/phonegap/Storage.java | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/framework/assets/js/storage.js b/framework/assets/js/storage.js index 792dbca4..ca6015f6 100755 --- a/framework/assets/js/storage.js +++ b/framework/assets/js/storage.js @@ -27,9 +27,8 @@ var DroidDB = function() { * @param rawdata JSON string of the row data * @param id Query id */ -DroidDB.prototype.addResult = function(rawdata, id) { +DroidDB.prototype.addResult = function(data, id) { try { - eval("var data = " + rawdata + ";"); var query = this.queryQueue[id]; query.resultSet.push(data); } catch (e) { diff --git a/framework/src/com/phonegap/Storage.java b/framework/src/com/phonegap/Storage.java index 3cf798c5..09266858 100755 --- a/framework/src/com/phonegap/Storage.java +++ b/framework/src/com/phonegap/Storage.java @@ -172,12 +172,12 @@ public class Storage extends Plugin { try { for (int i = 0; i < colCount; ++i) { key = cur.getColumnName(i); - value = cur.getString(i).replace("\"", "\\\""); // must escape " with \" for JavaScript + value = cur.getString(i); result.put(key, value); } - + // Send row back to JavaScript - this.sendJavascript("droiddb.addResult('" + result.toString() + "','" + tx_id + "');"); + this.sendJavascript("droiddb.addResult(" + result.toString() + ",'" + tx_id + "');"); } catch (JSONException e) { e.printStackTrace(); From 3a0101261dd327414836ad8d438d82f143554465 Mon Sep 17 00:00:00 2001 From: Jos Shepherd Date: Sat, 6 Nov 2010 20:01:22 +0000 Subject: [PATCH 2/2] Move data transfer for storage to completeQuery for speed up --- framework/assets/js/storage.js | 22 +++------------------- framework/src/com/phonegap/Storage.java | 15 +++++++++------ 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/framework/assets/js/storage.js b/framework/assets/js/storage.js index ca6015f6..2496ee0c 100755 --- a/framework/assets/js/storage.js +++ b/framework/assets/js/storage.js @@ -20,29 +20,13 @@ var DroidDB = function() { this.queryQueue = {}; }; -/** - * Callback from native code when result from a query is available. - * PRIVATE METHOD - * - * @param rawdata JSON string of the row data - * @param id Query id - */ -DroidDB.prototype.addResult = function(data, id) { - try { - var query = this.queryQueue[id]; - query.resultSet.push(data); - } catch (e) { - console.log("DroidDB.addResult(): Error="+e); - } -}; - /** * Callback from native code when query is complete. * PRIVATE METHOD * * @param id Query id */ -DroidDB.prototype.completeQuery = function(id) { +DroidDB.prototype.completeQuery = function(id, data) { var query = this.queryQueue[id]; if (query) { try { @@ -58,8 +42,8 @@ DroidDB.prototype.completeQuery = function(id) { // Save query results var r = new DroidDB_Result(); - r.rows.resultSet = query.resultSet; - r.rows.length = query.resultSet.length; + r.rows.resultSet = data; + r.rows.length = data.length; try { if (typeof query.successCallback == 'function') { query.successCallback(query.tx, r); diff --git a/framework/src/com/phonegap/Storage.java b/framework/src/com/phonegap/Storage.java index 09266858..de30afb2 100755 --- a/framework/src/com/phonegap/Storage.java +++ b/framework/src/com/phonegap/Storage.java @@ -160,24 +160,25 @@ public class Storage extends Plugin { */ public void processResults(Cursor cur, String tx_id) { + String result = "[]"; // If query result has rows + if (cur.moveToFirst()) { + JSONArray fullresult = new JSONArray(); String key = ""; String value = ""; int colCount = cur.getColumnCount(); // Build up JSON result object for each row do { - JSONObject result = new JSONObject(); + JSONObject row = new JSONObject(); try { for (int i = 0; i < colCount; ++i) { key = cur.getColumnName(i); value = cur.getString(i); - result.put(key, value); + row.put(key, value); } - - // Send row back to JavaScript - this.sendJavascript("droiddb.addResult(" + result.toString() + ",'" + tx_id + "');"); + fullresult.put(row); } catch (JSONException e) { e.printStackTrace(); @@ -185,9 +186,11 @@ public class Storage extends Plugin { } while (cur.moveToNext()); + result = fullresult.toString(); } + // Let JavaScript know that there are no more rows - this.sendJavascript("droiddb.completeQuery('" + tx_id + "');"); + this.sendJavascript("droiddb.completeQuery('" + tx_id + "', "+result+");"); }