11 @basePath + '/csv/unclassified.csv'
14 def initialize(config_file)
17 config = IniFile.load(config_file)
19 puts 'FATAL: Failed to load config file "' + config_file + '". Aborting initialization.'
23 section = config['database']
24 @dbhost = section['host']
26 @dbname = section['name']
27 @dbuser = section['user']
28 @dbpass = section['pass']
30 section = config['filesystem']
31 @basePath = section['basePath']
35 # @conn = PGconn.connect('localhost', 5432, '', '', 'quanlib', 'quanlib', 'quanlib')
36 @conn = 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),
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 #puts 'find_classification("' + author_grouping.inspect + '", "' + title_grouping.inspect + '")...'
293 sql = "SELECT id FROM Classifications WHERE author_grouping = $1 AND title_grouping = $2;"
294 @conn.exec_params(sql, [author_grouping, title_grouping]) do |rs|
296 #puts ' --> ' + rs[0]['id'].inspect
304 def load_classification(id)
305 sql = "SELECT ddc, lcc, author_grouping, author_sort, title_grouping, title "
306 sql += " FROM Classifications WHERE id=$1"
307 @conn.exec_params(sql, [id]) do |rs|
312 author_grouping = row['author_grouping']
313 author = row['author_sort']
314 title_grouping = row['title_grouping']
317 result = Classification.new(ddc, lcc, author_grouping, author, title_grouping, title)
331 mime_type = 'application/octet-stream'
333 sql = "SELECT mimeType FROM Efs WHERE id=$1"
334 @conn.exec_params(sql, [id]) do |rs|
336 raise "Expected one row but got " + rs.ntuples + ": " + sql + ": " + id
338 mime_type = rs[0]['mimeType']
341 (efspath, efsname) = construct_efs_path(id)
343 fullpath = @basePath + '/efs/' + efspath + '/' + efsname
345 return Cover.new(nil, fullpath, mime_type)
347 #File.open(fullpath, 'rb') do |is|
348 # return Cover.new(is, fullpath, mime_type)
354 def store_cover(book)
362 @conn.exec("SELECT nextval('efs_id')") do |rs|
363 efs_id = rs[0]['nextval']
370 (efspath, efsname) = construct_efs_path(efs_id)
372 efspath = @basePath + '/efs/' + efspath
374 FileUtils.mkdir_p(efspath)
376 (filepath, mimetype) = cover.write_image(efspath, efsname)
378 sql = "INSERT INTO efs VALUES ($1, $2)"
380 rs = @conn.exec_params(sql, [efs_id, mimetype])
381 rescue Exception => e
382 puts sql + ": " + efs_id + ", " + mimetype
389 return efs_id, mimetype
392 def exec_id_query(sql, args)
394 @conn.exec_params(sql, args) do |rs|
402 def exec_update(sql, args)
404 rs = @conn.exec_params(sql, args)
405 rescue Exception => e
406 puts sql + ": " + args.inspect()
414 def next_id(seq_name)
416 @conn.exec("SELECT nextval('" + seq_name + "');") do |rs|
417 id = rs[0]['nextval']
422 def get_series(grouping, code)
427 sql = "SELECT id FROM Series WHERE grouping=$1 AND code=$2;"
428 args = [grouping, code]
429 @conn.exec_params(sql, args).each do |row|
433 # TODO: Create a new series object here?
434 puts 'WARNING: series("' + grouping + '", "' + code + '") not found.'
439 sql = "SELECT age,genre,grouping,code,descr FROM Series WHERE id=$1;"
441 @conn.exec_params(sql, args) do |rs|
444 series = Series.new(id)
445 series.age = row['age']
446 series.genre = row['genre']
447 series.grouping = row['grouping']
448 series.code = row['code']
449 series.descr = row['descr']
456 def populate_classifications_table
457 puts "Populating the Classifications table..."
459 CSV.foreach(@basePath + '/csv/class.csv') do |row|
461 # skip the header row
465 # First, add a row to the Classifications table
467 id = next_id('classification_id')
470 author_grouping = row[2]
472 title_grouping = row[4]
475 sqlInsert = "INSERT INTO Classifications (id, ddc, lcc, author_grouping, author_sort, title_grouping, title) VALUES ($1, $2, $3, $4, $5, $6, $7);"
476 args = [id, ddc, lcc, author_grouping, author_sort, title_grouping, title]
477 exec_update(sqlInsert, args)
479 # Second, link up with the appropriate FAST table entries
484 fast = input.split(';')
487 fast.each do |fast_id|
488 sqlInsert = "INSERT INTO FAST_Classifications (fast, classification) VALUES ($1, $2);"
490 exec_update(sqlInsert, args)
496 def populate_fast_table
497 puts "Populating the FAST table..."
499 CSV.foreach(@basePath + '/csv/fast.csv') do |row|
501 first = false # skip the header row
505 sqlInsert = "INSERT INTO FAST (id, descr) VALUES ($1, $2);"
506 exec_update(sqlInsert, [id, descr])
511 def populate_series_table
512 puts "Populating the Series table..."
513 CSV.foreach(@basePath + '/csv/series.csv') do |row|
514 id = next_id('series_id')
515 sqlInsert = "INSERT INTO Series (id, age, genre, grouping, code, descr) VALUES ($1, $2, $3, $4, $5, $6);"
517 exec_update(sqlInsert, args)
521 def query_books_by_author(pattern)
524 SELECT b.id FROM Authors a
525 INNER JOIN Books b ON b.author=a.id
526 LEFT OUTER JOIN Series s on s.id=b.series
527 WHERE upper(a.grouping) LIKE $1
528 ORDER BY a.grouping, b.series, b.volume, b.title
530 return exec_id_query(sql, [pattern])
533 def query_books_by_ddc
536 SELECT b.id FROM Classifications c
537 INNER JOIN Books b ON b.classification=c.id
540 return exec_id_query(sql, [])
543 def query_books_by_series_id(id)
546 SELECT b.id FROM Books b
548 ORDER BY b.volume,b.title
550 return exec_id_query(sql, [id])
553 def query_series_by_age(pattern)
559 ORDER BY s.grouping,s.descr
561 return exec_id_query(sql, [pattern])