Commit 1e9a402c authored by Marco De Marco's avatar Marco De Marco
Browse files

DataModel refactored

parent 856c6c72
Loading
Loading
Loading
Loading
+63 −45
Original line number Diff line number Diff line
#include <DataModelManager.h>

#include <soci.h>
#include <soci/rowset.h>
#include <soci/use.h>
@@ -9,15 +10,13 @@
//==============================================================================
//      DataModelManager::DataModelManager()
//==============================================================================

DataModelManager::DataModelManager(Tango::DeviceImpl* device_impl_p,
        std::string host, unsigned short port, std::string user,
        std::string password, std::string schema, std::string instrumentTable,        
        std::string destinationTable, std::string mappingTable) :
        Tango::LogAdapter(device_impl_p),
        m_host(host), m_port(port), m_user(user), m_password(password),
        m_schema(schema), m_instrumentTable(instrumentTable),
        m_destinationTable(destinationTable), m_mappingTable(mappingTable)
    std::string host, unsigned short port, std::string user, std::string password, 
    std::string schema, std::string instrumentTable, std::string destinationTable,
    std::string mappingTable) : Tango::LogAdapter(device_impl_p), m_host(host),
    m_port(port), m_user(user), m_password(password), m_schema(schema),
    m_instrumentTable(instrumentTable), m_destinationTable(destinationTable), 
    m_mappingTable(mappingTable)
{
    DEBUG_STREAM << "DataModelManager::DataModelManager()" << endl;
    
@@ -34,6 +33,21 @@ DataModelManager::~DataModelManager()
    m_session_sp->close();
}

//==============================================================================
//      DataModelManager::create()
//==============================================================================
DataModelManager::SP DataModelManager::create(Tango::DeviceImpl* device_impl_p, 
    std::string host, unsigned short port, std::string user, std::string password, 
    std::string schema, std::string instrumentTable, std::string destinationTable, 
    std::string mappingTable)     
{
    DataModelManager::SP d_sp(new DataModelManager(device_impl_p, host, port, 
        user, password, schema, instrumentTable, destinationTable, mappingTable), 
        DataModelManager::Deleter());
    
    return d_sp;
}        

//==============================================================================
//      DataModelManager::connect()
//==============================================================================
@@ -48,8 +62,9 @@ void DataModelManager::connect() throw(soci::soci_error)
    connection << " password=" << m_password;
    connection << " dbname=" << m_schema;

    //DELETEME
    DEBUG_STREAM << "Connection string: " << connection.str() << endl;
#ifdef VERBOSE_DEBUG
    INFO_STREAM << "Connection: " << connection.str() << endl;
#endif
    
    m_session_sp->open(soci::mysql, connection.str());       
}
@@ -124,13 +139,12 @@ Instrument::SP DataModelManager::retrieveInstrument(std::string instrumentName)
{
    DEBUG_STREAM << "DataModelManager::retrieveInstrument()" << endl;
            
    InstrumentTupleVector instrumentTuple_vector;
    
    soci::rowset< InstrumentTuple > rows = (m_session_sp->prepare <<
        "SELECT id, name, fits_key, fits_value, fits_date, dest_id "
        "FROM " << m_instrumentTable << " WHERE name=:name",
        soci::use(instrumentName, "name"));
    
    InstrumentTupleVector instrumentTuple_vector;
    std::copy(rows.begin(), rows.end(), std::back_inserter(instrumentTuple_vector)); 
    
    if(instrumentTuple_vector.empty())
@@ -165,13 +179,16 @@ Instrument::SP DataModelManager::retrieveInstrument(std::string instrumentName)
        throw soci::soci_error("DestId not found for id " + instId);
    int destinationId = instrumentTuple.get<5>().get();
    
    //DELETEME
    DEBUG_STREAM << "Instrument: " << name << " fistKey: " << fitsKey 
            << " fitsValue: " << fitsValue << " fitsDate: " << fitsDate << endl;
#ifdef VERBOSE_DEBUG    
    INFO_STREAM << "*" << instId << "|" << name << "|" << fitsKey << "|"
        << fitsValue << "|" << fitsDate << "|" << destinationId << "*" << endl;
    INFO_STREAM << "-------------------------------------------------" << endl;
#endif
    
    Destination::SP destination_sp = retrieveDestination(destinationId);
    
    Instrument::SP instrument_sp = Instrument::create(name, fitsKey, fitsValue, fitsDate, destination_sp);
    Instrument::SP instrument_sp = Instrument::create(name, fitsKey, fitsValue, 
        fitsDate, destination_sp);
    
    return instrument_sp;
}        
@@ -184,13 +201,12 @@ Destination::SP DataModelManager::retrieveDestination(int destinationId)
{
    DEBUG_STREAM << "DataModelManager::retrieveDestinations()" << endl;
  
    DestinationTupleVector destinationTuple_vector;
    
    soci::rowset< DestinationTuple > rows = (m_session_sp->prepare <<
        "SELECT id, host, port, user, password, schema_name, table_name, "
        "storage_path FROM " << m_destinationTable << " WHERE id=:id",
        soci::use(destinationId,"id"));

    DestinationTupleVector destinationTuple_vector;
    std::copy(rows.begin(), rows.end(), std::back_inserter(destinationTuple_vector)); 

    if(destinationTuple_vector.empty())
@@ -229,10 +245,12 @@ Destination::SP DataModelManager::retrieveDestination(int destinationId)
            throw soci::soci_error("Storage not found for id " + destinationId);
    std::string storage = destinationTuple.get<7>().get();

    //DELETEME
    DEBUG_STREAM << "Host: " << host << " port: " << port << " user: " << user 
            << " password: " << password << " schema: " << schema 
            << " table: " << table << " storage: " << storage << endl;
#ifdef VERBOSE_DEBUG    
    INFO_STREAM << "*" << destinationId << "|" << host << "|" << port << "|"
        << user << "|" << password << "|" << schema << "|" << table << "|"
        << storage << "*" << endl;
    INFO_STREAM << "-------------------------------------------------" << endl;    
#endif
    
    Mapping::SPVector mapping_spvector = retrieveMapping(destinationId);    

@@ -250,12 +268,11 @@ Mapping::SPVector DataModelManager::retrieveMapping(int destinationId)
{
    DEBUG_STREAM << "DataModelManager::retrieveMappings()" << endl;
    
    MappingTupleVector mappingTupleList;
    
    soci::rowset< MappingTuple > rows = (m_session_sp->prepare <<
    "SELECT id, column_name, column_type, fits_key_pri, fits_key_sec, mandatory "
    "FROM " << m_mappingTable << " WHERE dest_id=:id", soci::use(destinationId,"id"));

    MappingTupleVector mappingTupleList;
    std::copy(rows.begin(), rows.end(), std::back_inserter(mappingTupleList));

    Mapping::SPVector mapping_spvector;
@@ -287,10 +304,11 @@ Mapping::SPVector DataModelManager::retrieveMapping(int destinationId)
                throw soci::soci_error("Mandatory not found for id " + mapId);
        int mandatory = it->get<5>().get();

        //DELETEME
        DEBUG_STREAM << "Column name: " << columnName << " columnType: " << columnType
                << " fitsPrimary: " << fitsPrimary << " fitsSecondary: " << fitsSecondary
                << " mandatory: " << mandatory << endl;
#ifdef VERBOSE_DEBUG
        INFO_STREAM << "*" << mapId << "|" << columnName << "|" << columnType << "|"
            << fitsPrimary << "|" << fitsSecondary << "|" << mandatory << "*" << endl;            
        INFO_STREAM << "-------------------------------------------------" << endl;
#endif        
        
        mapping_spvector.push_back( Mapping::create(columnName, columnType,
                 fitsPrimary, fitsSecondary, mandatory) );    
+25 −15
Original line number Diff line number Diff line
@@ -2,16 +2,12 @@
#define	DATAMODELMANAGER_H

#include <Instrument.h>

#include <tango.h>

#include <stdexcept>

#include <boost/shared_ptr.hpp>
#include <boost/optional/optional.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/scoped_ptr.hpp>

#include <soci/error.h>
#include <soci/session.h>

@@ -19,21 +15,35 @@ class DataModelManager : public Tango::LogAdapter
{
public:
//------------------------------------------------------------------------------
//  Shared pointer typedef
//  [Public] Shared pointer typedef
//------------------------------------------------------------------------------
    typedef boost::shared_ptr<DataModelManager> SP;

protected:    
//------------------------------------------------------------------------------
//	Constructor and destructor
//	[Protected] Constructor destructor deleter
//------------------------------------------------------------------------------
    DataModelManager(Tango::DeviceImpl*, std::string, unsigned short, std::string,
        std::string, std::string, std::string, std::string, std::string);
        
    virtual ~DataModelManager();
    
	class Deleter;
	friend class Deleter;
	class Deleter
	{
	public:
		void operator()(DataModelManager* d) { delete d; }
	};    

public:    
//------------------------------------------------------------------------------
//	Users methods
//	[Public] Users methods
//------------------------------------------------------------------------------
    static DataModelManager::SP create(Tango::DeviceImpl*, std::string, 
        unsigned short, std::string, std::string, std::string, std::string, 
        std::string, std::string);        
                
    virtual void connect() throw(soci::soci_error);
    
    virtual bool isConnected();
@@ -48,7 +58,7 @@ public:
  
protected:
//------------------------------------------------------------------------------
//  Utilities methods
//  [Protected] Utilities methods
//------------------------------------------------------------------------------
    virtual Instrument::SP retrieveInstrument(std::string)
        throw(soci::soci_error);
@@ -60,7 +70,7 @@ protected:
        throw(soci::soci_error);        

//------------------------------------------------------------------------------
//  Instrument tuple typedef
//  [Protected] Instrument tuple typedef
//------------------------------------------------------------------------------
    typedef boost::tuple< boost::optional<int>, boost::optional<std::string>,
            boost::optional<std::string>, boost::optional<std::string>,
@@ -69,7 +79,7 @@ protected:
    typedef std::vector< InstrumentTuple > InstrumentTupleVector;

//------------------------------------------------------------------------------
//  Destination tuple typedef
//  [Protected] Destination tuple typedef
//------------------------------------------------------------------------------
    typedef boost::tuple< boost::optional<int>, boost::optional<std::string>,
            boost::optional<int>, boost::optional<std::string>,
@@ -79,7 +89,7 @@ protected:
    typedef std::vector< DestinationTuple > DestinationTupleVector;

//------------------------------------------------------------------------------
//  Mapping tuple typedef
//  [Protected] Mapping tuple typedef
//------------------------------------------------------------------------------    
    typedef boost::tuple< boost::optional<int>, boost::optional<std::string>,
            boost::optional<std::string>, boost::optional<std::string>,
@@ -88,7 +98,7 @@ protected:
    typedef std::vector< MappingTuple > MappingTupleVector;
    
//------------------------------------------------------------------------------
//	Class variables
//	[Protected] Class variables
//------------------------------------------------------------------------------
    //Data model database host
    std::string m_host;