Send full db open arguments object to Java

This commit is contained in:
Chris Brody 2012-12-18 22:21:01 +01:00
parent 73cded6160
commit 810e1f4252
3 changed files with 58 additions and 43 deletions

View File

@ -1,17 +1,23 @@
do -> do ->
root = @ root = @
SQLitePlugin = (dbPath, openSuccess, openError) -> SQLitePlugin = (openargs, openSuccess, openError) ->
console.log "SQLitePlugin" console.log "SQLitePlugin"
throw new Error("Cannot create a SQLitePlugin instance without a dbPath") unless dbPath
@dbPath = dbPath if !(openargs and openargs['name'])
throw new Error("Cannot create a SQLitePlugin instance without a db name")
dbname = openargs.name
@openargs = openargs
@dbname = dbname
@openSuccess = openSuccess @openSuccess = openSuccess
@openError = openError @openError = openError
@openSuccess or @openSuccess or
@openSuccess = -> @openSuccess = ->
console.log "DB opened: " + dbPath console.log "DB opened: " + dbname
@openError or @openError or
@openError = (e) -> @openError = (e) ->
@ -23,7 +29,7 @@ do ->
SQLitePlugin::openDBs = {} SQLitePlugin::openDBs = {}
SQLitePlugin::transaction = (fn, error, success) -> SQLitePlugin::transaction = (fn, error, success) ->
t = new SQLitePluginTransaction(@dbPath) t = new SQLitePluginTransaction(@dbname)
fn t fn t
t.complete success, error t.complete success, error
return return
@ -31,19 +37,19 @@ do ->
SQLitePlugin::open = (success, error) -> SQLitePlugin::open = (success, error) ->
console.log "SQLitePlugin.prototype.open" console.log "SQLitePlugin.prototype.open"
unless @dbPath of @openDBs unless @dbname of @openDBs
@openDBs[@dbPath] = true @openDBs[@dbname] = true
cordova.exec success, error, "SQLitePlugin", "open", [ @dbPath ] cordova.exec success, error, "SQLitePlugin", "open", [ @openargs ]
return return
SQLitePlugin::close = (success, error) -> SQLitePlugin::close = (success, error) ->
console.log "SQLitePlugin.prototype.close" console.log "SQLitePlugin.prototype.close"
if @dbPath of @openDBs if @dbname of @openDBs
delete @openDBs[@dbPath] delete @openDBs[@dbname]
cordova.exec null, null, "SQLitePlugin", "close", [ @dbPath ] cordova.exec null, null, "SQLitePlugin", "close", [ @dbname ]
return return
@ -53,7 +59,7 @@ do ->
console.log "SQLitePlugin::executePragmaStatement" console.log "SQLitePlugin::executePragmaStatement"
pcb = success pcb = success
cordova.exec (-> 1), error, "SQLitePlugin", "executePragmaStatement", [ @dbPath, statement ] cordova.exec (-> 1), error, "SQLitePlugin", "executePragmaStatement", [ @dbname, statement ]
return return
SQLitePluginCallback = SQLitePluginCallback =
@ -75,8 +81,8 @@ do ->
transaction_queue = [] transaction_queue = []
transaction_callback_queue = {} transaction_callback_queue = {}
SQLitePluginTransaction = (dbPath) -> SQLitePluginTransaction = (dbname) ->
@dbPath = dbPath @dbname = dbname
@executes = [] @executes = []
@trans_id = get_unique_id() @trans_id = get_unique_id()
@__completed = false @__completed = false
@ -218,7 +224,7 @@ do ->
transaction_callback_queue[@trans_id]["success"] = successcb transaction_callback_queue[@trans_id]["success"] = successcb
transaction_callback_queue[@trans_id]["error"] = errorcb transaction_callback_queue[@trans_id]["error"] = errorcb
cordova.exec null, null, "SQLitePlugin", "executeSqlBatch", [ @dbPath, transaction_queue[@trans_id] ] cordova.exec null, null, "SQLitePlugin", "executeSqlBatch", [ @dbname, transaction_queue[@trans_id] ]
return return
SQLiteFactory = SQLiteFactory =
@ -226,30 +232,30 @@ do ->
if arguments.length < 1 then return null if arguments.length < 1 then return null
first = arguments[0] first = arguments[0]
dbname = "DB" openargs = null
okcb = null okcb = null
errorcb = null errorcb = null
if first.constructor == String if first.constructor == String
dbname = first openargs = {name: first}
if arguments.length >= 5 if arguments.length >= 5
okcb = arguments[4] okcb = arguments[4]
if arguments.length > 5 then errorcb = arguments[5] if arguments.length > 5 then errorcb = arguments[5]
else else
dbname = first['name'] openargs = first
if arguments.length >= 2 if arguments.length >= 2
okcb = arguments[1] okcb = arguments[1]
if arguments.length > 2 then errorcb = arguments[2] if arguments.length > 2 then errorcb = arguments[2]
new SQLitePlugin dbname, okcb, errorcb new SQLitePlugin openargs, okcb, errorcb
# Required for callbacks: # Required for callbacks:
root.SQLitePluginCallback = SQLitePluginCallback root.SQLitePluginCallback = SQLitePluginCallback
root.SQLitePluginTransactionCB = SQLitePluginTransactionCB root.SQLitePluginTransactionCB = SQLitePluginTransactionCB
root.sqlitePlugin = root.sqlitePlugin =
#openDatabase: (dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) ->
# new SQLitePlugin(dbPath, creationCallback, errorCallback)
openDatabase: SQLiteFactory.opendb openDatabase: SQLiteFactory.opendb

View File

@ -1,16 +1,19 @@
(function() { (function() {
var SQLiteFactory, SQLitePlugin, SQLitePluginCallback, SQLitePluginTransaction, SQLitePluginTransactionCB, get_unique_id, pcb, root, transaction_callback_queue, transaction_queue; var SQLiteFactory, SQLitePlugin, SQLitePluginCallback, SQLitePluginTransaction, SQLitePluginTransactionCB, get_unique_id, pcb, root, transaction_callback_queue, transaction_queue;
root = this; root = this;
SQLitePlugin = function(dbPath, openSuccess, openError) { SQLitePlugin = function(openargs, openSuccess, openError) {
var dbname;
console.log("SQLitePlugin"); console.log("SQLitePlugin");
if (!dbPath) { if (!(openargs && openargs['name'])) {
throw new Error("Cannot create a SQLitePlugin instance without a dbPath"); throw new Error("Cannot create a SQLitePlugin instance without a db name");
} }
this.dbPath = dbPath; dbname = openargs.name;
this.openargs = openargs;
this.dbname = dbname;
this.openSuccess = openSuccess; this.openSuccess = openSuccess;
this.openError = openError; this.openError = openError;
this.openSuccess || (this.openSuccess = function() { this.openSuccess || (this.openSuccess = function() {
return console.log("DB opened: " + dbPath); return console.log("DB opened: " + dbname);
}); });
this.openError || (this.openError = function(e) { this.openError || (this.openError = function(e) {
return console.log(e.message); return console.log(e.message);
@ -20,22 +23,22 @@
SQLitePlugin.prototype.openDBs = {}; SQLitePlugin.prototype.openDBs = {};
SQLitePlugin.prototype.transaction = function(fn, error, success) { SQLitePlugin.prototype.transaction = function(fn, error, success) {
var t; var t;
t = new SQLitePluginTransaction(this.dbPath); t = new SQLitePluginTransaction(this.dbname);
fn(t); fn(t);
t.complete(success, error); t.complete(success, error);
}; };
SQLitePlugin.prototype.open = function(success, error) { SQLitePlugin.prototype.open = function(success, error) {
console.log("SQLitePlugin.prototype.open"); console.log("SQLitePlugin.prototype.open");
if (!(this.dbPath in this.openDBs)) { if (!(this.dbname in this.openDBs)) {
this.openDBs[this.dbPath] = true; this.openDBs[this.dbname] = true;
cordova.exec(success, error, "SQLitePlugin", "open", [this.dbPath]); cordova.exec(success, error, "SQLitePlugin", "open", [this.openargs]);
} }
}; };
SQLitePlugin.prototype.close = function(success, error) { SQLitePlugin.prototype.close = function(success, error) {
console.log("SQLitePlugin.prototype.close"); console.log("SQLitePlugin.prototype.close");
if (this.dbPath in this.openDBs) { if (this.dbname in this.openDBs) {
delete this.openDBs[this.dbPath]; delete this.openDBs[this.dbname];
cordova.exec(null, null, "SQLitePlugin", "close", [this.dbPath]); cordova.exec(null, null, "SQLitePlugin", "close", [this.dbname]);
} }
}; };
pcb = function() { pcb = function() {
@ -46,7 +49,7 @@
pcb = success; pcb = success;
cordova.exec((function() { cordova.exec((function() {
return 1; return 1;
}), error, "SQLitePlugin", "executePragmaStatement", [this.dbPath, statement]); }), error, "SQLitePlugin", "executePragmaStatement", [this.dbname, statement]);
}; };
SQLitePluginCallback = { SQLitePluginCallback = {
p1: function(id, result) { p1: function(id, result) {
@ -70,8 +73,8 @@
}; };
transaction_queue = []; transaction_queue = [];
transaction_callback_queue = {}; transaction_callback_queue = {};
SQLitePluginTransaction = function(dbPath) { SQLitePluginTransaction = function(dbname) {
this.dbPath = dbPath; this.dbname = dbname;
this.executes = []; this.executes = [];
this.trans_id = get_unique_id(); this.trans_id = get_unique_id();
this.__completed = false; this.__completed = false;
@ -222,30 +225,32 @@
} }
transaction_callback_queue[this.trans_id]["success"] = successcb; transaction_callback_queue[this.trans_id]["success"] = successcb;
transaction_callback_queue[this.trans_id]["error"] = errorcb; transaction_callback_queue[this.trans_id]["error"] = errorcb;
cordova.exec(null, null, "SQLitePlugin", "executeSqlBatch", [this.dbPath, transaction_queue[this.trans_id]]); cordova.exec(null, null, "SQLitePlugin", "executeSqlBatch", [this.dbname, transaction_queue[this.trans_id]]);
}; };
SQLiteFactory = { SQLiteFactory = {
opendb: function() { opendb: function() {
var dbname, errorcb, first, okcb; var errorcb, first, okcb, openargs;
if (arguments.length < 1) return null; if (arguments.length < 1) return null;
first = arguments[0]; first = arguments[0];
dbname = "DB"; openargs = null;
okcb = null; okcb = null;
errorcb = null; errorcb = null;
if (first.constructor === String) { if (first.constructor === String) {
dbname = first; openargs = {
name: first
};
if (arguments.length >= 5) { if (arguments.length >= 5) {
okcb = arguments[4]; okcb = arguments[4];
if (arguments.length > 5) errorcb = arguments[5]; if (arguments.length > 5) errorcb = arguments[5];
} }
} else { } else {
dbname = first['name']; openargs = first;
if (arguments.length >= 2) { if (arguments.length >= 2) {
okcb = arguments[1]; okcb = arguments[1];
if (arguments.length > 2) errorcb = arguments[2]; if (arguments.length > 2) errorcb = arguments[2];
} }
} }
return new SQLitePlugin(dbname, okcb, errorcb); return new SQLitePlugin(openargs, okcb, errorcb);
} }
}; };
root.SQLitePluginCallback = SQLitePluginCallback; root.SQLitePluginCallback = SQLitePluginCallback;

View File

@ -56,7 +56,11 @@ public class SQLitePlugin extends CordovaPlugin
{ {
try { try {
if (action.equals("open")) { if (action.equals("open")) {
this.openDatabase(args.getString(0)); //this.openDatabase(args.getString(0));
JSONObject o = args.getJSONObject(0);
String dbname = o.getString("name");
this.openDatabase(dbname);
} }
else if (action.equals("close")) { else if (action.equals("close")) {
this.closeDatabase(args.getString(0)); this.closeDatabase(args.getString(0));