X-Git-Url: http://jaekl.net/gitweb/?p=quanlib.git;a=blobdiff_plain;f=store.rb;h=92d1ce57d1917ea7309d86527fa96f5aa826581e;hp=5faf7ce884c21371a58ea3dbafec2cece7899a0a;hb=HEAD;hpb=9b27f41cf97737185a8c6593e7db786cccb6efc1 diff --git a/store.rb b/store.rb index 5faf7ce..1a33ca3 100644 --- a/store.rb +++ b/store.rb @@ -48,8 +48,16 @@ class Store return path, name end + def cross_reference_lists +puts "@@@@@@@@@@@ CROSS-REF START @@@@@@@@@@@" + exec_update("TRUNCATE TABLE Lists CASCADE;", []) + + populate_lists_table +puts "@@@@@@@@@@@ CROSS-REF DONE @@@@@@@@@@@" + end + def create_schema(skip_class) - create_authors = + create_authors = < 0 return rs[0]['id'] end end + return nil end @@ -222,7 +276,7 @@ EOS id = next_id('author_id') sqlInsert = "INSERT INTO Authors(id, grouping, reading, sort) VALUES ($1, $2, $3, $4);" args = [id, author.grouping, author.reading_order, author.sort_order] - begin + begin rs = @conn.exec_params(sqlInsert, args) rescue Exception => e puts sqlInsert + ": " + args.inspect() @@ -257,7 +311,7 @@ EOS book.series_id = row['series'] book.title = row['title'] book.volume = row['volume'] - end + end rescue Exception => e puts sql + ": " + id puts e.message @@ -268,20 +322,20 @@ EOS end def store_book(book) - 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);" + sql = "INSERT INTO Books (id, arrived, author, classification, cover, description, language, path, series, title, volume) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);" 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.language, book.path, book.series_id, book.title, book.volume] + args = [book_id, book.arrived, 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) rescue Exception => e puts sql + ": " + args.inspect() - puts e.message + puts e.message puts $@ ensure rs.clear if rs @@ -378,7 +432,7 @@ EOS ensure rs.clear if rs end - + return efs_id, mimetype end @@ -408,7 +462,7 @@ EOS id = nil @conn.exec("SELECT nextval('" + seq_name + "');") do |rs| id = rs[0]['nextval'] - end + end return id end @@ -464,7 +518,7 @@ EOS author_sort = row[3] title_grouping = row[4] title = row[5] - + sqlInsert = "INSERT INTO Classifications (id, ddc, lcc, author_grouping, author_sort, title_grouping, title) VALUES ($1, $2, $3, $4, $5, $6, $7);" args = [id, ddc, lcc, author_grouping, author_sort, title_grouping, title] exec_update(sqlInsert, args) @@ -475,7 +529,7 @@ EOS input = row[6] if input.length > 0 fast = input.split(';') - end + end fast.each do |fast_id| sqlInsert = "INSERT INTO FAST_Classifications (fast, classification) VALUES ($1, $2);" @@ -501,6 +555,50 @@ EOS end end + def populate_lists_table + puts "Populating the Lists table..." + + CSV.foreach(@basePath + "/csv/lists.csv", headers: true) do |row| + author_ids = find_all_authors(row['author']) + if author_ids.empty? + specification = [row['age'], row['category'], row['code'], row['year'], row['author'], row['title']] + .map { |x| x.inspect } + .join(', ') + + puts "WARNING: For list entry (#{specification}), no such author was found." + + next + end + + sqlInsert = %Q( + INSERT INTO Lists (id, age, category, code, year, author, title) + VALUES ($1, $2, $3, $4, $5, $6, $7); + ) + author_ids.each do |author_id| + list_id = next_id('list_id') + args = [list_id, row['age'], row['category'], row['code'], row['year'], author_id, row['title']] + exec_update(sqlInsert, args) + + update_lists_books_table(list_id, author_id, row['title']) + end + end + end + + # Scan for books that match this Lists entry, and add any matches to the Lists_Books associative table + def update_lists_books_table(list_id, author_id, title) + title_pattern = Book.grouping_for_title(title).gsub('_', '%') + sqlSelect = "SELECT id FROM Books WHERE author = $1 AND title LIKE $2;" + args = [author_id, title_pattern] + + @conn.exec_params(sqlSelect, args) do |rs| + rs.each do |row| + sqlInsert = "INSERT INTO Lists_Books (list, book) VALUES ($1, $2)" + args = [list_id, row['id']] + exec_update(sqlInsert, args) + end + end + end + def populate_series_table puts "Populating the Series table..." CSV.foreach(@basePath + '/csv/series.csv') do |row| @@ -512,21 +610,21 @@ EOS end def query_books_by_author(pattern) - sql = + sql = <