]> git.decadent.org.uk Git - dak.git/blob - db_access.py
Initial revision
[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.1.1.1 2000-11-24 00:20:09 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     suite_id = q.getresult()[0][0]
48     suite_id_cache[suite] = suite_id
49
50     return suite_id
51
52 def get_architecture_id (architecture):
53     global architecture_id_cache
54
55     if architecture_id_cache.has_key(architecture):
56         return architecture_id_cache[architecture]
57
58     q = projectB.query("SELECT id FROM architecture WHERE arch_string = '%s'" % (architecture))
59     architecture_id = q.getresult()[0][0]
60     architecture_id_cache[architecture] = architecture_id
61
62     return architecture_id
63
64 def get_archive_id (archive):
65     global archive_id_cache
66
67     if archive_id_cache.has_key(archive):
68         return archive_id_cache[archive]
69
70     q = projectB.query("SELECT id FROM archive WHERE name = '%s'" % (archive))
71     archive_id = q.getresult()[0][0]
72     archive_id_cache[archive] = archive_id
73
74     return archive_id
75
76 def get_component_id (component):
77     global component_id_cache
78
79     if component_id_cache.has_key(component):
80         return component_id_cache[component]
81
82     q = projectB.query("SELECT id FROM component WHERE lower(name) = '%s'" % (string.lower(component)))
83     ql = q.getresult();
84     if ql == []:
85         return -1;
86
87     component_id = ql[0][0]
88     component_id_cache[component] = component_id
89
90     return component_id
91
92 def get_location_id (location, component, archive):
93     global location_id_cache
94
95     cache_key = location + '~' + component + '~' + location
96     if location_id_cache.has_key(cache_key):
97         return location_id_cache[cache_key]
98
99     archive_id = get_archive_id (archive)
100     if component != "":
101         component_id = get_component_id (component)
102         if component_id != -1:
103             q = projectB.query("SELECT id FROM location WHERE path = '%s' AND component = %d AND archive = %d" % (location, component_id, archive_id))
104     else:
105         q = projectB.query("SELECT id FROM location WHERE path = '%s' AND archive = %d" % (location, archive_id))
106     location_id = q.getresult()[0][0]
107     location_id_cache[cache_key] = location_id
108
109     return location_id
110
111 def get_source_id (source, version):
112     global source_id_cache
113
114     cache_key = source + '~' + version + '~'
115     if source_id_cache.has_key(cache_key):
116         return source_id_cache[cache_key]
117
118     q = projectB.query("SELECT id FROM source s WHERE s.source = '%s' AND s.version = '%s'" % (source, version))
119
120     if not q.getresult():
121         return None
122
123     source_id = q.getresult()[0][0]
124     source_id_cache[cache_key] = source_id
125
126     return source_id
127
128 ##########################################################################################
129
130 def get_or_set_maintainer_id (maintainer):
131     global maintainer_id_cache
132
133     if maintainer_id_cache.has_key(maintainer):
134         return maintainer_id_cache[maintainer]
135
136     q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
137     if not q.getresult():
138         projectB.query("INSERT INTO maintainer (name) VALUES ('%s')" % (maintainer))
139         q = projectB.query("SELECT id FROM maintainer WHERE name = '%s'" % (maintainer))
140     maintainer_id = q.getresult()[0][0]
141     maintainer_id_cache[maintainer] = maintainer_id
142
143     return maintainer_id
144
145 ##########################################################################################
146
147 def get_files_id (filename, size, md5sum, location_id):
148     global files_id_cache
149
150     cache_key = "%s~%d" % (filename, location_id);
151
152     if files_id_cache.has_key(cache_key):
153         return files_id_cache[files]
154
155     q = projectB.query("SELECT id, size, md5sum FROM files WHERE filename = '%s' AND location = %d" % (filename, location_id));
156     ql = q.getresult();
157     if ql:
158         if len(ql) != 1:
159             return -1;
160         ql = ql[0] 
161         orig_size = ql[1];
162         orig_md5sum = ql[2];
163         if orig_size != size or orig_md5sum != md5sum:
164             return -2;
165         files_id_cache[cache_key] = ql[0]
166         return files_id_cache[cache_key]
167     else:
168         return None
169
170
171 ##########################################################################################
172
173 def set_files_id (filename, size, md5sum, location_id):
174     global files_id_cache
175
176     cache_key = "%s~%d" % (filename, location_id);
177
178     #print "INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id);
179     projectB.query("INSERT INTO files (filename, size, md5sum, location) VALUES ('%s', %d, '%s', %d)" % (filename, long(size), md5sum, location_id)); 
180     q = projectB.query("SELECT id FROM files WHERE id = currval('files_id_seq')");
181     ql = q.getresult()[0];
182     files_id_cache[cache_key] = ql[0]
183
184     return files_id_cache[cache_key]
185
186 ##########################################################################################
187