diff --git a/SQLitePlugin.coffee.md b/SQLitePlugin.coffee.md index 9d1748d19..4f69c20ab 100644 --- a/SQLitePlugin.coffee.md +++ b/SQLitePlugin.coffee.md @@ -22,27 +22,27 @@ # Errors returned to callbacks must conform to `SqlError` with a code and message. # Some errors are of type `Error` or `string` and must be converted. - toSQLError = (error, code) -> + newSQLError = (error, code) -> sqlError = error code = 0 if !code # unknown by default - + if !sqlError sqlError = new Error "a plugin had an error but provided no response" sqlError.code = code - + if typeof sqlError is "string" sqlError = new Error error sqlError.code = code - + if !sqlError.code && sqlError.message sqlError.code = code - + if !sqlError.code && !sqlError.message sqlError = new Error "an unknown error was returned: " + JSON.stringify(sqlError) sqlError.code = code - + return sqlError - + nextTick = window.setImmediate || (fun) -> window.setTimeout(fun, 0) return @@ -71,7 +71,7 @@ console.log "SQLitePlugin openargs: #{JSON.stringify openargs}" if !(openargs and openargs['name']) - throw new Error("Cannot create a SQLitePlugin instance without a db name") + throw newSQLError "Cannot create a SQLitePlugin db instance without a db name" dbname = openargs.name @@ -110,15 +110,17 @@ SQLitePlugin::transaction = (fn, error, success) -> if !@openDBs[@dbname] - error(toSQLError('database not open')) + error newSQLError 'database not open' return + @addTransaction new SQLitePluginTransaction(this, fn, error, success, true, false) return SQLitePlugin::readTransaction = (fn, error, success) -> if !@openDBs[@dbname] - error(toSQLError('database not open')) + error newSQLError 'database not open' return + @addTransaction new SQLitePluginTransaction(this, fn, error, success, true, true) return @@ -135,16 +137,18 @@ SQLitePlugin::open = (success, error) -> onSuccess = () => success this - unless @dbname of @openDBs - @openDBs[@dbname] = true - cordova.exec onSuccess, error, "SQLitePlugin", "open", [ @openargs ] - else + + if @dbname of @openDBs ### for a re-open run onSuccess async so that the openDatabase return value can be used in the success handler as an alternative to the handler's db argument ### nextTick () -> onSuccess(); + else + @openDBs[@dbname] = true + cordova.exec onSuccess, error, "SQLitePlugin", "open", [ @openargs ] + return SQLitePlugin::close = (success, error) -> @@ -152,7 +156,7 @@ if @dbname of @openDBs if txLocks[@dbname] && txLocks[@dbname].inProgress - error(new Error('database cannot be closed while a transaction is in progress')) + error newSQLError 'database cannot be closed while a transaction is in progress' return delete @openDBs[@dbname] @@ -185,7 +189,7 @@ prevents us from stalling our txQueue if somebody passes a false value for fn. ### - throw new Error("transaction expected a function") + throw newSQLError "transaction expected a function" @db = db @fn = fn @@ -197,7 +201,7 @@ if txlock @executeSql "BEGIN", [], null, (tx, err) -> - throw toSQLError("unable to begin transaction: " + err.message, err.code) + throw newSQLError "unable to begin transaction: " + err.message, err.code return @@ -212,7 +216,7 @@ txLocks[@db.dbname].inProgress = false @db.startNextTransaction() if @error - @error toSQLError(err) + @error newSQLError err return SQLitePluginTransaction::executeSql = (sql, values, success, error) -> @@ -255,9 +259,9 @@ SQLitePluginTransaction::handleStatementFailure = (handler, response) -> if !handler - throw toSQLError("a statement with no error handler failed: " + response.message, response.code) + throw newSQLError "a statement with no error handler failed: " + response.message, response.code if handler(this, response) isnt false - throw toSQLError("a statement error callback did not return false: " + response.message, response.code) + throw newSQLError "a statement error callback did not return false: " + response.message, response.code return SQLitePluginTransaction::run = -> @@ -275,9 +279,10 @@ if didSucceed tx.handleStatementSuccess batchExecutes[index].success, response else - tx.handleStatementFailure batchExecutes[index].error, toSQLError(response) + tx.handleStatementFailure batchExecutes[index].error, newSQLError(response) catch err - txFailure = toSQLError(err) unless txFailure + if !txFailure + txFailure = newSQLError(err) if --waiting == 0 if txFailure @@ -346,7 +351,7 @@ failed = (tx, err) -> txLocks[tx.db.dbname].inProgress = false tx.db.startNextTransaction() - if tx.error then tx.error toSQLError("error while trying to roll back: " + err.message, err.code) + if tx.error then tx.error newSQLError("error while trying to roll back: " + err.message, err.code) return @finalized = true @@ -372,7 +377,7 @@ failed = (tx, err) -> txLocks[tx.db.dbname].inProgress = false tx.db.startNextTransaction() - if tx.error then tx.error toSQLError("error while trying to commit: " + err.message, err.code) + if tx.error then tx.error newSQLError("error while trying to commit: " + err.message, err.code) return @finalized = true @@ -436,7 +441,7 @@ else #console.log "delete db args: #{JSON.stringify first}" - if !(first and first['name']) then throw new Error("Please specify db name") + if !(first and first['name']) then throw new Error "Please specify db name" args.path = first.name dblocation = if !!first.location then dblocations[first.location] else null args.dblocation = dblocation || dblocations[0] diff --git a/www/SQLitePlugin.js b/www/SQLitePlugin.js index 9f23e6244..6a3a20ab1 100644 --- a/www/SQLitePlugin.js +++ b/www/SQLitePlugin.js @@ -1,5 +1,5 @@ (function() { - var READ_ONLY_REGEX, SQLiteFactory, SQLitePlugin, SQLitePluginTransaction, argsArray, dblocations, nextTick, root, toSQLError, txLocks; + var READ_ONLY_REGEX, SQLiteFactory, SQLitePlugin, SQLitePluginTransaction, argsArray, dblocations, newSQLError, nextTick, root, txLocks; root = this; @@ -7,7 +7,7 @@ txLocks = {}; - toSQLError = function(error, code) { + newSQLError = function(error, code) { var sqlError; sqlError = error; if (!code) { @@ -62,7 +62,7 @@ var dbname; console.log("SQLitePlugin openargs: " + (JSON.stringify(openargs))); if (!(openargs && openargs['name'])) { - throw new Error("Cannot create a SQLitePlugin instance without a db name"); + throw newSQLError("Cannot create a SQLitePlugin db instance without a db name"); } dbname = openargs.name; this.openargs = openargs; @@ -97,7 +97,7 @@ SQLitePlugin.prototype.transaction = function(fn, error, success) { if (!this.openDBs[this.dbname]) { - error(toSQLError('database not open')); + error(newSQLError('database not open')); return; } this.addTransaction(new SQLitePluginTransaction(this, fn, error, success, true, false)); @@ -105,7 +105,7 @@ SQLitePlugin.prototype.readTransaction = function(fn, error, success) { if (!this.openDBs[this.dbname]) { - error(toSQLError('database not open')); + error(newSQLError('database not open')); return; } this.addTransaction(new SQLitePluginTransaction(this, fn, error, success, true, true)); @@ -131,10 +131,7 @@ return success(_this); }; })(this); - if (!(this.dbname in this.openDBs)) { - this.openDBs[this.dbname] = true; - cordova.exec(onSuccess, error, "SQLitePlugin", "open", [this.openargs]); - } else { + if (this.dbname in this.openDBs) { /* for a re-open run onSuccess async so that the openDatabase return value @@ -144,13 +141,16 @@ nextTick(function() { return onSuccess(); }); + } else { + this.openDBs[this.dbname] = true; + cordova.exec(onSuccess, error, "SQLitePlugin", "open", [this.openargs]); } }; SQLitePlugin.prototype.close = function(success, error) { if (this.dbname in this.openDBs) { if (txLocks[this.dbname] && txLocks[this.dbname].inProgress) { - error(new Error('database cannot be closed while a transaction is in progress')); + error(newSQLError('database cannot be closed while a transaction is in progress')); return; } delete this.openDBs[this.dbname]; @@ -194,7 +194,7 @@ prevents us from stalling our txQueue if somebody passes a false value for fn. */ - throw new Error("transaction expected a function"); + throw newSQLError("transaction expected a function"); } this.db = db; this.fn = fn; @@ -205,7 +205,7 @@ this.executes = []; if (txlock) { this.executeSql("BEGIN", [], null, function(tx, err) { - throw toSQLError("unable to begin transaction: " + err.message, err.code); + throw newSQLError("unable to begin transaction: " + err.message, err.code); }); } }; @@ -224,7 +224,7 @@ txLocks[this.db.dbname].inProgress = false; this.db.startNextTransaction(); if (this.error) { - this.error(toSQLError(err)); + this.error(newSQLError(err)); } } }; @@ -268,10 +268,10 @@ SQLitePluginTransaction.prototype.handleStatementFailure = function(handler, response) { if (!handler) { - throw toSQLError("a statement with no error handler failed: " + response.message, response.code); + throw newSQLError("a statement with no error handler failed: " + response.message, response.code); } if (handler(this, response) !== false) { - throw toSQLError("a statement error callback did not return false: " + response.message, response.code); + throw newSQLError("a statement error callback did not return false: " + response.message, response.code); } }; @@ -290,12 +290,12 @@ if (didSucceed) { tx.handleStatementSuccess(batchExecutes[index].success, response); } else { - tx.handleStatementFailure(batchExecutes[index].error, toSQLError(response)); + tx.handleStatementFailure(batchExecutes[index].error, newSQLError(response)); } } catch (_error) { err = _error; if (!txFailure) { - txFailure = toSQLError(err); + txFailure = newSQLError(err); } } if (--waiting === 0) { @@ -372,7 +372,7 @@ txLocks[tx.db.dbname].inProgress = false; tx.db.startNextTransaction(); if (tx.error) { - tx.error(toSQLError("error while trying to roll back: " + err.message, err.code)); + tx.error(newSQLError("error while trying to roll back: " + err.message, err.code)); } }; this.finalized = true; @@ -401,7 +401,7 @@ txLocks[tx.db.dbname].inProgress = false; tx.db.startNextTransaction(); if (tx.error) { - tx.error(toSQLError("error while trying to commit: " + err.message, err.code)); + tx.error(newSQLError("error while trying to commit: " + err.message, err.code)); } }; this.finalized = true;