Start support for reading data back from the database.
[quanlib.git] / store.rb
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