X-Git-Url: https://git.mdrn.pl/ReadingsJQM.git/blobdiff_plain/45f8b33043d9aad64b1f89906323bc477efda0f3..refs/heads/readings:/js/app/Catalogue.js diff --git a/js/app/Catalogue.js b/js/app/Catalogue.js index 38fbce6..a1999bf 100644 --- a/js/app/Catalogue.js +++ b/js/app/Catalogue.js @@ -1,4 +1,3 @@ -// Generated by CoffeeScript 1.3.3 (function() { Readings.Catalogue = (function() { @@ -14,28 +13,30 @@ }; Catalogue.prototype.init = function(db) { - var _this = this; - if (!(db != null)) { - db = this.db; - } + var unneded_stmts, + _this = this; + unneded_stmts = function(stmt, idx) { + return stmt.indexOf("PRAGMA") === 0 || stmt.indexOf("BEGIN TRANSACTION") === 0 || stmt.indexOf("COMMIT") === 0 || /^\s*$/.exec(stmt); + }; + if (!(db != null)) db = this.db; console.log("initializing DB"); db.changeVersion("", Readings.config.get('db_version')); - return $.ajax("initdb.sql", { + return $.ajax(Readings.config.get('initdburl'), { type: "GET", dataType: 'text', success: function(sql) { var create; sql = sql.split(/;\n/); - sql = sql.slice(1, -2); + sql = $.grep(sql, unneded_stmts, true); create = function(tx) { var stmt, _i, _len, _results; _results = []; for (_i = 0, _len = sql.length; _i < _len; _i++) { stmt = sql[_i]; - console.log(stmt); _results.push(tx.executeSql(stmt, [], function(tx, rs) { return true; }, function(tx, err) { + console.error("error for " + stmt); return console.error(err); })); } @@ -62,9 +63,9 @@ }; Catalogue.prototype.map_rs = function(rs, mapper) { - var i, objs, _i, _ref; + var i, objs, _ref; objs = []; - for (i = _i = 0, _ref = rs.rows.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + for (i = 0, _ref = rs.rows.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) { objs.push(mapper(rs.rows.item(i))); } return objs; @@ -74,9 +75,9 @@ var rs_to_tags, _this = this; rs_to_tags = function(tx, data) { - var i, tags, _i, _ref; + var i, tags, _ref; tags = []; - for (i = _i = 0, _ref = data.rows.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + for (i = 0, _ref = data.rows.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) { tags.push(new Readings.Tag(data.rows.item(i))); } return callback(tags); @@ -113,6 +114,28 @@ }); }; + Catalogue.prototype.fetchBook = function(book_id, fetch_contents, cb) { + var _this = this; + if (!(typeof book_id === "number" || /^[0-9]+$/.exec(book_id))) { + throw Error("book_id = '" + book_id + "' is not a number"); + } + return this.db.readTransaction(function(tx) { + return tx.executeSql("SELECT * FROM book WHERE id=?", [book_id], function(tx, rs) { + var book; + if (rs.rows.length > 0) { + book = new Readings.Book(rs.rows.item(0)); + if (fetch_contents && !book.is_local()) { + return book.fetch(function(book) { + return cb(book); + }); + } else { + return cb(book); + } + } + }); + }); + }; + return Catalogue; })();