예제 #1
0
bool DatabaseForm::AddItemToDatabase(DatabaseItem* newItem)
{

	String        notesString(L"very empty");
	Database      pDatabase;
	DbStatement*  pStmt;
	DbEnumerator* pEnum;
	String        statement;
	result        r = E_SUCCESS;

	pDatabase.BeginTransaction();

	// Prepare the INSERT statement
	statement.Append(L"INSERT INTO movies(description) VALUES (?, ?)");
	pStmt = pDatabase.CreateStatementN(statement);

   // Bind the values from the current entry into this statement
	pStmt->BindString(0, newItem->__description);

	// Now execute the statement with our values included
	pEnum = pDatabase.ExecuteStatementN(*pStmt);
	pDatabase.CommitTransaction();

	delete pEnum;
	delete pStmt;
	return true;
}
void EventsWatcherDbOp::InsertEvent( const string& dadbuffer, const string& message )
{
	Database* data = getData();
	
	ParametersVector params;

	DataParameterBase *dadParam = m_DatabaseProvider->createParameter( DataType::LARGE_CHAR_TYPE );
	dadParam->setDimension( dadbuffer.length() );
	dadParam->setString( dadbuffer );
	dadParam->setName( "DAD_buffer" );
	params.push_back( dadParam );

	DataParameterBase *messageParam = m_DatabaseProvider->createParameter( DataType::LARGE_CHAR_TYPE );
	messageParam->setDimension( message.length() );
	messageParam->setName( "XML_buffer" );
	messageParam->setString( message );

	params.push_back( messageParam );

	DataParameterBase *paramReturnCode = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE, DataParameterBase::PARAM_OUT );
	DataParameterBase *paramReturnMessage = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE, DataParameterBase::PARAM_OUT );

	// return code from upload proc
	paramReturnCode->setDimension( 11 );
	params.push_back( paramReturnCode );
		
	// return message from upload proc
	paramReturnMessage->setDimension( 1024 );
	params.push_back( paramReturnMessage );

	data->BeginTransaction();
	try
	{
		data->ExecuteNonQueryCached( DataCommand::SP, "INSERTEVENTSBATCH", params );

		string returnCode = StringUtil::Trim( paramReturnCode->getString() );
		DEBUG( "Insert XML data return code : [" << returnCode << "]" );	 	
		if ( returnCode != "0" )
	 	{
	 		stringstream messageBuffer;
	  		messageBuffer << "Error inserting XML. Message : [" << paramReturnMessage->getString() << "]";
			TRACE( messageBuffer.str() );
			TRACE( "Original_buffer [" << message << "]" );

	 		throw runtime_error( messageBuffer.str() );
	 	}
	}
	catch( const std::exception& ex )
	{
		TRACE( "Insert event failed [" << ex.what() << "]" );
	}
	catch( ... )
	{
		TRACE( "Insert event failed [unknown reason]" );
	}

	data->EndTransaction( TransactionType::COMMIT );
	DEBUG2( "done" );
}
void EventsWatcherDbOp::UpdateServiceVersion( const string& serviceName, const string& name, const string& version, const string& machine, const string& hash )
{
	Database* config = getConfig();
	
	ParametersVector params;
	
	DataParameterBase *serviceIdParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	serviceIdParam->setDimension( serviceName.size() );
	serviceIdParam->setString( serviceName );
	params.push_back( serviceIdParam );
	
	DataParameterBase *nameParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	nameParam->setDimension( name.size() );
	nameParam->setString( name );
	params.push_back( nameParam );	

	DataParameterBase *versionParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	versionParam->setDimension( version.size() );
	versionParam->setString( version );
	params.push_back( versionParam );	

	DataParameterBase *machineParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	machineParam->setDimension( machine.size() );
	machineParam->setString( machine );
	params.push_back( machineParam );	

	DataParameterBase *hashParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	hashParam->setDimension( hash.size() );
	hashParam->setString( hash );
	params.push_back( hashParam );	
	
	try
	{
		config->BeginTransaction();
		config->ExecuteNonQuery( DataCommand::SP, "UPDATEVERSION", params );
	}
	catch( const std::exception& ex )
	{
		config->EndTransaction( TransactionType::ROLLBACK );
		TRACE( "Update service version failed [" << ex.what() << "] for [" << serviceName << "]" );
		
		throw;
	}
	catch( ... )
	{
		config->EndTransaction( TransactionType::ROLLBACK );
		TRACE( "Update service version failed [unknown reason] for [" << serviceName << "]" );

		throw;
	}

	config->EndTransaction( TransactionType::COMMIT );
}
DataSet* EventsWatcherDbOp::ReadServiceStatus()
{
	Database* config = getConfig();

	config->BeginTransaction();

	DEBUG_GLOBAL( "Reading service status for all services" );
	DataSet *retStatus = config->ExecuteQuery( DataCommand::SP, "GetServiceState" );

	config->EndTransaction( TransactionType::ROLLBACK );

	return retStatus;
}
void EventsWatcherDbOp::UpdateServiceState( const long serviceId, const long newState, const string& sessionId )
{
	Database* config = getConfig();
	
	ParametersVector params;
	
	DataParameterBase *serviceIdParam = m_DatabaseProvider->createParameter( DataType::LONGINT_TYPE );
	serviceIdParam->setLong( serviceId );
	params.push_back( serviceIdParam );
	
	DataParameterBase *newStateParam = m_DatabaseProvider->createParameter( DataType::LONGINT_TYPE );
	newStateParam->setLong( newState );
	params.push_back( newStateParam );

	DataParameterBase *sessionIdParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	sessionIdParam->setDimension( sessionId.size() );
	sessionIdParam->setString( sessionId );
	params.push_back( sessionIdParam );	
	
	try
	{
		config->BeginTransaction();
		config->ExecuteNonQuery( DataCommand::SP, "UPDATESERVICESTATE", params );
	}
	catch( const std::exception& ex )
	{
		config->EndTransaction( TransactionType::ROLLBACK );
		TRACE( "Update service state failed [" << ex.what() << "] for [" << serviceId << "]" );
		
		throw;
	}
	catch( ... )
	{
		config->EndTransaction( TransactionType::ROLLBACK );
		TRACE( "Update service state failed [unknown reason] for [" << serviceId << "]" );

		throw;
	}

	config->EndTransaction( TransactionType::COMMIT );
}
void EventsWatcherDbOp::InsertEvent( const long serviceId, const string& correlationId, const string& sessionId,
	const string& evtype, const string& machine, const string& date, const string& messageBuffer, 
	const string& event_class, const string& additionalInfo, const string& innerException )
{
	Database* data = getData();
	
	ParametersVector params;
	
	string guid = Collaboration::GenerateGuid();
	DEBUG2( "guidParam [" << guid << "]" );
	DataParameterBase *guidParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	guidParam->setDimension( guid.size() );
	guidParam->setString( guid );
	params.push_back( guidParam );
		
	DEBUG2( "serviceIdParam [" << serviceId << "]" );
	DataParameterBase *serviceIdParam = m_DatabaseProvider->createParameter( DataType::LONGINT_TYPE );
	serviceIdParam->setLong( serviceId );
	params.push_back( serviceIdParam );

	DEBUG2( "correl [" << correlationId << "]" );
	DataParameterBase *correlIdParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	correlIdParam->setDimension( correlationId.size() );
	correlIdParam->setString( correlationId );
	params.push_back( correlIdParam );
		
	DEBUG2( "session [" << sessionId << "]" );
	DataParameterBase *sessionIdParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	sessionIdParam->setDimension( sessionId.size() );
	sessionIdParam->setString( sessionId );
	params.push_back( sessionIdParam );	

	DEBUG2( "type [" << evtype << "]" );
	DataParameterBase *typeParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	typeParam->setDimension( evtype.size() );
	typeParam->setString( evtype );
	params.push_back( typeParam );	
	
	DEBUG2( "machine [" << machine << "]" );
	DataParameterBase *machineParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	machineParam->setDimension( machine.size() );
	machineParam->setString( machine );
	params.push_back( machineParam );
	
	DEBUG2( "eventdate [" << date << "]" );
	DataParameterBase *dateParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	dateParam->setDimension( date.size() );
	dateParam->setString( date );
	params.push_back( dateParam );

	DEBUG2( "message" );
	DataParameterBase *messageParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	string::size_type messageBufferLength = messageBuffer.length();
	if( messageBufferLength > 256 )
	{
		messageParam->setDimension( 256 );
		messageParam->setString( messageBuffer.substr( 0, 256 ) );
	}
	else
	{
		messageParam->setDimension( messageBufferLength );
		messageParam->setString( messageBuffer );
	}
	params.push_back( messageParam );

	DEBUG2( "class [" << event_class << "]" );
	DataParameterBase *classParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	classParam->setDimension( event_class.size() );
	classParam->setString( event_class );
	params.push_back( classParam );
		
	DEBUG2( "addinfo [" << additionalInfo << "]" );
	DataParameterBase *addInfoParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );
	if( ( additionalInfo.length() == 0 ) && ( messageBufferLength > 256 ) )
	{
		addInfoParam->setDimension( messageBufferLength );
		addInfoParam->setString( messageBuffer );
	}
	else
	{
		if ( additionalInfo.length() > 2999 )
		{
			addInfoParam->setDimension( 2999 );
			addInfoParam->setString( additionalInfo.substr( 0, 2999 ) );
		}
		else
		{
			addInfoParam->setDimension( additionalInfo.length() );
			addInfoParam->setString( additionalInfo );
		}
	}
	params.push_back( addInfoParam );	

	DEBUG2( "innerex [" << innerException << "]" );
	DataParameterBase *innerExParam = m_DatabaseProvider->createParameter( DataType::CHAR_TYPE );

	if( innerException.length() > 3499 )
	{
		innerExParam->setDimension( 3499 );
		innerExParam->setString( innerException.substr( 0, 3499 ) );
	}
	else
	{
		innerExParam->setDimension( innerException.length() );
		innerExParam->setString( innerException );
	}
	params.push_back( innerExParam );	
	
	data->BeginTransaction();
	try
	{
		data->ExecuteNonQueryCached( DataCommand::SP, "INSERTEVENT", params );
	}
	catch( const std::exception& ex )
	{
		TRACE( "Insert event failed [" << ex.what() << "]" );
	}
	catch( ... )
	{
		TRACE( "Insert event failed [unknown reason]" );
	}

	data->EndTransaction( TransactionType::COMMIT );
	DEBUG_GLOBAL( "done" );
}