]> git.decadent.org.uk Git - dak.git/blob - db_access.py
* katie.py (source_exists): expand the list of distributionsthe source may exist...
[dak.git] / db_access.py
1 #!/usr/bin/env python
2
3 # DB access fucntions
4 # Copyright (C) 2000, 2001, 2002, 2003  James Troup <james@nocrew.org>
5 # $Id: db_access.py,v 1.15 2003-02-11 18:09:59 troup 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;
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 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     return r;
60
61 ################################################################################
62
63 def get_suite_id (suite):
64     global suite_id_cache
65
66     if suite_id_cache.has_key(suite):
67         return suite_id_cache[suite]
68
69     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
70     ql = q.getresult();
71     if not ql:
72         return -1;
73
74     suite_id = ql[0][0];
75     suite_id_cache[suite] = suite_id
76
77     return suite_id
78
79 def get_section_id (section):
80     global section_id_cache
81
82     if section_id_cache.has_key(section):
83         return section_id_cache[section]
84
85     q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
86     ql = q.getresult();
87     if not ql:
88         return -1;
89
90     section_id = ql[0][0];
91     section_id_cache[section] = section_id
92
93     return section_id
94
95 def get_priority_id (priority):
96     global priority_id_cache
97
98     if priority_id_cache.has_key(priority):
99         return priority_id_cache[priority]
100
101     q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
102     ql = q.getresult();
103     if not ql:
104         return -1;
105
106     priority_id = ql[0][0];
107     priority_id_cache[priority] = priority_id
108
109     return priority_id
110
111 def get_override_type_id (type):
112     global override_type_id_cache;
113
114     if override_type_id_cache.has_key(type):
115         return override_type_id_cache[type];
116
117     q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type));
118     ql = q.getresult();
119     if not ql:
120         return -1;
121
122     override_type_id = ql[0][0];
123     override_type_id_cache[type] = override_type_id;
124
125     return override_type_id;
126
127 def get_architecture_id (architecture):
128     global architecture_id_cache;
129
130     if architecture_id_cache.has_key(architecture):
131         return architecture_id_cache[architecture];
132
133     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
134     ql = q.getresult();
135     if not ql:
136         return -1;
137
138     architecture_id = ql[0][0];
139     architecture_id_cache[architecture] = architecture_id;
140
141     return architecture_id;
142
143 def get_archive_id (archive):
144     global archive_id_cache
145
146     archive = archive.lower();
147
148     if archive_id_cache.has_key(archive):
149         return archive_id_cache[archive]
150
151     q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive));
152     ql = q.getresult();
153     if not ql:
154         return -1;
155
156     archive_id = ql[0][0]
157     archive_id_cache[archive] = archive_id
158
159     return archive_id
160
161 def get_component_id (component):
162     global component_id_cache
163
164     component = component.lower();
165
166     if component_id_cache.has_key(component):
167         return component_id_cache[component]
168
169     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
170     ql = q.getresult();
171     if not ql:
172         return -1;
173
174     component_id = ql[0][0];
175     component_id_cache[component] = component_id
176
177     return component_id
178
179 def get_location_id (location, component, archive):
180     global location_id_cache
181
182     cache_key = location + '~' + component + '~' + location
183     if location_id_cache.has_key(cache_key):
184         return location_id_cache[cache_key]
185
186     archive_id = get_archive_id (archive)
187     if component != "":
188         component_id = get_component_id (component)
189         if component_id != -1:
190             q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
191     else:
192         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
193     ql = q.getresult();
194     if not ql:
195         return -1;
196
197     location_id = ql[0][0]
198     location_id_cache[cache_key] = location_id
199
200     return location_id
201
202 def get_source_id (source, version):
203     global source_id_cache
204
205     cache_key = source + '~' + version + '~'
206     if source_id_cache.has_key(cache_key):
207         return source_id_cache[cache_key]
208
209     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
210
211     if not q.getresult():
212         return None
213
214     source_id = q.getresult()[0][0]
215     source_id_cache[cache_key] = source_id
216
217     return source_id
218
219 ################################################################################
220
221 def get_or_set_maintainer_id (maintainer):
222     global maintainer_id_cache
223
224     if maintainer_id_cache.has_key(maintainer):
225         return maintainer_id_cache[maintainer]
226
227     q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
228     if not q.getresult():
229         projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
230         q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
231     maintainer_id = q.getresult()[0][0]
232     maintainer_id_cache[maintainer] = maintainer_id
233
234     return maintainer_id
235
236 ################################################################################
237
238 def get_or_set_uid_id (uid):
239     global uid_id_cache;
240
241     if uid_id_cache.has_key(uid):
242         return uid_id_cache[uid];
243
244     q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
245     if not q.getresult():
246         projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid));
247         q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid));
248     uid_id = q.getresult()[0][0];
249     uid_id_cache[uid] = uid_id;
250
251     return uid_id;
252
253 ################################################################################
254
255 def get_or_set_fingerprint_id (fingerprint):
256     global fingerprint_id_cache;
257
258     if fingerprint_id_cache.has_key(fingerprint):
259         return fingerprint_id_cache[fingerprint]
260
261     q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint));
262     if not q.getresult():
263         projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint));
264         q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint));
265     fingerprint_id = q.getresult()[0][0];
266     fingerprint_id_cache[fingerprint] = fingerprint_id;
267
268     return fingerprint_id;
269
270 ################################################################################
271
272 def get_files_id (filename, size, md5sum, location_id):
273     global files_id_cache
274
275     cache_key = "%s~%d" % (filename, location_id);
276
277     if files_id_cache.has_key(cache_key):
278         return files_id_cache[cache_key]
279
280     size = int(size);
281     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
282     ql = q.getresult();
283     if ql:
284         if len(ql) != 1:
285             return -1;
286         ql = ql[0];
287         orig_size = int(ql[1]);
288         orig_md5sum = ql[2];
289         if orig_size != size or orig_md5sum != md5sum:
290             return -2;
291         files_id_cache[cache_key] = ql[0];
292         return files_id_cache[cache_key]
293     else:
294         return None
295
296
297 ################################################################################
298
299 def set_files_id (filename, size, md5sum, location_id):
300     global files_id_cache
301
302     projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id));
303
304     return get_files_id (filename, size, md5sum, location_id);
305
306     ### currval has issues with postgresql 7.1.3 when the table is big;
307     ### it was taking ~3 seconds to return on auric which is very Not
308     ### Cool(tm).
309     ##
310     ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
311     ##ql = q.getresult()[0];
312     ##cache_key = "%s~%d" % (filename, location_id);
313     ##files_id_cache[cache_key] = ql[0]
314     ##return files_id_cache[cache_key];
315
316 ################################################################################
317
318 def get_maintainer (maintainer_id):
319     global maintainer_cache;
320
321     if not maintainer_cache.has_key(maintainer_id):
322         q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id));
323         maintainer_cache[maintainer_id] = q.getresult()[0][0];
324
325     return maintainer_cache[maintainer_id];
326
327 ################################################################################