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 = {}
44 testing_version_cache = {}
46 ################################################################################
48 def init (config, sql):
56 sys.stderr.write("query: \"%s\" ... " % (q))
59 time_diff = time.time()-before
60 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
62 sys.stderr.write("int result: %s\n" % (r))
63 elif type(r) is types.NoneType:
64 sys.stderr.write("result: None\n")
66 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
69 ################################################################################
71 def get_suite_id (suite):
74 if suite_id_cache.has_key(suite):
75 return suite_id_cache[suite]
77 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
83 suite_id_cache[suite] = suite_id
87 def get_section_id (section):
88 global section_id_cache
90 if section_id_cache.has_key(section):
91 return section_id_cache[section]
93 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
99 section_id_cache[section] = section_id
103 def get_priority_id (priority):
104 global priority_id_cache
106 if priority_id_cache.has_key(priority):
107 return priority_id_cache[priority]
109 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
114 priority_id = ql[0][0]
115 priority_id_cache[priority] = priority_id
119 def get_override_type_id (type):
120 global override_type_id_cache
122 if override_type_id_cache.has_key(type):
123 return override_type_id_cache[type]
125 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
130 override_type_id = ql[0][0]
131 override_type_id_cache[type] = override_type_id
133 return override_type_id
135 def get_architecture_id (architecture):
136 global architecture_id_cache
138 if architecture_id_cache.has_key(architecture):
139 return architecture_id_cache[architecture]
141 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
146 architecture_id = ql[0][0]
147 architecture_id_cache[architecture] = architecture_id
149 return architecture_id
151 def get_archive_id (archive):
152 global archive_id_cache
154 archive = archive.lower()
156 if archive_id_cache.has_key(archive):
157 return archive_id_cache[archive]
159 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
164 archive_id = ql[0][0]
165 archive_id_cache[archive] = archive_id
169 def get_component_id (component):
170 global component_id_cache
172 component = component.lower()
174 if component_id_cache.has_key(component):
175 return component_id_cache[component]
177 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
182 component_id = ql[0][0]
183 component_id_cache[component] = component_id
187 def get_location_id (location, component, archive):
188 global location_id_cache
190 cache_key = location + '_' + component + '_' + location
191 if location_id_cache.has_key(cache_key):
192 return location_id_cache[cache_key]
194 archive_id = get_archive_id (archive)
196 component_id = get_component_id (component)
197 if component_id != -1:
198 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
200 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
205 location_id = ql[0][0]
206 location_id_cache[cache_key] = location_id
210 def get_source_id (source, version):
211 global source_id_cache
213 cache_key = source + '_' + version + '_'
214 if source_id_cache.has_key(cache_key):
215 return source_id_cache[cache_key]
217 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
219 if not q.getresult():
222 source_id = q.getresult()[0][0]
223 source_id_cache[cache_key] = source_id
227 def get_testing_version(source):
228 global testing_version_cache
230 if testing_version_cache.has_key(source):
231 return testing_version_cache[source]
233 q = projectB.query("""
234 SELECT s.version FROM source s, suite su, src_associations sa
237 AND su.suite_name='testing'
241 if not q.getresult():
244 version = q.getresult()[0][0]
245 testing_version_cache[source] = version
249 ################################################################################
251 def get_or_set_maintainer_id (maintainer):
252 global maintainer_id_cache
254 if maintainer_id_cache.has_key(maintainer):
255 return maintainer_id_cache[maintainer]
257 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
258 if not q.getresult():
259 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
260 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
261 maintainer_id = q.getresult()[0][0]
262 maintainer_id_cache[maintainer] = maintainer_id
266 ################################################################################
268 def get_or_set_keyring_id (keyring):
269 global keyring_id_cache
271 if keyring_id_cache.has_key(keyring):
272 return keyring_id_cache[keyring]
274 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
275 if not q.getresult():
276 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
277 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
278 keyring_id = q.getresult()[0][0]
279 keyring_id_cache[keyring] = keyring_id
283 ################################################################################
285 def get_or_set_uid_id (uid):
288 if uid_id_cache.has_key(uid):
289 return uid_id_cache[uid]
291 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
292 if not q.getresult():
293 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
294 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
295 uid_id = q.getresult()[0][0]
296 uid_id_cache[uid] = uid_id
300 ################################################################################
302 def get_or_set_fingerprint_id (fingerprint):
303 global fingerprint_id_cache
305 if fingerprint_id_cache.has_key(fingerprint):
306 return fingerprint_id_cache[fingerprint]
308 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
309 if not q.getresult():
310 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
311 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
312 fingerprint_id = q.getresult()[0][0]
313 fingerprint_id_cache[fingerprint] = fingerprint_id
315 return fingerprint_id
317 ################################################################################
319 def get_files_id (filename, size, md5sum, location_id):
320 global files_id_cache
322 cache_key = "%s_%d" % (filename, location_id)
324 if files_id_cache.has_key(cache_key):
325 return files_id_cache[cache_key]
328 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
334 orig_size = int(ql[1])
336 if orig_size != size or orig_md5sum != md5sum:
338 files_id_cache[cache_key] = ql[0]
339 return files_id_cache[cache_key]
343 ################################################################################
345 def get_or_set_queue_id (queue):
346 global queue_id_cache
348 if queue_id_cache.has_key(queue):
349 return queue_id_cache[queue]
351 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
352 if not q.getresult():
353 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
354 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
355 queue_id = q.getresult()[0][0]
356 queue_id_cache[queue] = queue_id
360 ################################################################################
362 def set_files_id (filename, size, md5sum, location_id):
363 global files_id_cache
365 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id))
367 return get_files_id (filename, size, md5sum, location_id)
369 ### currval has issues with postgresql 7.1.3 when the table is big
370 ### it was taking ~3 seconds to return on auric which is very Not
373 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
374 ##ql = q.getresult()[0]
375 ##cache_key = "%s_%d" % (filename, location_id)
376 ##files_id_cache[cache_key] = ql[0]
377 ##return files_id_cache[cache_key]
379 ################################################################################
381 def get_maintainer (maintainer_id):
382 global maintainer_cache
384 if not maintainer_cache.has_key(maintainer_id):
385 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
386 maintainer_cache[maintainer_id] = q.getresult()[0][0]
388 return maintainer_cache[maintainer_id]
390 ################################################################################