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 create_schema(skip_class)
54 CREATE TABLE Authors (
55 id INTEGER PRIMARY KEY,
65 id INTEGER PRIMARY KEY,
66 author INTEGER REFERENCES Authors(id),
67 classification INTEGER REFERENCES Classifications(id),
71 series INTEGER REFERENCES Series(id),
77 create_classification =
79 CREATE TABLE Classifications (
80 id INTEGER PRIMARY KEY,
83 author_grouping VARCHAR(64),
84 author_sort VARCHAR(128),
85 title_grouping VARCHAR(256),
93 id INTEGER PRIMARY KEY,
101 id VARCHAR(32) PRIMARY KEY,
106 # Associative entity, linking FAST and Classifications tables
107 # in a 0..n to 0..m relationship
108 create_fast_classifications =
110 CREATE TABLE FAST_Classifications (
111 fast VARCHAR(32) REFERENCES FAST(id),
112 classification INTEGER REFERENCES Classifications(id)
118 CREATE TABLE Series (
119 id INTEGER PRIMARY KEY,
122 grouping VARCHAR(64),
130 create_classification,
135 create_fast_classifications,
136 'CREATE SEQUENCE author_id;',
137 'CREATE SEQUENCE book_id;',
138 'CREATE SEQUENCE classification_id;',
139 'CREATE SEQUENCE efs_id;',
140 'CREATE SEQUENCE series_id;'
147 if skip_class == false
148 populate_fast_table()
149 populate_classifications_table()
152 populate_series_table()
159 'DROP TABLE FAST_Classifications;',
160 'DROP TABLE Authors;',
161 'DROP TABLE Classifications;',
164 'DROP TABLE Series;',
165 'DROP SEQUENCE author_id;',
166 'DROP SEQUENCE book_id;',
167 'DROP SEQUENCE classification_id;',
168 'DROP SEQUENCE efs_id;',
169 'DROP SEQUENCE series_id;'
175 rescue Exception => exc
176 puts 'WARNING: "' + stmt + '" failed: ' + exc.to_s
181 def find_author(author)
182 sqlSelect = "SELECT id FROM Authors WHERE grouping=$1 AND reading=$2 AND sort=$3;"
183 args = [author.grouping, author.reading_order, author.sort_order]
184 @conn.exec_params(sqlSelect, args) do |rs|
192 def init_db(skip_class)
193 sql = "SELECT 1 FROM pg_tables WHERE tableowner='quanlib' AND tablename='books'"
195 @conn.exec(sql).each do |row|
200 create_schema(skip_class)
205 sqlSelect = "SELECT grouping, reading, sort FROM Authors WHERE id=$1"
207 @conn.exec_params(sqlSelect, args) do |rs|
209 raise "Expected 1 row for " + id + " but got " + rs.ntuples + ": " + sqlSelect
212 author = Author.new(row['grouping'], row['reading'], row['sort'])
218 def store_author(author)
219 id = find_author(author)
221 id = next_id('author_id')
222 sqlInsert = "INSERT INTO Authors(id, grouping, reading, sort) VALUES ($1, $2, $3, $4);"
223 args = [id, author.grouping, author.reading_order, author.sort_order]
225 rs = @conn.exec_params(sqlInsert, args)
226 rescue Exception => e
227 puts sqlInsert + ": " + args.inspect()
238 sql = "SELECT author, classification, cover, description, path, series, title, volume FROM Books WHERE id=$1;"
242 @conn.exec_params(sql, [id]) do |rs|
244 raise 'Expected one row in Books for id ' + id + ', but found ' + rs.length + '.'
249 book = Book.new(self)
250 book.author = load_author(row['author'])
251 book.classification_id = row['classification']
252 book.cover = load_cover(row['cover'])
253 book.description = row['description']
254 book.path = row['path']
255 book.series_id = row['series']
256 book.title = row['title']
257 book.volume = row['volume']
259 rescue Exception => e
269 sql = "INSERT INTO Books (id, author, classification, cover, description, path, series, title, volume) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);"
271 book_id = next_id('book_id')
273 author_id = store_author(book.author)
274 (efs_id, mime_type) = store_cover(book)
276 args = [book_id, author_id, book.classification_id, efs_id, book.description(), book.path(), book.series_id(), book.title(), book.volume()]
279 rs = @conn.exec_params(sql, args)
280 rescue Exception => e
281 puts sql + ": " + args.inspect()
291 def find_classification(author_grouping, title_grouping)
292 sql = "SELECT id FROM Classifications WHERE author_grouping = $1 AND title_grouping = $2;"
293 @conn.exec_params(sql, [author_grouping, title_grouping]) do |rs|
301 def load_classification(id)
302 sql = "SELECT ddc, lcc, author_grouping, author_sort, title_grouping, title "
303 sql += " FROM Classifications WHERE id=$1"
304 @conn.exec_params(sql, [id]) do |rs|
309 author_grouping = row['author_grouping']
310 author = row['author_sort']
311 title_grouping = row['title_grouping']
314 result = Classification.new(ddc, lcc, author_grouping, author, title_grouping, title)
328 mime_type = 'application/octet-stream'
330 sql = "SELECT mimeType FROM Efs WHERE id=$1"
331 @conn.exec_params(sql, [id]) do |rs|
333 raise "Expected one row but got " + rs.ntuples + ": " + sql + ": " + id
335 mime_type = rs[0]['mimeType']
338 (efspath, efsname) = construct_efs_path(id)
340 fullpath = @basePath + '/efs/' + efspath + '/' + efsname
342 return Cover.new(nil, fullpath, mime_type)
345 def store_cover(book)
353 @conn.exec("SELECT nextval('efs_id')") do |rs|
354 efs_id = rs[0]['nextval']
361 (efspath, efsname) = construct_efs_path(efs_id)
363 efspath = @basePath + '/efs/' + efspath
365 FileUtils.mkdir_p(efspath)
367 (filepath, mimetype) = cover.write_image(efspath, efsname)
369 sql = "INSERT INTO efs VALUES ($1, $2)"
371 rs = @conn.exec_params(sql, [efs_id, mimetype])
372 rescue Exception => e
373 puts sql + ": " + efs_id + ", " + mimetype
380 return efs_id, mimetype
383 def exec_id_query(sql, args)
385 @conn.exec_params(sql, args) do |rs|
393 def exec_update(sql, args)
395 rs = @conn.exec_params(sql, args)
396 rescue Exception => e
397 puts sql + ": " + args.inspect()
405 def next_id(seq_name)
407 @conn.exec("SELECT nextval('" + seq_name + "');") do |rs|
408 id = rs[0]['nextval']
413 def get_series(grouping, code)
418 sql = "SELECT id FROM Series WHERE grouping=$1 AND code=$2;"
419 args = [grouping, code]
420 @conn.exec_params(sql, args).each do |row|
424 # TODO: Create a new series object here?
425 puts 'WARNING: series("' + grouping + '", "' + code + '") not found.'
430 sql = "SELECT age,genre,grouping,code,descr FROM Series WHERE id=$1;"
432 @conn.exec_params(sql, args) do |rs|
435 series = Series.new(id)
436 series.age = row['age']
437 series.genre = row['genre']
438 series.grouping = row['grouping']
439 series.code = row['code']
440 series.descr = row['descr']
447 def populate_classifications_table
448 puts "Populating the Classifications table..."
450 CSV.foreach(@basePath + '/csv/class.csv') do |row|
452 # skip the header row
456 # First, add a row to the Classifications table
458 id = next_id('classification_id')
461 author_grouping = row[2]
463 title_grouping = row[4]
466 sqlInsert = "INSERT INTO Classifications (id, ddc, lcc, author_grouping, author_sort, title_grouping, title) VALUES ($1, $2, $3, $4, $5, $6, $7);"
467 args = [id, ddc, lcc, author_grouping, author_sort, title_grouping, title]
468 exec_update(sqlInsert, args)
470 # Second, link up with the appropriate FAST table entries
475 fast = input.split(';')
478 fast.each do |fast_id|
479 sqlInsert = "INSERT INTO FAST_Classifications (fast, classification) VALUES ($1, $2);"
481 exec_update(sqlInsert, args)
487 def populate_fast_table
488 puts "Populating the FAST table..."
490 CSV.foreach(@basePath + '/csv/fast.csv') do |row|
492 first = false # skip the header row
496 sqlInsert = "INSERT INTO FAST (id, descr) VALUES ($1, $2);"
497 exec_update(sqlInsert, [id, descr])
502 def populate_series_table
503 puts "Populating the Series table..."
504 CSV.foreach(@basePath + '/csv/series.csv') do |row|
505 id = next_id('series_id')
506 sqlInsert = "INSERT INTO Series (id, age, genre, grouping, code, descr) VALUES ($1, $2, $3, $4, $5, $6);"
508 exec_update(sqlInsert, args)
512 def query_books_by_author(pattern)
515 SELECT b.id FROM Authors a
516 INNER JOIN Books b ON b.author=a.id
517 LEFT OUTER JOIN Series s on s.id=b.series
518 WHERE upper(a.grouping) LIKE $1
519 ORDER BY a.grouping, b.series, b.volume, b.title
521 return exec_id_query(sql, [pattern])
524 def query_books_by_ddc
527 SELECT b.id FROM Classifications c
528 INNER JOIN Books b ON b.classification=c.id
531 return exec_id_query(sql, [])
534 def query_books_by_series_id(id)
537 SELECT b.id FROM Books b
539 ORDER BY b.volume,b.title
541 return exec_id_query(sql, [id])
544 def query_series_by_age(pattern)
550 ORDER BY s.grouping,s.descr
552 return exec_id_query(sql, [pattern])