38fbce65ec3a625395a568bbacc7db6b6d17ca09
[ReadingsJQM.git] / js / app / Catalogue.js
1 // Generated by CoffeeScript 1.3.3
2 (function() {
3
4   Readings.Catalogue = (function() {
5
6     function Catalogue() {}
7
8     Catalogue.prototype.open = function() {
9       var version;
10       version = Readings.config.get('db_version');
11       console.log("opening db ver " + version);
12       this.db = openDatabase('catalogue', version, 'Catalogue', 65535, this.init);
13       return this;
14     };
15
16     Catalogue.prototype.init = function(db) {
17       var _this = this;
18       if (!(db != null)) {
19         db = this.db;
20       }
21       console.log("initializing DB");
22       db.changeVersion("", Readings.config.get('db_version'));
23       return $.ajax("initdb.sql", {
24         type: "GET",
25         dataType: 'text',
26         success: function(sql) {
27           var create;
28           sql = sql.split(/;\n/);
29           sql = sql.slice(1, -2);
30           create = function(tx) {
31             var stmt, _i, _len, _results;
32             _results = [];
33             for (_i = 0, _len = sql.length; _i < _len; _i++) {
34               stmt = sql[_i];
35               console.log(stmt);
36               _results.push(tx.executeSql(stmt, [], function(tx, rs) {
37                 return true;
38               }, function(tx, err) {
39                 return console.error(err);
40               }));
41             }
42             return _results;
43           };
44           return db.transaction(create);
45         },
46         error: function() {
47           return console.error("can't get initial sql");
48         }
49       });
50     };
51
52     Catalogue.prototype.wrap_error = function(error_cb) {
53       return function(tx, error) {
54         window.last_error = error;
55         alert("SQL ERROR: " + error.message);
56         if (error_cb) {
57           return error_cb(error);
58         } else {
59           throw error;
60         }
61       };
62     };
63
64     Catalogue.prototype.map_rs = function(rs, mapper) {
65       var i, objs, _i, _ref;
66       objs = [];
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)));
69       }
70       return objs;
71     };
72
73     Catalogue.prototype.withCategory = function(category, callback, error) {
74       var rs_to_tags,
75         _this = this;
76       rs_to_tags = function(tx, data) {
77         var i, tags, _i, _ref;
78         tags = [];
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)));
81         }
82         return callback(tags);
83       };
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));
86       });
87     };
88
89     Catalogue.prototype.withTag = function(tag_id, cb) {
90       var _this = this;
91       return this.db.readTransaction(function(tx) {
92         return tx.executeSql("SELECT * FROM tag WHERE id=?", [tag_id], function(tx, rs) {
93           var tag;
94           if (rs.rows.length > 0) {
95             tag = new Readings.Tag(rs.rows.item(0));
96             return cb(tag);
97           } else {
98             return alert("No tag id " + tag_id);
99           }
100         }, _this.wrap_error());
101       });
102     };
103
104     Catalogue.prototype.withBooks = function(tag, cb) {
105       var _this = this;
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);
111           }));
112         }), _this.wrap_error());
113       });
114     };
115
116     return Catalogue;
117
118   })();
119
120 }).call(this);