2 class Readings.Catalogue
4 version = Readings.config.get 'db_version'
5 console.log "opening db ver #{version}"
6 @db = openDatabase 'catalogue', version, 'Catalogue', 65535, @init
12 console.log "initializing DB"
13 db.changeVersion "", Readings.config.get 'db_version'
24 tx.executeSql stmt, [],
26 (tx,err) => console.error(err)
30 console.error "can't get initial sql"
32 wrap_error: (error_cb) ->
34 window.last_error = error
35 alert "SQL ERROR: #{error.message}"
41 map_rs: (rs, mapper) ->
43 for i in [0...rs.rows.length]
44 objs.push(mapper(rs.rows.item(i)))
48 withCategory: (category, callback, error) ->
49 rs_to_tags = (tx, data) ->
51 for i in [0...data.rows.length]
52 tags.push new Readings.Tag(data.rows.item(i))
55 @db.readTransaction (tx)=>
56 tx.executeSql "SELECT * FROM tag WHERE category=? ORDER BY sort_key",
57 [category], rs_to_tags, @wrap_error(error)
59 withTag: (tag_id, cb) ->
60 @db.readTransaction (tx) =>
61 tx.executeSql "SELECT * FROM tag WHERE id=?", [tag_id],
64 tag = new Readings.Tag rs.rows.item(0)
67 alert "No tag id #{tag_id}"
71 withBooks: (tag, cb) ->
72 @db.readTransaction (tx)=>
73 console.log "books in tag: #{tag.books}"
74 tx.executeSql "SELECT * FROM book WHERE id IN (#{tag.books}) ORDER BY sort_key",
75 [], ((tx, rs) => cb(@map_rs(rs, (rec)->new Readings.Book(rec)))), @wrap_error()