7 require_relative 'series'
8 require_relative 'tconn'
12 @basePath + '/csv/unclassified.csv'
15 def initialize(config_file)
18 config = IniFile.load(config_file)
20 puts 'FATAL: Failed to load config file "' + config_file + '". Aborting initialization.'
24 section = config['database']
25 @dbhost = section['host']
27 @dbname = section['name']
28 @dbuser = section['user']
29 @dbpass = section['pass']
31 section = config['filesystem']
32 @basePath = section['basePath']
36 @conn = TimedConn.new(PG.connect(@dbhost, @dbport, '', '', @dbname, @dbuser, @dbpass))
44 def construct_efs_path(efs_id)
45 id_str = sprintf('%010d', efs_id)
46 path = sprintf('%s/%s/%s/%s', id_str[0,2], id_str[2,2], id_str[4,2], id_str[6,2])
47 name = id_str + '.dat'
51 def cross_reference_lists
52 puts "@@@@@@@@@@@ CROSS-REF START @@@@@@@@@@@"
53 exec_update("TRUNCATE TABLE Lists CASCADE;", [])
56 puts "@@@@@@@@@@@ CROSS-REF DONE @@@@@@@@@@@"
59 def create_schema(skip_class)
62 CREATE TABLE Authors (
63 id INTEGER PRIMARY KEY,
73 id INTEGER PRIMARY KEY,
74 author INTEGER REFERENCES Authors(id),
75 classification INTEGER REFERENCES Classifications(id),
80 series INTEGER REFERENCES Series(id),
86 create_classification =
88 CREATE TABLE Classifications (
89 id INTEGER PRIMARY KEY,
92 author_grouping VARCHAR(64),
93 author_sort VARCHAR(128),
94 title_grouping VARCHAR(256),
102 id INTEGER PRIMARY KEY,
110 id VARCHAR(32) PRIMARY KEY,
115 # Associative entity, linking FAST and Classifications tables
116 # in a 0..n to 0..m relationship
117 create_fast_classifications =
119 CREATE TABLE FAST_Classifications (
120 fast VARCHAR(32) REFERENCES FAST(id),
121 classification INTEGER REFERENCES Classifications(id)
128 id INTEGER PRIMARY KEY,
130 category VARCHAR(32),
133 author INTEGER REFERENCES Authors(id),
138 # Associative entity, linking Lists and Books tables
139 # in a 0..n to 0..m relationship
142 CREATE TABLE Lists_Books (
143 list INTEGER REFERENCES Lists(id),
144 book INTEGER REFERENCES Books(id)
150 CREATE TABLE Series (
151 id INTEGER PRIMARY KEY,
154 grouping VARCHAR(64),
162 create_classification,
167 create_fast_classifications,
170 'CREATE SEQUENCE author_id;',
171 'CREATE SEQUENCE book_id;',
172 'CREATE SEQUENCE classification_id;',
173 'CREATE SEQUENCE efs_id;',
174 'CREATE SEQUENCE list_id;',
175 'CREATE SEQUENCE series_id;'
182 if skip_class == false
184 populate_classifications_table
187 populate_series_table
192 'DROP TABLE Lists_Books;',
195 'DROP TABLE FAST_Classifications;',
196 'DROP TABLE Authors;',
197 'DROP TABLE Classifications;',
200 'DROP TABLE Series;',
201 'DROP SEQUENCE author_id;',
202 'DROP SEQUENCE book_id;',
203 'DROP SEQUENCE classification_id;',
204 'DROP SEQUENCE efs_id;',
205 'DROP SEQUENCE list_id;',
206 'DROP SEQUENCE series_id;'
212 rescue Exception => exc
213 puts 'WARNING: "' + stmt + '" failed: ' + exc.to_s
218 def find_all_authors(author_name)
221 sqlSelect = "SELECT id FROM Authors WHERE grouping=$1;"
224 @conn.exec_params(sqlSelect, args) do |rs|
233 def find_author(author)
234 sqlSelect = "SELECT id FROM Authors WHERE grouping=$1 AND reading=$2 AND sort=$3;"
235 args = [author.grouping, author.reading_order, author.sort_order]
237 @conn.exec_params(sqlSelect, args) do |rs|
246 def init_db(skip_class)
247 sql = "SELECT 1 FROM pg_tables WHERE tableowner='quanlib' AND tablename='books'"
249 @conn.exec(sql).each do |row|
254 create_schema(skip_class)
259 sqlSelect = "SELECT grouping, reading, sort FROM Authors WHERE id=$1"
261 @conn.exec_params(sqlSelect, args) do |rs|
263 raise "Expected 1 row for " + id + " but got " + rs.ntuples + ": " + sqlSelect
266 author = Author.new(row['grouping'], row['reading'], row['sort'])
272 def store_author(author)
273 id = find_author(author)
275 id = next_id('author_id')
276 sqlInsert = "INSERT INTO Authors(id, grouping, reading, sort) VALUES ($1, $2, $3, $4);"
277 args = [id, author.grouping, author.reading_order, author.sort_order]
279 rs = @conn.exec_params(sqlInsert, args)
280 rescue Exception => e
281 puts sqlInsert + ": " + args.inspect()
292 sql = "SELECT author, classification, cover, description, language, path, series, title, volume FROM Books WHERE id=$1;"
296 @conn.exec_params(sql, [id]) do |rs|
298 raise 'Expected one row in Books for id ' + id + ', but found ' + rs.length + '.'
303 book = Book.new(self)
304 book.author = load_author(row['author'])
305 book.classification_id = row['classification']
306 book.cover = load_cover(row['cover'])
307 book.description = row['description']
308 book.language = row['language']
309 book.path = row['path']
310 book.series_id = row['series']
311 book.title = row['title']
312 book.volume = row['volume']
314 rescue Exception => e
324 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);"
326 book_id = next_id('book_id')
328 author_id = store_author(book.author)
329 (efs_id, mime_type) = store_cover(book)
331 args = [book_id, author_id, book.classification_id, efs_id, book.description, book.language, book.path, book.series_id, book.title, book.volume]
334 rs = @conn.exec_params(sql, args)
335 rescue Exception => e
336 puts sql + ": " + args.inspect()
346 def find_classification(author_grouping, title_grouping)
347 sql = "SELECT id FROM Classifications WHERE author_grouping = $1 AND title_grouping = $2;"
348 @conn.exec_params(sql, [author_grouping, title_grouping]) do |rs|
356 def load_classification(id)
357 sql = "SELECT ddc, lcc, author_grouping, author_sort, title_grouping, title "
358 sql += " FROM Classifications WHERE id=$1"
359 @conn.exec_params(sql, [id]) do |rs|
364 author_grouping = row['author_grouping']
365 author = row['author_sort']
366 title_grouping = row['title_grouping']
369 result = Classification.new(ddc, lcc, author_grouping, author, title_grouping, title)
383 mime_type = 'application/octet-stream'
385 sql = "SELECT mimeType FROM Efs WHERE id=$1"
386 @conn.exec_params(sql, [id]) do |rs|
388 raise "Expected one row but got " + rs.ntuples + ": " + sql + ": " + id
390 mime_type = rs[0]['mimeType']
393 (efspath, efsname) = construct_efs_path(id)
395 fullpath = @basePath + '/efs/' + efspath + '/' + efsname
397 return Cover.new(nil, fullpath, mime_type)
400 def store_cover(book)
408 @conn.exec("SELECT nextval('efs_id')") do |rs|
409 efs_id = rs[0]['nextval']
416 (efspath, efsname) = construct_efs_path(efs_id)
418 efspath = @basePath + '/efs/' + efspath
420 FileUtils.mkdir_p(efspath)
422 (filepath, mimetype) = cover.write_image(efspath, efsname)
424 sql = "INSERT INTO efs VALUES ($1, $2)"
426 rs = @conn.exec_params(sql, [efs_id, mimetype])
427 rescue Exception => e
428 puts sql + ": " + efs_id + ", " + mimetype
435 return efs_id, mimetype
438 def exec_id_query(sql, args)
440 @conn.exec_params(sql, args) do |rs|
448 def exec_update(sql, args)
450 rs = @conn.exec_params(sql, args)
451 rescue Exception => e
452 puts sql + ": " + args.inspect()
460 def next_id(seq_name)
462 @conn.exec("SELECT nextval('" + seq_name + "');") do |rs|
463 id = rs[0]['nextval']
468 def get_series(grouping, code)
473 sql = "SELECT id FROM Series WHERE grouping=$1 AND code=$2;"
474 args = [grouping, code]
475 @conn.exec_params(sql, args).each do |row|
479 # TODO: Create a new series object here?
480 puts 'WARNING: series("' + grouping + '", "' + code + '") not found.'
485 sql = "SELECT age,genre,grouping,code,descr FROM Series WHERE id=$1;"
487 @conn.exec_params(sql, args) do |rs|
490 series = Series.new(id)
491 series.age = row['age']
492 series.genre = row['genre']
493 series.grouping = row['grouping']
494 series.code = row['code']
495 series.descr = row['descr']
502 def populate_classifications_table
503 puts "Populating the Classifications table..."
505 CSV.foreach(@basePath + '/csv/class.csv') do |row|
507 # skip the header row
511 # First, add a row to the Classifications table
513 id = next_id('classification_id')
516 author_grouping = row[2]
518 title_grouping = row[4]
521 sqlInsert = "INSERT INTO Classifications (id, ddc, lcc, author_grouping, author_sort, title_grouping, title) VALUES ($1, $2, $3, $4, $5, $6, $7);"
522 args = [id, ddc, lcc, author_grouping, author_sort, title_grouping, title]
523 exec_update(sqlInsert, args)
525 # Second, link up with the appropriate FAST table entries
530 fast = input.split(';')
533 fast.each do |fast_id|
534 sqlInsert = "INSERT INTO FAST_Classifications (fast, classification) VALUES ($1, $2);"
536 exec_update(sqlInsert, args)
542 def populate_fast_table
543 puts "Populating the FAST table..."
545 CSV.foreach(@basePath + '/csv/fast.csv') do |row|
547 first = false # skip the header row
551 sqlInsert = "INSERT INTO FAST (id, descr) VALUES ($1, $2);"
552 exec_update(sqlInsert, [id, descr])
557 def populate_lists_table
558 puts "Populating the Lists table..."
560 CSV.foreach(@basePath + "/csv/lists.csv", headers: true) do |row|
561 author_ids = find_all_authors(row['author'])
563 specification = [row['age'], row['category'], row['code'], row['year'], row['author'], row['title']]
564 .map { |x| x.inspect }
567 puts "WARNING: For list entry (#{specification}), no such author was found."
573 INSERT INTO Lists (id, age, category, code, year, author, title)
574 VALUES ($1, $2, $3, $4, $5, $6, $7);
576 author_ids.each do |author_id|
577 list_id = next_id('list_id')
578 args = [list_id, row['age'], row['category'], row['code'], row['year'], author_id, row['title']]
579 exec_update(sqlInsert, args)
581 update_lists_books_table(list_id, author_id, row['title'])
586 # Scan for books that match this Lists entry, and add any matches to the Lists_Books associative table
587 def update_lists_books_table(list_id, author_id, title)
588 title_pattern = Book.grouping_for_title(title).gsub('_', '%')
589 sqlSelect = "SELECT id FROM Books WHERE author = $1 AND title LIKE $2;"
590 args = [author_id, title_pattern]
592 @conn.exec_params(sqlSelect, args) do |rs|
594 sqlInsert = "INSERT INTO Lists_Books (list, book) VALUES ($1, $2)"
595 args = [list_id, row['id']]
596 exec_update(sqlInsert, args)
601 def populate_series_table
602 puts "Populating the Series table..."
603 CSV.foreach(@basePath + '/csv/series.csv') do |row|
604 id = next_id('series_id')
605 sqlInsert = "INSERT INTO Series (id, age, genre, grouping, code, descr) VALUES ($1, $2, $3, $4, $5, $6);"
607 exec_update(sqlInsert, args)
611 def query_books_by_author(pattern)
614 SELECT b.id FROM Authors a
615 INNER JOIN Books b ON b.author=a.id
616 LEFT OUTER JOIN Series s on s.id=b.series
617 WHERE upper(a.grouping) LIKE $1
618 ORDER BY a.grouping, b.series, b.volume, b.title
620 return exec_id_query(sql, [pattern])
623 def query_books_by_ddc
626 SELECT b.id FROM Classifications c
627 INNER JOIN Books b ON b.classification=c.id
630 return exec_id_query(sql, [])
633 def query_books_by_series_id(id)
636 SELECT b.id FROM Books b
638 ORDER BY b.volume,b.title
640 return exec_id_query(sql, [id])
643 def query_series_by_age(pattern)
649 ORDER BY s.grouping,s.descr
651 return exec_id_query(sql, [pattern])