Example #1
0
/** 
  * DbManager::addDataNode
  *
  * Adds the DataNode to the Entity table.
  *
  * @param parentKey The pkey of the parent.
  * @return unsigned int The pkey of the datanode.
  */
unsigned int DbManager::addDataNode( std::string name, unsigned int agentKey )
{
    unsigned int dataNodeType = getEntityTypeKey( "DataNode" );
    unsigned int dataNodeKey = 0;

    IData* id = NULL;
    std::vector<std::string> columns;
    columns.push_back( "pkey" );

    // See if the agent already exists.
    id = m_db.executeQuery( "SELECT pkey FROM entity WHERE name = '"+name+"'", columns );

    // Add the agent if it doesn't.
    if ( id->getNumRows() == 0 )
    {
        delete id;
        id = NULL;

        std::ostringstream query;
        query << "INSERT INTO entity ";
        query << "(pkey,name,address,description,subsystemkey,locationkey,typekey,";
        query << "agentkey,parentkey) VALUES (ENTITY_SEQ.NEXTVAL,'";
        query << name;
        query << "','Test DataNode','Test DataNode',6,1,";
        query << dataNodeType;
        query << ",";
        query << agentKey;
        query << ",";
        query << agentKey;
        query << ")";
        m_db.executeModification( query.str() );

        // Get the DataNode's pkey.
        id = m_db.executeQuery( "SELECT pkey FROM entity WHERE name = '"+name+"'", columns );
        dataNodeKey = id->getUnsignedLongData( 0, "pkey" );

        delete id;
        id = NULL;
    }
    else
    {
        // If the DataNode exists, make sure no parameters are set.
        dataNodeKey = id->getUnsignedLongData( 0, "pkey" );

        delete id;
        id = NULL;

        // Make sure the DataNode has no parameters.
        removeParameters( dataNodeKey );
    }
    
    return dataNodeKey;
}
Example #2
0
/** 
  * DbManager::getEntityParameterKey
  *
  * Determines the pkey for EntityParameter 'name' associated with 'type'.
  *
  * @param name The name to find.
  * @param type The type of the parameter.
  *
  * @return unsigned int The pkey.
  *
  */
unsigned int DbManager::getEntityParameterKey( std::string name, unsigned int type )
{
    // Search the type key map for the provided type name.
    ParameterMap::iterator it = m_parameterKeys.find( StringUintPair(name,type) );
    if ( it != m_parameterKeys.end() )
    {
	    return it->second;
    }

    // If the key wasn't found retrieve it from the database.
    
    unsigned int pkey = 0;

    IData* id = NULL;
    std::vector<std::string> columns;
    columns.push_back( "pkey" );

    std::ostringstream query;
    query << "SELECT pkey FROM entityparameter WHERE name = '";
    query << name;
    query << "' AND typekey = ";
    query << type;
    id = m_db.executeQuery( query.str(), columns );

    if ( id->getNumRows() == 1 )
    {
        pkey = id->getUnsignedLongData( 0, "pkey" );

        delete id;
        id = NULL;
    }
    else
    {
        delete id;
        id = NULL;

        std::string message = "Couldnt find unique EntityParameter "+name;
        throw DatabaseException( message.c_str(), DatabaseException::REQUEST_FAILED );
    }

    // Insert the parameter into the map.
    m_parameterKeys.insert( std::pair<StringUintPair,unsigned int>( StringUintPair(name,type), pkey ) );

    return pkey;
}
Example #3
0
/** 
  * DbManager::addTestAgent
  *
  * Adds the test agent to the Entity table.
  *
  * @return unsigned int The pkey of the test agent.
  */
unsigned int DbManager::addTestAgent( unsigned int scadaRootType )
{
    unsigned int agentKey = 0;

    // Get the SCADAROOT type.
    IData* id = NULL;
    std::vector<std::string> columns;
    columns.push_back( "pkey" );

    // See if the agent already exists.
    id = m_db.executeQuery( "SELECT pkey FROM entity WHERE name = '"+AgentName+"'", columns );

    // Add the agent if it doesn't.
    if ( id->getNumRows() == 0 )
    {
        delete id;
        id = NULL;

        std::ostringstream query;
        query << "INSERT INTO entity ";
        query << "(pkey,name,address,description,subsystemkey,locationkey,typekey,";
        query << "agentkey,parentkey) VALUES (ENTITY_SEQ.NEXTVAL,'";
        query << AgentName;
        query << "','VIRTUAL','Test Agent',6,1,";
        query << scadaRootType;
        query << ",ENTITY_SEQ.NEXTVAL,0)";
        m_db.executeModification( query.str() );

        // Get the pkey of the agent.
        id = m_db.executeQuery( "SELECT pkey FROM entity WHERE name = '"+AgentName+"'", columns );
    }

    agentKey = id->getUnsignedLongData( 0, "pkey" );

    delete id;
    id = NULL;
   
    return agentKey;
}
Example #4
0
/** 
  * DbManager::getEntityTypeKey
  *
  * Determines the pkey for EntityType 'name'.
  *
  * @param name The name to find.
  *
  * @return unsigned int The pkey.
  *
  */
unsigned int DbManager::getEntityTypeKey( std::string name )
{
    // Search the type key map for the provided type name.
    KeyMap::iterator it = m_typeKeys.find( name );
    if ( it != m_typeKeys.end() )
    {
	    return it->second;
    }

    // If the key wasn't found retrieve it from the database.
    unsigned int pkey = 0;

    IData* id = NULL;
    std::vector<std::string> columns;
    columns.push_back( "pkey" );
    id = m_db.executeQuery( "SELECT pkey FROM entitytype WHERE name = '"+name+"'", columns );

    if ( id->getNumRows() == 1 )
    {
        pkey = id->getUnsignedLongData( 0, "pkey" );

        delete id;
        id = NULL;
    }
    else
    {
        delete id;
        id = NULL;

        std::string message = "Couldnt find EntityType "+name;
        throw DatabaseException( message.c_str(), DatabaseException::REQUEST_FAILED );
    }

    // Insert the type into the map.
    m_typeKeys.insert( std::pair<std::string,unsigned int>( name, pkey ) );

    return pkey;
}
Example #5
0
/**
  * DbManager::addSafetyOutputInfo
  *
  * Adds the correct information to the SafetyOutputDataPoint.
  *
  * @param entityKey The entity key to add the information to.
  */
void DbManager::addSafetyOutputInfo( unsigned int safetyKey, unsigned int output0Key, unsigned int output1Key )
{
    unsigned int state0Key = 0;
    unsigned int state1Key = 0;
    IData* id = NULL;
    std::vector<std::string> columns;
    columns.push_back( "scdsta_id" );

    std::ostringstream query;

    // Add the states.
    query.str("");
    query << "INSERT INTO sc_derived_state ( scdsta_id,derived_dp_pkey,state_value,";
    query << "state_description,alarm_enabled,alarm_delay,alarm_message,";
    query << "alarm_severity ) VALUES ( SCADA_SEQ.NEXTVAL,";
    query << safetyKey;
    query << ",0,'State 0',0,0,'State 0 Alarm',0 )";
    m_db.executeModification( query.str() );

    query.str("");
    query << "INSERT INTO sc_derived_state ( scdsta_id,derived_dp_pkey,state_value,";
    query << "state_description,alarm_enabled,alarm_delay,alarm_message,";
    query << "alarm_severity ) VALUES ( SCADA_SEQ.NEXTVAL,";
    query << safetyKey;
    query << ",1,'State 1',0,0,'State 1 Alarm',0 )";
    m_db.executeModification( query.str() );

    // Get the keys.
    query.str("");
    query << "SELECT scdsta_id FROM sc_derived_state WHERE derived_dp_pkey = ";
    query << safetyKey;
    query << " ORDER BY scdsta_id";
    id = m_db.executeQuery( query.str(), columns );
    if ( id->getNumRows() != 2 )
    {
        delete id;
        id = NULL;
        throw DatabaseException( "Couldn't find both safety output states!", DatabaseException::REQUEST_FAILED );
    }
    state0Key = id->getUnsignedLongData( 0, "scdsta_id" );
    state1Key = id->getUnsignedLongData( 1, "scdsta_id" );
    delete id;
    id = NULL;

    // Add the output associations.
    query.str("");
    query << "INSERT INTO sc_derived_output_association ( scdoas_id,scdsta_id,";
    query << "output_dp_pkey,output_value ) VALUES ( SCADA_SEQ.NEXTVAL,";
    query << state0Key;
    query << ",";
    query << output0Key;
    query << ",1 )";
    m_db.executeModification( query.str() );

    query.str("");
    query << "INSERT INTO sc_derived_output_association ( scdoas_id,scdsta_id,";
    query << "output_dp_pkey,output_value ) VALUES ( SCADA_SEQ.NEXTVAL,";
    query << state0Key;
    query << ",";
    query << output1Key;
    query << ",0 )";
    m_db.executeModification( query.str() );

    query.str("");
    query << "INSERT INTO sc_derived_output_association ( scdoas_id,scdsta_id,";
    query << "output_dp_pkey,output_value ) VALUES ( SCADA_SEQ.NEXTVAL,";
    query << state1Key;
    query << ",";
    query << output0Key;
    query << ",0 )";
    m_db.executeModification( query.str() );

    query.str("");
    query << "INSERT INTO sc_derived_output_association ( scdoas_id,scdsta_id,";
    query << "output_dp_pkey,output_value ) VALUES ( SCADA_SEQ.NEXTVAL,";
    query << state1Key;
    query << ",";
    query << output1Key;
    query << ",1 )";
    m_db.executeModification( query.str() );
}