mirror of
https://github.com/apache/cordova-android.git
synced 2026-04-23 00:00:09 +08:00
Fix database for Android 1.x devices. It now behaves like HTML5 database API.
This commit is contained in:
Regular → Executable
+133
-29
@@ -5,71 +5,175 @@
|
||||
* most manufacturers ship with Android 1.5 and do not do OTA Updates, this is required
|
||||
*/
|
||||
|
||||
/**
|
||||
* Storage object that is called by native code when performing queries.
|
||||
* PRIVATE METHOD
|
||||
*/
|
||||
var DroidDB = function() {
|
||||
this.txQueue = [];
|
||||
this.txQueue = {};
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
DroidDB.prototype.addResult = function(rawdata, tx_id) {
|
||||
eval("var data = " + rawdata);
|
||||
var tx = this.txQueue[tx_id];
|
||||
tx.resultSet.push(data);
|
||||
try {
|
||||
eval("var data = " + rawdata + ";");
|
||||
var tx = this.txQueue[tx_id];
|
||||
tx.resultSet.push(data);
|
||||
} catch (e) {
|
||||
console.log("DroidDB.addResult(): Error="+e);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Callback from native code when query is complete.
|
||||
* PRIVATE METHOD
|
||||
*
|
||||
* @param tx_id
|
||||
*/
|
||||
DroidDB.prototype.completeQuery = function(tx_id) {
|
||||
var tx = this.txQueue[tx_id];
|
||||
var r = new result();
|
||||
r.rows.resultSet = tx.resultSet;
|
||||
r.rows.length = tx.resultSet.length;
|
||||
tx.win(r);
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Callback from native code when query fails
|
||||
* PRIVATE METHOD
|
||||
*
|
||||
* @param reason
|
||||
* @param tx_id
|
||||
*/
|
||||
DroidDB.prototype.fail = function(reason, tx_id) {
|
||||
var tx = this.txQueue[tx_id];
|
||||
tx.fail(reason);
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
var DatabaseShell = function() {
|
||||
};
|
||||
|
||||
/**
|
||||
* Start a transaction.
|
||||
*
|
||||
* @param process {Function} The transaction function
|
||||
*/
|
||||
DatabaseShell.prototype.transaction = function(process) {
|
||||
tx = new Tx();
|
||||
var tx = new DroidDB_Tx();
|
||||
process(tx);
|
||||
};
|
||||
|
||||
var Tx = function() {
|
||||
droiddb.txQueue.push(this);
|
||||
this.id = droiddb.txQueue.length - 1;
|
||||
/**
|
||||
* 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
|
||||
this.resultSet = [];
|
||||
};
|
||||
|
||||
Tx.prototype.executeSql = function(query, params, win, fail) {
|
||||
/**
|
||||
* 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
|
||||
PhoneGap.execAsync(null, null, "Storage", "executeSql", [query, params, this.id]);
|
||||
tx.win = win;
|
||||
tx.fail = fail;
|
||||
};
|
||||
|
||||
var result = function() {
|
||||
this.rows = new Rows();
|
||||
/**
|
||||
* SQL result set that is returned to user.
|
||||
* PRIVATE METHOD
|
||||
*/
|
||||
DroidDB_Result = function() {
|
||||
this.rows = new DroidDB_Rows();
|
||||
};
|
||||
|
||||
var Rows = function() {
|
||||
this.resultSet = [];
|
||||
this.length = 0;
|
||||
/**
|
||||
* SQL result set object
|
||||
* PRIVATE METHOD
|
||||
*/
|
||||
DroidDB_Rows = function() {
|
||||
this.resultSet = []; // results array
|
||||
this.length = 0; // number of rows
|
||||
};
|
||||
|
||||
Rows.prototype.item = function(row_id) {
|
||||
return this.resultSet[id];
|
||||
/**
|
||||
* 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];
|
||||
};
|
||||
|
||||
var dbSetup = function(name, version, display_name, size) {
|
||||
/**
|
||||
* 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) {
|
||||
PhoneGap.execAsync(null, null, "Storage", "openDatabase", [name, version, display_name, size]);
|
||||
db_object = new DatabaseShell();
|
||||
return db_object;
|
||||
var db = new DatabaseShell();
|
||||
return db;
|
||||
};
|
||||
|
||||
PhoneGap.addConstructor(function() {
|
||||
if (typeof window.openDatabase == "undefined") {
|
||||
navigator.openDatabase = window.openDatabase = dbSetup;
|
||||
navigator.openDatabase = window.openDatabase = DroidDB_openDatabase;
|
||||
window.droiddb = new DroidDB();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user