diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..1f615d0 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +all: coffee + +coffee: + mkdir -p build + coffee -p src/sqlite_plugin.js.coffee > build/sqlite_plugin.js + +clean: + rm -f build/sqlite_plugin.js diff --git a/README.md b/README.md index e20824a..20a6993 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,8 @@ Installing Drag .h and .m files into your project's Plugins folder (in xcode) -- I always just have "Create references" as the option selected. -Compile the coffeescript file to javascript WITH the top-level function wrapper -option (default). +Take the precompiled javascript file from build/, or compile the coffeescript +file in src/ to javascript WITH the top-level function wrapper option (default). Use the resulting javascript file in your HTML. diff --git a/build/sqlite_plugin.js b/build/sqlite_plugin.js new file mode 100644 index 0000000..63f7e9d --- /dev/null +++ b/build/sqlite_plugin.js @@ -0,0 +1,121 @@ +(function() { + var callbacks, counter, fnref, getOptions, root; + root = this; + callbacks = {}; + counter = 0; + fnref = function(fn) { + var f; + f = "cb" + (counter += 1); + callbacks[f] = fn; + return f; + }; + getOptions = function(opts, success, error) { + if (typeof success === "function") { + opts.successCallback = fnref(success); + } + if (typeof error === "function") { + opts.errorCallback = fnref(error); + } + return opts; + }; + root.PGSQLitePlugin = (function() { + PGSQLitePlugin.prototype.openDBs = {}; + function PGSQLitePlugin(dbPath, openSuccess, openError) { + this.dbPath = dbPath; + this.openSuccess = openSuccess; + this.openError = openError; + if (!dbPath) { + throw new Error("Cannot create a PGSQLitePlugin instance without a dbPath"); + } + this.openSuccess || (this.openSuccess = function() { + console.log("DB opened: " + dbPath); + }); + this.openError || (this.openError = function(e) { + console.log(e.message); + }); + this.open(this.openSuccess, this.openError); + } + PGSQLitePlugin.handleCallback = function() { + var args, f, _ref; + args = Array.prototype.slice.call(arguments); + f = args.shift(); + if ((_ref = callbacks[f]) != null) { + _ref.apply(null, args); + } + callbacks[f] = null; + delete callbacks[f]; + }; + PGSQLitePlugin.prototype.executeSql = function(sql, success, error) { + var opts; + if (!sql) { + throw new Error("Cannot executeSql without a query"); + } + opts = getOptions({ + query: [].concat(sql || []), + path: this.dbPath + }, success, error); + PhoneGap.exec("PGSQLitePlugin.backgroundExecuteSql", opts); + }; + PGSQLitePlugin.prototype.transaction = function(fn, success, error) { + var t; + t = new root.PGSQLitePluginTransaction(this.dbPath); + fn(t); + return t.complete(success, error); + }; + PGSQLitePlugin.prototype.open = function(success, error) { + var opts; + if (!(this.dbPath in this.openDBs)) { + this.openDBs[this.dbPath] = true; + opts = getOptions({ + path: this.dbPath + }, success, error); + PhoneGap.exec("PGSQLitePlugin.open", opts); + } + }; + PGSQLitePlugin.prototype.close = function(success, error) { + var opts; + if (this.dbPath in this.openDBs) { + delete this.openDBs[this.dbPath]; + opts = getOptions({ + path: this.dbPath + }, success, error); + PhoneGap.exec("PGSQLitePlugin.close", opts); + } + }; + return PGSQLitePlugin; + })(); + root.PGSQLitePluginTransaction = (function() { + function PGSQLitePluginTransaction(dbPath) { + this.dbPath = dbPath; + this.executes = []; + } + PGSQLitePluginTransaction.prototype.executeSql = function(sql, success, error) { + this.executes.push(getOptions({ + query: [].concat(sql || []), + path: this.dbPath + }, success, error)); + }; + PGSQLitePluginTransaction.prototype.complete = function(success, error) { + var begin_opts, commit_opts, executes, opts; + if (this.__completed) { + throw new Error("Transaction already run"); + } + this.__completed = true; + begin_opts = getOptions({ + query: ["BEGIN;"], + path: this.dbPath + }); + commit_opts = getOptions({ + query: ["COMMIT;"], + path: this.dbPath + }, success, error); + executes = [begin_opts].concat(this.executes).concat([commit_opts]); + opts = { + executes: executes + }; + PhoneGap.exec("PGSQLitePlugin.backgroundExecuteSqlBatch", opts); + this.executes = []; + }; + return PGSQLitePluginTransaction; + })(); +}).call(this); diff --git a/www/sqlite_plugin.js.coffee b/src/sqlite_plugin.js.coffee similarity index 98% rename from www/sqlite_plugin.js.coffee rename to src/sqlite_plugin.js.coffee index 66542bb..760ae23 100644 --- a/www/sqlite_plugin.js.coffee +++ b/src/sqlite_plugin.js.coffee @@ -9,7 +9,7 @@ callbacks = {} counter = 0 fnref = (fn) -> - f = "_sqliteplugin#{counter+=1}" + f = "cb#{counter+=1}" callbacks[f] = fn f