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|
117 sqlSelect = "SELECT grouping, reading, sort FROM Authors WHERE id=$1"
119 @conn.exec_params(sqlSelect, args) do |rs|
121 raise "Expected 1 row for " + id + " but got " + rs.ntuples + ": " + sqlSelect
124 return Author.new(row['grouping'], row['reading'], row['sort'])
128 def store_author(author)
129 id = find_author(author)
131 sqlInsert = "INSERT INTO Authors(grouping, reading, sort) VALUES ($1, $2, $3);"
132 args = [author.grouping, author.reading_order, author.sort_order]
134 rs = @conn.exec_params(sqlInsert, args)
135 rescue Exception => e
136 puts sqlInsert + ": " + args.inspect()
143 return find_author(author)
147 sql = "SELECT author, cover, description, path, series, title, volume FROM Books WHERE id=$1;"
151 @conn.exec_params(sql, [id]) do |rs|
153 raise 'Expected one row in Books for id ' + id + ', but found ' + rs.length + '.'
159 book.author = load_author(row['author'])
160 book.cover = load_cover(row['cover'])
161 book.description = row['description']
162 book.path = row['path']
163 book.series = row['series']
164 book.title = row['title']
165 book.volume = row['volume']
167 rescue Exception => e
176 sql = "INSERT INTO Books (author, cover, description, path, series, title, volume) VALUES ($1, $2, $3, $4, $5, $6, $7);"
178 author_id = store_author(book.author)
179 (efs_id, mime_type) = store_cover(book)
181 args = [author_id, efs_id, book.description(), book.path(), book.series(), book.title(), book.volume()]
184 rs = @conn.exec_params(sql, args)
185 rescue Exception => e
186 puts sql + ": " + args.inspect()
195 (efspath, efsname) = construct_efs_path(id)
196 efspath = @basepath + '/' + efspath
198 cover.load_image(efspath + '/' + efsname)
202 def store_cover(book)
210 @conn.exec("SELECT nextval('efs_id')") do |rs|
211 efs_id = rs[0]['nextval']
218 (efspath, efsname) = construct_efs_path(efs_id)
220 efspath = @basepath + '/' + efspath
222 FileUtils.mkdir_p(efspath)
224 (filepath, mimetype) = cover.write_image(efspath, efsname)
226 sql = "INSERT INTO efs VALUES ($1, $2)"
228 rs = @conn.exec_params(sql, [efs_id, mimetype])
229 rescue Exception => e
236 return efs_id, mimetype