Start support for reading data back from the database.
authorChris Jaekl <cejaekl@yahoo.com>
Fri, 26 May 2017 23:07:08 +0000 (08:07 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Fri, 26 May 2017 23:07:08 +0000 (08:07 +0900)
book.rb
cover.rb
main.rb
store.rb
walkdir.rb

diff --git a/book.rb b/book.rb
index bfd3b250dcc358d958af23ff706f4e9065c049e8..03a5f259beb0005d4250bd7988edab21729ba41d 100644 (file)
--- 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
 
index b88f3406267c9963822e46b3eae3c3e3a14081fa..b00d002dfdac8439857b08833738485510ed7376 100644 (file)
--- 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 bc175fbf51ae33205a37735d1390872a3ea630e1..596e11ff526fec0f09227bda8bcad927ab163180 100644 (file)
--- 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 = '<img height="200px" src="' + path + '"/>'
     else
       image = '(No cover image)'
index 0b7b476bbd860ae9e55ff1fc2e887f88066dc84f..645224a7c4bfa22bdcc66d08e031ad91d7c0f176 100644 (file)
--- 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
index 5d5bf84ec90804e5c4b4be4ff30bdb69ab62a741..ce1954ca7a11d27417afecf739af99fdbd98c83e 100644 (file)
@@ -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