2010-09-07 02:13:09 +08:00
|
|
|
|
2009-12-09 06:08:48 +08:00
|
|
|
/*
|
|
|
|
* This is purely for the Android 1.5/1.6 HTML 5 Storage
|
2010-09-07 02:13:09 +08:00
|
|
|
* I was hoping that Android 2.0 would deprecate this, but given the fact that
|
2009-12-09 06:08:48 +08:00
|
|
|
* most manufacturers ship with Android 1.5 and do not do OTA Updates, this is required
|
|
|
|
*/
|
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* Storage object that is called by native code when performing queries.
|
|
|
|
* PRIVATE METHOD
|
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
var DroidDB = function() {
|
2010-10-13 04:53:57 +08:00
|
|
|
this.txQueue = {};
|
2010-09-07 02:13:09 +08:00
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* Callback from native code when result from a query is available.
|
|
|
|
* PRIVATE METHOD
|
|
|
|
*
|
|
|
|
* @param rawdata JSON string of the row data
|
|
|
|
* @param tx_id Transaction id
|
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
DroidDB.prototype.addResult = function(rawdata, tx_id) {
|
2010-10-13 04:53:57 +08:00
|
|
|
try {
|
|
|
|
eval("var data = " + rawdata + ";");
|
|
|
|
var tx = this.txQueue[tx_id];
|
|
|
|
tx.resultSet.push(data);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("DroidDB.addResult(): Error="+e);
|
|
|
|
}
|
2010-09-07 02:13:09 +08:00
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* Callback from native code when query is complete.
|
|
|
|
* PRIVATE METHOD
|
|
|
|
*
|
|
|
|
* @param tx_id
|
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
DroidDB.prototype.completeQuery = function(tx_id) {
|
2010-10-13 04:53:57 +08:00
|
|
|
var tx = null;
|
|
|
|
try {
|
|
|
|
tx = this.txQueue[tx_id];
|
|
|
|
var r = new DroidDB_Result();
|
|
|
|
r.rows.resultSet = tx.resultSet;
|
|
|
|
r.rows.length = tx.resultSet.length;
|
|
|
|
delete this.txQueue[tx_id];
|
|
|
|
} catch (e) {
|
|
|
|
console.log("DroidDB.completeQuery(): Error="+e);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
tx.successCallback(tx, r);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("DroidDB.completeQuery(): Error calling user success callback="+e);
|
|
|
|
}
|
2010-09-07 02:13:09 +08:00
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* Callback from native code when query fails
|
|
|
|
* PRIVATE METHOD
|
|
|
|
*
|
|
|
|
* @param reason
|
|
|
|
* @param tx_id
|
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
DroidDB.prototype.fail = function(reason, tx_id) {
|
2010-10-13 04:53:57 +08:00
|
|
|
var tx = null;
|
|
|
|
try {
|
|
|
|
tx = this.txQueue[tx_id];
|
|
|
|
delete this.txQueue[tx_id];
|
|
|
|
} catch (e) {
|
|
|
|
console.log("DroidDB.fail(): Error="+e);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
tx.errorCallback(reason);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("DroidDB.fail(): Error calling user error callback="+e);
|
|
|
|
}
|
2010-09-07 02:13:09 +08:00
|
|
|
};
|
2009-12-16 02:38:38 +08:00
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
var DatabaseShell = function() {
|
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* Start a transaction.
|
|
|
|
*
|
|
|
|
* @param process {Function} The transaction function
|
|
|
|
*/
|
2010-09-07 02:13:09 +08:00
|
|
|
DatabaseShell.prototype.transaction = function(process) {
|
2010-10-13 04:53:57 +08:00
|
|
|
var tx = new DroidDB_Tx();
|
2010-09-07 02:13:09 +08:00
|
|
|
process(tx);
|
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* Transaction object
|
|
|
|
* PRIVATE METHOD
|
|
|
|
*/
|
|
|
|
var DroidDB_Tx = function() {
|
|
|
|
|
|
|
|
// Set the id of the transaction
|
|
|
|
this.id = PhoneGap.createUUID();
|
|
|
|
|
|
|
|
// Add this transaction to the queue
|
|
|
|
droiddb.txQueue[this.id] = this;
|
|
|
|
|
|
|
|
// Init result
|
2010-09-07 02:13:09 +08:00
|
|
|
this.resultSet = [];
|
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* Execute SQL statement
|
|
|
|
*
|
|
|
|
* @param query
|
|
|
|
* @param params
|
|
|
|
* @param successCallback
|
|
|
|
* @param errorCallback
|
|
|
|
*/
|
|
|
|
DroidDB_Tx.prototype.executeSql = function(query, params, successCallback, errorCallback) {
|
|
|
|
|
|
|
|
// Init params array
|
|
|
|
if (typeof params == 'undefined') {
|
|
|
|
params = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save callbacks
|
|
|
|
var tx = droiddb.txQueue[this.id];
|
|
|
|
tx.successCallback = successCallback;
|
|
|
|
tx.errorCallback = errorCallback;
|
|
|
|
|
|
|
|
// Call native code
|
2010-09-11 04:13:53 +08:00
|
|
|
PhoneGap.execAsync(null, null, "Storage", "executeSql", [query, params, this.id]);
|
2010-09-07 02:13:09 +08:00
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* SQL result set that is returned to user.
|
|
|
|
* PRIVATE METHOD
|
|
|
|
*/
|
|
|
|
DroidDB_Result = function() {
|
|
|
|
this.rows = new DroidDB_Rows();
|
2010-09-07 02:13:09 +08:00
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* SQL result set object
|
|
|
|
* PRIVATE METHOD
|
|
|
|
*/
|
|
|
|
DroidDB_Rows = function() {
|
|
|
|
this.resultSet = []; // results array
|
|
|
|
this.length = 0; // number of rows
|
2010-09-07 02:13:09 +08:00
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* Get item from SQL result set
|
|
|
|
*
|
|
|
|
* @param row The row number to return
|
|
|
|
* @return The row object
|
|
|
|
*/
|
|
|
|
DroidDB_Rows.prototype.item = function(row) {
|
|
|
|
return this.resultSet[row];
|
2010-09-07 02:13:09 +08:00
|
|
|
};
|
2009-12-09 06:08:48 +08:00
|
|
|
|
2010-10-13 04:53:57 +08:00
|
|
|
/**
|
|
|
|
* Open database
|
|
|
|
*
|
|
|
|
* @param name Database name
|
|
|
|
* @param version Database version
|
|
|
|
* @param display_name Database display name
|
|
|
|
* @param size Database size in bytes
|
|
|
|
* @return Database object
|
|
|
|
*/
|
|
|
|
DroidDB_openDatabase = function(name, version, display_name, size) {
|
2010-09-11 04:13:53 +08:00
|
|
|
PhoneGap.execAsync(null, null, "Storage", "openDatabase", [name, version, display_name, size]);
|
2010-10-13 04:53:57 +08:00
|
|
|
var db = new DatabaseShell();
|
|
|
|
return db;
|
2010-09-07 02:13:09 +08:00
|
|
|
};
|
2009-12-09 07:58:07 +08:00
|
|
|
|
2009-12-09 06:08:48 +08:00
|
|
|
PhoneGap.addConstructor(function() {
|
2010-09-07 02:13:09 +08:00
|
|
|
if (typeof window.openDatabase == "undefined") {
|
2010-10-13 04:53:57 +08:00
|
|
|
navigator.openDatabase = window.openDatabase = DroidDB_openDatabase;
|
2010-09-07 02:13:09 +08:00
|
|
|
window.droiddb = new DroidDB();
|
|
|
|
}
|
2009-12-09 06:08:48 +08:00
|
|
|
});
|