1 // Generated by CoffeeScript 1.3.3
4 Readings.Catalogue = (function() {
6 function Catalogue() {}
8 Catalogue.prototype.open = function() {
10 version = Readings.config.get('db_version');
11 console.log("opening db ver " + version);
12 this.db = openDatabase('catalogue', version, 'Catalogue', 65535, this.init);
16 Catalogue.prototype.init = function(db) {
21 console.log("initializing DB");
22 db.changeVersion("", Readings.config.get('db_version'));
23 return $.ajax("initdb.sql", {
26 success: function(sql) {
28 sql = sql.split(/;\n/);
29 sql = sql.slice(1, -2);
30 create = function(tx) {
31 var stmt, _i, _len, _results;
33 for (_i = 0, _len = sql.length; _i < _len; _i++) {
36 _results.push(tx.executeSql(stmt, [], function(tx, rs) {
38 }, function(tx, err) {
39 return console.error(err);
44 return db.transaction(create);
47 return console.error("can't get initial sql");
52 Catalogue.prototype.wrap_error = function(error_cb) {
53 return function(tx, error) {
54 window.last_error = error;
55 alert("SQL ERROR: " + error.message);
57 return error_cb(error);
64 Catalogue.prototype.map_rs = function(rs, mapper) {
65 var i, objs, _i, _ref;
67 for (i = _i = 0, _ref = rs.rows.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
68 objs.push(mapper(rs.rows.item(i)));
73 Catalogue.prototype.withCategory = function(category, callback, error) {
76 rs_to_tags = function(tx, data) {
77 var i, tags, _i, _ref;
79 for (i = _i = 0, _ref = data.rows.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
80 tags.push(new Readings.Tag(data.rows.item(i)));
82 return callback(tags);
84 return this.db.readTransaction(function(tx) {
85 return tx.executeSql("SELECT * FROM tag WHERE category=? ORDER BY sort_key", [category], rs_to_tags, _this.wrap_error(error));
89 Catalogue.prototype.withTag = function(tag_id, cb) {
91 return this.db.readTransaction(function(tx) {
92 return tx.executeSql("SELECT * FROM tag WHERE id=?", [tag_id], function(tx, rs) {
94 if (rs.rows.length > 0) {
95 tag = new Readings.Tag(rs.rows.item(0));
98 return alert("No tag id " + tag_id);
100 }, _this.wrap_error());
104 Catalogue.prototype.withBooks = function(tag, cb) {
106 return this.db.readTransaction(function(tx) {
107 console.log("books in tag: " + tag.books);
108 return tx.executeSql("SELECT * FROM book WHERE id IN (" + tag.books + ") ORDER BY sort_key", [], (function(tx, rs) {
109 return cb(_this.map_rs(rs, function(rec) {
110 return new Readings.Book(rec);
112 }), _this.wrap_error());