app.js 1.93 KB
angular.module('focaSqlite', [])
    .factory('focaSqliteService', [
        function() {
            function error(error) {
                console.error(error);
            }
            return {
                openDataBase: function() {
                    var db = null;
                    db = window.sqlitePlugin.openDatabase({
                        name: 'foca.distribuidor',
                        location: 'default',
                    });
                    this.db = 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) {
                        tx.executeSql(queryCreate);
                    }, error);
                },
                updateQuery: function(route, body, type, response, success) {
                    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)
                },
                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);
                }
            }
        }
    ]);