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