4 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006 James Troup <james@nocrew.org>
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.
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.
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
20 ################################################################################
22 import sys, time, types
24 ################################################################################
30 priority_id_cache = {}
31 override_type_id_cache = {}
32 architecture_id_cache = {}
34 component_id_cache = {}
35 location_id_cache = {}
36 maintainer_id_cache = {}
41 fingerprint_id_cache = {}
45 ################################################################################
47 def init (config, sql):
55 sys.stderr.write("query: \"%s\" ... " % (q))
58 time_diff = time.time()-before
59 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
61 sys.stderr.write("int result: %s\n" % (r))
62 elif type(r) is types.NoneType:
63 sys.stderr.write("result: None\n")
65 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
68 ################################################################################
70 def get_suite_id (suite):
73 if suite_id_cache.has_key(suite):
74 return suite_id_cache[suite]
76 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
82 suite_id_cache[suite] = suite_id
86 def get_section_id (section):
87 global section_id_cache
89 if section_id_cache.has_key(section):
90 return section_id_cache[section]
92 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
98 section_id_cache[section] = section_id
102 def get_priority_id (priority):
103 global priority_id_cache
105 if priority_id_cache.has_key(priority):
106 return priority_id_cache[priority]
108 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
113 priority_id = ql[0][0]
114 priority_id_cache[priority] = priority_id
118 def get_override_type_id (type):
119 global override_type_id_cache
121 if override_type_id_cache.has_key(type):
122 return override_type_id_cache[type]
124 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
129 override_type_id = ql[0][0]
130 override_type_id_cache[type] = override_type_id
132 return override_type_id
134 def get_architecture_id (architecture):
135 global architecture_id_cache
137 if architecture_id_cache.has_key(architecture):
138 return architecture_id_cache[architecture]
140 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
145 architecture_id = ql[0][0]
146 architecture_id_cache[architecture] = architecture_id
148 return architecture_id
150 def get_archive_id (archive):
151 global archive_id_cache
153 archive = archive.lower()
155 if archive_id_cache.has_key(archive):
156 return archive_id_cache[archive]
158 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
163 archive_id = ql[0][0]
164 archive_id_cache[archive] = archive_id
168 def get_component_id (component):
169 global component_id_cache
171 component = component.lower()
173 if component_id_cache.has_key(component):
174 return component_id_cache[component]
176 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
181 component_id = ql[0][0]
182 component_id_cache[component] = component_id
186 def get_location_id (location, component, archive):
187 global location_id_cache
189 cache_key = location + '_' + component + '_' + location
190 if location_id_cache.has_key(cache_key):
191 return location_id_cache[cache_key]
193 archive_id = get_archive_id (archive)
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))
199 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
204 location_id = ql[0][0]
205 location_id_cache[cache_key] = location_id
209 def get_source_id (source, version):
210 global source_id_cache
212 cache_key = source + '_' + version + '_'
213 if source_id_cache.has_key(cache_key):
214 return source_id_cache[cache_key]
216 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
218 if not q.getresult():
221 source_id = q.getresult()[0][0]
222 source_id_cache[cache_key] = source_id
226 def get_testing_version(source):
227 global testing_version_cache
229 if testing_version_cache.has_key(source):
230 return testing_version_cache[source]
232 q = Upload.projectB.query("""
233 SELECT s.version FROM source s, suite su, src_associations sa
236 AND su.suite_name='testing'
240 if not q.getresult():
243 version = q.getresult()[0][0]
244 testing_version_cache[source] = version
248 ################################################################################
250 def get_or_set_maintainer_id (maintainer):
251 global maintainer_id_cache
253 if maintainer_id_cache.has_key(maintainer):
254 return maintainer_id_cache[maintainer]
256 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
257 if not q.getresult():
258 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
259 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
260 maintainer_id = q.getresult()[0][0]
261 maintainer_id_cache[maintainer] = maintainer_id
265 ################################################################################
267 def get_or_set_keyring_id (keyring):
268 global keyring_id_cache
270 if keyring_id_cache.has_key(keyring):
271 return keyring_id_cache[keyring]
273 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
274 if not q.getresult():
275 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
276 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
277 keyring_id = q.getresult()[0][0]
278 keyring_id_cache[keyring] = keyring_id
282 ################################################################################
284 def get_or_set_uid_id (uid):
287 if uid_id_cache.has_key(uid):
288 return uid_id_cache[uid]
290 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
291 if not q.getresult():
292 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
293 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
294 uid_id = q.getresult()[0][0]
295 uid_id_cache[uid] = uid_id
299 ################################################################################
301 def get_or_set_fingerprint_id (fingerprint):
302 global fingerprint_id_cache
304 if fingerprint_id_cache.has_key(fingerprint):
305 return fingerprint_id_cache[fingerprint]
307 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
308 if not q.getresult():
309 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
310 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
311 fingerprint_id = q.getresult()[0][0]
312 fingerprint_id_cache[fingerprint] = fingerprint_id
314 return fingerprint_id
316 ################################################################################
318 def get_files_id (filename, size, md5sum, location_id):
319 global files_id_cache
321 cache_key = "%s_%d" % (filename, location_id)
323 if files_id_cache.has_key(cache_key):
324 return files_id_cache[cache_key]
327 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
333 orig_size = int(ql[1])
335 if orig_size != size or orig_md5sum != md5sum:
337 files_id_cache[cache_key] = ql[0]
338 return files_id_cache[cache_key]
342 ################################################################################
344 def get_or_set_queue_id (queue):
345 global queue_id_cache
347 if queue_id_cache.has_key(queue):
348 return queue_id_cache[queue]
350 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
351 if not q.getresult():
352 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
353 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
354 queue_id = q.getresult()[0][0]
355 queue_id_cache[queue] = queue_id
359 ################################################################################
361 def set_files_id (filename, size, md5sum, location_id):
362 global files_id_cache
364 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id))
366 return get_files_id (filename, size, md5sum, location_id)
368 ### currval has issues with postgresql 7.1.3 when the table is big
369 ### it was taking ~3 seconds to return on auric which is very Not
372 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
373 ##ql = q.getresult()[0]
374 ##cache_key = "%s_%d" % (filename, location_id)
375 ##files_id_cache[cache_key] = ql[0]
376 ##return files_id_cache[cache_key]
378 ################################################################################
380 def get_maintainer (maintainer_id):
381 global maintainer_cache
383 if not maintainer_cache.has_key(maintainer_id):
384 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
385 maintainer_cache[maintainer_id] = q.getresult()[0][0]
387 return maintainer_cache[maintainer_id]
389 ################################################################################