- self.update = function(data, success, error) {
- var addBookSql = new Sql("\
- INSERT OR REPLACE INTO book \
- (id, title, html_file, html_file_size, parent, parent_number, sort_key, pretty_size, authors) \
- VALUES \
- ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}')");
- var addTagSql = new Sql("INSERT OR REPLACE INTO tag (id, category, name, sort_key, books) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}')");
-
- var sqls = [];
-
- if (data.deleted) {
- for (i in data.deleted.books) {
- var book_id = data.deleted.books[i];
- sqls.push("DELETE FROM book WHERE id=" + book_id);
- FileRepo.deleteIfExists(book_id);
- }
-
- for (i in data.deleted.tags) {
- var tag_id = data.deleted.tags[i];
- sqls.push("DELETE FROM tag WHERE id=" + tag_id);
- }
- }
-
- if (data.updated) {
- for (i in data.updated.books) {
- var book = data.updated.books[i];
- if (!book.html) book.html = {};
- if (!book.html.url) book.html.url = '';
- if (!book.html.size) book.html.size = '';
- if (!book.parent) book.parent = '';
- if (!book.parent_number) book.parent_number = '';
- var pretty_size = prettySize(book.html.size);
- sqls.push(addBookSql.prepare(
- book.id, book.title, book.html.url, book.html.size,
- book.parent, book.parent_number, book.sort_key, pretty_size, book.author
- ));
- FileRepo.deleteIfExists(book.id);
- }
-
- for (i in data.updated.tags) {
- var tag = data.updated.tags[i];
- var category = categories[tag.category];
- var books = tag.books.join(',');
- sqls.push(addTagSql.prepare(tag.id, category, tag.name, tag.sort_key, books));
- }
- }
-
- sqls.push("UPDATE state SET last_checked=" + data.time_checked);
-
- self.chainSqls(sqls, success, error);
- };
-
-
- this.sync = function(success, error) {
- self.withState(function(state) {
- var url = WL_UPDATE.replace("SINCE", state.last_checked);
- console.log('sync: ' + url);
- var xhr = new XMLHttpRequest();
- xhr.open("GET", url);
- xhr.onload = function() {
- console.log('sync: fetched by ajax: ' + url);
- self.update(JSON.parse(xhr.responseText), success, error);
- }
- xhr.onerror = function(e) {
- error && error("Błąd aktualizacji bazy danych." + e);
- }
- xhr.send();
- });
- };
-
- this.updateLocal = function() {
- FileRepo.withLocal(function(local) {
- self.db.transaction(function(tx) {
- tx.executeSql("UPDATE book SET _local=0", [], function(tx, results) {
- ll = local.length;
- var ids = [];
- for (var i = 0; i < ll; i ++) {
- ids.push(local[i].name);
- }
- ids = ids.join(',');
- tx.executeSql("UPDATE book SET _local=1 where id in ("+ids+")");
- });
- });
- }, function() {
- self.db.transaction(function(tx) {
- tx.executeSql("UPDATE book SET _local=0");
- });
- });
- };