4 # Copyright (C) 2008 Mark Hymers <mhy@debian.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 ################################################################################
22 # < mhy> I need a funny comment
23 # < sgran> two peanuts were walking down a dark street
24 # < sgran> one was a-salted
25 # * mhy looks up the definition of "funny"
27 ################################################################################
30 from psycopg2.extras import DictCursor
32 from Singleton import Singleton
33 from Config import Config
35 ################################################################################
38 def __init__(self, hashfunc=None):
40 self.hashfunc = hashfunc
42 self.hashfunc = lambda x: x['value']
46 def SetValue(self, keys, value):
47 self.data[self.hashfunc(keys)] = value
49 def GetValue(self, keys):
50 return self.data.get(self.hashfunc(keys))
52 ################################################################################
54 class DBConn(Singleton):
56 A DBConn object is a singleton containing
57 information about the connection to the SQL Database
59 def __init__(self, *args, **kwargs):
60 super(DBConn, self).__init__(*args, **kwargs)
62 def _startup(self, *args, **kwargs):
66 ## Connection functions
67 def __createconn(self):
68 connstr = Config().GetDBConnString()
69 self.db_con = psycopg2.connect(connstr)
74 except psycopg2.InterfaceError:
81 def __init_caches(self):
82 self.caches = {'suite': Cache(),
85 'override_type': Cache(),
86 'architecture': Cache(),
89 'location': Cache(lambda x: '%s_%s_%s' % (x['location'], x['component'], x['location'])),
90 'maintainer': {}, # TODO
92 'source': Cache(lambda x: '%s_%s_' % (x['source'], x['version'])),
94 'maintainer': {}, # TODO
95 'fingerprint': {}, # TODO
98 'suite_version': Cache(lambda x: '%s_%s' % (x['source'], x['suite'])),
101 def clear_caches(self):
104 ## Functions to pass through to the database connector
106 return self.db_con.cursor()
109 return self.db_con.commit()
112 def __get_single_id(self, query, values, cachename=None):
113 # This is a bit of a hack but it's an internal function only
114 if cachename is not None:
115 res = self.caches[cachename].GetValue(values)
119 c = self.db_con.cursor()
120 c.execute(query, values)
125 res = c.fetchone()[0]
127 if cachename is not None:
128 self.caches[cachename].SetValue(values, res)
132 def __get_id(self, retfield, table, qfield, value):
133 query = "SELECT %s FROM %s WHERE %s = %%(value)s" % (retfield, table, qfield)
134 return self.__get_single_id(query, {'value': value}, cachename=table)
136 def get_suite_id(self, suite):
137 return self.__get_id('id', 'suite', 'suite_name', suite)
139 def get_section_id(self, section):
140 return self.__get_id('id', 'section', 'section', section)
142 def get_priority_id(self, priority):
143 return self.__get_id('id', 'priority', 'priority', priority)
145 def get_override_type_id(self, override_type):
146 return self.__get_id('id', 'override_type', 'override_type', override_type)
148 def get_architecture_id(self, architecture):
149 return self.__get_id('id', 'architecture', 'arch_string', architecture)
151 def get_archive_id(self, archive):
152 return self.__get_id('id', 'archive', 'lower(name)', archive)
154 def get_component_id(self, component):
155 return self.__get_id('id', 'component', 'lower(name)', component)
157 def get_location_id(self, location, component, archive):
158 archive_id = self.get_archive_id(archive)
166 component_id = self.get_component_id(component)
168 res = self.__get_single_id("SELECT id FROM location WHERE path=%(location)s AND component=%(component)d AND archive=%(archive)d",
169 {'location': location, 'archive': archive_id, 'component': component_id}, cachename='location')
171 res = self.__get_single_id("SELECT id FROM location WHERE path=%(location)s AND archive=%(archive)d",
172 {'location': location, 'archive': archive_id, 'component': ''}, cachename='location')
176 def get_source_id(self, source, version):
177 return self.__get_single_id("SELECT id FROM source s WHERE s.source=%(source)s AND s.version=%(version)s",
178 {'source': source, 'version': version}, cachename='source')
180 def get_suite_version(self, source, suite):
181 return self.__get_single_id("""
182 SELECT s.version FROM source s, suite su, src_associations sa
185 AND su.suite_name=%(suite)s
186 AND s.source=%(source)""", {'suite': suite, 'source': source}, cachename='suite_version')