2 # Copyright (C) 2000, 2001 James Troup <james@nocrew.org>
3 # $Id: db_access.py,v 1.8 2001-09-27 01:12:42 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
21 ############################################################################################
26 section_id_cache = {};
27 priority_id_cache = {};
28 override_type_id_cache = {};
29 architecture_id_cache = {};
30 archive_id_cache = {};
31 component_id_cache = {};
32 location_id_cache = {};
33 maintainer_id_cache = {};
38 ############################################################################################
40 def init (config, sql):
46 ############################################################################################
48 def get_suite_id (suite):
51 if suite_id_cache.has_key(suite):
52 return suite_id_cache[suite]
54 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
60 suite_id_cache[suite] = suite_id
64 def get_section_id (section):
65 global section_id_cache
67 if section_id_cache.has_key(section):
68 return section_id_cache[section]
70 q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
75 section_id = ql[0][0];
76 section_id_cache[section] = section_id
80 def get_priority_id (priority):
81 global priority_id_cache
83 if priority_id_cache.has_key(priority):
84 return priority_id_cache[priority]
86 q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
91 priority_id = ql[0][0];
92 priority_id_cache[priority] = priority_id
96 def get_override_type_id (type):
97 global override_type_id_cache;
99 if override_type_id_cache.has_key(type):
100 return override_type_id_cache[type];
102 q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type));
107 override_type_id = ql[0][0];
108 override_type_id_cache[type] = override_type_id;
110 return override_type_id;
112 def get_architecture_id (architecture):
113 global architecture_id_cache;
115 if architecture_id_cache.has_key(architecture):
116 return architecture_id_cache[architecture];
118 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
123 architecture_id = ql[0][0];
124 architecture_id_cache[architecture] = architecture_id;
126 return architecture_id;
128 def get_archive_id (archive):
129 global archive_id_cache
131 if archive_id_cache.has_key(archive):
132 return archive_id_cache[archive]
134 q = projectB.query("SELECT id FROM archive WHERE name = '%s'" % (archive))
135 archive_id = q.getresult()[0][0]
136 archive_id_cache[archive] = archive_id
140 def get_component_id (component):
141 global component_id_cache
143 if component_id_cache.has_key(component):
144 return component_id_cache[component]
146 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (string.lower(component)))
151 component_id = ql[0][0];
152 component_id_cache[component] = component_id
156 def get_location_id (location, component, archive):
157 global location_id_cache
159 cache_key = location + '~' + component + '~' + location
160 if location_id_cache.has_key(cache_key):
161 return location_id_cache[cache_key]
163 archive_id = get_archive_id (archive)
165 component_id = get_component_id (component)
166 if component_id != -1:
167 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
169 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
170 location_id = q.getresult()[0][0]
171 location_id_cache[cache_key] = location_id
175 def get_source_id (source, version):
176 global source_id_cache
178 cache_key = source + '~' + version + '~'
179 if source_id_cache.has_key(cache_key):
180 return source_id_cache[cache_key]
182 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
184 if not q.getresult():
187 source_id = q.getresult()[0][0]
188 source_id_cache[cache_key] = source_id
192 ##########################################################################################
194 def get_or_set_maintainer_id (maintainer):
195 global maintainer_id_cache
197 if maintainer_id_cache.has_key(maintainer):
198 return maintainer_id_cache[maintainer]
200 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
201 if not q.getresult():
202 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
203 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
204 maintainer_id = q.getresult()[0][0]
205 maintainer_id_cache[maintainer] = maintainer_id
209 ##########################################################################################
211 def get_files_id (filename, size, md5sum, location_id):
212 global files_id_cache
214 cache_key = "%s~%d" % (filename, location_id);
216 if files_id_cache.has_key(cache_key):
217 return files_id_cache[cache_key]
220 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
226 orig_size = int(ql[1]);
228 if orig_size != size or orig_md5sum != md5sum:
230 files_id_cache[cache_key] = ql[0];
231 return files_id_cache[cache_key]
236 ##########################################################################################
238 def set_files_id (filename, size, md5sum, location_id):
239 global files_id_cache
241 cache_key = "%s~%d" % (filename, location_id);
243 #print "INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id);
244 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id));
245 q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
246 ql = q.getresult()[0];
247 files_id_cache[cache_key] = ql[0]
249 return files_id_cache[cache_key]
251 ##########################################################################################
253 def get_maintainer (maintainer_id):
254 global maintainer_cache;
256 if not maintainer_cache.has_key(maintainer_id):
257 q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id));
258 maintainer_cache[maintainer_id] = q.getresult()[0][0];
260 return maintainer_cache[maintainer_id];
262 ##########################################################################################