Commit 71a71362d2d6b2e7a6699c77a39cfef1c198a40b
1 parent
be366cb32f
Exists in
master
sqlite response
Showing
1 changed file
with
2 additions
and
2 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; | 4 | var dataBase; |
| 5 | var baseReady; | 5 | var baseReady; |
| 6 | function error(error) { | 6 | function error(error) { |
| 7 | console.error(error); | 7 | console.error(error); |
| 8 | } | 8 | } |
| 9 | return { | 9 | return { |
| 10 | openDataBase: function() { | 10 | openDataBase: function() { |
| 11 | var db = null; | 11 | var db = null; |
| 12 | db = window.sqlitePlugin.openDatabase({ | 12 | db = window.sqlitePlugin.openDatabase({ |
| 13 | name: 'foca.distribuidor', | 13 | name: 'foca.distribuidor', |
| 14 | location: 'default', | 14 | location: 'default', |
| 15 | }); | 15 | }); |
| 16 | dataBase = db; | 16 | dataBase = db; |
| 17 | var queryCreate = "CREATE TABLE IF NOT EXISTS querys " + | 17 | var queryCreate = "CREATE TABLE IF NOT EXISTS querys " + |
| 18 | "(route text," + | 18 | "(route text," + |
| 19 | "type text," + | 19 | "type text," + |
| 20 | "body text," + | 20 | "body text," + |
| 21 | "response text," + | 21 | "response text," + |
| 22 | "PRIMARY KEY(route, body, type))"; | 22 | "PRIMARY KEY(route, body, type))"; |
| 23 | dataBase.transaction(function(tx) { | 23 | dataBase.transaction(function(tx) { |
| 24 | tx.executeSql(queryCreate); | 24 | tx.executeSql(queryCreate); |
| 25 | baseReady = true; | 25 | baseReady = true; |
| 26 | }, error); | 26 | }, error); |
| 27 | }, | 27 | }, |
| 28 | updateQuery: function(route, body, type, response, success) { | 28 | updateQuery: function(route, body, type, response, success) { |
| 29 | 29 | ||
| 30 | function callQuery() { | 30 | function callQuery() { |
| 31 | if (baseReady) { | 31 | if (baseReady) { |
| 32 | 32 | ||
| 33 | var query = 'INSERT OR REPLACE INTO querys (route, type, body, response)' + | 33 | var query = 'INSERT OR REPLACE INTO querys (route, type, body, response)' + |
| 34 | "VALUES('" + route + "'" + | 34 | "VALUES('" + route + "'" + |
| 35 | ", '" + type + "'" + | 35 | ", '" + type + "'" + |
| 36 | ", '" + body + "'" + | 36 | ", '" + body + "'" + |
| 37 | ", '" + response + "')"; | 37 | ", '" + response + "')"; |
| 38 | 38 | ||
| 39 | dataBase.transaction(function(tx) { | 39 | dataBase.transaction(function(tx) { |
| 40 | tx.executeSql(query); | 40 | tx.executeSql(query); |
| 41 | }, error, success) | 41 | }, error, success) |
| 42 | } else { | 42 | } else { |
| 43 | setTimeout(function() { | 43 | setTimeout(function() { |
| 44 | 44 | ||
| 45 | callQuery(); | 45 | callQuery(); |
| 46 | }, 1000) | 46 | }, 1000) |
| 47 | } | 47 | } |
| 48 | } | 48 | } |
| 49 | callQuery(); | 49 | callQuery(); |
| 50 | }, | 50 | }, |
| 51 | getQuery: function(route, body, type, success) { | 51 | getQuery: function(route, body, type, success) { |
| 52 | function callQuery() { | 52 | function callQuery() { |
| 53 | 53 | ||
| 54 | if(baseReady) { | 54 | if (baseReady) { |
| 55 | 55 | ||
| 56 | var query= "SELECT * FROM querys " + | 56 | var query= "SELECT * FROM querys " + |
| 57 | "WHERE route = '" + route + "'" + | 57 | "WHERE route = '" + route + "'" + |
| 58 | " and body = '" + body + "'" + | 58 | " and body = '" + body + "'" + |
| 59 | " and type = '" + type + "'"; | 59 | " and type = '" + type + "'"; |
| 60 | dataBase.executeSql(query, [], function(data) { | 60 | dataBase.executeSql(query, [], function(data) { |
| 61 | success(data.rows.item(0).response); | 61 | success(data.rows.item(0)); |
| 62 | }, error); | 62 | }, error); |
| 63 | } else { | 63 | } else { |
| 64 | setTimeout(function() { | 64 | setTimeout(function() { |
| 65 | callQuery(); | 65 | callQuery(); |
| 66 | }, 1000); | 66 | }, 1000); |
| 67 | } | 67 | } |
| 68 | } | 68 | } |
| 69 | callQuery(); | 69 | callQuery(); |
| 70 | } | 70 | } |
| 71 | } | 71 | } |
| 72 | } | 72 | } |
| 73 | ]); | 73 | ]); |
| 74 | 74 |