#include "Global.h"
#include "Exception.h"
#include "__mysql.h"
#include <iostream>
#include <stdlib.h>

namespace jet {

   Global::Global(char **envp) : envp(envp) {
      
   }

   Global::~Global() {

   }

   void Global::dump() {
      for (auto i = variables.begin(); i != variables.end(); i++)
	std::cout << i->first << "=[" << i->second << "]" << std::endl; 
   }

   bool Global::sessionExists(coreutils::MString sessionId) {
      return sessions.find(sessionId) != sessions.end();
   }
   
   void Global::addSession(coreutils::MString sessionId, __mysql *mysql) {
      if(sessionExists(sessionId))
	coreutils::Exception("sessionid already exists.");
      sessions[sessionId] = mysql;
   }
   
   void Global::removeSession(coreutils::MString sessionId) {
      sessions.erase(sessionId);
   }
         
   __mysql * Global::getSession(coreutils::MString sessionId) {
      if(sessions.find(sessionId) == sessions.end())
	throw coreutils::Exception("requested session is not available.");
      return sessions[sessionId];
   }
   
   coreutils::ZString Global::getSessionVariable(coreutils::MString &splitName) {      
      if(sessions.find(splitName[0]) == sessions.end())
	throw coreutils::Exception("requested session is not available in variable.");
      return sessions[splitName[0]]->getColumnValue(splitName[1]);
   }

   void Global::outputHeaders() {
      if(headers.size() > 0) {	
	 for(auto header = headers.begin();
	     header != headers.end();
	     ++header) {
	    std::cout << header->first << ": " << header->second << std::endl;
	 }
	 std::cout << std::endl;
      }   
   }

}
