]> git.decadent.org.uk Git - dak.git/blob - db_access.py
lots and lots of python 2.1 changes. rene: remove bogus argument handling. katie...
[dak.git] / db_access.py
1 #!/usr/bin/env python
2
3 # DB access fucntions
4 # Copyright (C) 2000, 2001, 2002  James Troup <james@nocrew.org>
5 # $Id: db_access.py,v 1.14 2002-10-16 02:47:32 troup Exp $
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 ############################################################################################
22
23 import sys, time;
24
25 ############################################################################################
26
27 Cnf = None;
28 projectB = None;
29 suite_id_cache = {};
30 section_id_cache = {};
31 priority_id_cache = {};
32 override_type_id_cache = {};
33 architecture_id_cache = {};
34 archive_id_cache = {};
35 component_id_cache = {};
36 location_id_cache = {};
37 maintainer_id_cache = {};
38 source_id_cache = {};
39 files_id_cache = {};
40 maintainer_cache = {};
41 fingerprint_id_cache = {};
42
43 ############################################################################################
44
45 def init (config, sql):
46     global Cnf, projectB
47
48     Cnf = config;
49     projectB = sql;
50
51
52 def do_query(q):
53     sys.stderr.write("query: \"%s\" ... " % (q));
54     before = time.time();
55     r = projectB.query(q);
56     time_diff = time.time()-before;
57     sys.stderr.write("took %.3f seconds.\n" % (time_diff));
58     return r;
59
60 ############################################################################################
61
62 def get_suite_id (suite):
63     global suite_id_cache
64
65     if suite_id_cache.has_key(suite):
66         return suite_id_cache[suite]
67
68     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
69     ql = q.getresult();
70     if not ql:
71         return -1;
72
73     suite_id = ql[0][0];
74     suite_id_cache[suite] = suite_id
75
76     return suite_id
77
78 def get_section_id (section):
79     global section_id_cache
80
81     if section_id_cache.has_key(section):
82         return section_id_cache[section]
83
84     q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
85     ql = q.getresult();
86     if not ql:
87         return -1;
88
89     section_id = ql[0][0];
90     section_id_cache[section] = section_id
91
92     return section_id
93
94 def get_priority_id (priority):
95     global priority_id_cache
96
97     if priority_id_cache.has_key(priority):
98         return priority_id_cache[priority]
99
100     q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
101     ql = q.getresult();
102     if not ql:
103         return -1;
104
105     priority_id = ql[0][0];
106     priority_id_cache[priority] = priority_id
107
108     return priority_id
109
110 def get_override_type_id (type):
111     global override_type_id_cache;
112
113     if override_type_id_cache.has_key(type):
114         return override_type_id_cache[type];
115
116     q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type));
117     ql = q.getresult();
118     if not ql:
119         return -1;
120
121     override_type_id = ql[0][0];
122     override_type_id_cache[type] = override_type_id;
123
124     return override_type_id;
125
126 def get_architecture_id (architecture):
127     global architecture_id_cache;
128
129     if architecture_id_cache.has_key(architecture):
130         return architecture_id_cache[architecture];
131
132     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
133     ql = q.getresult();
134     if not ql:
135         return -1;
136
137     architecture_id = ql[0][0];
138     architecture_id_cache[architecture] = architecture_id;
139
140     return architecture_id;
141
142 def get_archive_id (archive):
143     global archive_id_cache
144
145     archive = archive.lower();
146
147     if archive_id_cache.has_key(archive):
148         return archive_id_cache[archive]
149
150     q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive));
151     ql = q.getresult();
152     if not ql:
153         return -1;
154
155     archive_id = ql[0][0]
156     archive_id_cache[archive] = archive_id
157
158     return archive_id
159
160 def get_component_id (component):
161     global component_id_cache
162
163     component = component.lower();
164
165     if component_id_cache.has_key(component):
166         return component_id_cache[component]
167
168     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
169     ql = q.getresult();
170     if not ql:
171         return -1;
172
173     component_id = ql[0][0];
174     component_id_cache[component] = component_id
175
176     return component_id
177
178 def get_location_id (location, component, archive):
179     global location_id_cache
180
181     cache_key = location + '~' + component + '~' + location
182     if location_id_cache.has_key(cache_key):
183         return location_id_cache[cache_key]
184
185     archive_id = get_archive_id (archive)
186     if component != "":
187         component_id = get_component_id (component)
188         if component_id != -1:
189             q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
190     else:
191         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
192     ql = q.getresult();
193     if not ql:
194         return -1;
195
196     location_id = ql[0][0]
197     location_id_cache[cache_key] = location_id
198
199     return location_id
200
201 def get_source_id (source, version):
202     global source_id_cache
203
204     cache_key = source + '~' + version + '~'
205     if source_id_cache.has_key(cache_key):
206         return source_id_cache[cache_key]
207
208     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
209
210     if not q.getresult():
211         return None
212
213     source_id = q.getresult()[0][0]
214     source_id_cache[cache_key] = source_id
215
216     return source_id
217
218 ##########################################################################################
219
220 def get_or_set_maintainer_id (maintainer):
221     global maintainer_id_cache
222
223     if maintainer_id_cache.has_key(maintainer):
224         return maintainer_id_cache[maintainer]
225
226     q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
227     if not q.getresult():
228         projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
229         q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
230     maintainer_id = q.getresult()[0][0]
231     maintainer_id_cache[maintainer] = maintainer_id
232
233     return maintainer_id
234
235 ##########################################################################################
236
237 def get_or_set_fingerprint_id (fingerprint):
238     global fingerprint_id_cache
239
240     if fingerprint_id_cache.has_key(fingerprint):
241         return fingerprint_id_cache[fingerprint]
242
243     q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
244     if not q.getresult():
245         projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
246         q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
247     fingerprint_id = q.getresult()[0][0]
248     fingerprint_id_cache[fingerprint] = fingerprint_id
249
250     return fingerprint_id
251
252 ##########################################################################################
253
254 def get_files_id (filename, size, md5sum, location_id):
255     global files_id_cache
256
257     cache_key = "%s~%d" % (filename, location_id);
258
259     if files_id_cache.has_key(cache_key):
260         return files_id_cache[cache_key]
261
262     size = int(size);
263     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
264     ql = q.getresult();
265     if ql:
266         if len(ql) != 1:
267             return -1;
268         ql = ql[0];
269         orig_size = int(ql[1]);
270         orig_md5sum = ql[2];
271         if orig_size != size or orig_md5sum != md5sum:
272             return -2;
273         files_id_cache[cache_key] = ql[0];
274         return files_id_cache[cache_key]
275     else:
276         return None
277
278
279 ##########################################################################################
280
281 def set_files_id (filename, size, md5sum, location_id):
282     global files_id_cache
283
284     projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id));
285
286     return get_files_id (filename, size, md5sum, location_id);
287
288     ### currval has issues with postgresql 7.1.3 when the table is big;
289     ### it was taking ~3 seconds to return on auric which is very Not
290     ### Cool(tm).
291     ##
292     ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
293     ##ql = q.getresult()[0];
294     ##cache_key = "%s~%d" % (filename, location_id);
295     ##files_id_cache[cache_key] = ql[0]
296     ##return files_id_cache[cache_key];
297
298 ##########################################################################################
299
300 def get_maintainer (maintainer_id):
301     global maintainer_cache;
302
303     if not maintainer_cache.has_key(maintainer_id):
304         q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id));
305         maintainer_cache[maintainer_id] = q.getresult()[0][0];
306
307     return maintainer_cache[maintainer_id];
308
309 ##########################################################################################