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 ################################################################################
228 def get_or_set_maintainer_id (maintainer):
229 global maintainer_id_cache
231 if maintainer_id_cache.has_key(maintainer):
232 return maintainer_id_cache[maintainer]
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
243 ################################################################################
245 def get_or_set_keyring_id (keyring):
246 global keyring_id_cache
248 if keyring_id_cache.has_key(keyring):
249 return keyring_id_cache[keyring]
251 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
252 if not q.getresult():
253 projectB.query("INSERT INTO keyrings (name) VALUES ('%s')" % (keyring))
254 q = projectB.query("SELECT id FROM keyrings WHERE name = '%s'" % (keyring))
255 keyring_id = q.getresult()[0][0]
256 keyring_id_cache[keyring] = keyring_id
260 ################################################################################
262 def get_or_set_uid_id (uid):
265 if uid_id_cache.has_key(uid):
266 return uid_id_cache[uid]
268 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
269 if not q.getresult():
270 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
271 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
272 uid_id = q.getresult()[0][0]
273 uid_id_cache[uid] = uid_id
277 ################################################################################
279 def get_or_set_fingerprint_id (fingerprint):
280 global fingerprint_id_cache
282 if fingerprint_id_cache.has_key(fingerprint):
283 return fingerprint_id_cache[fingerprint]
285 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
286 if not q.getresult():
287 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
288 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
289 fingerprint_id = q.getresult()[0][0]
290 fingerprint_id_cache[fingerprint] = fingerprint_id
292 return fingerprint_id
294 ################################################################################
296 def get_files_id (filename, size, md5sum, location_id):
297 global files_id_cache
299 cache_key = "%s_%d" % (filename, location_id)
301 if files_id_cache.has_key(cache_key):
302 return files_id_cache[cache_key]
305 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
311 orig_size = int(ql[1])
313 if orig_size != size or orig_md5sum != md5sum:
315 files_id_cache[cache_key] = ql[0]
316 return files_id_cache[cache_key]
320 ################################################################################
322 def get_or_set_queue_id (queue):
323 global queue_id_cache
325 if queue_id_cache.has_key(queue):
326 return queue_id_cache[queue]
328 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
329 if not q.getresult():
330 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
331 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
332 queue_id = q.getresult()[0][0]
333 queue_id_cache[queue] = queue_id
337 ################################################################################
339 def set_files_id (filename, size, md5sum, location_id):
340 global files_id_cache
342 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id))
344 return get_files_id (filename, size, md5sum, location_id)
346 ### currval has issues with postgresql 7.1.3 when the table is big
347 ### it was taking ~3 seconds to return on auric which is very Not
350 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
351 ##ql = q.getresult()[0]
352 ##cache_key = "%s_%d" % (filename, location_id)
353 ##files_id_cache[cache_key] = ql[0]
354 ##return files_id_cache[cache_key]
356 ################################################################################
358 def get_maintainer (maintainer_id):
359 global maintainer_cache
361 if not maintainer_cache.has_key(maintainer_id):
362 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
363 maintainer_cache[maintainer_id] = q.getresult()[0][0]
365 return maintainer_cache[maintainer_id]
367 ################################################################################