Commit be366cb32f87f2eb0a1596a7ec7cc4f76a544428
1 parent
0220b68d98
Exists in
master
update version
Showing
1 changed file
with
41 additions
and
14 deletions
Show diff stats
src/js/app.js
| 1 | 1 | angular.module('focaSqlite', []) |
| 2 | 2 | .factory('focaSqliteService', [ |
| 3 | 3 | function() { |
| 4 | + var dataBase; | |
| 5 | + var baseReady; | |
| 4 | 6 | function error(error) { |
| 5 | 7 | console.error(error); |
| 6 | 8 | } |
| ... | ... | @@ -11,35 +13,60 @@ angular.module('focaSqlite', []) |
| 11 | 13 | name: 'foca.distribuidor', |
| 12 | 14 | location: 'default', |
| 13 | 15 | }); |
| 14 | - this.db = db; | |
| 16 | + dataBase = db; | |
| 15 | 17 | var queryCreate = "CREATE TABLE IF NOT EXISTS querys " + |
| 16 | 18 | "(route text," + |
| 17 | 19 | "type text," + |
| 18 | 20 | "body text," + |
| 19 | 21 | "response text," + |
| 20 | 22 | "PRIMARY KEY(route, body, type))"; |
| 21 | - this.db.transaction(function(tx) { | |
| 23 | + dataBase.transaction(function(tx) { | |
| 22 | 24 | tx.executeSql(queryCreate); |
| 25 | + baseReady = true; | |
| 23 | 26 | }, error); |
| 24 | 27 | }, |
| 25 | 28 | updateQuery: function(route, body, type, response, success) { |
| 26 | - var query = 'INSERT OR REPLACE INTO querys (route, type, body, response)' + | |
| 27 | - "VALUES('" + route + "'" + | |
| 29 | + | |
| 30 | + function callQuery() { | |
| 31 | + if (baseReady) { | |
| 32 | + | |
| 33 | + var query = 'INSERT OR REPLACE INTO querys (route, type, body, response)' + | |
| 34 | + "VALUES('" + route + "'" + | |
| 28 | 35 | ", '" + type + "'" + |
| 29 | 36 | ", '" + body + "'" + |
| 30 | 37 | ", '" + response + "')"; |
| 31 | - this.db.transaction(function(tx) { | |
| 32 | - tx.executeSql(query); | |
| 33 | - }, error, success) | |
| 38 | + | |
| 39 | + dataBase.transaction(function(tx) { | |
| 40 | + tx.executeSql(query); | |
| 41 | + }, error, success) | |
| 42 | + } else { | |
| 43 | + setTimeout(function() { | |
| 44 | + | |
| 45 | + callQuery(); | |
| 46 | + }, 1000) | |
| 47 | + } | |
| 48 | + } | |
| 49 | + callQuery(); | |
| 34 | 50 | }, |
| 35 | 51 | getQuery: function(route, body, type, success) { |
| 36 | - var query= "SELECT * FROM querys " + | |
| 37 | - "WHERE route = '" + route + "'" + | |
| 38 | - " and body = '" + body + "'" + | |
| 39 | - " and type = '" + type + "'"; | |
| 40 | - this.db.executeSql(query, [], function(data) { | |
| 41 | - success(data.rows.item(0).response); | |
| 42 | - }, error); | |
| 52 | + function callQuery() { | |
| 53 | + | |
| 54 | + if(baseReady) { | |
| 55 | + | |
| 56 | + var query= "SELECT * FROM querys " + | |
| 57 | + "WHERE route = '" + route + "'" + | |
| 58 | + " and body = '" + body + "'" + | |
| 59 | + " and type = '" + type + "'"; | |
| 60 | + dataBase.executeSql(query, [], function(data) { | |
| 61 | + success(data.rows.item(0).response); | |
| 62 | + }, error); | |
| 63 | + } else { | |
| 64 | + setTimeout(function() { | |
| 65 | + callQuery(); | |
| 66 | + }, 1000); | |
| 67 | + } | |
| 68 | + } | |
| 69 | + callQuery(); | |
| 43 | 70 | } |
| 44 | 71 | } |
| 45 | 72 | } |