2 # Copyright (C) 2000, 2001 James Troup <james@nocrew.org>
3 # $Id: db_access.py,v 1.11 2002-02-12 23:13:49 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 ############################################################################################
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):
49 ############################################################################################
51 def get_suite_id (suite):
54 if suite_id_cache.has_key(suite):
55 return suite_id_cache[suite]
57 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
63 suite_id_cache[suite] = suite_id
67 def get_section_id (section):
68 global section_id_cache
70 if section_id_cache.has_key(section):
71 return section_id_cache[section]
73 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
78 section_id = ql[0][0];
79 section_id_cache[section] = section_id
83 def get_priority_id (priority):
84 global priority_id_cache
86 if priority_id_cache.has_key(priority):
87 return priority_id_cache[priority]
89 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
94 priority_id = ql[0][0];
95 priority_id_cache[priority] = priority_id
99 def get_override_type_id (type):
100 global override_type_id_cache;
102 if override_type_id_cache.has_key(type):
103 return override_type_id_cache[type];
105 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type));
110 override_type_id = ql[0][0];
111 override_type_id_cache[type] = override_type_id;
113 return override_type_id;
115 def get_architecture_id (architecture):
116 global architecture_id_cache;
118 if architecture_id_cache.has_key(architecture):
119 return architecture_id_cache[architecture];
121 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
126 architecture_id = ql[0][0];
127 architecture_id_cache[architecture] = architecture_id;
129 return architecture_id;
131 def get_archive_id (archive):
132 global archive_id_cache
134 archive = string.lower(archive);
136 if archive_id_cache.has_key(archive):
137 return archive_id_cache[archive]
139 q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive));
144 archive_id = ql[0][0]
145 archive_id_cache[archive] = archive_id
149 def get_component_id (component):
150 global component_id_cache
152 component = string.lower(component);
154 if component_id_cache.has_key(component):
155 return component_id_cache[component]
157 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
162 component_id = ql[0][0];
163 component_id_cache[component] = component_id
167 def get_location_id (location, component, archive):
168 global location_id_cache
170 cache_key = location + '~' + component + '~' + location
171 if location_id_cache.has_key(cache_key):
172 return location_id_cache[cache_key]
174 archive_id = get_archive_id (archive)
176 component_id = get_component_id (component)
177 if component_id != -1:
178 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
180 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
185 location_id = ql[0][0]
186 location_id_cache[cache_key] = location_id
190 def get_source_id (source, version):
191 global source_id_cache
193 cache_key = source + '~' + version + '~'
194 if source_id_cache.has_key(cache_key):
195 return source_id_cache[cache_key]
197 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
199 if not q.getresult():
202 source_id = q.getresult()[0][0]
203 source_id_cache[cache_key] = source_id
207 ##########################################################################################
209 def get_or_set_maintainer_id (maintainer):
210 global maintainer_id_cache
212 if maintainer_id_cache.has_key(maintainer):
213 return maintainer_id_cache[maintainer]
215 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
216 if not q.getresult():
217 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
218 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
219 maintainer_id = q.getresult()[0][0]
220 maintainer_id_cache[maintainer] = maintainer_id
224 ##########################################################################################
226 def get_or_set_fingerprint_id (fingerprint):
227 global fingerprint_id_cache
229 if fingerprint_id_cache.has_key(fingerprint):
230 return fingerprint_id_cache[fingerprint]
232 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
233 if not q.getresult():
234 projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
235 q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
236 fingerprint_id = q.getresult()[0][0]
237 fingerprint_id_cache[fingerprint] = fingerprint_id
239 return fingerprint_id
241 ##########################################################################################
243 def get_files_id (filename, size, md5sum, location_id):
244 global files_id_cache
246 cache_key = "%s~%d" % (filename, location_id);
248 if files_id_cache.has_key(cache_key):
249 return files_id_cache[cache_key]
252 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
258 orig_size = int(ql[1]);
260 if orig_size != size or orig_md5sum != md5sum:
262 files_id_cache[cache_key] = ql[0];
263 return files_id_cache[cache_key]
268 ##########################################################################################
270 def set_files_id (filename, size, md5sum, location_id):
271 global files_id_cache
273 cache_key = "%s~%d" % (filename, location_id);
275 #print "INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id);
276 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id));
277 q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
278 ql = q.getresult()[0];
279 files_id_cache[cache_key] = ql[0]
281 return files_id_cache[cache_key]
283 ##########################################################################################
285 def get_maintainer (maintainer_id):
286 global maintainer_cache;
288 if not maintainer_cache.has_key(maintainer_id):
289 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id));
290 maintainer_cache[maintainer_id] = q.getresult()[0][0];
292 return maintainer_cache[maintainer_id];
294 ##########################################################################################