Merge branch 'jos3000-master'

This commit is contained in:
Bryce Curtis 2010-11-29 12:30:49 -06:00
commit b2a82975e5
2 changed files with 13 additions and 27 deletions

View File

@ -20,30 +20,13 @@ var DroidDB = function() {
this.queryQueue = {}; 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(rawdata, id) {
try {
eval("var data = " + rawdata + ";");
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. * Callback from native code when query is complete.
* PRIVATE METHOD * PRIVATE METHOD
* *
* @param id Query id * @param id Query id
*/ */
DroidDB.prototype.completeQuery = function(id) { DroidDB.prototype.completeQuery = function(id, data) {
var query = this.queryQueue[id]; var query = this.queryQueue[id];
if (query) { if (query) {
try { try {
@ -59,8 +42,8 @@ DroidDB.prototype.completeQuery = function(id) {
// Save query results // Save query results
var r = new DroidDB_Result(); var r = new DroidDB_Result();
r.rows.resultSet = query.resultSet; r.rows.resultSet = data;
r.rows.length = query.resultSet.length; r.rows.length = data.length;
try { try {
if (typeof query.successCallback == 'function') { if (typeof query.successCallback == 'function') {
query.successCallback(query.tx, r); query.successCallback(query.tx, r);

View File

@ -160,24 +160,25 @@ public class Storage extends Plugin {
*/ */
public void processResults(Cursor cur, String tx_id) { public void processResults(Cursor cur, String tx_id) {
String result = "[]";
// If query result has rows // If query result has rows
if (cur.moveToFirst()) { if (cur.moveToFirst()) {
JSONArray fullresult = new JSONArray();
String key = ""; String key = "";
String value = ""; String value = "";
int colCount = cur.getColumnCount(); int colCount = cur.getColumnCount();
// Build up JSON result object for each row // Build up JSON result object for each row
do { do {
JSONObject result = new JSONObject(); JSONObject row = new JSONObject();
try { try {
for (int i = 0; i < colCount; ++i) { for (int i = 0; i < colCount; ++i) {
key = cur.getColumnName(i); key = cur.getColumnName(i);
value = cur.getString(i).replace("\"", "\\\""); // must escape " with \" for JavaScript value = cur.getString(i);
result.put(key, value); row.put(key, value);
} }
fullresult.put(row);
// Send row back to JavaScript
this.sendJavascript("droiddb.addResult('" + result.toString() + "','" + tx_id + "');");
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -185,9 +186,11 @@ public class Storage extends Plugin {
} while (cur.moveToNext()); } while (cur.moveToNext());
result = fullresult.toString();
} }
// Let JavaScript know that there are no more rows // Let JavaScript know that there are no more rows
this.sendJavascript("droiddb.completeQuery('" + tx_id + "');"); this.sendJavascript("droiddb.completeQuery('" + tx_id + "', "+result+");");
} }