Пример #1
0
    def memcache_purge(self):
        '''If the data has been cached, purge it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()
        self._memcache.purge(self._memcache_key)
Пример #2
0
    def memcache_retrieve(self):
        '''If the data needs to be cached, cache it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return None

        if self._memcache is None:
            self._memcache = Memcache()
        return self._memcache.retrieve(self._memcache_key)
Пример #3
0
    def memcache_store(self, data):
        '''If there is data and it should be cached, cache it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()

        self._memcache.store(self._memcache_key, data, self._memcache_ttl,
                             self._memcache_ttl)
Пример #4
0
    def memcache_retrieve(self):
        '''If the data needs to be cached, cache it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return None

        if self._memcache is None:
            self._memcache = Memcache()
        return self._memcache.retrieve(self._memcache_key)
Пример #5
0
    def memcache_purge(self):
        '''If the data has been cached, purge it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()
        self._memcache.purge(self._memcache_key)
Пример #6
0
    def close(self):
        self.__close_cursor()
        Memcache().clear_local_cache()

        if self.__postgresql:
            if self.persistent is False:
                self.__postgresql.close()
                self.__postgresql = None

        if self._memcache is not None:
            if self.persistent is False:
                self._memcache.close()
                self._memcache = None
Пример #7
0
    def memcache_store(self, data):
        '''If there is data and it should be cached, cache it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()

        self._memcache.store(
            self._memcache_key,
            data,
            self._memcache_ttl,
            self._memcache_ttl)
Пример #8
0
    def close(self, force=False):
        '''Close the active database connection.'''
        self.__close_cursor()
        Memcache().clear_local_cache()

        if self.__mysql:
            if self.persistent is False or force is True:
                self.__mysql.close()
                self.__mysql = None

        if self._memcache is not None:
            if self.persistent is False:
                self._memcache.close()
                self._memcache = None
Пример #9
0
class RDBMSBase(__DataStoreBase):
    '''
    Defines a data store that handles interactions with a RDBMS (MySQL,
    PostgreSQL, etc.).
    '''

    def close(self):
        '''
        Manually close the database connection.
        '''

        raise NotImplementedError

    def commit(self):
        '''
        Manually commit the active transaction.
        '''

        raise NotImplementedError

    def configure(self, settings, db, group):
        '''
        Configure the connection settings.
        '''

        if group not in settings:
            raise DataStoreException(
                'group "{}" not found in settings'
                    .format(group)
            )

        self.close()

        self._settings = settings
        self._db = db
        self._group = group
        return self

    def count(self, sql, binds=tuple()):
        '''
        Given a count(*) query, only return the resultant count.
        '''

        return None

    def create(target, data=tuple(), return_insert_id=False):
        '''
        Create a new record in the RDBMS.
        '''

        return '' if return_insert_id else None

    def delete(target, where=tuple(), binds=tuple()):
        '''
        Delete a record from the RDBMS.
        '''

        return False

    def memcache(self, key, ttl=0):
        '''
        Specify that the result set should be cached in Memcache.
        '''

        self._memcache_key = key
        self._memcache_ttl = ttl
        return self

    def memcache_purge(self):
        '''
        If the data has been cached, purge it.
        '''

        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()
        self._memcache.purge(self._memcache_key)

    def memcache_retrieve(self):
        '''
        If the data needs to be cached, cache it.
        '''

        if self._memcache_key is None or Context.env_unit_test():
            return None

        if self._memcache is None:
            self._memcache = Memcache()

        return self._memcache.retrieve(self._memcache_key)

    def memcache_store(self, data):
        '''
        If there is data and it should be cached, cache it.
        '''

        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()

        self._memcache.store(
            self._memcache_key,
            data,
            self._memcache_ttl,
            self._memcache_ttl
        )

    def nth(self, index, sql, binds=tuple()):
        '''
        Return the value at the Nth position of the result set.
        '''

        return None

    def one(self, sql, binds=tuple(), obj=None):
        '''
        Return the first (and only the first) of the result set.
        '''

        record = self.nth(0, sql, binds)
        if obj is None:
            return record

        if record is None:
            raise RuntimeError('no data is present to assign to object')

        for key, value in record.items():
            setattr(obj, key, value)

        return record

    def ordered_dict_cursor(self):
        self._ordered_dict_cursor = True
        return self

    def query(self, query, binds = []):
        '''
        Execute an arbitrary query and return all of the results.
        '''

        return None

    def _reset_memcache(self):
        self._memcache_key = None
        self._memcache_ttl = None

    def rollback(self):
        '''
        Manually rollback the active transaction.
        '''

        raise NotImplementedError

    def set_charset(self, charset):
        '''
        Set the character for the RDBMS.
        '''

        self._charset = charset
        return self

    def set_persistent(self, persistent):
        self.persistent = persistent
        return self

    def should_ping(self):
        if self.persistent is False:
            return False

        should_ping = False
        if (time.time() - self._inactive_since) >= (self._ping_interval - 3):
            should_ping = True
            self._inactive_since = time.time()

        return should_ping
Пример #10
0
class RDBMSBase(__DataStoreBase):
    '''Defines a data store that handles interactions with a RDBMS (MySQL,
       PostgreSQL, etc.).'''
    def close(self):
        '''Manually close the database connection.'''
        raise NotImplementedError

    def commit(self):
        '''Manually commit the active transaction.'''
        raise NotImplementedError

    def count(self, sql, binds=tuple()):
        '''Given a count(*) query, only return the resultant count.'''
        return None

    def create(target, data=tuple(), return_insert_id=False):
        '''Create a new record in the RDBMS.'''
        return '' if return_insert_id else None

    def delete(target, where=tuple(), binds=tuple()):
        '''Delete a record from the RDBMS.'''
        return False

    def memcache(self, key, ttl=0):
        '''Specify that the result set should be cached in Memcache.'''
        self._memcache_key = key
        self._memcache_ttl = ttl
        return self

    def memcache_purge(self):
        '''If the data has been cached, purge it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()
        self._memcache.purge(self._memcache_key)

    def memcache_retrieve(self):
        '''If the data needs to be cached, cache it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return None

        if self._memcache is None:
            self._memcache = Memcache()
        return self._memcache.retrieve(self._memcache_key)

    def memcache_store(self, data):
        '''If there is data and it should be cached, cache it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()

        self._memcache.store(self._memcache_key, data, self._memcache_ttl,
                             self._memcache_ttl)

    def nth(self, index, sql, binds=tuple()):
        '''Return the value at the Nth position of the result set.'''
        return None

    def one(self, sql, binds=tuple(), obj=None):
        '''Return the first (and only the first) of the result set.'''
        record = self.nth(0, sql, binds)
        if obj is None:
            return record

        if record is None:
            raise RuntimeError('no data is present to assign to object')

        for key, value in record.items():
            setattr(obj, key, value)

        return record

    def ping(self):
        '''Make sure the connection is still open and, if not, reconnect.'''
        raise NotImplementedError

    def query(self, query, binds=[]):
        '''Execute an arbitrary query and return all of the results.'''
        return None

    def _reset_memcache(self):
        self._memcache_key = None
        self._memcache_ttl = None

    def rollback(self):
        '''Manually rollback the active transaction.'''
        raise NotImplementedError

    def select_db(self, connection, db):
        '''Select which connection and database schema to use.'''
        if self._connection_name != connection or self._db_name != db:
            self.__mysql = None

        self._connection_name = connection
        self._db_name = db
        return self

    def set_charset(self, charset):
        '''Set the character for the RDBMS.'''
        self._charset = charset
        return self

    def set_persistent(self, persistent):
        self.persistent = persistent
        return self
Пример #11
0
class RDBMSBase(__DataStoreBase):
    '''Defines a data store that handles interactions with a RDBMS (MySQL,
       PostgreSQL, etc.).'''

    def close(self):
        '''Manually close the database connection.'''
        raise NotImplementedError


    def commit(self):
        '''Manually commit the active transaction.'''
        raise NotImplementedError


    def count(self, sql, binds=tuple()):
        '''Given a count(*) query, only return the resultant count.'''
        return None


    def create(target, data=tuple(), return_insert_id=False):
        '''Create a new record in the RDBMS.'''
        return '' if return_insert_id else None


    def delete(target, where=tuple(), binds=tuple()):
        '''Delete a record from the RDBMS.'''
        return False


    def memcache(self, key, ttl=0):
        '''Specify that the result set should be cached in Memcache.'''
        self._memcache_key = key
        self._memcache_ttl = ttl
        return self


    def memcache_purge(self):
        '''If the data has been cached, purge it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()
        self._memcache.purge(self._memcache_key)


    def memcache_retrieve(self):
        '''If the data needs to be cached, cache it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return None

        if self._memcache is None:
            self._memcache = Memcache()
        return self._memcache.retrieve(self._memcache_key)


    def memcache_store(self, data):
        '''If there is data and it should be cached, cache it.'''
        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()

        self._memcache.store(
            self._memcache_key,
            data,
            self._memcache_ttl,
            self._memcache_ttl)


    def nth(self, index, sql, binds=tuple()):
        '''Return the value at the Nth position of the result set.'''
        return None


    def one(self, sql, binds=tuple(), obj=None):
        '''Return the first (and only the first) of the result set.'''
        record = self.nth(0, sql, binds)
        if obj is None:
            return record

        if record is None:
            raise RuntimeError('no data is present to assign to object')

        for key, value in record.items():
            setattr(obj, key, value)

        return record


    def ping(self):
        '''Make sure the connection is still open and, if not, reconnect.'''
        raise NotImplementedError


    def query(self, query, binds = []):
        '''Execute an arbitrary query and return all of the results.'''
        return None


    def _reset_memcache(self):
        self._memcache_key = None
        self._memcache_ttl = None


    def rollback(self):
        '''Manually rollback the active transaction.'''
        raise NotImplementedError


    def select_db(self, connection, db):
        '''Select which connection and database schema to use.'''
        if self._connection_name != connection or self._db_name != db:
            self.__mysql = None

        self._connection_name = connection
        self._db_name = db
        return self


    def set_charset(self, charset):
        '''Set the character for the RDBMS.'''
        self._charset = charset
        return self


    def set_persistent(self, persistent):
        self.persistent = persistent
        return self
Пример #12
0
class RDBMSBase(__DataStoreBase):
    '''
    Defines a data store that handles interactions with a RDBMS (MySQL,
    PostgreSQL, etc.).
    '''
    def close(self):
        '''
        Manually close the database connection.
        '''

        raise NotImplementedError

    def commit(self):
        '''
        Manually commit the active transaction.
        '''

        raise NotImplementedError

    def configure(self, settings, db, group):
        '''
        Configure the connection settings.
        '''

        if group not in settings:
            raise DataStoreException(
                'group "{}" not found in settings'.format(group))

        self.close()

        self._settings = settings
        self._db = db
        self._group = group
        return self

    def count(self, sql, binds=tuple()):
        '''
        Given a count(*) query, only return the resultant count.
        '''

        return None

    def create(target, data=tuple(), return_insert_id=False):
        '''
        Create a new record in the RDBMS.
        '''

        return '' if return_insert_id else None

    def delete(target, where=tuple(), binds=tuple()):
        '''
        Delete a record from the RDBMS.
        '''

        return False

    def memcache(self, key, ttl=0):
        '''
        Specify that the result set should be cached in Memcache.
        '''

        self._memcache_key = key
        self._memcache_ttl = ttl
        return self

    def memcache_purge(self):
        '''
        If the data has been cached, purge it.
        '''

        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()
        self._memcache.purge(self._memcache_key)

    def memcache_retrieve(self):
        '''
        If the data needs to be cached, cache it.
        '''

        if self._memcache_key is None or Context.env_unit_test():
            return None

        if self._memcache is None:
            self._memcache = Memcache()

        return self._memcache.retrieve(self._memcache_key)

    def memcache_store(self, data):
        '''
        If there is data and it should be cached, cache it.
        '''

        if self._memcache_key is None or Context.env_unit_test():
            return

        if self._memcache is None:
            self._memcache = Memcache()

        self._memcache.store(self._memcache_key, data, self._memcache_ttl,
                             self._memcache_ttl)

    def nth(self, index, sql, binds=tuple()):
        '''
        Return the value at the Nth position of the result set.
        '''

        return None

    def one(self, sql, binds=tuple(), obj=None):
        '''
        Return the first (and only the first) of the result set.
        '''

        record = self.nth(0, sql, binds)
        if obj is None:
            return record

        if record is None:
            raise RuntimeError('no data is present to assign to object')

        for key, value in record.items():
            setattr(obj, key, value)

        return record

    def ordered_dict_cursor(self):
        self._ordered_dict_cursor = True
        return self

    def query(self, query, binds=[]):
        '''
        Execute an arbitrary query and return all of the results.
        '''

        return None

    def _reset_memcache(self):
        self._memcache_key = None
        self._memcache_ttl = None

    def rollback(self):
        '''
        Manually rollback the active transaction.
        '''

        raise NotImplementedError

    def set_charset(self, charset):
        '''
        Set the character for the RDBMS.
        '''

        self._charset = charset
        return self

    def set_persistent(self, persistent):
        self.persistent = persistent
        return self

    def should_ping(self):
        if self.persistent is False:
            return False

        should_ping = False
        if (time.time() - self._inactive_since) >= (self._ping_interval - 3):
            should_ping = True
            self._inactive_since = time.time()

        return should_ping