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 | angular.module('focaSqlite', []) | 1 | angular.module('focaSqlite', []) |
| 2 | .factory('focaSqliteService', [ | 2 | .factory('focaSqliteService', [ |
| 3 | function() { | 3 | function() { |
| 4 | var dataBase; | ||
| 5 | var baseReady; | ||
| 4 | function error(error) { | 6 | function error(error) { |
| 5 | console.error(error); | 7 | console.error(error); |
| 6 | } | 8 | } |
| 7 | return { | 9 | return { |
| 8 | openDataBase: function() { | 10 | openDataBase: function() { |
| 9 | var db = null; | 11 | var db = null; |
| 10 | db = window.sqlitePlugin.openDatabase({ | 12 | db = window.sqlitePlugin.openDatabase({ |
| 11 | name: 'foca.distribuidor', | 13 | name: 'foca.distribuidor', |
| 12 | location: 'default', | 14 | location: 'default', |
| 13 | }); | 15 | }); |
| 14 | this.db = db; | 16 | dataBase = db; |
| 15 | var queryCreate = "CREATE TABLE IF NOT EXISTS querys " + | 17 | var queryCreate = "CREATE TABLE IF NOT EXISTS querys " + |
| 16 | "(route text," + | 18 | "(route text," + |
| 17 | "type text," + | 19 | "type text," + |
| 18 | "body text," + | 20 | "body text," + |
| 19 | "response text," + | 21 | "response text," + |
| 20 | "PRIMARY KEY(route, body, type))"; | 22 | "PRIMARY KEY(route, body, type))"; |
| 21 | this.db.transaction(function(tx) { | 23 | dataBase.transaction(function(tx) { |
| 22 | tx.executeSql(queryCreate); | 24 | tx.executeSql(queryCreate); |
| 25 | baseReady = true; | ||
| 23 | }, error); | 26 | }, error); |
| 24 | }, | 27 | }, |
| 25 | updateQuery: function(route, body, type, response, success) { | 28 | updateQuery: function(route, body, type, response, success) { |
| 26 | var query = 'INSERT OR REPLACE INTO querys (route, type, body, response)' + | 29 | |
| 27 | "VALUES('" + route + "'" + | 30 | function callQuery() { |
| 31 | if (baseReady) { | ||
| 32 | |||
| 33 | var query = 'INSERT OR REPLACE INTO querys (route, type, body, response)' + | ||
| 34 | "VALUES('" + route + "'" + | ||
| 28 | ", '" + type + "'" + | 35 | ", '" + type + "'" + |
| 29 | ", '" + body + "'" + | 36 | ", '" + body + "'" + |
| 30 | ", '" + response + "')"; | 37 | ", '" + response + "')"; |
| 31 | this.db.transaction(function(tx) { | 38 | |
| 32 | tx.executeSql(query); | 39 | dataBase.transaction(function(tx) { |
| 33 | }, error, success) | 40 | tx.executeSql(query); |
| 41 | }, error, success) | ||
| 42 | } else { | ||
| 43 | setTimeout(function() { | ||
| 44 | |||
| 45 | callQuery(); | ||
| 46 | }, 1000) | ||
| 47 | } | ||
| 48 | } | ||
| 49 | callQuery(); | ||
| 34 | }, | 50 | }, |
| 35 | getQuery: function(route, body, type, success) { | 51 | getQuery: function(route, body, type, success) { |
| 36 | var query= "SELECT * FROM querys " + | 52 | function callQuery() { |
| 37 | "WHERE route = '" + route + "'" + | 53 | |
| 38 | " and body = '" + body + "'" + | 54 | if(baseReady) { |
| 39 | " and type = '" + type + "'"; | 55 | |
| 40 | this.db.executeSql(query, [], function(data) { | 56 | var query= "SELECT * FROM querys " + |
| 41 | success(data.rows.item(0).response); | 57 | "WHERE route = '" + route + "'" + |
| 42 | }, error); | 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 | } |
| 46 | ]); | 73 | ]); |
| 47 | 74 |