]> git.decadent.org.uk Git - dak.git/blob - db_access.py
First working version of rhona.
[dak.git] / db_access.py
1 # DB access fucntions
2 # Copyright (C) 2000  James Troup <james@nocrew.org>
3 # $Id: db_access.py,v 1.4 2000-12-18 07:11:25 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 Cnf = None
22 projectB = None
23 suite_id_cache = {}
24 architecture_id_cache = {}
25 archive_id_cache = {}
26 component_id_cache = {}
27 location_id_cache = {}
28 maintainer_id_cache = {}
29 source_id_cache = {}
30 files_id_cache = {}
31
32 def init (config, sql):
33     global Cnf, projectB
34     
35     Cnf = config;
36     projectB = sql;
37
38 ############################################################################################
39
40 def get_suite_id (suite):
41     global suite_id_cache
42
43     if suite_id_cache.has_key(suite):
44         return suite_id_cache[suite]
45
46     q = projectB.query("SELECT id FROM suite WHERE suite_name = '%s'" % (suite))
47     ql = q.getresult();
48     if ql == []:
49         return -1; 
50     
51     suite_id = q.getresult()[0][0]
52     suite_id_cache[suite] = suite_id
53
54     return suite_id
55
56 def get_architecture_id (architecture):
57     global architecture_id_cache
58
59     if architecture_id_cache.has_key(architecture):
60         return architecture_id_cache[architecture]
61
62     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
63     architecture_id = q.getresult()[0][0]
64     architecture_id_cache[architecture] = architecture_id
65
66     return architecture_id
67
68 def get_archive_id (archive):
69     global archive_id_cache
70
71     if archive_id_cache.has_key(archive):
72         return archive_id_cache[archive]
73
74     q = projectB.query("SELECT id FROM archive WHERE name = '%s'" % (archive))
75     archive_id = q.getresult()[0][0]
76     archive_id_cache[archive] = archive_id
77
78     return archive_id
79
80 def get_component_id (component):
81     global component_id_cache
82
83     if component_id_cache.has_key(component):
84         return component_id_cache[component]
85
86     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (string.lower(component)))
87     ql = q.getresult();
88     if ql == []:
89         return -1;
90
91     component_id = ql[0][0]
92     component_id_cache[component] = component_id
93
94     return component_id
95
96 def get_location_id (location, component, archive):
97     global location_id_cache
98
99     cache_key = location + '~' + component + '~' + location
100     if location_id_cache.has_key(cache_key):
101         return location_id_cache[cache_key]
102
103     archive_id = get_archive_id (archive)
104     if component != "":
105         component_id = get_component_id (component)
106         if component_id != -1:
107             q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
108     else:
109         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
110     location_id = q.getresult()[0][0]
111     location_id_cache[cache_key] = location_id
112
113     return location_id
114
115 def get_source_id (source, version):
116     global source_id_cache
117
118     cache_key = source + '~' + version + '~'
119     if source_id_cache.has_key(cache_key):
120         return source_id_cache[cache_key]
121
122     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
123
124     if not q.getresult():
125         return None
126
127     source_id = q.getresult()[0][0]
128     source_id_cache[cache_key] = source_id
129
130     return source_id
131
132 ##########################################################################################
133
134 def get_or_set_maintainer_id (maintainer):
135     global maintainer_id_cache
136
137     if maintainer_id_cache.has_key(maintainer):
138         return maintainer_id_cache[maintainer]
139
140     q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
141     if not q.getresult():
142         projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
143         q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
144     maintainer_id = q.getresult()[0][0]
145     maintainer_id_cache[maintainer] = maintainer_id
146
147     return maintainer_id
148
149 ##########################################################################################
150
151 def get_files_id (filename, size, md5sum, location_id):
152     global files_id_cache
153
154     cache_key = "%s~%d" % (filename, location_id);
155
156     if files_id_cache.has_key(cache_key):
157         return files_id_cache[cache_key]
158
159     size = int(size);
160     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
161     ql = q.getresult();
162     if ql:
163         if len(ql) != 1:
164             return -1;
165         ql = ql[0] 
166         orig_size = int(ql[1]);
167         orig_md5sum = ql[2];
168         if orig_size != size or orig_md5sum != md5sum:
169             return -2;
170         files_id_cache[cache_key] = ql[0]
171         return files_id_cache[cache_key]
172     else:
173         return None
174
175
176 ##########################################################################################
177
178 def set_files_id (filename, size, md5sum, location_id):
179     global files_id_cache
180
181     cache_key = "%s~%d" % (filename, location_id);
182
183     #print "INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id);
184     projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id)); 
185     q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
186     ql = q.getresult()[0];
187     files_id_cache[cache_key] = ql[0]
188
189     return files_id_cache[cache_key]
190
191 ##########################################################################################
192