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