I'm trying to create a windows hybrid app, with offline functionality.
I was able to attain the Sqlite Database using following Plugins -
But they work fine untill I'm debugging the app using Visual Studio, but as I try to make it work completely disconnected, everything fails.
I have no idea, how to debug this as it needs to work in offline also.
I'm posting the service I created for Database operations.
app.service( 'StorageService', function( $rootScope, $q, $injector, INDEX_KEYS, localStorageService, logicModel, HttpPostService ){
document.addEventListener( 'deviceready', init, false);
var db;
function init(){
//db = openDatabase({ name: "bizom.db", location: 1 }, successcb, errorcb);
db = openDatabase("Bizom", "", "Bizom", 100, function () {
console.log('db successfully opened or created');
});
db.transaction(function(tx) {
var queryCreateQueue = 'CREATE TABLE IF NOT EXISTS queue ( id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT, sequence INTEGER, postdata TEXT, contentype TEXT, postType INTEGER, transactionId TEXT )';
// postType: 1 - postRequest, 2 - postRequestFORMDATA, 3 - sendMultipartFormdata
var queryCreateStorage = 'CREATE TABLE IF NOT EXISTS storage ( id INTEGER PRIMARY KEY AUTOINCREMENT, indexKey TEXT, data TEXT, type TEXT, UNIQUE( indexKey ) ON CONFLICT REPLACE )';
var queryDropQueue = 'DROP TABLE IF EXISTS queue';
var queryDropStorage = 'DROP TABLE IF EXISTS storage';
tx.executeSql( queryDropStorage, [], successcb, errorcb );
tx.executeSql(queryCreateStorage, [], function (tx, res) {
angular.forEach( INDEX_KEYS, function( value ){
var saveArr = [ value, '', '' ];
var query = "INSERT INTO storage ( indexKey, data, type ) VALUES ( ?, ?, ? )";
insert( query, saveArr );
});
}, errorcb);
tx.executeSql( queryDropQueue, [], successcb, errorcb );
tx.executeSql( queryCreateQueue, [], successcb, errorcb );
}, function( error ){
console.log( error.message );
});
function successcb( tx, res ){
console.log( res);
}
function errorcb( err ){
console.log("Error processing SQL: "+err.message );
}
};
function insert( query, saveArr ){
// db = window.sqlitePlugin.openDatabase("Bizom", "1.0", "Demo", -1);
db.transaction(function(tx) {
tx.executeSql( query, saveArr, function( tx, res ){
console.log( res );
}, function( err ){
console.log("Error processing SQL: "+err.message );
});
}, function( err ){
console.log("Error processing SQL: "+err.message );
});
};
function runquery( query ){
var deffered = $q.defer();
function querySuccess( tx, result ){
var tempData = [];
var len = result.rows.length;
for ( var i = 0; i < len; i++ ) {
tempData.push( result.rows.item(i) );
}
deffered.resolve( tempData );
};
db.transaction(function(tx) {
tx.executeSql( query, [], querySuccess, function( err ){
console.log("Error processing SQL: "+err.message );
deffered.reject();
});
}, function( err ){
console.log("Error processing SQL: "+err.message );
deffered.reject();
});
return deffered.promise;
};
function isIntFloat(n){
if( Number(n) === n && n % 1 === 0 ){
return 'int';
} else {
return 'float';
}
}
function deleteFromQueue( transactionId ){
var query = "DELETE FROM queue WHERE transactionId = '"+transactionId+"'";
runquery( query );
}
var storageFunctions = {
set : function( key, data ){
var saveData = null;
var type = '';
if( typeof data == 'object' ){
saveData = JSON.stringify( data );
type = 'object';
} else if( typeof data == 'number' ){
saveData = '' + data;
type = isIntFloat( data );
} else {
saveData = data;
type = 'string';
}
// var saveArr = [ key, saveData, type ];
var query = "UPDATE storage SET data = '" + saveData + "',type = '" + type +"' where indexKey = '" + key + "'";
var result = insert( query, [] );
return result;
},
get : function( keys ){
var deffered = $q.defer();
var result = {};
var condition = '';
if( typeof keys == 'object' ){
angular.forEach( keys, function( key ){
condition += "'"+key+"',";
});
condition = condition.slice(0, -1);
}
//var query = 'SELECT * FROM storage';
var query = "SELECT * FROM storage WHERE indexKey IN (" + condition + ")";
runquery( query, [] ).then(function( temp ){
var len = temp.length;
for( var i = 0; i < len; i++ ){
var key = temp[i].indexKey;
if( temp[i].type == 'object' ){
result[ key ] = JSON.parse( temp[i].data );
} else if( temp[i].type == 'int' ){
result[ key ] = parseInt( temp[i].data );
} else if( temp[i].type == 'float' ){
result[ key ] = parseFloat( temp[i].data );
} else {
result[ key ] = temp[i].data;
}
}
deffered.resolve( result );
}, function( error ){
console.log( query );
console.log( error );
});
return deffered.promise;
},
backgroundPostStep : function(){
var AuthData = localStorageService.get('AuthData');
var query = "SELECT * FROM queue";
runquery( query ).then(function( data ){
angular.forEach( data, function( value, key ){
var postData = JSON.parse( value.postdata );
var postType = value.postType;
var url = value.url;
var contentype = value.contentype;
var transactionId = value.transactionId;
if( postType == 1 ){
HttpPostService.postRequest( url, postData, contentype ).then(function( response ){
if( response.Result == 'true' ){
deleteFromQueue( transactionId );
} else {
console.log('Failed To Submit Data!');
}
}, function(){
console.log('Failed To Submit Data!');
});
} else if ( postType == 2 ){
HttpPostService.postRequestFORMDATA( url, postData, contentype ).then(function( response ){
if( response.Result == 'true' ){
deleteFromQueue( transactionId );
} else {
console.log('Failed To Submit Data!');
}
}, function(){
console.log('Failed To Submit Data!');
});
} else {
var formData = new FormData();
angular.forEach( postData, function( val, key){
if( typeof val == 'object' && 'blob' in val && 'imageName' in val ){
formData.append( key, val.blob, val.imageName );
} else {
formData.append( key, val );
}
});
HttpPostService.sendMultipartFormdata( formData, url ).then(function( response ){
if( response.Result == 'true' ){
deleteFromQueue( transactionId );
} else {
console.log('Failed To Submit Data!');
}
}, function(){
console.log('Failed To Submit Data!');
});
}
});
});
},
savePostData : function( url, postdata, contentType, postType, transactionId ){
var deffered = $q.defer();
var lastSequence = localStorageService.get('lastSequence');
var sequence = 1;
transactionId = ''+transactionId;
if( lastSequence != null ){
sequence = parseInt( lastSequence ) + 1;
}
var data = JSON.stringify( postdata );
var saveArr = [ url, sequence, data, contentType, postType, transactionId ];
var query = "INSERT INTO queue ( url, sequence, postdata, contentype, postType, transactionId ) VALUES ( ?, ?, ?, ?, ?, ? )";
insert( query, saveArr );
deffered.resolve(true);
return deffered.promise;
},
updatePostData : function( response, transactionId, updateKey ){
var query = "SELECT * FROM queue WHERE transactionId = '"+transactionId+"'";
runquery( query ).then(function( data ){
if( typeof data == 'object' && data[0].postdata != null ){
var func = logicModel[updateKey];
var tmpData = JSON.parse( data[0].postdata );
tmpData = func(tmpData, response);console.log(tmpData);
tmpData = JSON.stringify( tmpData );
var query = "UPDATE queue SET postdata = '" + tmpData + "' where transactionId = '" + transactionId + "'";
var result = insert( query, [] );
}
}, function( error ){
console.log( error );
});
},
clearAll : function(){
runquery('DELETE FROM queue');
insert("UPDATE storage SET data = '',type = ''");
}
};
return storageFunctions;
});
Aucun commentaire:
Enregistrer un commentaire