Keep orig JS and coffee version for Android version

This commit is contained in:
Chris Brody 2012-04-22 07:32:46 +02:00
parent be688e8869
commit 4f2c50bcc5
2 changed files with 457 additions and 0 deletions

View File

@ -0,0 +1,193 @@
(->
SQLitePlugin = (dbPath, openSuccess, openError) ->
console.log "SQLitePlugin"
@dbPath = dbPath
@openSuccess = openSuccess
@openError = openError
throw new Error("Cannot create a SQLitePlugin instance without a dbPath") unless dbPath
@openSuccess or (@openSuccess = ->
console.log "DB opened: " + dbPath
)
@openError or (@openError = (e) ->
console.log e.message
)
@open @openSuccess, @openError
SQLitePluginTransaction = (dbPath) ->
console.log "root.SQLitePluginTransaction.SQLitePluginTransaction"
@dbPath = dbPath
@executes = []
@trans_id = get_unique_id()
@__completed = false
@__submitted = false
@optimization_no_nested_callbacks = true
console.log "root.SQLitePluginTransaction - this.trans_id:" + @trans_id
transaction_queue[@trans_id] = []
transaction_callback_queue[@trans_id] = new Object()
root = undefined
root = this
console.log "root.SQLitePlugin"
SQLitePlugin::openDBs = {}
SQLitePlugin::transaction = (fn, error, success) ->
console.log "SQLitePlugin.prototype.transaction"
t = undefined
t = new root.SQLitePluginTransaction(@dbPath)
fn t
t.complete success, error
SQLitePlugin::open = (success, error) ->
console.log "SQLitePlugin.prototype.open"
opts = undefined
unless @dbPath of @openDBs
@openDBs[@dbPath] = true
PhoneGap.exec success, error, "SQLitePlugin", "open", [ @dbPath ]
SQLitePlugin::close = (success, error) ->
console.log "SQLitePlugin.prototype.close"
opts = undefined
if @dbPath of @openDBs
delete @openDBs[@dbPath]
PhoneGap.exec null, null, "SQLitePlugin", "close", [ @dbPath ]
return SQLitePlugin
root.SQLitePlugin = SQLitePlugin
get_unique_id = ->
id = new Date().getTime()
id2 = new Date().getTime()
id2 = new Date().getTime() while id is id2
id2 + "000"
transaction_queue = []
transaction_callback_queue = new Object()
console.log "root.SQLitePluginTransaction"
SQLitePluginTransaction.queryCompleteCallback = (transId, queryId, result) ->
console.log "SQLitePluginTransaction.queryCompleteCallback"
query = null
for x of transaction_queue[transId]
if transaction_queue[transId][x]["query_id"] is queryId
query = transaction_queue[transId][x]
if transaction_queue[transId].length is 1
transaction_queue[transId] = []
else
transaction_queue[transId].splice x, 1
break
query["callback"] result if query and query["callback"]
SQLitePluginTransaction.queryErrorCallback = (transId, queryId, result) ->
query = null
for x of transaction_queue[transId]
if transaction_queue[transId][x]["query_id"] is queryId
query = transaction_queue[transId][x]
if transaction_queue[transId].length is 1
transaction_queue[transId] = []
else
transaction_queue[transId].splice x, 1
break
query["err_callback"] result if query and query["err_callback"]
SQLitePluginTransaction.txCompleteCallback = (transId) ->
unless typeof transId is "undefined"
transaction_callback_queue[transId]["success"]() if transId and transaction_callback_queue[transId] and transaction_callback_queue[transId]["success"]
else
console.log "SQLitePluginTransaction.txCompleteCallback---transId = NULL"
SQLitePluginTransaction.txErrorCallback = (transId, error) ->
unless typeof transId is "undefined"
console.log "SQLitePluginTransaction.txErrorCallback---transId:" + transId
transaction_callback_queue[transId]["error"] error if transId and transaction_callback_queue[transId]["error"]
delete transaction_queue[transId]
delete transaction_callback_queue[transId]
else
console.log "SQLitePluginTransaction.txErrorCallback---transId = NULL"
SQLitePluginTransaction::add_to_transaction = (trans_id, query, params, callback, err_callback) ->
new_query = new Object()
new_query["trans_id"] = trans_id
if callback or not @optimization_no_nested_callbacks
new_query["query_id"] = get_unique_id()
else new_query["query_id"] = "" if @optimization_no_nested_callbacks
new_query["query"] = query
if params
new_query["params"] = params
else
new_query["params"] = []
new_query["callback"] = callback
new_query["err_callback"] = err_callback
transaction_queue[trans_id] = [] unless transaction_queue[trans_id]
transaction_queue[trans_id].push new_query
SQLitePluginTransaction::executeSql = (sql, values, success, error) ->
console.log "SQLitePluginTransaction.prototype.executeSql"
errorcb = undefined
successcb = undefined
txself = undefined
txself = this
successcb = null
if success
console.log "success not null:" + sql
successcb = (execres) ->
console.log "executeSql callback:" + JSON.stringify(execres)
res = undefined
saveres = undefined
saveres = execres
res =
rows:
item: (i) ->
saveres[i]
length: saveres.length
rowsAffected: saveres.rowsAffected
insertId: saveres.insertId or null
success txself, res
else
console.log "success NULL:" + sql
errorcb = null
if error
errorcb = (res) ->
error txself, res
@add_to_transaction @trans_id, sql, values, successcb, errorcb
console.log "executeSql - add_to_transaction" + sql
SQLitePluginTransaction::complete = (success, error) ->
console.log "SQLitePluginTransaction.prototype.complete"
begin_opts = undefined
commit_opts = undefined
errorcb = undefined
executes = undefined
opts = undefined
successcb = undefined
txself = undefined
throw new Error("Transaction already run") if @__completed
throw new Error("Transaction already submitted") if @__submitted
@__submitted = true
txself = this
successcb = ->
if transaction_queue[txself.trans_id].length > 0 and not txself.optimization_no_nested_callbacks
txself.__submitted = false
txself.complete success, error
else
@__completed = true
success txself if success
errorcb = (res) ->
if error
errorcb = (res) ->
error txself, res
transaction_callback_queue[@trans_id]["success"] = successcb
transaction_callback_queue[@trans_id]["error"] = errorcb
PhoneGap.exec null, null, "SQLitePlugin", "executeSqlBatch", transaction_queue[@trans_id]
return SQLitePluginTransaction
root.SQLitePluginTransaction = SQLitePluginTransaction
root.sqlitePlugin = openDatabase: (dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) ->
version = null unless version?
displayName = null unless displayName?
estimatedSize = 0 unless estimatedSize?
creationCallback = null unless creationCallback?
errorCallback = null unless errorCallback?
new SQLitePlugin(dbPath, creationCallback, errorCallback)
).call this

View File

@ -0,0 +1,264 @@
(function() {
var root;
root = this;
//root.SQLitePlugin = (function() {
console.log("root.SQLitePlugin");
SQLitePlugin.prototype.openDBs = {};
function SQLitePlugin(dbPath, openSuccess, openError)
{
console.log("SQLitePlugin");
this.dbPath = dbPath;
this.openSuccess = openSuccess;
this.openError = openError;
if (!dbPath) {
throw new Error("Cannot create a SQLitePlugin 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);
}
SQLitePlugin.prototype.transaction = function(fn, error, success)
{
console.log("SQLitePlugin.prototype.transaction");
var t;
t = new root.SQLitePluginTransaction(this.dbPath);
fn(t);
return t.complete(success, error);
};
SQLitePlugin.prototype.open = function(success, error)
{
console.log("SQLitePlugin.prototype.open");
var opts;
if (!(this.dbPath in this.openDBs)) {
this.openDBs[this.dbPath] = true;
PhoneGap.exec(success, error, "SQLitePlugin", "open", [this.dbPath]);
}
};
SQLitePlugin.prototype.close = function(success, error)
{
console.log("SQLitePlugin.prototype.close");
var opts;
if (this.dbPath in this.openDBs) {
delete this.openDBs[this.dbPath];
PhoneGap.exec(null, null, "SQLitePlugin", "close", [this.dbPath]);
}
};
return SQLitePlugin;
//})();
root.SQLitePlugin = SQLitePlugin;
get_unique_id = function()
{
var id = new Date().getTime();
var id2 = new Date().getTime();
while(id === id2)
{
id2 = new Date().getTime();
}
return id2+'000';
}
transaction_queue = [];
transaction_callback_queue = new Object();
//root.SQLitePluginTransaction = (function() {
console.log("root.SQLitePluginTransaction");
function SQLitePluginTransaction(dbPath)
{
console.log("root.SQLitePluginTransaction.SQLitePluginTransaction");
this.dbPath = dbPath;
this.executes = [];
this.trans_id = get_unique_id();
this.__completed = false;
this.__submitted = false;
//this.optimization_no_nested_callbacks: default is true.
//if set to true large batches of queries within a transaction will be much faster but
//you will lose the ability to do multi level nesting of executeSQL callbacks
this.optimization_no_nested_callbacks = true;
console.log("root.SQLitePluginTransaction - this.trans_id:"+this.trans_id);
transaction_queue[this.trans_id] = [];
transaction_callback_queue[this.trans_id] = new Object();
}
SQLitePluginTransaction.queryCompleteCallback = function(transId, queryId, result)
{
console.log("SQLitePluginTransaction.queryCompleteCallback");
var query = null;
for (var 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;
}
}
// if(query)
// console.log("SQLitePluginTransaction.completeCallback---query:"+query['query']);
if(query && query['callback'])
{
query['callback'](result)
}
}
SQLitePluginTransaction.queryErrorCallback = function(transId, queryId, result)
{
var query = null;
for (var 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;
}
}
//if(query)
// console.log("SQLitePluginTransaction.queryErrorCallback---query:"+query['query']);
if(query && query['err_callback'])
query['err_callback'](result)
}
SQLitePluginTransaction.txCompleteCallback = function(transId)
{
if(typeof transId != 'undefined')
{
if(transId && transaction_callback_queue[transId] && transaction_callback_queue[transId]['success'])
{
transaction_callback_queue[transId]['success']();
}
// delete transaction_queue[transId];
// delete transaction_callback_queue[transId];
}
else
console.log("SQLitePluginTransaction.txCompleteCallback---transId = NULL");
}
SQLitePluginTransaction.txErrorCallback = function(transId, error)
{
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];
delete transaction_callback_queue[transId];
}
else
console.log("SQLitePluginTransaction.txErrorCallback---transId = NULL");
}
SQLitePluginTransaction.prototype.add_to_transaction = function(trans_id, query, params, callback, err_callback)
{
var 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);
}
SQLitePluginTransaction.prototype.executeSql = function(sql, values, success, error) {
console.log("SQLitePluginTransaction.prototype.executeSql");
var errorcb, successcb, txself;
txself = this;
successcb = null;
if (success)
{
console.log("success not null:"+sql);
successcb = function(execres)
{
console.log("executeSql callback:"+JSON.stringify(execres));
var res, saveres;
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);
};
}
this.add_to_transaction(this.trans_id, sql, values, successcb, errorcb);
console.log("executeSql - add_to_transaction"+sql);
};
SQLitePluginTransaction.prototype.complete = function(success, error) {
console.log("SQLitePluginTransaction.prototype.complete");
var begin_opts, commit_opts, errorcb, executes, opts, successcb, txself;
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;
txself.complete(success, error);
}
else
{
this.__completed = true;
if(success)
return success(txself);
}
};
errorcb = function(res) {};
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;
PhoneGap.exec(null, null, "SQLitePlugin", "executeSqlBatch", transaction_queue[this.trans_id]);
};
return SQLitePluginTransaction;
//})();
root.SQLitePluginTransaction = SQLitePluginTransaction;
root.sqlitePlugin = {
openDatabase: function(dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) {
if (version == null) version = null;
if (displayName == null) displayName = null;
if (estimatedSize == null) estimatedSize = 0;
if (creationCallback == null) creationCallback = null;
if (errorCallback == null) errorCallback = null;
return new SQLitePlugin(dbPath, creationCallback, errorCallback);
}
};
}).call(this);