@dpfavand I tried both alternatives, still getting the same problem, I think that is something wrong with my database.js could you look at my code and see if there is something odd with it?
'use strict';
angular.module('starter.database', [])
.factory('dbFactory', function ($q, $http, $rootScope, $ionicPlatform, Service, CordovaNetwork, onlineStatus) {
var deferred = $q.defer();
var dbFactory = {};
var db = window.openDatabase("rpmais", "1.0", "rpmais", 200000);
$ionicPlatform.ready(function () {
populateDB();
});
function log(message) {
console.log(message);
}
function populateDB() {
createTable();
CordovaNetwork.isOnline().then(function (isConnected) {
if (isConnected == true || onlineStatus.isOnline()) {
syncClientes('clientes/b3JkZW0obW9kaWZpY2Fkb1tERVNDXSk=');
syncPedidos('pedidos/b3JkZW0obW9kaWZpY2Fkb1tERVNDXSk=');
} else {
alert("Sem conexão com a internet");
}
}).catch(function (err) {
console.log(err);
});
}
function createTable() {
db.transaction(
function (tx) {
//TABELA CLIENTES
tx.executeSql("CREATE TABLE IF NOT EXISTS clientes (" +
"clientes_id Integer PRIMARY KEY AUTOINCREMENT, " +
"_criado Text, " +
"_modificado Text, " +
"_status Text, " +
"id_rm Integer, " +
"credencial_id Integer, " +
"informacao_adicional, " +
"nome, " +
"tipo, " +
"CONSTRAINT unique_clientes_id UNIQUE ('clientes_id'))");
tx.executeSql('CREATE INDEX IF NOT EXISTS "clientes.index_clientes_id" ON "clientes"("clientes_id");');
//TABELA PEDIDOS
tx.executeSql("CREATE TABLE IF NOT EXISTS pedidos (" +
"pedidos_id Integer PRIMARY KEY AUTOINCREMENT, " +
"_criado Text, " +
"_modificado Text, " +
"_status Text, " +
"id_rm Integer, " +
"id_rm_cliente Integer, " +
"id_rm_representada Integer, " +
"credencial_id Integer, " +
"data_emissao Text, " +
"enviado_para_representada Integer, " +
"nome_cliente Text, " +
"condicao_de_pagamento Text, " +
"nome_fantasia Text, " +
"razao_social_representada Text, " +
"status_pedido Text, " +
"valor_total REAL, " +
"valor_total_formatado Text, " +
"vendedor Text, " +
"numero_pedido Text, " +
"CONSTRAINT unique_pedidos_id UNIQUE ('pedidos_id'))");
tx.executeSql('CREATE INDEX IF NOT EXISTS "pedidos.index_pedidos_id" ON "pedidos"("pedidos_id");');
//TABELA VIAGENS
tx.executeSql("CREATE TABLE IF NOT EXISTS viagens (" +
"viagens_id Integer PRIMARY KEY AUTOINCREMENT, " +
"_criado Text, " +
"_modificado Text, " +
"_status Text, " +
"id_rm Integer, " +
"id_rm_cliente Integer, " +
"id_rm_empresa Integer, " +
"id_rm_credencial Integer, " +
"cod_id Text, " +
"id_usuario Integer, " +
"data_chegada Text, " +
"data_saida Text, " +
"credencial_nome Text, " +
"observacao Text, " +
"qtd_cliente_agendado Integer, " +
"status_da_viagem Text, " +
"CONSTRAINT unique_viagens_id UNIQUE ('viagens_id'))");
tx.executeSql('CREATE INDEX IF NOT EXISTS "viagens.index_viagens_id" ON "viagens"("viagens_id");');
},
txErrorHandler,
function () {
log('tables successfully created');
}
);
}
function dropTable() {
db.transaction(
function (tx) {
tx.executeSql('DROP TABLE IF EXISTS clientes');
},
txErrorHandler,
function () {
}
);
}
function getChanges(url, modifiedSince, callback) {
Service.getAll(url, modifiedSince).success(function (data) {
log("The server returned " + data.dados.length + " changes that occurred after " + modifiedSince);
callback(data.dados);
}).error(function (error) {
console.log(error);
});
}
var showClientes = function findAllClientes() {
db.transaction(
function(tx) {
var sql = "SELECT * FROM clientes";
tx.executeSql(sql, [],
function(tx, results) {
var len = results.rows.length,
clientes = [],
i = 0;
for (; i < len; i = i + 1) {
clientes[i] = results.rows.item(i);
}
log(i + ' clientes found');
deferred.resolve(clientes);
}
);
},txErrorHandler,
function () { }
);
return deferred.promise;
};
var showPedidos = function findAllPedidos() {
db.transaction(
function(tx) {
var sql = "SELECT * FROM pedidos";
tx.executeSql(sql, [],
function(tx, results) {
var tam = results.rows.length,
pedidos = [],
i = 0;
for (; i < tam; i = i + 1) {
pedidos[i] = results.rows.item(i);
}
log(i + ' pedidos found');
deferred.resolve(pedidos);
}
);
},txErrorHandler,
function () { }
);
return deferred.promise;
};
return {
showClientes: showClientes,
showPedidos: showPedidos
};
//SINCRONIZA CLIENTES
function getLastSyncClientes(param) {
db.transaction(
function (tx) {
var sql = "SELECT MAX(_modificado) as lastS FROM clientes";
tx.executeSql(sql, [],
function (tx, results) {
var lastSync = results.rows.item(0).lastS;
param(lastSync);
log('Last local timestamp is ' + lastSync);
}
);
},
txErrorHandler,
function () {
}
);
}
function applyClientesChanges(clientes) {
db.transaction(
function (tx) {
var l = clientes.length;
angular.forEach(clientes, function (c) {
var sql = 'UPDATE clientes SET nome = "' + c.nome + ' ",' +
'tipo = "' + c.tipo_pessoa + ' ",' +
'_criado = "' + c.criado + '",' +
'_modificado = "' + c.modificado + '",' +
'_status = "' + c.status + '",' +
'id_rm = "' + c.id + '",' +
'informacao_adicional = "' + c.informacoes_adicionais + '" WHERE id_rm = "' + c.id + '"';
tx.executeSql(sql);
log(sql + 'Synchronization complete (' + l + ' items synchronized)');
});
},
txErrorHandler,
function (tx) {
}
);
}
function getClientes(Service) {
Service.getAll('clientes').success(function (data) {
angular.forEach(data.dados, function (item) {
//debugger
db.transaction(
function (tx) {
tx.executeSql('INSERT OR REPLACE INTO clientes (nome, tipo, ' +
'_criado,' +
'_modificado , ' +
'_status, ' +
'id_rm, ' +
'informacao_adicional ) VALUES (?,?,?,?,?,?,?)',
[item.nome, item.tipo_pessoa, item.criado, item.modificado, item.status, item.id, item.informacoes_adicionais]);
},
txErrorHandler,
function () {
log('Record inserted successfully');
}
);
});
}).error(function (error) {
console.log(error);
});
}
function syncClientes(url) {
log('Starting synchronization...');
getLastSyncClientes(function (lastSync) {
if (lastSync == null) {
getClientes(Service);
} else {
getChanges(url, lastSync, function (changes) {
angular.forEach(changes, function (item) {
if (item.modificado > lastSync) {
applyClientesChanges(changes);
} else {
console.log('Nothing to synchronize');
}
});
}
);
}
});
}
//SINCRONIZA PRODUTOS
function getLastSyncPedidos(param) {
db.transaction(
function (tx) {
var sql = "SELECT MAX(_modificado) as lastS FROM pedidos";
tx.executeSql(sql, [],
function (tx, results) {
var lastSync = results.rows.item(0).lastS;
param(lastSync);
log('Last local timestamp is ' + lastSync);
}
);
},
txErrorHandler,
function () {
}
);
}
function applyPedidosChanges(pedidos) {
db.transaction(
function (tx) {
var l = pedidos.length;
angular.forEach(pedidos, function (p) {
var sql = 'UPDATE pedidos SET numero_pedido = "' + p.numero_pedido + ' ",' +
'vendedor = "' + p.vendedor + ' ",' +
'_criado = "' + p.criado + '",' +
'_modificado = "' + p.modificado + '",' +
'_status = "' + p.status + '",' +
'id_rm = "' + p.id + '",' +
'id_rm_cliente = "' + p.id_rm_cliente + '",' +
'id_rm_representada = "' + p.id_rm_representada + '",' +
'data_emissao = "' + p.data_emissao + '",' +
'enviado_para_representada = "' + p.enviado_para_representada + '",' +
'nome_cliente = "' + p.nome_cliente + '",' +
'condicao_de_pagamento = "' + p.condicao_de_pagamento + '",' +
'nome_fantasia = "' + p.nome_fantasia + '",' +
'razao_social_representada = "' + p.razao_social_representada + '",' +
'status_pedido = "' + p.status_pedido + '",' +
'valor_total = "' + p.valor_total + '",' +
'valor_total_formatado = "' + p.valor_total_formatado + '",' + '" WHERE id_rm = "' + p.id + '"';
tx.executeSql(sql);
log(sql + 'Synchronization complete (' + l + ' items synchronized)');
});
},
txErrorHandler,
function (tx) {
}
);
}
function getPedidos(Service) {
Service.getAll('pedidos').success(function (data) {
angular.forEach(data.dados, function (item) {
//debugger
db.transaction(
function (tx) {
tx.executeSql('INSERT OR REPLACE INTO pedidos (nome_cliente, nome_fantasia, ' +
'_criado,' +
'_modificado , ' +
'_status, ' +
'id_rm, ' +
'id_rm_representada, ' +
'data_emissao, ' +
'enviado_para_representada, ' +
'condicao_de_pagamento, ' +
'razao_social_representada, ' +
'status_pedido, ' +
'valor_total, ' +
'valor_total_formatado, ' +
'vendedor, ' +
'numero_pedido ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
[item.nome_cliente,
item.nome_fantasia,
item.criado,
item.modificado,
item.status,
item.id,
item.id_rm_representada,
item.data_emissao,
item.enviado_para_representada,
item.condicao_de_pagamento,
item.razao_social_representada,
item.status_pedido,
item.valor_total,
item.valor_total_formatado,
item.vendedor,
item.numero_pedido]);
},
txErrorHandler,
function () {
log('Record inserted successfully');
}
);
});
}).error(function (error) {
console.log(error);
});
}
function syncPedidos(url) {
log('Starting synchronization...');
getLastSyncPedidos(function (lastSync) {
if (lastSync == null) {
getPedidos(Service);
} else {
getChanges(url, lastSync, function (changes) {
angular.forEach(changes, function (item) {
if (item.modificado > lastSync) {
applyPedidosChanges(changes);
} else {
console.log('Nothing to synchronize');
}
});
}
);
}
});
}
//SINCRONIZA VIAGENS
function txErrorHandler(tx) {
log(tx.message);
}
});