2 # Copyright (C) 2000 James Troup <james@nocrew.org>
3 # $Id: db_access.py,v 1.1.1.1 2000-11-24 00:20:09 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))
47 suite_id = q.getresult()[0][0]
48 suite_id_cache[suite] = suite_id
52 def get_architecture_id (architecture):
53 global architecture_id_cache
55 if architecture_id_cache.has_key(architecture):
56 return architecture_id_cache[architecture]
58 q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
59 architecture_id = q.getresult()[0][0]
60 architecture_id_cache[architecture] = architecture_id
62 return architecture_id
64 def get_archive_id (archive):
65 global archive_id_cache
67 if archive_id_cache.has_key(archive):
68 return archive_id_cache[archive]
70 q = projectB.query("SELECT id FROM archive WHERE name = '%s'" % (archive))
71 archive_id = q.getresult()[0][0]
72 archive_id_cache[archive] = archive_id
76 def get_component_id (component):
77 global component_id_cache
79 if component_id_cache.has_key(component):
80 return component_id_cache[component]
82 q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (string.lower(component)))
87 component_id = ql[0][0]
88 component_id_cache[component] = component_id
92 def get_location_id (location, component, archive):
93 global location_id_cache
95 cache_key = location + '~' + component + '~' + location
96 if location_id_cache.has_key(cache_key):
97 return location_id_cache[cache_key]
99 archive_id = get_archive_id (archive)
101 component_id = get_component_id (component)
102 if component_id != -1:
103 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
105 q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
106 location_id = q.getresult()[0][0]
107 location_id_cache[cache_key] = location_id
111 def get_source_id (source, version):
112 global source_id_cache
114 cache_key = source + '~' + version + '~'
115 if source_id_cache.has_key(cache_key):
116 return source_id_cache[cache_key]
118 q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
120 if not q.getresult():
123 source_id = q.getresult()[0][0]
124 source_id_cache[cache_key] = source_id
128 ##########################################################################################
130 def get_or_set_maintainer_id (maintainer):
131 global maintainer_id_cache
133 if maintainer_id_cache.has_key(maintainer):
134 return maintainer_id_cache[maintainer]
136 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
137 if not q.getresult():
138 projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
139 q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
140 maintainer_id = q.getresult()[0][0]
141 maintainer_id_cache[maintainer] = maintainer_id
145 ##########################################################################################
147 def get_files_id (filename, size, md5sum, location_id):
148 global files_id_cache
150 cache_key = "%s~%d" % (filename, location_id);
152 if files_id_cache.has_key(cache_key):
153 return files_id_cache[files]
155 q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
163 if orig_size != size or orig_md5sum != md5sum:
165 files_id_cache[cache_key] = ql[0]
166 return files_id_cache[cache_key]
171 ##########################################################################################
173 def set_files_id (filename, size, md5sum, location_id):
174 global files_id_cache
176 cache_key = "%s~%d" % (filename, location_id);
178 #print "INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id);
179 projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id));
180 q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
181 ql = q.getresult()[0];
182 files_id_cache[cache_key] = ql[0]
184 return files_id_cache[cache_key]
186 ##########################################################################################