From 4b53af822cda819dd82d0d3e7ed066c2966ae4bf Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Sat, 27 May 2017 08:07:08 +0900 Subject: [PATCH] Start support for reading data back from the database. --- book.rb | 8 +++++--- cover.rb | 8 +++++++- main.rb | 2 +- store.rb | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- walkdir.rb | 3 ++- 5 files changed, 65 insertions(+), 7 deletions(-) diff --git a/book.rb b/book.rb index bfd3b25..03a5f25 100644 --- a/book.rb +++ b/book.rb @@ -8,16 +8,18 @@ require 'cover' class Book @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/' - def initialize(fileName) - #puts 'InitBook(' + fileName + ')' + def initialize @author = nil @cover = nil @description = nil - @path = fileName + @path = nil @series = nil @title = nil @volume = nil + end + def loadFromFile(fileName) + @path = fileName parseFileName!(fileName) end diff --git a/cover.rb b/cover.rb index b88f340..b00d002 100644 --- a/cover.rb +++ b/cover.rb @@ -22,11 +22,17 @@ class Cover return '(Cover:' + info.join(',') + ')' end + def read_image(filename) + open(filename, 'rb') do |fd| + @data = fd.read() + end + end + def to_s return inspect end - def writeImage(outputDir, baseName) + def write_image(outputDir, baseName) filename = baseName + getExt() open(outputDir + '/' + filename, 'wb') do |fd| fd.write(@data) diff --git a/main.rb b/main.rb index bc175fb..596e11f 100644 --- a/main.rb +++ b/main.rb @@ -64,7 +64,7 @@ open(outputDir + '/index.html', 'w') do |fd| image = nil if nil != book.cover imageCount += 1 - (path, mimeType) = book.cover.writeImage(outputDir, 'image' + imageCount.to_s) + (path, mimeType) = book.cover.write_image(outputDir, 'image' + imageCount.to_s) image = '' else image = '(No cover image)' 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 diff --git a/walkdir.rb b/walkdir.rb index 5d5bf84..ce1954c 100644 --- a/walkdir.rb +++ b/walkdir.rb @@ -32,7 +32,8 @@ class WalkDir result = [] for file in @files.sort if Book.canHandle?(file) - book = Book.new(file) + book = Book.new() + book.loadFromFile(file) @store.store_book(book) result.push(book) end -- 2.39.2