""" File defining classes to handle the table in the iotlab dedicated database. """ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker # from sfa.util.config import Config from sfa.util.sfalogging import logger from sqlalchemy import Column, Integer, String from sqlalchemy import Table, MetaData from sqlalchemy.ext.declarative import declarative_base # from sqlalchemy.dialects import postgresql from sqlalchemy.exc import NoSuchTableError #Dict holding the columns names of the table as keys #and their type, used for creation of the table slice_table = {'record_id_user': 'integer PRIMARY KEY references X ON DELETE \ CASCADE ON UPDATE CASCADE', 'oar_job_id': 'integer DEFAULT -1', 'record_id_slice': 'integer', 'slice_hrn': 'text NOT NULL'} #Dict with all the specific iotlab tables # tablenames_dict = {'lease_table': slice_table} TestbedBase = declarative_base() class LeaseTableXP (TestbedBase): """ SQL alchemy class to manipulate the rows of the slice_iotlab table in lease_table database. Handles the records representation and creates the table if it does not exist yet. """ __tablename__ = 'lease_table' slice_hrn = Column(String) experiment_id = Column(Integer, primary_key=True) end_time = Column(Integer, nullable=False) def __init__(self, slice_hrn=None, experiment_id=None, end_time=None): """ Defines a row of the slice_iotlab table """ if slice_hrn: self.slice_hrn = slice_hrn if experiment_id: self.experiment_id = experiment_id if end_time: self.end_time = end_time def __repr__(self): """Prints the SQLAlchemy record to the format defined by the function. """ result = " 0: self.testbed_session.query(LeaseTableXP).filter(LeaseTableXP.experiment_id.in_(deleted_experiments)).delete(synchronize_session='fetch') self.testbed_session.commit() return def __init__(self, config, debug=False): self.sl_base = TestbedBase # Check whether we already have an instance if TestbedAdditionalSfaDB._connection_singleton is None: TestbedAdditionalSfaDB._connection_singleton = \ TestbedAdditionalSfaDB.Singleton(config, debug) # Store instance reference as the only member in the handle self._EventHandler_singleton = \ TestbedAdditionalSfaDB._connection_singleton def __getattr__(self, aAttr): """ Delegate access to implementation. :param aAttr: Attribute wanted. :returns: Attribute """ return getattr(self._connection_singleton, aAttr) # def __setattr__(self, aAttr, aValue): # """Delegate access to implementation. # :param attr: Attribute wanted. # :param value: Vaule to be set. # :return: Result of operation. # """ # return setattr(self._connection_singleton, aAttr, aValue) def exists(self, tablename): """ Checks if the table specified as tablename exists. :param tablename: name of the table in the db that has to be checked. :type tablename: string :returns: True if the table exists, False otherwise. :rtype: bool """ metadata = MetaData(bind=self.testbed_engine) try: table = Table(tablename, metadata, autoload=True) return True except NoSuchTableError: logger.log_exc("SLABPOSTGRES tablename %s does not exist" % (tablename)) return False def createtable(self): """ Creates all the table sof the engine. Uses the global dictionnary holding the tablenames and the table schema. """ logger.debug("IOTLABPOSTGRES createtable \ TestbedBase.metadata.sorted_tables %s \r\n engine %s" % (TestbedBase.metadata.sorted_tables, self.testbed_engine)) TestbedBase.metadata.create_all(self.testbed_engine) return