""" 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 = {'iotlab_xp': slice_table} IotlabBase = declarative_base() class IotlabXP (IotlabBase): """ SQL alchemy class to manipulate the rows of the slice_iotlab table in iotlab_sfa database. Handles the records representation and creates the table if it does not exist yet. """ __tablename__ = 'iotlab_xp' slice_hrn = Column(String) job_id = Column(Integer, primary_key=True) end_time = Column(Integer, nullable=False) def __init__(self, slice_hrn=None, job_id=None, end_time=None): """ Defines a row of the slice_iotlab table """ if slice_hrn: self.slice_hrn = slice_hrn if job_id: self.job_id = job_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.iotlab_session.query(IotlabXP).filter(IotlabXP.job_id.in_(deleted_jobs)).delete(synchronize_session='fetch') self.iotlab_session.commit() return def __init__(self, config, debug=False): self.sl_base = IotlabBase # Check whether we already have an instance if IotlabDB._connection_singleton is None: IotlabDB._connection_singleton = IotlabDB.Singleton(config, debug) # Store instance reference as the only member in the handle self._EventHandler_singleton = IotlabDB._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.iotlab_engine) try: table = Table(tablename, metadata, autoload=True) return True except NoSuchTableError: logger.log_exc("IOTLABPOSTGRES 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 \ IotlabBase.metadata.sorted_tables %s \r\n engine %s" % (IotlabBase.metadata.sorted_tables, self.iotlab_engine)) IotlabBase.metadata.create_all(self.iotlab_engine) return