2 # Copyright (C) 2000, 2001, 2002 James Troup <james@nocrew.org>
3 # $Id: db_access.py,v 1.13 2002-05-03 16:06:45 troup Exp $
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ############################################################################################
21 import string, sys, time;
23 ############################################################################################
28 section_id_cache = {};
29 priority_id_cache = {};
30 override_type_id_cache = {};
31 architecture_id_cache = {};
32 archive_id_cache = {};
33 component_id_cache = {};
34 location_id_cache = {};
35 maintainer_id_cache = {};
38 maintainer_cache = {};
39 fingerprint_id_cache = {};
41 ############################################################################################
43 def init (config, sql):
51 sys.stderr.write("query: \"%s\" ... " % (q));
53 r = projectB.query(q);
54 time_diff = time.time()-before;
55 sys.stderr.write("took %.3f seconds.\n" % (time_diff));
58 ############################################################################################
60 def get_suite_id (suite):
63 if suite_id_cache.has_key(suite):
64 return suite_id_cache[suite]
66 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
72 suite_id_cache[suite] = suite_id
76 def get_section_id (section):
77 global section_id_cache
79 if section_id_cache.has_key(section):
80 return section_id_cache[section]
82 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
87 section_id = ql[0][0];
88 section_id_cache[section] = section_id
92 def get_priority_id (priority):
93 global priority_id_cache
95 if priority_id_cache.has_key(priority):
96 return priority_id_cache[priority]
98 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
103 priority_id = ql[0][0];
104 priority_id_cache[priority] = priority_id
108 def get_override_type_id (type):
109 global override_type_id_cache;
111 if override_type_id_cache.has_key(type):
112 return override_type_id_cache[type];
114 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type));
119 override_type_id = ql[0][0];
120 override_type_id_cache[type] = override_type_id;
122 return override_type_id;
124 def get_architecture_id (architecture):
125 global architecture_id_cache;
127 if architecture_id_cache.has_key(architecture):
128 return architecture_id_cache[architecture];
130 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
135 architecture_id = ql[0][0];
136 architecture_id_cache[architecture] = architecture_id;
138 return architecture_id;
140 def get_archive_id (archive):
141 global archive_id_cache
143 archive = string.lower(archive);
145 if archive_id_cache.has_key(archive):
146 return archive_id_cache[archive]
148 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive));
153 archive_id = ql[0][0]
154 archive_id_cache[archive] = archive_id
158 def get_component_id (component):
159 global component_id_cache
161 component = string.lower(component);
163 if component_id_cache.has_key(component):
164 return component_id_cache[component]
166 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
171 component_id = ql[0][0];
172 component_id_cache[component] = component_id
176 def get_location_id (location, component, archive):
177 global location_id_cache
179 cache_key = location + '~' + component + '~' + location
180 if location_id_cache.has_key(cache_key):
181 return location_id_cache[cache_key]
183 archive_id = get_archive_id (archive)
185 component_id = get_component_id (component)
186 if component_id != -1:
187 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
189 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
194 location_id = ql[0][0]
195 location_id_cache[cache_key] = location_id
199 def get_source_id (source, version):
200 global source_id_cache
202 cache_key = source + '~' + version + '~'
203 if source_id_cache.has_key(cache_key):
204 return source_id_cache[cache_key]
206 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
208 if not q.getresult():
211 source_id = q.getresult()[0][0]
212 source_id_cache[cache_key] = source_id
216 ##########################################################################################
218 def get_or_set_maintainer_id (maintainer):
219 global maintainer_id_cache
221 if maintainer_id_cache.has_key(maintainer):
222 return maintainer_id_cache[maintainer]
224 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
225 if not q.getresult():
226 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
227 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
228 maintainer_id = q.getresult()[0][0]
229 maintainer_id_cache[maintainer] = maintainer_id
233 ##########################################################################################
235 def get_or_set_fingerprint_id (fingerprint):
236 global fingerprint_id_cache
238 if fingerprint_id_cache.has_key(fingerprint):
239 return fingerprint_id_cache[fingerprint]
241 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
242 if not q.getresult():
243 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
244 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
245 fingerprint_id = q.getresult()[0][0]
246 fingerprint_id_cache[fingerprint] = fingerprint_id
248 return fingerprint_id
250 ##########################################################################################
252 def get_files_id (filename, size, md5sum, location_id):
253 global files_id_cache
255 cache_key = "%s~%d" % (filename, location_id);
257 if files_id_cache.has_key(cache_key):
258 return files_id_cache[cache_key]
261 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
267 orig_size = int(ql[1]);
269 if orig_size != size or orig_md5sum != md5sum:
271 files_id_cache[cache_key] = ql[0];
272 return files_id_cache[cache_key]
277 ##########################################################################################
279 def set_files_id (filename, size, md5sum, location_id):
280 global files_id_cache
282 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id));
284 return get_files_id (filename, size, md5sum, location_id);
286 ### currval has issues with postgresql 7.1.3 when the table is big;
287 ### it was taking ~3 seconds to return on auric which is very Not
290 ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
291 ##ql = q.getresult()[0];
292 ##cache_key = "%s~%d" % (filename, location_id);
293 ##files_id_cache[cache_key] = ql[0]
294 ##return files_id_cache[cache_key];
296 ##########################################################################################
298 def get_maintainer (maintainer_id):
299 global maintainer_cache;
301 if not maintainer_cache.has_key(maintainer_id):
302 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id));
303 maintainer_cache[maintainer_id] = q.getresult()[0][0];
305 return maintainer_cache[maintainer_id];
307 ##########################################################################################