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