X-Git-Url: http://jaekl.net/gitweb/?p=quanlib.git;a=blobdiff_plain;f=store.rb;h=645224a7c4bfa22bdcc66d08e031ad91d7c0f176;hp=0b7b476bbd860ae9e55ff1fc2e887f88066dc84f;hb=4b53af822cda819dd82d0d3e7ed066c2966ae4bf;hpb=93ad56f506f87af56145059fb8be9b35c5c61567 diff --git a/store.rb b/store.rb index 0b7b476..645224a 100644 --- a/store.rb +++ b/store.rb @@ -113,6 +113,18 @@ EOS return nil end + def load_author(id) + sqlSelect = "SELECT grouping, reading, sort FROM Authors WHERE id=$1" + args = [id] + @conn.exec_params(sqlSelect, args) do |rs| + if rs.ntuples != 1 + raise "Expected 1 row for " + id + " but got " + rs.ntuples + ": " + sqlSelect + end + row = rs[0] + return Author.new(row['grouping'], row['reading'], row['sort']) + end + end + def store_author(author) id = find_author(author) if nil == id @@ -131,6 +143,35 @@ EOS return find_author(author) end + def load_book(id) + sql = "SELECT author, cover, description, path, series, title, volume FROM Books WHERE id=$1;" + book = nil + + begin + @conn.exec_params(sql, [id]) do |rs| + if 1 != rs.ntuples + raise 'Expected one row in Books for id ' + id + ', but found ' + rs.length + '.' + return nil + end + row = rs[0] + + book = Book.new() + book.author = load_author(row['author']) + book.cover = load_cover(row['cover']) + book.description = row['description'] + book.path = row['path'] + book.series = row['series'] + book.title = row['title'] + book.volume = row['volume'] + end + rescue Exception => e + puts sql + ": " + id + puts e.message + end + + return book + end + def store_book(book) sql = "INSERT INTO Books (author, cover, description, path, series, title, volume) VALUES ($1, $2, $3, $4, $5, $6, $7);" @@ -150,6 +191,14 @@ EOS end end + def load_cover(id) + (efspath, efsname) = construct_efs_path(id) + efspath = @basepath + '/' + efspath + cover = Cover.new() + cover.load_image(efspath + '/' + efsname) + return cover + end + def store_cover(book) efs_id = nil cover = book.cover() @@ -172,7 +221,7 @@ EOS FileUtils.mkdir_p(efspath) - (filepath, mimetype) = cover.writeImage(efspath, efsname) + (filepath, mimetype) = cover.write_image(efspath, efsname) sql = "INSERT INTO efs VALUES ($1, $2)" begin