84a999dca328d142b1703b5f0fb507a9e8877ae1
[openlib.git] / db.py
1 #!/usr/bin/python
2
3 import MySQLdb
4 import ConfigParser
5
6 def SortName(name):
7   if database:
8     c = database.cursor()
9     c.execute('SELECT metadata.SortStr(%s)', (name))
10     return c.fetchone()[0]
11   else:
12     print "No connection to DB"
13     exit()
14
15 def SortAuthorName(name):
16   if database:
17     c = database.cursor()
18     c.execute('SELECT metadata.SortAuthor(%s)', (name))
19     return c.fetchone()[0]
20   else:
21     print "No connection to DB"
22     exit()
23
24 def GetOrCreateAuthor(name):
25   if database:
26     c = database.cursor()
27     c.execute('SELECT metadata.GetOrCreateAuthor(%s)', (name))
28     return c.fetchone()[0]
29   else:
30     print "No connection to DB"
31     exit()
32
33 def GetOrCreateLang(name):
34   if database:
35     c = database.cursor()
36     c.execute('SELECT metadata.GetOrCreateLang(%s)', (name))
37     return c.fetchone()[0]
38   else:
39     print "No connection to DB"
40     exit()
41
42 def GetOrCreatePublisher(name):
43   if database:
44     c = database.cursor()
45     c.execute('SELECT metadata.GetOrCreatePublisher(%s)', (name))
46     return c.fetchone()[0]
47   else:
48     print "No connection to DB"
49     exit()
50
51 def GetOrCreateSeries(name):
52   if database:
53     c = database.cursor()
54     c.execute('SELECT metadata.GetOrCreateSeries(%s)', (name))
55     return c.fetchone()[0]
56   else:
57     print "No connection to DB"
58     exit()
59
60 def GetOrCreateTag(name):
61   if database:
62     c = database.cursor()
63     c.execute('SELECT metadata.GetOrCreateTag(%s)', (name))
64     return c.fetchone()[0]
65   else:
66     print "No connection to DB"
67     exit()
68
69 def CreateBook(title,pubdate,series_index,isbn):
70   if database:
71     c = database.cursor()
72     c.execute('SELECT metadata.CreateBook(%s,%s,%s,%s)', (title,pubdate,series_index,isbn))
73     return c.fetchone()[0]
74   else:
75     print "No connection to DB"
76     exit()
77
78 def LinkBookToAuthors(book_id,author_ids):
79   if database:
80     c = database.cursor()
81     for author_id in author_ids:
82       c.execute('INSERT INTO metadata.books_authors_link(book,author) VALUES (%s,%s)', (book_id,author_id))
83   else:
84     print "No connection to DB"
85     exit()
86   
87 def LinkBookToLangs(book_id,lang_ids):
88   if database:
89     c = database.cursor()
90     io = 0
91     for lang_id in lang_ids:
92       io = io + 1
93       c.execute('INSERT INTO metadata.books_languages_link(book,lang_code,item_order) VALUES (%s,%s,%s)', (book_id,lang_id,io))
94   else:
95     print "No connection to DB"
96     exit()
97
98 def LinkBookToPublishers(book_id,pub_id):
99   if database:
100     c = database.cursor()
101     c.execute('INSERT INTO metadata.books_publishers_link(book,publisher) VALUES (%s,%s)', (book_id,pub_id))
102   else:
103     print "No connection to DB"
104     exit()
105
106 def LinkBookToSeries(book_id,ser_id):
107   if database:
108     c = database.cursor()
109     c.execute('INSERT INTO metadata.books_series_link(book,series) VALUES (%s,%s)', (book_id,ser_id))
110   else:
111     print "No connection to DB"
112     exit()
113
114 def LinkBookToTags(book_id,tag_ids):
115   if database:
116     c = database.cursor()
117     for tag_id in tag_ids:
118       c.execute('INSERT INTO metadata.books_tags_link(book,tag) VALUES (%s,%s)', (book_id,tag_id))
119   else:
120     print "No connection to DB"
121     exit()
122
123 def SetPath(book_id,path,dataname,filesize,cover):
124   if database:
125     c = database.cursor()
126     c.execute('UPDATE metadata.books SET path=%s, has_cover=%s WHERE id=%s', (path,cover,book_id))
127     c.execute('INSERT INTO metadata.data(book,format,uncompressed_size,name) values (%s,%s,%s,%s)',(book_id,'FB2',filesize,dataname))
128   else:
129     print "No connection to DB"
130     exit()
131
132 def StoreComment(book_id,comment):
133   if database:
134     c = database.cursor()
135     c.execute('INSERT INTO metadata.comments(book,text) values (%s,%s)',(book_id,comment))
136   else:
137     print "No connection to DB"
138     exit()
139   
140 def PathByID(book_id):
141   if database:
142     c = database.cursor()
143     c.execute('SELECT path FROM metadata.books WHERE id=%s',(book_id))
144     return c.fetchone()[0]
145   else:
146     print "No connection to DB"
147     exit()
148
149 def DataByID(book_id,format):
150   if database:
151     c = database.cursor()
152     c.execute('SELECT name FROM metadata.data WHERE book=%s and format=%s',(book_id,format))
153     return c.fetchone()[0]+'.'+format.lower()
154   else:
155     print "No connection to DB"
156     exit()
157
158 def DelBook(book_id):
159   if database:
160     c = database.cursor()
161     c.execute('DELETE FROM metadata.books WHERE id=%s',(book_id))
162   else:
163     print "No connection to DB"
164     exit()
165
166 def ChangeBookFormat(book_id,old_format,new_format):
167   if database:
168     c = database.cursor()
169     c.execute('UPDATE metadata.data SET format=%s WHERE book=%s and format=%s',(new_format,book_id,old_format))
170   else:
171     print "No connection to DB"
172     exit()
173
174 def TestArchive(name):
175   if database:
176     c = database.cursor()
177     c.execute('SELECT count(*) from metadata.processed_archives WHERE filename=%s',(name))
178     return c.fetchone()[0]
179   else:
180     print "No connection to DB"
181     exit()
182
183 def MarkArchive(name):
184   if database:
185     c = database.cursor()
186     c.execute('insert into metadata.processed_archives(filename) values (%s)',(name))
187   else:
188     print "No connection to DB"
189     exit()
190
191 def ListByFormat(format,limit=100):
192   if database:
193     c = database.cursor()
194     c.execute('SELECT DISTINCT book FROM metadata.data WHERE format=%s ORDER BY book LIMIT 0,%s',(format,limit))
195     return c.fetchall()
196   else:
197     print "No connection to DB"
198     exit()
199
200 def Commit():
201   if database:
202     database.commit()
203   else:
204     print "No connection to DB"
205     exit()
206
207 def Rollback():
208   if database:
209     database.rollback()
210   else:
211     print "No connection to DB"
212     exit()
213
214
215 try:
216
217   cfg = ConfigParser.RawConfigParser(allow_no_value=True)
218   cfg.readfp(open('/etc/openlib.conf'))
219   dbhost = cfg.get("mysql","host")
220   dbuser = cfg.get("mysql","user")
221   dbpasswd = cfg.get("mysql","passwd")
222   file_root = cfg.get("storage","files")
223   tmp_files = cfg.get("storage","temp")
224   failed_files = cfg.get("storage","failed")
225   upload_files = cfg.get("storage","upload")
226    
227 except:
228
229   print "Error reading configuration file"
230   exit()
231
232 try:
233
234   database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,use_unicode=True)
235   database.set_character_set('utf8')
236   c = database.cursor()
237   c.execute('SET NAMES utf8;')
238
239 except:
240
241   print "Error connecting database"
242   exit()
243