diff --git a/src/js/app.js b/src/js/app.js index 9fe73b4..10c4e66 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -1,6 +1,8 @@ angular.module('focaSqlite', []) .factory('focaSqliteService', [ function() { + var dataBase; + var baseReady; function error(error) { console.error(error); } @@ -11,35 +13,60 @@ angular.module('focaSqlite', []) name: 'foca.distribuidor', location: 'default', }); - this.db = db; + dataBase = db; var queryCreate = "CREATE TABLE IF NOT EXISTS querys " + "(route text," + "type text," + "body text," + "response text," + "PRIMARY KEY(route, body, type))"; - this.db.transaction(function(tx) { + dataBase.transaction(function(tx) { tx.executeSql(queryCreate); + baseReady = true; }, error); }, updateQuery: function(route, body, type, response, success) { - var query = 'INSERT OR REPLACE INTO querys (route, type, body, response)' + - "VALUES('" + route + "'" + + + function callQuery() { + if (baseReady) { + + var query = 'INSERT OR REPLACE INTO querys (route, type, body, response)' + + "VALUES('" + route + "'" + ", '" + type + "'" + ", '" + body + "'" + ", '" + response + "')"; - this.db.transaction(function(tx) { - tx.executeSql(query); - }, error, success) + + dataBase.transaction(function(tx) { + tx.executeSql(query); + }, error, success) + } else { + setTimeout(function() { + + callQuery(); + }, 1000) + } + } + callQuery(); }, getQuery: function(route, body, type, success) { - var query= "SELECT * FROM querys " + - "WHERE route = '" + route + "'" + - " and body = '" + body + "'" + - " and type = '" + type + "'"; - this.db.executeSql(query, [], function(data) { - success(data.rows.item(0).response); - }, error); + function callQuery() { + + if(baseReady) { + + var query= "SELECT * FROM querys " + + "WHERE route = '" + route + "'" + + " and body = '" + body + "'" + + " and type = '" + type + "'"; + dataBase.executeSql(query, [], function(data) { + success(data.rows.item(0).response); + }, error); + } else { + setTimeout(function() { + callQuery(); + }, 1000); + } + } + callQuery(); } } }