2 # Copyright (C) 2000 James Troup <james@nocrew.org>
3 # $Id: db_access.py,v 1.4 2000-12-18 07:11:25 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
24 architecture_id_cache = {}
26 component_id_cache = {}
27 location_id_cache = {}
28 maintainer_id_cache = {}
32 def init (config, sql):
38 ############################################################################################
40 def get_suite_id (suite):
43 if suite_id_cache.has_key(suite):
44 return suite_id_cache[suite]
46 q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
51 suite_id = q.getresult()[0][0]
52 suite_id_cache[suite] = suite_id
56 def get_architecture_id (architecture):
57 global architecture_id_cache
59 if architecture_id_cache.has_key(architecture):
60 return architecture_id_cache[architecture]
62 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
63 architecture_id = q.getresult()[0][0]
64 architecture_id_cache[architecture] = architecture_id
66 return architecture_id
68 def get_archive_id (archive):
69 global archive_id_cache
71 if archive_id_cache.has_key(archive):
72 return archive_id_cache[archive]
74 q = projectB.query("SELECT id FROM archive WHERE name = '%s'" % (archive))
75 archive_id = q.getresult()[0][0]
76 archive_id_cache[archive] = archive_id
80 def get_component_id (component):
81 global component_id_cache
83 if component_id_cache.has_key(component):
84 return component_id_cache[component]
86 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (string.lower(component)))
91 component_id = ql[0][0]
92 component_id_cache[component] = component_id
96 def get_location_id (location, component, archive):
97 global location_id_cache
99 cache_key = location + '~' + component + '~' + location
100 if location_id_cache.has_key(cache_key):
101 return location_id_cache[cache_key]
103 archive_id = get_archive_id (archive)
105 component_id = get_component_id (component)
106 if component_id != -1:
107 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
109 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
110 location_id = q.getresult()[0][0]
111 location_id_cache[cache_key] = location_id
115 def get_source_id (source, version):
116 global source_id_cache
118 cache_key = source + '~' + version + '~'
119 if source_id_cache.has_key(cache_key):
120 return source_id_cache[cache_key]
122 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
124 if not q.getresult():
127 source_id = q.getresult()[0][0]
128 source_id_cache[cache_key] = source_id
132 ##########################################################################################
134 def get_or_set_maintainer_id (maintainer):
135 global maintainer_id_cache
137 if maintainer_id_cache.has_key(maintainer):
138 return maintainer_id_cache[maintainer]
140 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
141 if not q.getresult():
142 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
143 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
144 maintainer_id = q.getresult()[0][0]
145 maintainer_id_cache[maintainer] = maintainer_id
149 ##########################################################################################
151 def get_files_id (filename, size, md5sum, location_id):
152 global files_id_cache
154 cache_key = "%s~%d" % (filename, location_id);
156 if files_id_cache.has_key(cache_key):
157 return files_id_cache[cache_key]
160 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
166 orig_size = int(ql[1]);
168 if orig_size != size or orig_md5sum != md5sum:
170 files_id_cache[cache_key] = ql[0]
171 return files_id_cache[cache_key]
176 ##########################################################################################
178 def set_files_id (filename, size, md5sum, location_id):
179 global files_id_cache
181 cache_key = "%s~%d" % (filename, location_id);
183 #print "INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id);
184 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id));
185 q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
186 ql = q.getresult()[0];
187 files_id_cache[cache_key] = ql[0]
189 return files_id_cache[cache_key]
191 ##########################################################################################