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),
72 series INTEGER REFERENCES Series(id),
78 create_classification =
80 CREATE TABLE Classifications (
81 id INTEGER PRIMARY KEY,
84 author_grouping VARCHAR(64),
85 author_sort VARCHAR(128),
86 title_grouping VARCHAR(256),
94 id INTEGER PRIMARY KEY,
102 id VARCHAR(32) PRIMARY KEY,
107 # Associative entity, linking FAST and Classifications tables
108 # in a 0..n to 0..m relationship
109 create_fast_classifications =
111 CREATE TABLE FAST_Classifications (
112 fast VARCHAR(32) REFERENCES FAST(id),
113 classification INTEGER REFERENCES Classifications(id)
119 CREATE TABLE Series (
120 id INTEGER PRIMARY KEY,
123 grouping VARCHAR(64),
131 create_classification,
136 create_fast_classifications,
137 'CREATE SEQUENCE author_id;',
138 'CREATE SEQUENCE book_id;',
139 'CREATE SEQUENCE classification_id;',
140 'CREATE SEQUENCE efs_id;',
141 'CREATE SEQUENCE series_id;'
148 if skip_class == false
149 populate_fast_table()
150 populate_classifications_table()
153 populate_series_table()
160 'DROP TABLE FAST_Classifications;',
161 'DROP TABLE Authors;',
162 'DROP TABLE Classifications;',
165 'DROP TABLE Series;',
166 'DROP SEQUENCE author_id;',
167 'DROP SEQUENCE book_id;',
168 'DROP SEQUENCE classification_id;',
169 'DROP SEQUENCE efs_id;',
170 'DROP SEQUENCE series_id;'
176 rescue Exception => exc
177 puts 'WARNING: "' + stmt + '" failed: ' + exc.to_s
182 def find_author(author)
183 sqlSelect = "SELECT id FROM Authors WHERE grouping=$1 AND reading=$2 AND sort=$3;"
184 args = [author.grouping, author.reading_order, author.sort_order]
185 @conn.exec_params(sqlSelect, args) do |rs|
193 def init_db(skip_class)
194 sql = "SELECT 1 FROM pg_tables WHERE tableowner='quanlib' AND tablename='books'"
196 @conn.exec(sql).each do |row|
201 create_schema(skip_class)
206 sqlSelect = "SELECT grouping, reading, sort FROM Authors WHERE id=$1"
208 @conn.exec_params(sqlSelect, args) do |rs|
210 raise "Expected 1 row for " + id + " but got " + rs.ntuples + ": " + sqlSelect
213 author = Author.new(row['grouping'], row['reading'], row['sort'])
219 def store_author(author)
220 id = find_author(author)
222 id = next_id('author_id')
223 sqlInsert = "INSERT INTO Authors(id, grouping, reading, sort) VALUES ($1, $2, $3, $4);"
224 args = [id, author.grouping, author.reading_order, author.sort_order]
226 rs = @conn.exec_params(sqlInsert, args)
227 rescue Exception => e
228 puts sqlInsert + ": " + args.inspect()
239 sql = "SELECT author, classification, cover, description, language, path, series, title, volume FROM Books WHERE id=$1;"
243 @conn.exec_params(sql, [id]) do |rs|
245 raise 'Expected one row in Books for id ' + id + ', but found ' + rs.length + '.'
250 book = Book.new(self)
251 book.author = load_author(row['author'])
252 book.classification_id = row['classification']
253 book.cover = load_cover(row['cover'])
254 book.description = row['description']
255 book.language = row['language']
256 book.path = row['path']
257 book.series_id = row['series']
258 book.title = row['title']
259 book.volume = row['volume']
261 rescue Exception => e
271 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);"
273 book_id = next_id('book_id')
275 author_id = store_author(book.author)
276 (efs_id, mime_type) = store_cover(book)
278 args = [book_id, author_id, book.classification_id, efs_id, book.description, book.language, book.path, book.series_id, book.title, book.volume]
281 rs = @conn.exec_params(sql, args)
282 rescue Exception => e
283 puts sql + ": " + args.inspect()
293 def find_classification(author_grouping, title_grouping)
294 sql = "SELECT id FROM Classifications WHERE author_grouping = $1 AND title_grouping = $2;"
295 @conn.exec_params(sql, [author_grouping, title_grouping]) do |rs|
303 def load_classification(id)
304 sql = "SELECT ddc, lcc, author_grouping, author_sort, title_grouping, title "
305 sql += " FROM Classifications WHERE id=$1"
306 @conn.exec_params(sql, [id]) do |rs|
311 author_grouping = row['author_grouping']
312 author = row['author_sort']
313 title_grouping = row['title_grouping']
316 result = Classification.new(ddc, lcc, author_grouping, author, title_grouping, title)
330 mime_type = 'application/octet-stream'
332 sql = "SELECT mimeType FROM Efs WHERE id=$1"
333 @conn.exec_params(sql, [id]) do |rs|
335 raise "Expected one row but got " + rs.ntuples + ": " + sql + ": " + id
337 mime_type = rs[0]['mimeType']
340 (efspath, efsname) = construct_efs_path(id)
342 fullpath = @basePath + '/efs/' + efspath + '/' + efsname
344 return Cover.new(nil, fullpath, mime_type)
347 def store_cover(book)
355 @conn.exec("SELECT nextval('efs_id')") do |rs|
356 efs_id = rs[0]['nextval']
363 (efspath, efsname) = construct_efs_path(efs_id)
365 efspath = @basePath + '/efs/' + efspath
367 FileUtils.mkdir_p(efspath)
369 (filepath, mimetype) = cover.write_image(efspath, efsname)
371 sql = "INSERT INTO efs VALUES ($1, $2)"
373 rs = @conn.exec_params(sql, [efs_id, mimetype])
374 rescue Exception => e
375 puts sql + ": " + efs_id + ", " + mimetype
382 return efs_id, mimetype
385 def exec_id_query(sql, args)
387 @conn.exec_params(sql, args) do |rs|
395 def exec_update(sql, args)
397 rs = @conn.exec_params(sql, args)
398 rescue Exception => e
399 puts sql + ": " + args.inspect()
407 def next_id(seq_name)
409 @conn.exec("SELECT nextval('" + seq_name + "');") do |rs|
410 id = rs[0]['nextval']
415 def get_series(grouping, code)
420 sql = "SELECT id FROM Series WHERE grouping=$1 AND code=$2;"
421 args = [grouping, code]
422 @conn.exec_params(sql, args).each do |row|
426 # TODO: Create a new series object here?
427 puts 'WARNING: series("' + grouping + '", "' + code + '") not found.'
432 sql = "SELECT age,genre,grouping,code,descr FROM Series WHERE id=$1;"
434 @conn.exec_params(sql, args) do |rs|
437 series = Series.new(id)
438 series.age = row['age']
439 series.genre = row['genre']
440 series.grouping = row['grouping']
441 series.code = row['code']
442 series.descr = row['descr']
449 def populate_classifications_table
450 puts "Populating the Classifications table..."
452 CSV.foreach(@basePath + '/csv/class.csv') do |row|
454 # skip the header row
458 # First, add a row to the Classifications table
460 id = next_id('classification_id')
463 author_grouping = row[2]
465 title_grouping = row[4]
468 sqlInsert = "INSERT INTO Classifications (id, ddc, lcc, author_grouping, author_sort, title_grouping, title) VALUES ($1, $2, $3, $4, $5, $6, $7);"
469 args = [id, ddc, lcc, author_grouping, author_sort, title_grouping, title]
470 exec_update(sqlInsert, args)
472 # Second, link up with the appropriate FAST table entries
477 fast = input.split(';')
480 fast.each do |fast_id|
481 sqlInsert = "INSERT INTO FAST_Classifications (fast, classification) VALUES ($1, $2);"
483 exec_update(sqlInsert, args)
489 def populate_fast_table
490 puts "Populating the FAST table..."
492 CSV.foreach(@basePath + '/csv/fast.csv') do |row|
494 first = false # skip the header row
498 sqlInsert = "INSERT INTO FAST (id, descr) VALUES ($1, $2);"
499 exec_update(sqlInsert, [id, descr])
504 def populate_series_table
505 puts "Populating the Series table..."
506 CSV.foreach(@basePath + '/csv/series.csv') do |row|
507 id = next_id('series_id')
508 sqlInsert = "INSERT INTO Series (id, age, genre, grouping, code, descr) VALUES ($1, $2, $3, $4, $5, $6);"
510 exec_update(sqlInsert, args)
514 def query_books_by_author(pattern)
517 SELECT b.id FROM Authors a
518 INNER JOIN Books b ON b.author=a.id
519 LEFT OUTER JOIN Series s on s.id=b.series
520 WHERE upper(a.grouping) LIKE $1
521 ORDER BY a.grouping, b.series, b.volume, b.title
523 return exec_id_query(sql, [pattern])
526 def query_books_by_ddc
529 SELECT b.id FROM Classifications c
530 INNER JOIN Books b ON b.classification=c.id
533 return exec_id_query(sql, [])
536 def query_books_by_series_id(id)
539 SELECT b.id FROM Books b
541 ORDER BY b.volume,b.title
543 return exec_id_query(sql, [id])
546 def query_series_by_age(pattern)
552 ORDER BY s.grouping,s.descr
554 return exec_id_query(sql, [pattern])