7 @basepath = '/home/chris/prog/quanlib/efs' # TODO: FIXME: configure this in a sane way
10 #@dburl = 'dbi:Pg:quanlib:localhost'
19 # @conn = PGconn.connect('localhost', 5432, '', '', 'quanlib', 'quanlib', 'quanlib')
20 @conn = PG.connect('localhost', 5432, '', '', 'quanlib', 'quanlib', 'quanlib')
28 def construct_efs_path(efs_id)
29 id_str = sprintf('%010d', efs_id)
30 path = sprintf('%s/%s/%s/%s', id_str[0,2], id_str[2,2], id_str[4,2], id_str[6,2])
31 name = id_str + '.dat'
38 CREATE TABLE Authors (
39 id SERIAL PRIMARY KEY,
49 id SERIAL PRIMARY KEY,
50 author INTEGER REFERENCES Authors(id),
72 'CREATE SEQUENCE efs_id;'
83 'DROP TABLE Authors;',
85 'DROP SEQUENCE efs_id;'
94 sql = "SELECT 1 FROM pg_tables WHERE tableowner='quanlib' AND tablename='books'"
96 @conn.exec(sql).each do |row|
105 def find_author(author)
106 sqlSelect = "SELECT id FROM Authors WHERE grouping=$1 AND reading=$2 AND sort=$3;"
107 args = [author.grouping, author.reading_order, author.sort_order]
108 @conn.exec_params(sqlSelect, args) do |rs|
116 def store_author(author)
117 id = find_author(author)
119 sqlInsert = "INSERT INTO Authors(grouping, reading, sort) VALUES ($1, $2, $3);"
120 args = [author.grouping, author.reading_order, author.sort_order]
122 rs = @conn.exec_params(sqlInsert, args)
123 rescue Exception => e
124 puts sqlInsert + ": " + args.inspect()
131 return find_author(author)
135 sql = "INSERT INTO Books (author, cover, description, path, series, title, volume) VALUES ($1, $2, $3, $4, $5, $6, $7);"
137 author_id = store_author(book.author)
138 (efs_id, mime_type) = store_cover(book)
140 args = [author_id, efs_id, book.description(), book.path(), book.series(), book.title(), book.volume()]
143 rs = @conn.exec_params(sql, args)
144 rescue Exception => e
145 puts sql + ": " + args.inspect()
153 def store_cover(book)
161 @conn.exec("SELECT nextval('efs_id')") do |rs|
162 efs_id = rs[0]['nextval']
169 (efspath, efsname) = construct_efs_path(efs_id)
171 efspath = @basepath + '/' + efspath
173 FileUtils.mkdir_p(efspath)
175 (filepath, mimetype) = cover.writeImage(efspath, efsname)
177 sql = "INSERT INTO efs VALUES ($1, $2)"
179 rs = @conn.exec_params(sql, [efs_id, mimetype])
180 rescue Exception => e
187 return efs_id, mimetype