Ejemplo n.º 1
0
    def __delitem__(self, cpv):
        """delete a key from the cache.
		This shouldn't be overriden in derived classes since it handles the readonly checks"""
        if self.readonly:
            raise cache_errors.ReadOnlyRestriction()
        if not self.autocommits:
            self.updates += 1
        self._delitem(cpv)
        if self.updates > self.sync_rate:
            self.commit()
            self.updates = 0
Ejemplo n.º 2
0
    def _initdb_con(self, config):
        """ensure needed tables are in place.
		If the derived class needs a different set of table creation commands, overload the approriate
		SCHEMA_ attributes.  If it needs additional execution beyond, override"""

        self._dbconnect(config)
        if not self._table_exists(self.SCHEMA_PACKAGE_NAME):
            if self.readonly:
                raise cache_errors.ReadOnlyRestriction("table %s doesn't exist" % \
                 self.SCHEMA_PACKAGE_NAME)
            try:
                self.con.execute(self.SCHEMA_PACKAGE_CREATE)
            except self._BaseError, e:
                raise cache_errors.InitializationError(self.__class__, e)
Ejemplo n.º 3
0
    def __setitem__(self, cpv, values):
        """set a cpv to values
		This shouldn't be overriden in derived classes since it handles the readonly checks"""
        if self.readonly:
            raise cache_errors.ReadOnlyRestriction()
        d = copy.copy(values)
        if "_eclasses_" in d:
            d["_eclasses_"] = serialize_eclasses(d["_eclasses_"])
        self._setitem(cpv, d)
        if not self.autocommits:
            self.updates += 1
            if self.updates > self.sync_rate:
                self.commit()
                self.updates = 0
Ejemplo n.º 4
0
    def __setitem__(self, cpv, values):
        """set a cpv to values
		This shouldn't be overriden in derived classes since it handles the readonly checks"""
        if self.readonly:
            raise cache_errors.ReadOnlyRestriction()
        if self.cleanse_keys:
            d = copy.copy(values)
            for k in d.keys():
                if d[k] == '':
                    del d[k]
            if "_eclasses_" in values:
                d = values.copy()
                d["_eclasses_"] = serialize_eclasses(d["_eclasses_"])
        elif "_eclasses_" in values:
            d = values.copy()
            d["_eclasses_"] = serialize_eclasses(d["_eclasses_"])
        else:
            d = values
        self._setitem(cpv, d)
        if not self.autocommits:
            self.updates += 1
            if self.updates > self.sync_rate:
                self.commit()
                self.updates = 0
Ejemplo n.º 5
0
class SQLDatabase(template.database):
    """template class for RDBM based caches
	
	This class is designed such that derivatives don't have to change much code, mostly constant strings.
	_BaseError must be an exception class that all Exceptions thrown from the derived RDBMS are derived
	from.

	SCHEMA_INSERT_CPV_INTO_PACKAGE should be modified dependant on the RDBMS, as should SCHEMA_PACKAGE_CREATE-
	basically you need to deal with creation of a unique pkgid.  If the dbapi2 rdbms class has a method of 
	recovering that id, then modify _insert_cpv to remove the extra select.

	Creation of a derived class involves supplying _initdb_con, and table_exists.
	Additionally, the default schemas may have to be modified.
	"""

    SCHEMA_PACKAGE_NAME = "package_cache"
    SCHEMA_PACKAGE_CREATE = "CREATE TABLE %s (\
		pkgid INTEGER PRIMARY KEY, label VARCHAR(255), cpv VARCHAR(255), UNIQUE(label, cpv))" % SCHEMA_PACKAGE_NAME
    SCHEMA_PACKAGE_DROP = "DROP TABLE %s" % SCHEMA_PACKAGE_NAME

    SCHEMA_VALUES_NAME = "values_cache"
    SCHEMA_VALUES_CREATE = "CREATE TABLE %s ( pkgid integer references %s (pkgid) on delete cascade, \
		key varchar(255), value text, UNIQUE(pkgid, key))" % (SCHEMA_VALUES_NAME,
                                                        SCHEMA_PACKAGE_NAME)
    SCHEMA_VALUES_DROP = "DROP TABLE %s" % SCHEMA_VALUES_NAME
    SCHEMA_INSERT_CPV_INTO_PACKAGE = "INSERT INTO %s (label, cpv) VALUES(%%s, %%s)" % SCHEMA_PACKAGE_NAME

    _BaseError = ()
    _dbClass = None

    autocommits = False

    # boolean indicating if the derived RDBMS class supports replace syntax
    _supports_replace = False

    def __init__(self, label, auxdbkeys, **config):
        """initialize the instance.
		derived classes shouldn't need to override this"""

        super(SQLDatabase, self).__init__(label, auxdbkeys, **config)

        config.setdefault("host", "127.0.0.1")
        config.setdefault("autocommit", self.autocommits)
        self._initdb_con(config)

        self.label = self._sfilter(self.label)

    def _dbconnect(self, config):
        """should be overridden if the derived class needs special parameters for initializing
		the db connection, or cursor"""
        self.db = self._dbClass(**config)
        self.con = self.db.cursor()

    def _initdb_con(self, config):
        """ensure needed tables are in place.
		If the derived class needs a different set of table creation commands, overload the approriate
		SCHEMA_ attributes.  If it needs additional execution beyond, override"""

        self._dbconnect(config)
        if not self._table_exists(self.SCHEMA_PACKAGE_NAME):
            if self.readonly:
                raise cache_errors.ReadOnlyRestriction("table %s doesn't exist" % \
                 self.SCHEMA_PACKAGE_NAME)
            try:
                self.con.execute(self.SCHEMA_PACKAGE_CREATE)
            except self._BaseError, e:
                raise cache_errors.InitializationError(self.__class__, e)

        if not self._table_exists(self.SCHEMA_VALUES_NAME):
            if self.readonly:
                raise cache_errors.ReadOnlyRestriction("table %s doesn't exist" % \
                 self.SCHEMA_VALUES_NAME)
            try:
                self.con.execute(self.SCHEMA_VALUES_CREATE)
            except self._BaseError, e:
                raise cache_errors.InitializationError(self.__class__, e)