PhoneGap-SQLitePlugin-Android/Android/assets/www/SQLitePlugin.js

257 lines
8.7 KiB
JavaScript
Raw Normal View History

(function() {
var SQLiteFactory, SQLitePlugin, SQLitePluginCallback, SQLitePluginTransaction, SQLitePluginTransactionCB, get_unique_id, pcb, root, transaction_callback_queue, transaction_queue;
2012-04-22 02:22:47 -04:00
root = this;
SQLitePlugin = function(dbPath, openSuccess, openError) {
console.log("SQLitePlugin");
if (!dbPath) {
throw new Error("Cannot create a SQLitePlugin instance without a dbPath");
}
this.dbPath = dbPath;
this.openSuccess = openSuccess;
this.openError = openError;
2012-04-22 02:22:47 -04:00
this.openSuccess || (this.openSuccess = function() {
return console.log("DB opened: " + dbPath);
});
this.openError || (this.openError = function(e) {
return console.log(e.message);
});
this.open(this.openSuccess, this.openError);
};
SQLitePlugin.prototype.openDBs = {};
SQLitePlugin.prototype.transaction = function(fn, error, success) {
var t;
t = new SQLitePluginTransaction(this.dbPath);
fn(t);
t.complete(success, error);
2012-04-22 02:22:47 -04:00
};
SQLitePlugin.prototype.open = function(success, error) {
console.log("SQLitePlugin.prototype.open");
if (!(this.dbPath in this.openDBs)) {
this.openDBs[this.dbPath] = true;
cordova.exec(success, error, "SQLitePlugin", "open", [this.dbPath]);
2012-04-22 02:22:47 -04:00
}
};
SQLitePlugin.prototype.close = function(success, error) {
console.log("SQLitePlugin.prototype.close");
if (this.dbPath in this.openDBs) {
delete this.openDBs[this.dbPath];
cordova.exec(null, null, "SQLitePlugin", "close", [this.dbPath]);
}
2012-04-22 02:22:47 -04:00
};
pcb = function() {
return 1;
};
SQLitePlugin.prototype.executePragmaStatement = function(statement, success, error) {
console.log("SQLitePlugin::executePragmaStatement");
pcb = success;
cordova.exec((function() {
return 1;
}), error, "SQLitePlugin", "executePragmaStatement", [this.dbPath, statement]);
};
SQLitePluginCallback = {
p1: function(id, result) {
var mycb;
console.log("PRAGMA CB");
mycb = pcb;
pcb = function() {
return 1;
};
mycb(result);
}
};
2012-04-22 02:22:47 -04:00
get_unique_id = function() {
var id, id2;
id = new Date().getTime();
id2 = new Date().getTime();
while (id === id2) {
id2 = new Date().getTime();
}
2012-04-22 02:22:47 -04:00
return id2 + "000";
};
transaction_queue = [];
transaction_callback_queue = {};
SQLitePluginTransaction = function(dbPath) {
this.dbPath = dbPath;
this.executes = [];
this.trans_id = get_unique_id();
this.__completed = false;
this.__submitted = false;
this.optimization_no_nested_callbacks = false;
2012-04-22 02:22:47 -04:00
console.log("SQLitePluginTransaction - this.trans_id:" + this.trans_id);
transaction_queue[this.trans_id] = [];
transaction_callback_queue[this.trans_id] = new Object();
};
SQLitePluginTransactionCB = {};
SQLitePluginTransactionCB.queryCompleteCallback = function(transId, queryId, result) {
2012-04-22 02:22:47 -04:00
var query, x;
console.log("SQLitePluginTransaction.queryCompleteCallback");
query = null;
for (x in transaction_queue[transId]) {
if (transaction_queue[transId][x]["query_id"] === queryId) {
query = transaction_queue[transId][x];
if (transaction_queue[transId].length === 1) {
transaction_queue[transId] = [];
} else {
transaction_queue[transId].splice(x, 1);
}
break;
}
}
2012-04-22 02:22:47 -04:00
if (query && query["callback"]) return query["callback"](result);
};
SQLitePluginTransactionCB.queryErrorCallback = function(transId, queryId, result) {
2012-04-22 02:22:47 -04:00
var query, x;
query = null;
for (x in transaction_queue[transId]) {
if (transaction_queue[transId][x]["query_id"] === queryId) {
query = transaction_queue[transId][x];
if (transaction_queue[transId].length === 1) {
transaction_queue[transId] = [];
} else {
transaction_queue[transId].splice(x, 1);
}
break;
}
}
2012-04-22 02:22:47 -04:00
if (query && query["err_callback"]) return query["err_callback"](result);
};
SQLitePluginTransactionCB.txCompleteCallback = function(transId) {
2012-04-22 02:22:47 -04:00
if (typeof transId !== "undefined") {
if (transId && transaction_callback_queue[transId] && transaction_callback_queue[transId]["success"]) {
return transaction_callback_queue[transId]["success"]();
}
} else {
return console.log("SQLitePluginTransaction.txCompleteCallback---transId = NULL");
}
2012-04-22 02:22:47 -04:00
};
SQLitePluginTransactionCB.txErrorCallback = function(transId, error) {
2012-04-22 02:22:47 -04:00
if (typeof transId !== "undefined") {
console.log("SQLitePluginTransaction.txErrorCallback---transId:" + transId);
if (transId && transaction_callback_queue[transId]["error"]) {
transaction_callback_queue[transId]["error"](error);
}
delete transaction_queue[transId];
return delete transaction_callback_queue[transId];
} else {
return console.log("SQLitePluginTransaction.txErrorCallback---transId = NULL");
}
};
SQLitePluginTransaction.prototype.add_to_transaction = function(trans_id, query, params, callback, err_callback) {
var new_query;
new_query = new Object();
new_query["trans_id"] = trans_id;
if (callback || !this.optimization_no_nested_callbacks) {
new_query["query_id"] = get_unique_id();
} else {
if (this.optimization_no_nested_callbacks) new_query["query_id"] = "";
}
new_query["query"] = query;
if (params) {
new_query["params"] = params;
} else {
new_query["params"] = [];
}
new_query["callback"] = callback;
new_query["err_callback"] = err_callback;
if (!transaction_queue[trans_id]) transaction_queue[trans_id] = [];
transaction_queue[trans_id].push(new_query);
2012-04-22 02:22:47 -04:00
};
SQLitePluginTransaction.prototype.executeSql = function(sql, values, success, error) {
var errorcb, successcb, txself;
console.log("SQLitePluginTransaction.prototype.executeSql");
errorcb = void 0;
successcb = void 0;
txself = void 0;
txself = this;
successcb = null;
if (success) {
console.log("success not null:" + sql);
successcb = function(execres) {
var res, saveres;
console.log("executeSql callback:" + JSON.stringify(execres));
res = void 0;
saveres = void 0;
saveres = execres;
res = {
rows: {
item: function(i) {
return saveres[i];
},
length: saveres.length
},
rowsAffected: saveres.rowsAffected,
insertId: saveres.insertId || null
};
return success(txself, res);
};
} else {
console.log("success NULL:" + sql);
}
errorcb = null;
if (error) {
errorcb = function(res) {
return error(txself, res);
};
}
console.log("executeSql - add_to_transaction" + sql);
2012-04-22 02:22:47 -04:00
this.add_to_transaction(this.trans_id, sql, values, successcb, errorcb);
};
SQLitePluginTransaction.prototype.complete = function(success, error) {
var errorcb, successcb, txself;
2012-04-22 02:22:47 -04:00
console.log("SQLitePluginTransaction.prototype.complete");
if (this.__completed) throw new Error("Transaction already run");
if (this.__submitted) throw new Error("Transaction already submitted");
this.__submitted = true;
txself = this;
successcb = function() {
if (transaction_queue[txself.trans_id].length > 0 && !txself.optimization_no_nested_callbacks) {
txself.__submitted = false;
return txself.complete(success, error);
} else {
this.__completed = true;
if (success) return success(txself);
}
};
errorcb = function(res) {
return null;
};
2012-04-22 02:22:47 -04:00
if (error) {
errorcb = function(res) {
return error(txself, res);
};
}
transaction_callback_queue[this.trans_id]["success"] = successcb;
transaction_callback_queue[this.trans_id]["error"] = errorcb;
cordova.exec(null, null, "SQLitePlugin", "executeSqlBatch", [this.dbPath, transaction_queue[this.trans_id]]);
2012-04-22 02:22:47 -04:00
};
SQLiteFactory = {
opendb: function() {
var dbname, errorcb, first, okcb;
if (arguments.length < 1) return null;
first = arguments[0];
dbname = "DB";
okcb = null;
errorcb = null;
if (first.constructor === String) {
dbname = first;
if (arguments.length >= 5) {
okcb = arguments[4];
if (arguments.length > 5) errorcb = arguments[5];
}
} else {
dbname = first['name'];
if (arguments.length >= 2) {
okcb = arguments[1];
if (arguments.length > 2) errorcb = arguments[2];
}
}
return new SQLitePlugin(dbname, okcb, errorcb);
}
};
root.SQLitePluginCallback = SQLitePluginCallback;
root.SQLitePluginTransactionCB = SQLitePluginTransactionCB;
2012-04-22 02:22:47 -04:00
return root.sqlitePlugin = {
openDatabase: SQLiteFactory.opendb
};
2012-04-22 02:22:47 -04:00
})();