/** * @brief gateway class for protocol table */ bool Protocol::findAllProtocols(DataSet<TextSerializer> & ds) { //Response should be : //LEN REQUEST { REQUESTID=1 METHOD=getProtocol ARGS{PROTOCOLID=ALL} STATUS= [STATMESSAGE=] // [RESULTSET { R1=(ID NAME TYPE VERSION ) R2=(ID NAME TYPE VERSION ) ... }] } std::cout << "Entered findAllProtocols"; Connection* conn=Connection::getInstance(); ConnPtr dbHandle=conn->getConnection(); // execute the transaction try { // giving it a scope { if(! dbHandle) { ds.add<std::string>(req_, stat_, "false"); std::ostringstream oss; oss << "Database connection not successful" <<std::endl; ds.add<std::string>(req_, msg_, oss.str()); LOG_ERROR(oss.str()); return false; } pqxx::work w(*dbHandle); pqxx::result R = w.exec("SELECT protocol_id,name,type,version FROM dbo.protocol where status=1 and rec_act=1 order by protocol_id"); //Check whether result is empty if(R.empty()) { ds.add<std::string>(req_, stat_, "true"); return true; } PropertiesPtr resultSet (new Properties()); int i=1; for (pqxx::result::const_iterator r = R.begin(); r != R.end(); ++r) { //name StringsPtr record(new Strings()); record->push_back(r[1].c_str()); //type record->push_back(r[2].c_str()); //version record->push_back(r[3].c_str()); //ID record->push_back(r[0].c_str()); std::ostringstream ostr; ostr << "r" << i; resultSet->insert(std::make_pair(ostr.str(), record)); i++; } ds.add<PropertiesPtr>("REQUEST", "RESULTSET", resultSet); } ds.add<std::string>(req_, stat_, "true"); return true; } catch(pqxx::broken_connection& e) { std::ostringstream oss; oss << "Connection to database broken try again" <<std::endl; ds.add<std::string>(req_, stat_, "false"); ds.add<std::string>(req_, msg_, oss.str()); LOG_ERROR(oss.str()); return false; } catch(pqxx::sql_error& e) { std::ostringstream oss; oss << e.what() << std::endl; ds.add<std::string>(req_, stat_, "false"); ds.add<std::string>(req_, msg_, std::string(e.what())); LOG_ERROR(oss.str()); return false; } catch(...) { std::ostringstream oss; oss << "Unknown error while inserting" << std::endl; ds.add<std::string>(req_, stat_, "false"); ds.add<std::string>(req_, msg_, oss.str()); LOG_ERROR(oss.str()); return false; } return true; }
bool Message :: findAllMessages(DataSet<TextSerializer> &ds) { //Response should be : //LEN REQUEST { REQUESTID=1 METHOD=getMessage ARGS{PROTOCOLID=ALL MESSAGEID=ALL} STATUS= [STATMESSAGE=] // [RESULTSET { R1=(PROTOCOLID MESSAGEID PROTOCOL MDESCRIPTION ) R2=(PROTOCOLID MESSAGEID PROTOCOL MDESCRIPTION ) ... }] } std::cout << "Entered findAllMessages"; // extract pertinent values from Dataset std::string query; std::string* protId = NULL; protId = ds.get<std::string>(args_, protocolId_); if(! protId) { ds.add<std::string>(req_, stat_, "false"); std::ostringstream oss; oss << "PROTOCOLID not found in the request" <<std::endl; ds.add<std::string>(req_, statMsg_, oss.str()); LOG_ERROR(oss.str()); return false; } else { /* If ProtocolID is "ALL" then fetch all the messages of ALL the protocols*/ if( strcmp((*protId).c_str(), "ALL") == 0 ) { query = "SELECT M.protocol_msg_id, M.protocol_id, P.name, M.name, M.descr From dbo.protocol_message M, dbo.protocol P" " WHERE M.protocol_id = P.Protocol_id" " AND M.rec_act = 1 AND M.status = 1" " Order by P.name, M.name"; } /* If ProtocolID is not "ALL" then fetch all the messages for the particular protocol */ else { query = "SELECT M.protocol_msg_id, M.protocol_id, P.name, M.name, M.descr From dbo.protocol_message M, dbo.protocol P" " WHERE M.protocol_id = P.Protocol_id" " AND P.Protocol_id = '" + *protId + "' AND M.rec_act = 1 AND M.status = 1" " Order by P.name, M.name"; } } std::cout << "query = "<< query << std::endl; Connection* conn=Connection::getInstance(); ConnPtr dbHandle=conn->getConnection(); // execute the transaction try { // giving it a scope { if(! dbHandle) { ds.add<std::string>(req_, stat_, "false"); std::ostringstream oss; oss << "Database connection not successful" <<std::endl; ds.add<std::string>(req_, statMsg_, oss.str()); LOG_ERROR(oss.str()); return false; } pqxx::work w(*dbHandle); pqxx::result R = w.exec(query); //Check whether result is empty if(R.empty()) { ds.add<std::string>(req_, stat_, "true"); return true; } PropertiesPtr resultSet (new Properties()); int i=1; for (pqxx::result::const_iterator r = R.begin(); r != R.end(); ++r) { StringsPtr record(new Strings()); record->push_back(r[2].c_str()); record->push_back(r[0].c_str()); record->push_back(r[1].c_str()); record->push_back(r[3].c_str()); record->push_back(r[4].c_str()); std::ostringstream ostr; ostr << "r" << i; resultSet->insert(std::make_pair(ostr.str(), record)); i++; } ds.add<PropertiesPtr>("REQUEST", "RESULTSET", resultSet); } ds.add<std::string>(req_, stat_, "true"); return true; } catch(pqxx::broken_connection& e) { std::ostringstream oss; oss << "Connection to database broken try again" <<std::endl; ds.add<std::string>(req_, stat_, "false"); ds.add<std::string>(req_, statMsg_, oss.str()); LOG_ERROR(oss.str()); return false; } catch(pqxx::sql_error& e) { std::ostringstream oss; oss << e.what() << std::endl; ds.add<std::string>(req_, stat_, "false"); ds.add<std::string>(req_, statMsg_, std::string(e.what())); LOG_ERROR(oss.str()); return false; } catch(...) { std::ostringstream oss; oss << "Unknown error while fetching data" << std::endl; ds.add<std::string>(req_, stat_, "false"); ds.add<std::string>(req_, statMsg_, oss.str()); LOG_ERROR(oss.str()); return false; } return true; }