Skip to content

Commit

Permalink
Rename toSQLError to newSQLError in SQLitePlugin.coffee.md (ref: PR #170
Browse files Browse the repository at this point in the history
) & other minor cleanups
  • Loading branch information
Chris Brody committed Feb 28, 2015
1 parent 3567f47 commit e07a74a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
55 changes: 30 additions & 25 deletions SQLitePlugin.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -135,24 +137,26 @@
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) ->
#console.log "SQLitePlugin.prototype.close"
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]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) ->
Expand Down Expand Up @@ -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 = ->
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
38 changes: 19 additions & 19 deletions www/SQLitePlugin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(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;

READ_ONLY_REGEX = /^\s*(?:drop|delete|insert|update|create)\s/i;

txLocks = {};

toSQLError = function(error, code) {
newSQLError = function(error, code) {
var sqlError;
sqlError = error;
if (!code) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -97,15 +97,15 @@

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));
};

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));
Expand All @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -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;
Expand All @@ -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);
});
}
};
Expand All @@ -224,7 +224,7 @@
txLocks[this.db.dbname].inProgress = false;
this.db.startNextTransaction();
if (this.error) {
this.error(toSQLError(err));
this.error(newSQLError(err));
}
}
};
Expand Down Expand Up @@ -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);
}
};

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e07a74a

Please sign in to comment.