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 $
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.
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.
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
21 ################################################################################
25 ################################################################################
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 = {};
40 maintainer_cache = {};
41 fingerprint_id_cache = {};
44 ################################################################################
46 def init (config, sql):
54 sys.stderr.write("query: \"%s\" ... " % (q));
56 r = projectB.query(q);
57 time_diff = time.time()-before;
58 sys.stderr.write("took %.3f seconds.\n" % (time_diff));
61 ################################################################################
63 def get_suite_id (suite):
66 if suite_id_cache.has_key(suite):
67 return suite_id_cache[suite]
69 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
75 suite_id_cache[suite] = suite_id
79 def get_section_id (section):
80 global section_id_cache
82 if section_id_cache.has_key(section):
83 return section_id_cache[section]
85 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
90 section_id = ql[0][0];
91 section_id_cache[section] = section_id
95 def get_priority_id (priority):
96 global priority_id_cache
98 if priority_id_cache.has_key(priority):
99 return priority_id_cache[priority]
101 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
106 priority_id = ql[0][0];
107 priority_id_cache[priority] = priority_id
111 def get_override_type_id (type):
112 global override_type_id_cache;
114 if override_type_id_cache.has_key(type):
115 return override_type_id_cache[type];
117 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type));
122 override_type_id = ql[0][0];
123 override_type_id_cache[type] = override_type_id;
125 return override_type_id;
127 def get_architecture_id (architecture):
128 global architecture_id_cache;
130 if architecture_id_cache.has_key(architecture):
131 return architecture_id_cache[architecture];
133 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
138 architecture_id = ql[0][0];
139 architecture_id_cache[architecture] = architecture_id;
141 return architecture_id;
143 def get_archive_id (archive):
144 global archive_id_cache
146 archive = archive.lower();
148 if archive_id_cache.has_key(archive):
149 return archive_id_cache[archive]
151 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive));
156 archive_id = ql[0][0]
157 archive_id_cache[archive] = archive_id
161 def get_component_id (component):
162 global component_id_cache
164 component = component.lower();
166 if component_id_cache.has_key(component):
167 return component_id_cache[component]
169 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
174 component_id = ql[0][0];
175 component_id_cache[component] = component_id
179 def get_location_id (location, component, archive):
180 global location_id_cache
182 cache_key = location + '~' + component + '~' + location
183 if location_id_cache.has_key(cache_key):
184 return location_id_cache[cache_key]
186 archive_id = get_archive_id (archive)
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))
192 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
197 location_id = ql[0][0]
198 location_id_cache[cache_key] = location_id
202 def get_source_id (source, version):
203 global source_id_cache
205 cache_key = source + '~' + version + '~'
206 if source_id_cache.has_key(cache_key):
207 return source_id_cache[cache_key]
209 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
211 if not q.getresult():
214 source_id = q.getresult()[0][0]
215 source_id_cache[cache_key] = source_id
219 ################################################################################
221 def get_or_set_maintainer_id (maintainer):
222 global maintainer_id_cache
224 if maintainer_id_cache.has_key(maintainer):
225 return maintainer_id_cache[maintainer]
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
236 ################################################################################
238 def get_or_set_uid_id (uid):
241 if uid_id_cache.has_key(uid):
242 return uid_id_cache[uid];
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;
253 ################################################################################
255 def get_or_set_fingerprint_id (fingerprint):
256 global fingerprint_id_cache;
258 if fingerprint_id_cache.has_key(fingerprint):
259 return fingerprint_id_cache[fingerprint]
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;
268 return fingerprint_id;
270 ################################################################################
272 def get_files_id (filename, size, md5sum, location_id):
273 global files_id_cache
275 cache_key = "%s~%d" % (filename, location_id);
277 if files_id_cache.has_key(cache_key):
278 return files_id_cache[cache_key]
281 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
287 orig_size = int(ql[1]);
289 if orig_size != size or orig_md5sum != md5sum:
291 files_id_cache[cache_key] = ql[0];
292 return files_id_cache[cache_key]
297 ################################################################################
299 def set_files_id (filename, size, md5sum, location_id):
300 global files_id_cache
302 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id));
304 return get_files_id (filename, size, md5sum, location_id);
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
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];
316 ################################################################################
318 def get_maintainer (maintainer_id):
319 global maintainer_cache;
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];
325 return maintainer_cache[maintainer_id];
327 ################################################################################