X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fiotlab%2Fiotlabpostgres.py;h=097e61e5f3c6dcf02e17e3008fef65bfa373e58a;hb=d748c93a11a23b22d9e310db3c58c2bfb18b38fb;hp=eed8769a4ef53a189510708e365dda5e2a0ac148;hpb=0cc524b0816203ed36589fa68337f9291fad0b7b;p=sfa.git diff --git a/sfa/iotlab/iotlabpostgres.py b/sfa/iotlab/iotlabpostgres.py index eed8769a..097e61e5 100644 --- a/sfa/iotlab/iotlabpostgres.py +++ b/sfa/iotlab/iotlabpostgres.py @@ -1,38 +1,22 @@ """ -File defining classes to handle the table in the iotlab dedicated database. -""" +File holding a class to define the table in the iotlab dedicated table. +The table is the SFA dtabase, therefore all the access mecanism +(session, engine...) is handled by alchemy.py. -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker -# from sfa.util.config import Config -from sfa.util.sfalogging import logger +..seealso:: alchemy.py +""" +from sfa.storage.model import Base 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 (Base): + """ SQL alchemy class to manipulate the rows of the lease_table table in the + SFA database. Handles the records representation and creates. + Table creation is made by the importer if it is not in the database yet. -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. + .. seealso:: init_tables in model.py, run in iotlabimporter.py """ __tablename__ = 'lease_table' @@ -43,7 +27,7 @@ class LeaseTableXP (TestbedBase): def __init__(self, slice_hrn=None, experiment_id=None, end_time=None): """ - Defines a row of the slice_iotlab table + Defines a row of the lease_table table """ if slice_hrn: self.slice_hrn = slice_hrn @@ -56,218 +40,8 @@ class LeaseTableXP (TestbedBase): """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