]> git.decadent.org.uk Git - dak.git/blob - daklib/database.py
f511d27d21900dc1e5c8880801437338bdd7dbdd
[dak.git] / daklib / database.py
1 #!/usr/bin/env python
2
3 # DB access fucntions
4 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006  James Troup <james@nocrew.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 ################################################################################
21
22 import sys, time, types
23
24 ################################################################################
25
26 Cnf = None
27 projectB = None
28 suite_id_cache = {}
29 section_id_cache = {}
30 priority_id_cache = {}
31 override_type_id_cache = {}
32 architecture_id_cache = {}
33 archive_id_cache = {}
34 component_id_cache = {}
35 location_id_cache = {}
36 maintainer_id_cache = {}
37 source_id_cache = {}
38 files_id_cache = {}
39 maintainer_cache = {}
40 fingerprint_id_cache = {}
41 queue_id_cache = {}
42 uid_id_cache = {}
43
44 ################################################################################
45
46 def init (config, sql):
47     global Cnf, projectB
48
49     Cnf = config
50     projectB = sql
51
52
53 def do_query(q):
54     sys.stderr.write("query: \"%s\" ... " % (q))
55     before = time.time()
56     r = projectB.query(q)
57     time_diff = time.time()-before
58     sys.stderr.write("took %.3f seconds.\n" % (time_diff))
59     if type(r) is int:
60         sys.stderr.write("int result: %s\n" % (r))
61     elif type(r) is types.NoneType:
62         sys.stderr.write("result: None\n")
63     else:
64         sys.stderr.write("pgresult: %s\n" % (r.getresult()))
65     return r
66
67 ################################################################################
68
69 def get_suite_id (suite):
70     global suite_id_cache
71
72     if suite_id_cache.has_key(suite):
73         return suite_id_cache[suite]
74
75     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
76     ql = q.getresult()
77     if not ql:
78         return -1
79
80     suite_id = ql[0][0]
81     suite_id_cache[suite] = suite_id
82
83     return suite_id
84
85 def get_section_id (section):
86     global section_id_cache
87
88     if section_id_cache.has_key(section):
89         return section_id_cache[section]
90
91     q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
92     ql = q.getresult()
93     if not ql:
94         return -1
95
96     section_id = ql[0][0]
97     section_id_cache[section] = section_id
98
99     return section_id
100
101 def get_priority_id (priority):
102     global priority_id_cache
103
104     if priority_id_cache.has_key(priority):
105         return priority_id_cache[priority]
106
107     q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
108     ql = q.getresult()
109     if not ql:
110         return -1
111
112     priority_id = ql[0][0]
113     priority_id_cache[priority] = priority_id
114
115     return priority_id
116
117 def get_override_type_id (type):
118     global override_type_id_cache
119
120     if override_type_id_cache.has_key(type):
121         return override_type_id_cache[type]
122
123     q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
124     ql = q.getresult()
125     if not ql:
126         return -1
127
128     override_type_id = ql[0][0]
129     override_type_id_cache[type] = override_type_id
130
131     return override_type_id
132
133 def get_architecture_id (architecture):
134     global architecture_id_cache
135
136     if architecture_id_cache.has_key(architecture):
137         return architecture_id_cache[architecture]
138
139     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
140     ql = q.getresult()
141     if not ql:
142         return -1
143
144     architecture_id = ql[0][0]
145     architecture_id_cache[architecture] = architecture_id
146
147     return architecture_id
148
149 def get_archive_id (archive):
150     global archive_id_cache
151
152     archive = archive.lower()
153
154     if archive_id_cache.has_key(archive):
155         return archive_id_cache[archive]
156
157     q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
158     ql = q.getresult()
159     if not ql:
160         return -1
161
162     archive_id = ql[0][0]
163     archive_id_cache[archive] = archive_id
164
165     return archive_id
166
167 def get_component_id (component):
168     global component_id_cache
169
170     component = component.lower()
171
172     if component_id_cache.has_key(component):
173         return component_id_cache[component]
174
175     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
176     ql = q.getresult()
177     if not ql:
178         return -1
179
180     component_id = ql[0][0]
181     component_id_cache[component] = component_id
182
183     return component_id
184
185 def get_location_id (location, component, archive):
186     global location_id_cache
187
188     cache_key = location + '~' + component + '~' + location
189     if location_id_cache.has_key(cache_key):
190         return location_id_cache[cache_key]
191
192     archive_id = get_archive_id (archive)
193     if component != "":
194         component_id = get_component_id (component)
195         if component_id != -1:
196             q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
197     else:
198         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
199     ql = q.getresult()
200     if not ql:
201         return -1
202
203     location_id = ql[0][0]
204     location_id_cache[cache_key] = location_id
205
206     return location_id
207
208 def get_source_id (source, version):
209     global source_id_cache
210
211     cache_key = source + '~' + version + '~'
212     if source_id_cache.has_key(cache_key):
213         return source_id_cache[cache_key]
214
215     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
216
217     if not q.getresult():
218         return None
219
220     source_id = q.getresult()[0][0]
221     source_id_cache[cache_key] = source_id
222
223     return source_id
224
225 ################################################################################
226
227 def get_or_set_maintainer_id (maintainer):
228     global maintainer_id_cache
229
230     if maintainer_id_cache.has_key(maintainer):
231         return maintainer_id_cache[maintainer]
232
233     q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
234     if not q.getresult():
235         projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
236         q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
237     maintainer_id = q.getresult()[0][0]
238     maintainer_id_cache[maintainer] = maintainer_id
239
240     return maintainer_id
241
242 ################################################################################
243
244 def get_or_set_uid_id (uid):
245     global uid_id_cache
246
247     if uid_id_cache.has_key(uid):
248         return uid_id_cache[uid]
249
250     q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
251     if not q.getresult():
252         projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
253         q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
254     uid_id = q.getresult()[0][0]
255     uid_id_cache[uid] = uid_id
256
257     return uid_id
258
259 ################################################################################
260
261 def get_or_set_fingerprint_id (fingerprint):
262     global fingerprint_id_cache
263
264     if fingerprint_id_cache.has_key(fingerprint):
265         return fingerprint_id_cache[fingerprint]
266
267     q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
268     if not q.getresult():
269         projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
270         q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
271     fingerprint_id = q.getresult()[0][0]
272     fingerprint_id_cache[fingerprint] = fingerprint_id
273
274     return fingerprint_id
275
276 ################################################################################
277
278 def get_files_id (filename, size, md5sum, location_id):
279     global files_id_cache
280
281     cache_key = "%s~%d" % (filename, location_id)
282
283     if files_id_cache.has_key(cache_key):
284         return files_id_cache[cache_key]
285
286     size = int(size)
287     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
288     ql = q.getresult()
289     if ql:
290         if len(ql) != 1:
291             return -1
292         ql = ql[0]
293         orig_size = int(ql[1])
294         orig_md5sum = ql[2]
295         if orig_size != size or orig_md5sum != md5sum:
296             return -2
297         files_id_cache[cache_key] = ql[0]
298         return files_id_cache[cache_key]
299     else:
300         return None
301
302 ################################################################################
303
304 def get_or_set_queue_id (queue):
305     global queue_id_cache
306
307     if queue_id_cache.has_key(queue):
308         return queue_id_cache[queue]
309
310     q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
311     if not q.getresult():
312         projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
313         q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
314     queue_id = q.getresult()[0][0]
315     queue_id_cache[queue] = queue_id
316
317     return queue_id
318
319 ################################################################################
320
321 def set_files_id (filename, size, md5sum, location_id):
322     global files_id_cache
323
324     projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id))
325
326     return get_files_id (filename, size, md5sum, location_id)
327
328     ### currval has issues with postgresql 7.1.3 when the table is big
329     ### it was taking ~3 seconds to return on auric which is very Not
330     ### Cool(tm).
331     ##
332     ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
333     ##ql = q.getresult()[0]
334     ##cache_key = "%s~%d" % (filename, location_id)
335     ##files_id_cache[cache_key] = ql[0]
336     ##return files_id_cache[cache_key]
337
338 ################################################################################
339
340 def get_maintainer (maintainer_id):
341     global maintainer_cache
342
343     if not maintainer_cache.has_key(maintainer_id):
344         q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
345         maintainer_cache[maintainer_id] = q.getresult()[0][0]
346
347     return maintainer_cache[maintainer_id]
348
349 ################################################################################