]> git.decadent.org.uk Git - dak.git/blob - db_access.py
update for 2.2r7
[dak.git] / db_access.py
1 # DB access fucntions
2 # Copyright (C) 2000, 2001, 2002  James Troup <james@nocrew.org>
3 # $Id: db_access.py,v 1.13 2002-05-03 16:06:45 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, sys, time;
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 def do_query(q):
51     sys.stderr.write("query: \"%s\" ... " % (q));
52     before = time.time();
53     r = projectB.query(q);
54     time_diff = time.time()-before;
55     sys.stderr.write("took %.3f seconds.\n" % (time_diff));
56     return r;
57
58 ############################################################################################
59
60 def get_suite_id (suite):
61     global suite_id_cache
62
63     if suite_id_cache.has_key(suite):
64         return suite_id_cache[suite]
65
66     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
67     ql = q.getresult();
68     if not ql:
69         return -1;
70
71     suite_id = ql[0][0];
72     suite_id_cache[suite] = suite_id
73
74     return suite_id
75
76 def get_section_id (section):
77     global section_id_cache
78
79     if section_id_cache.has_key(section):
80         return section_id_cache[section]
81
82     q = projectB.query("SELECT id FROM section WHERE section = '%s'" % (section))
83     ql = q.getresult();
84     if not ql:
85         return -1;
86
87     section_id = ql[0][0];
88     section_id_cache[section] = section_id
89
90     return section_id
91
92 def get_priority_id (priority):
93     global priority_id_cache
94
95     if priority_id_cache.has_key(priority):
96         return priority_id_cache[priority]
97
98     q = projectB.query("SELECT id FROM priority WHERE priority = '%s'" % (priority))
99     ql = q.getresult();
100     if not ql:
101         return -1;
102
103     priority_id = ql[0][0];
104     priority_id_cache[priority] = priority_id
105
106     return priority_id
107
108 def get_override_type_id (type):
109     global override_type_id_cache;
110
111     if override_type_id_cache.has_key(type):
112         return override_type_id_cache[type];
113
114     q = projectB.query("SELECT id FROM override_type WHERE type = '%s'" % (type));
115     ql = q.getresult();
116     if not ql:
117         return -1;
118
119     override_type_id = ql[0][0];
120     override_type_id_cache[type] = override_type_id;
121
122     return override_type_id;
123
124 def get_architecture_id (architecture):
125     global architecture_id_cache;
126
127     if architecture_id_cache.has_key(architecture):
128         return architecture_id_cache[architecture];
129
130     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
131     ql = q.getresult();
132     if not ql:
133         return -1;
134
135     architecture_id = ql[0][0];
136     architecture_id_cache[architecture] = architecture_id;
137
138     return architecture_id;
139
140 def get_archive_id (archive):
141     global archive_id_cache
142
143     archive = string.lower(archive);
144
145     if archive_id_cache.has_key(archive):
146         return archive_id_cache[archive]
147
148     q = projectB.query("SELECT id FROM archive WHERE lower(name) = '%s'" % (archive));
149     ql = q.getresult();
150     if not ql:
151         return -1;
152
153     archive_id = ql[0][0]
154     archive_id_cache[archive] = archive_id
155
156     return archive_id
157
158 def get_component_id (component):
159     global component_id_cache
160
161     component = string.lower(component);
162
163     if component_id_cache.has_key(component):
164         return component_id_cache[component]
165
166     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (component))
167     ql = q.getresult();
168     if not ql:
169         return -1;
170
171     component_id = ql[0][0];
172     component_id_cache[component] = component_id
173
174     return component_id
175
176 def get_location_id (location, component, archive):
177     global location_id_cache
178
179     cache_key = location + '~' + component + '~' + location
180     if location_id_cache.has_key(cache_key):
181         return location_id_cache[cache_key]
182
183     archive_id = get_archive_id (archive)
184     if component != "":
185         component_id = get_component_id (component)
186         if component_id != -1:
187             q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
188     else:
189         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
190     ql = q.getresult();
191     if not ql:
192         return -1;
193
194     location_id = ql[0][0]
195     location_id_cache[cache_key] = location_id
196
197     return location_id
198
199 def get_source_id (source, version):
200     global source_id_cache
201
202     cache_key = source + '~' + version + '~'
203     if source_id_cache.has_key(cache_key):
204         return source_id_cache[cache_key]
205
206     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
207
208     if not q.getresult():
209         return None
210
211     source_id = q.getresult()[0][0]
212     source_id_cache[cache_key] = source_id
213
214     return source_id
215
216 ##########################################################################################
217
218 def get_or_set_maintainer_id (maintainer):
219     global maintainer_id_cache
220
221     if maintainer_id_cache.has_key(maintainer):
222         return maintainer_id_cache[maintainer]
223
224     q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
225     if not q.getresult():
226         projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
227         q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
228     maintainer_id = q.getresult()[0][0]
229     maintainer_id_cache[maintainer] = maintainer_id
230
231     return maintainer_id
232
233 ##########################################################################################
234
235 def get_or_set_fingerprint_id (fingerprint):
236     global fingerprint_id_cache
237
238     if fingerprint_id_cache.has_key(fingerprint):
239         return fingerprint_id_cache[fingerprint]
240
241     q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
242     if not q.getresult():
243         projectB.query("INSERT INTO fingerprint (fingerprint) VALUES ('%s')" % (fingerprint))
244         q = projectB.query("SELECT id FROM fingerprint WHERE fingerprint = '%s'" % (fingerprint))
245     fingerprint_id = q.getresult()[0][0]
246     fingerprint_id_cache[fingerprint] = fingerprint_id
247
248     return fingerprint_id
249
250 ##########################################################################################
251
252 def get_files_id (filename, size, md5sum, location_id):
253     global files_id_cache
254
255     cache_key = "%s~%d" % (filename, location_id);
256
257     if files_id_cache.has_key(cache_key):
258         return files_id_cache[cache_key]
259
260     size = int(size);
261     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
262     ql = q.getresult();
263     if ql:
264         if len(ql) != 1:
265             return -1;
266         ql = ql[0];
267         orig_size = int(ql[1]);
268         orig_md5sum = ql[2];
269         if orig_size != size or orig_md5sum != md5sum:
270             return -2;
271         files_id_cache[cache_key] = ql[0];
272         return files_id_cache[cache_key]
273     else:
274         return None
275
276
277 ##########################################################################################
278
279 def set_files_id (filename, size, md5sum, location_id):
280     global files_id_cache
281
282     projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id));
283
284     return get_files_id (filename, size, md5sum, location_id);
285
286     ### currval has issues with postgresql 7.1.3 when the table is big;
287     ### it was taking ~3 seconds to return on auric which is very Not
288     ### Cool(tm).
289     ##
290     ##q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
291     ##ql = q.getresult()[0];
292     ##cache_key = "%s~%d" % (filename, location_id);
293     ##files_id_cache[cache_key] = ql[0]
294     ##return files_id_cache[cache_key];
295
296 ##########################################################################################
297
298 def get_maintainer (maintainer_id):
299     global maintainer_cache;
300
301     if not maintainer_cache.has_key(maintainer_id):
302         q = projectB.query("SELECT name FROM maintainer WHERE id = %s" % (maintainer_id));
303         maintainer_cache[maintainer_id] = q.getresult()[0][0];
304
305     return maintainer_cache[maintainer_id];
306
307 ##########################################################################################