]> git.decadent.org.uk Git - dak.git/blob - db_access.py
sync
[dak.git] / db_access.py
1 # DB access fucntions
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 $
4
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.
9
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.
14
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
18
19 ############################################################################################
20
21 import string
22
23 ############################################################################################
24
25 Cnf = None;
26 projectB = None;
27 suite_id_cache = {};
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 = {};
36 source_id_cache = {};
37 files_id_cache = {};
38 maintainer_cache = {};
39 fingerprint_id_cache = {};
40
41 ############################################################################################
42
43 def init (config, sql):
44     global Cnf, projectB
45
46     Cnf = config;
47     projectB = sql;
48
49 ############################################################################################
50
51 def get_suite_id (suite):
52     global suite_id_cache
53
54     if suite_id_cache.has_key(suite):
55         return suite_id_cache[suite]
56
57     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
58     ql = q.getresult();
59     if not ql:
60         return -1;
61
62     suite_id = ql[0][0];
63     suite_id_cache[suite] = suite_id
64
65     return suite_id
66
67 def get_section_id (section):
68     global section_id_cache
69
70     if section_id_cache.has_key(section):
71         return section_id_cache[section]
72
73     q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
74     ql = q.getresult();
75     if not ql:
76         return -1;
77
78     section_id = ql[0][0];
79     section_id_cache[section] = section_id
80
81     return section_id
82
83 def get_priority_id (priority):
84     global priority_id_cache
85
86     if priority_id_cache.has_key(priority):
87         return priority_id_cache[priority]
88
89     q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
90     ql = q.getresult();
91     if not ql:
92         return -1;
93
94     priority_id = ql[0][0];
95     priority_id_cache[priority] = priority_id
96
97     return priority_id
98
99 def get_override_type_id (type):
100     global override_type_id_cache;
101
102     if override_type_id_cache.has_key(type):
103         return override_type_id_cache[type];
104
105     q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type));
106     ql = q.getresult();
107     if not ql:
108         return -1;
109
110     override_type_id = ql[0][0];
111     override_type_id_cache[type] = override_type_id;
112
113     return override_type_id;
114
115 def get_architecture_id (architecture):
116     global architecture_id_cache;
117
118     if architecture_id_cache.has_key(architecture):
119         return architecture_id_cache[architecture];
120
121     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
122     ql = q.getresult();
123     if not ql:
124         return -1;
125
126     architecture_id = ql[0][0];
127     architecture_id_cache[architecture] = architecture_id;
128
129     return architecture_id;
130
131 def get_archive_id (archive):
132     global archive_id_cache
133
134     archive = string.lower(archive);
135
136     if archive_id_cache.has_key(archive):
137         return archive_id_cache[archive]
138
139     q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive));
140     ql = q.getresult();
141     if not ql:
142         return -1;
143
144     archive_id = ql[0][0]
145     archive_id_cache[archive] = archive_id
146
147     return archive_id
148
149 def get_component_id (component):
150     global component_id_cache
151
152     component = string.lower(component);
153
154     if component_id_cache.has_key(component):
155         return component_id_cache[component]
156
157     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
158     ql = q.getresult();
159     if not ql:
160         return -1;
161
162     component_id = ql[0][0];
163     component_id_cache[component] = component_id
164
165     return component_id
166
167 def get_location_id (location, component, archive):
168     global location_id_cache
169
170     cache_key = location + '~' + component + '~' + location
171     if location_id_cache.has_key(cache_key):
172         return location_id_cache[cache_key]
173
174     archive_id = get_archive_id (archive)
175     if component != "":
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))
179     else:
180         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
181     ql = q.getresult();
182     if not ql:
183         return -1;
184
185     location_id = ql[0][0]
186     location_id_cache[cache_key] = location_id
187
188     return location_id
189
190 def get_source_id (source, version):
191     global source_id_cache
192
193     cache_key = source + '~' + version + '~'
194     if source_id_cache.has_key(cache_key):
195         return source_id_cache[cache_key]
196
197     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
198
199     if not q.getresult():
200         return None
201
202     source_id = q.getresult()[0][0]
203     source_id_cache[cache_key] = source_id
204
205     return source_id
206
207 ##########################################################################################
208
209 def get_or_set_maintainer_id (maintainer):
210     global maintainer_id_cache
211
212     if maintainer_id_cache.has_key(maintainer):
213         return maintainer_id_cache[maintainer]
214
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
221
222     return maintainer_id
223
224 ##########################################################################################
225
226 def get_or_set_fingerprint_id (fingerprint):
227     global fingerprint_id_cache
228
229     if fingerprint_id_cache.has_key(fingerprint):
230         return fingerprint_id_cache[fingerprint]
231
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
238
239     return fingerprint_id
240
241 ##########################################################################################
242
243 def get_files_id (filename, size, md5sum, location_id):
244     global files_id_cache
245
246     cache_key = "%s~%d" % (filename, location_id);
247
248     if files_id_cache.has_key(cache_key):
249         return files_id_cache[cache_key]
250
251     size = int(size);
252     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
253     ql = q.getresult();
254     if ql:
255         if len(ql) != 1:
256             return -1;
257         ql = ql[0];
258         orig_size = int(ql[1]);
259         orig_md5sum = ql[2];
260         if orig_size != size or orig_md5sum != md5sum:
261             return -2;
262         files_id_cache[cache_key] = ql[0];
263         return files_id_cache[cache_key]
264     else:
265         return None
266
267
268 ##########################################################################################
269
270 def set_files_id (filename, size, md5sum, location_id):
271     global files_id_cache
272
273     cache_key = "%s~%d" % (filename, location_id);
274
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]
280
281     return files_id_cache[cache_key]
282
283 ##########################################################################################
284
285 def get_maintainer (maintainer_id):
286     global maintainer_cache;
287
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];
291
292     return maintainer_cache[maintainer_id];
293
294 ##########################################################################################