2012-04-07 23:08:44 -04:00
( function ( ) {
2012-04-08 10:19:39 -04:00
var root ;
root = this ;
root . SQLitePlugin = ( function ( ) {
2012-04-07 23:08:44 -04:00
console . log ( "root.SQLitePlugin" ) ;
SQLitePlugin . prototype . openDBs = { } ;
2012-04-08 10:19:39 -04:00
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 ) ;
2012-04-07 23:08:44 -04:00
}
2012-04-08 10:19:39 -04:00
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 ) ;
2012-04-07 23:08:44 -04:00
} ;
2012-04-08 10:19:39 -04:00
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 ] ) ;
}
2012-04-07 23:08:44 -04:00
} ;
2012-04-08 10:19:39 -04:00
SQLitePlugin . prototype . close = function ( success , error )
{
2012-04-07 23:08:44 -04:00
console . log ( "SQLitePlugin.prototype.close" ) ;
2012-04-08 10:19:39 -04:00
var opts ;
if ( this . dbPath in this . openDBs ) {
delete this . openDBs [ this . dbPath ] ;
PhoneGap . exec ( null , null , "SQLitePlugin" , "close" , [ this . dbPath ] ) ;
}
2012-04-07 23:08:44 -04:00
} ;
return SQLitePlugin ;
} ) ( ) ;
2012-04-08 10:19:39 -04:00
get _unique _id = function ( )
2012-04-07 23:08:44 -04:00
{
2012-04-08 10:19:39 -04:00
var id = new Date ( ) . getTime ( ) ;
var id2 = new Date ( ) . getTime ( ) ;
while ( id === id2 )
{
id2 = new Date ( ) . getTime ( ) ;
}
return id2 + '000' ;
2012-04-07 23:08:44 -04:00
}
2012-04-08 10:19:39 -04:00
transaction _queue = [ ] ;
transaction _callback _queue = new Object ( ) ;
root . SQLitePluginTransaction = ( function ( ) {
console . log ( "root.SQLitePluginTransaction" ) ;
function SQLitePluginTransaction ( dbPath )
{
2012-04-07 23:08:44 -04:00
console . log ( "root.SQLitePluginTransaction.SQLitePluginTransaction" ) ;
2012-04-08 10:19:39 -04:00
this . dbPath = dbPath ;
this . executes = [ ] ;
this . trans _id = get _unique _id ( ) ;
2012-04-09 13:40:43 -04:00
this . _ _completed = false ;
this . _ _submitted = false ;
2012-04-10 21:55:58 -04:00
this . optimization _no _nested _callbacks = true ; //if set to true large batches of queries within a transaction will be much faster but you lose the ability to do muiti level nesting of executeSQL callbacks
2012-04-08 10:19:39 -04:00
console . log ( "root.SQLitePluginTransaction - this.trans_id:" + this . trans _id ) ;
transaction _queue [ this . trans _id ] = [ ] ;
transaction _callback _queue [ this . trans _id ] = new Object ( ) ;
}
2012-04-07 23:08:44 -04:00
SQLitePluginTransaction . queryCompleteCallback = function ( transId , queryId , result )
{
2012-04-09 13:40:43 -04:00
console . log ( "SQLitePluginTransaction.queryCompleteCallback" ) ;
2012-04-07 23:08:44 -04:00
var query = null ;
for ( var x in transaction _queue [ transId ] )
{
if ( transaction _queue [ transId ] [ x ] [ 'query_id' ] == queryId )
{
query = transaction _queue [ transId ] [ x ] ;
2012-04-09 13:40:43 -04:00
if ( transaction _queue [ transId ] . length == 1 )
transaction _queue [ transId ] = [ ] ;
else
transaction _queue [ transId ] . splice ( x , 1 ) ;
2012-04-07 23:08:44 -04:00
break ;
}
}
2012-04-09 13:40:43 -04:00
// if(query)
// console.log("SQLitePluginTransaction.completeCallback---query:"+query['query']);
2012-04-08 10:19:39 -04:00
if ( query && query [ 'callback' ] )
2012-04-09 13:40:43 -04:00
{
2012-04-07 23:08:44 -04:00
query [ 'callback' ] ( result )
2012-04-09 13:40:43 -04:00
}
2012-04-07 23:08:44 -04:00
}
2012-04-08 10:19:39 -04:00
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 ] ;
2012-04-09 13:40:43 -04:00
if ( transaction _queue [ transId ] . length == 1 )
transaction _queue [ transId ] = [ ] ;
else
transaction _queue [ transId ] . splice ( x , 1 ) ;
2012-04-08 10:19:39 -04:00
break ;
}
}
2012-04-09 13:40:43 -04:00
//if(query)
// console.log("SQLitePluginTransaction.queryErrorCallback---query:"+query['query']);
2012-04-08 10:19:39 -04:00
if ( query && query [ 'err_callback' ] )
query [ 'err_callback' ] ( result )
}
2012-04-07 23:08:44 -04:00
SQLitePluginTransaction . txCompleteCallback = function ( transId )
{
if ( typeof transId != 'undefined' )
{
2012-04-08 12:02:24 -04:00
if ( transId && transaction _callback _queue [ transId ] && transaction _callback _queue [ transId ] [ 'success' ] )
2012-04-09 13:40:43 -04:00
{
2012-04-08 10:19:39 -04:00
transaction _callback _queue [ transId ] [ 'success' ] ( ) ;
2012-04-09 13:40:43 -04:00
}
2012-04-08 10:19:39 -04:00
2012-04-09 13:40:43 -04:00
// delete transaction_queue[transId];
// delete transaction_callback_queue[transId];
2012-04-07 23:08:44 -04:00
}
else
console . log ( "SQLitePluginTransaction.txCompleteCallback---transId = NULL" ) ;
}
2012-04-08 10:19:39 -04:00
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" ) ;
}
2012-04-07 23:08:44 -04:00
SQLitePluginTransaction . prototype . add _to _transaction = function ( trans _id , query , params , callback , err _callback )
{
var new _query = new Object ( ) ; ;
new _query [ 'trans_id' ] = trans _id ;
2012-04-09 22:53:37 -04:00
if ( callback || ! this . optimization _no _nested _callbacks )
2012-04-08 10:19:39 -04:00
new _query [ 'query_id' ] = get _unique _id ( ) ;
2012-04-09 22:53:37 -04:00
else if ( this . optimization _no _nested _callbacks )
new _query [ 'query_id' ] = "" ;
2012-04-07 23:08:44 -04:00
new _query [ 'query' ] = query ;
2012-04-09 13:40:43 -04:00
if ( params )
2012-04-08 21:51:06 -04:00
new _query [ 'params' ] = params ;
2012-04-09 13:40:43 -04:00
else
new _query [ 'params' ] = [ ] ;
2012-04-07 23:08:44 -04:00
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 ) ;
}
2012-04-08 10:19:39 -04:00
SQLitePluginTransaction . prototype . executeSql = function ( sql , values , success , error ) {
2012-04-07 23:08:44 -04:00
console . log ( "SQLitePluginTransaction.prototype.executeSql" ) ;
2012-04-08 10:19:39 -04:00
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 ) ;
} ;
}
2012-04-07 23:08:44 -04:00
this . add _to _transaction ( this . trans _id , sql , values , successcb , errorcb ) ;
console . log ( "executeSql - add_to_transaction" + sql ) ;
2012-04-08 10:19:39 -04:00
} ;
2012-04-07 23:08:44 -04:00
2012-04-08 10:19:39 -04:00
SQLitePluginTransaction . prototype . complete = function ( success , error ) {
2012-04-07 23:08:44 -04:00
console . log ( "SQLitePluginTransaction.prototype.complete" ) ;
2012-04-08 10:19:39 -04:00
var begin _opts , commit _opts , errorcb , executes , opts , successcb , txself ;
if ( this . _ _completed ) throw new Error ( "Transaction already run" ) ;
2012-04-09 13:40:43 -04:00
if ( this . _ _submitted ) throw new Error ( "Transaction already submitted" ) ;
this . _ _submitted = true ;
2012-04-08 10:19:39 -04:00
txself = this ;
2012-04-09 13:40:43 -04:00
successcb = function ( )
{
2012-04-09 22:53:37 -04:00
if ( transaction _queue [ txself . trans _id ] . length > 0 && ! txself . optimization _no _nested _callbacks )
2012-04-09 13:40:43 -04:00
{
txself . _ _submitted = false ;
txself . complete ( success , error ) ;
}
else
{
this . _ _completed = true ;
2012-04-09 17:11:26 -04:00
if ( success )
return success ( txself ) ;
2012-04-09 13:40:43 -04:00
}
} ;
2012-04-08 10:19:39 -04:00
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 ] ) ;
2012-04-07 23:08:44 -04:00
} ;
return SQLitePluginTransaction ;
} ) ( ) ;
2012-04-09 17:29:06 -04:00
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 ) ;
}
} ;
2012-04-07 23:08:44 -04:00
} ) . call ( this ) ;