Add language to book data.
[quanlib.git] / store.rb
index 69e1278d40393ea9d3d7e75d34cdf452645e8ed1..5faf7ce884c21371a58ea3dbafec2cece7899a0a 100644 (file)
--- a/store.rb
+++ b/store.rb
@@ -4,7 +4,8 @@ require 'fileutils'
 require 'inifile'
 require 'pg'
 
-require 'series'
+require_relative 'series'
+require_relative 'tconn'
 
 class Store
   def unclassified_csv
@@ -32,8 +33,7 @@ class Store
   end
 
   def connect
-    # @conn = PGconn.connect('localhost', 5432, '', '', 'quanlib', 'quanlib', 'quanlib')
-    @conn = PG.connect(@dbhost, @dbport, '', '', @dbname, @dbuser, @dbpass)
+    @conn = TimedConn.new(PG.connect(@dbhost, @dbport, '', '', @dbname, @dbuser, @dbpass))
     return @conn
   end
 
@@ -66,6 +66,7 @@ EOS
         author         INTEGER REFERENCES Authors(id),
         classification INTEGER REFERENCES Classifications(id),
         cover          INTEGER,
+        language       VARCHAR(64),
         description    TEXT,
         path           VARCHAR(256),
         series         INTEGER REFERENCES Series(id),
@@ -90,7 +91,7 @@ EOS
     create_efs = 
 <<EOS
       CREATE TABLE EFS (
-        id          INTEGER,
+        id          INTEGER PRIMARY KEY,
         mimetype    VARCHAR(64)
       );
 EOS
@@ -235,7 +236,7 @@ EOS
   end
 
   def load_book(id)
-    sql = "SELECT author, classification, cover, description, path, series, title, volume FROM Books WHERE id=$1;"
+    sql = "SELECT author, classification, cover, description, language, path, series, title, volume FROM Books WHERE id=$1;"
     book = nil
 
     begin
@@ -251,6 +252,7 @@ EOS
         book.classification_id = row['classification']
         book.cover = load_cover(row['cover'])
         book.description = row['description']
+        book.language = row['language']
         book.path = row['path']
         book.series_id = row['series']
         book.title = row['title']
@@ -266,14 +268,14 @@ EOS
   end
 
   def store_book(book)
-    sql = "INSERT INTO Books (id, author, classification, cover, description, path, series, title, volume) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);"
+    sql = "INSERT INTO Books (id, author, classification, cover, description, language, path, series, title, volume) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);"
 
     book_id = next_id('book_id')
 
     author_id = store_author(book.author)
     (efs_id, mime_type) = store_cover(book)
 
-    args = [book_id, author_id, book.classification_id, efs_id, book.description(), book.path(), book.series_id(), book.title(), book.volume()]
+    args = [book_id, author_id, book.classification_id, efs_id, book.description, book.language, book.path, book.series_id, book.title, book.volume]
 
     begin
       rs = @conn.exec_params(sql, args)
@@ -289,15 +291,12 @@ EOS
   end
 
   def find_classification(author_grouping, title_grouping)
-    #puts 'find_classification("' + author_grouping.inspect + '", "' + title_grouping.inspect + '")...'
     sql = "SELECT id FROM Classifications WHERE author_grouping = $1 AND title_grouping = $2;"
     @conn.exec_params(sql, [author_grouping, title_grouping]) do |rs|
       if rs.ntuples > 0
-        #puts '  --> ' + rs[0]['id'].inspect
         return rs[0]['id']
       end
     end
-    #puts '  --> NIL'
     return nil
   end
 
@@ -343,12 +342,6 @@ EOS
     fullpath = @basePath + '/efs/' + efspath + '/' + efsname
 
     return Cover.new(nil, fullpath, mime_type)
-
-    #File.open(fullpath, 'rb') do |is|
-    #  return Cover.new(is, fullpath, mime_type)
-    #end
-    #
-    #return nil
   end
 
   def store_cover(book)