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 = {}
40 fingerprint_id_cache = {}
44 ################################################################################
46 def init (config, sql):
54 sys.stderr.write("query: \"%s\" ... " % (q))
57 time_diff = time.time()-before
58 sys.stderr.write("took %.3f seconds.\n" % (time_diff))
60 sys.stderr.write("int result: %s\n" % (r))
61 elif type(r) is types.NoneType:
62 sys.stderr.write("result: None\n")
64 sys.stderr.write("pgresult: %s\n" % (r.getresult()))
67 ################################################################################
69 def get_suite_id (suite):
72 if suite_id_cache.has_key(suite):
73 return suite_id_cache[suite]
75 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
81 suite_id_cache[suite] = suite_id
85 def get_section_id (section):
86 global section_id_cache
88 if section_id_cache.has_key(section):
89 return section_id_cache[section]
91 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
97 section_id_cache[section] = section_id
101 def get_priority_id (priority):
102 global priority_id_cache
104 if priority_id_cache.has_key(priority):
105 return priority_id_cache[priority]
107 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
112 priority_id = ql[0][0]
113 priority_id_cache[priority] = priority_id
117 def get_override_type_id (type):
118 global override_type_id_cache
120 if override_type_id_cache.has_key(type):
121 return override_type_id_cache[type]
123 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type))
128 override_type_id = ql[0][0]
129 override_type_id_cache[type] = override_type_id
131 return override_type_id
133 def get_architecture_id (architecture):
134 global architecture_id_cache
136 if architecture_id_cache.has_key(architecture):
137 return architecture_id_cache[architecture]
139 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
144 architecture_id = ql[0][0]
145 architecture_id_cache[architecture] = architecture_id
147 return architecture_id
149 def get_archive_id (archive):
150 global archive_id_cache
152 archive = archive.lower()
154 if archive_id_cache.has_key(archive):
155 return archive_id_cache[archive]
157 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive))
162 archive_id = ql[0][0]
163 archive_id_cache[archive] = archive_id
167 def get_component_id (component):
168 global component_id_cache
170 component = component.lower()
172 if component_id_cache.has_key(component):
173 return component_id_cache[component]
175 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
180 component_id = ql[0][0]
181 component_id_cache[component] = component_id
185 def get_location_id (location, component, archive):
186 global location_id_cache
188 cache_key = location + '~' + component + '~' + location
189 if location_id_cache.has_key(cache_key):
190 return location_id_cache[cache_key]
192 archive_id = get_archive_id (archive)
194 component_id = get_component_id (component)
195 if component_id != -1:
196 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
198 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
203 location_id = ql[0][0]
204 location_id_cache[cache_key] = location_id
208 def get_source_id (source, version):
209 global source_id_cache
211 cache_key = source + '~' + version + '~'
212 if source_id_cache.has_key(cache_key):
213 return source_id_cache[cache_key]
215 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
217 if not q.getresult():
220 source_id = q.getresult()[0][0]
221 source_id_cache[cache_key] = source_id
225 ################################################################################
227 def get_or_set_maintainer_id (maintainer):
228 global maintainer_id_cache
230 if maintainer_id_cache.has_key(maintainer):
231 return maintainer_id_cache[maintainer]
233 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
234 if not q.getresult():
235 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
236 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
237 maintainer_id = q.getresult()[0][0]
238 maintainer_id_cache[maintainer] = maintainer_id
242 ################################################################################
244 def get_or_set_uid_id (uid):
247 if uid_id_cache.has_key(uid):
248 return uid_id_cache[uid]
250 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
251 if not q.getresult():
252 projectB.query("INSERT INTO uid (uid) VALUES ('%s')" % (uid))
253 q = projectB.query("SELECT id FROM uid WHERE uid = '%s'" % (uid))
254 uid_id = q.getresult()[0][0]
255 uid_id_cache[uid] = uid_id
259 ################################################################################
261 def get_or_set_fingerprint_id (fingerprint):
262 global fingerprint_id_cache
264 if fingerprint_id_cache.has_key(fingerprint):
265 return fingerprint_id_cache[fingerprint]
267 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
268 if not q.getresult():
269 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
270 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
271 fingerprint_id = q.getresult()[0][0]
272 fingerprint_id_cache[fingerprint] = fingerprint_id
274 return fingerprint_id
276 ################################################################################
278 def get_files_id (filename, size, md5sum, location_id):
279 global files_id_cache
281 cache_key = "%s~%d" % (filename, location_id)
283 if files_id_cache.has_key(cache_key):
284 return files_id_cache[cache_key]
287 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id))
293 orig_size = int(ql[1])
295 if orig_size != size or orig_md5sum != md5sum:
297 files_id_cache[cache_key] = ql[0]
298 return files_id_cache[cache_key]
302 ################################################################################
304 def get_or_set_queue_id (queue):
305 global queue_id_cache
307 if queue_id_cache.has_key(queue):
308 return queue_id_cache[queue]
310 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
311 if not q.getresult():
312 projectB.query("INSERT INTO queue (queue_name) VALUES ('%s')" % (queue))
313 q = projectB.query("SELECT id FROM queue WHERE queue_name = '%s'" % (queue))
314 queue_id = q.getresult()[0][0]
315 queue_id_cache[queue] = queue_id
319 ################################################################################
321 def set_files_id (filename, size, md5sum, location_id):
322 global files_id_cache
324 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id))
326 return get_files_id (filename, size, md5sum, location_id)
328 ### currval has issues with postgresql 7.1.3 when the table is big
329 ### it was taking ~3 seconds to return on auric which is very Not
332 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')")
333 ##ql = q.getresult()[0]
334 ##cache_key = "%s~%d" % (filename, location_id)
335 ##files_id_cache[cache_key] = ql[0]
336 ##return files_id_cache[cache_key]
338 ################################################################################
340 def get_maintainer (maintainer_id):
341 global maintainer_cache
343 if not maintainer_cache.has_key(maintainer_id):
344 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id))
345 maintainer_cache[maintainer_id] = q.getresult()[0][0]
347 return maintainer_cache[maintainer_id]
349 ################################################################################