续--connection.cpp

来源:互联网 发布:固高运动控制卡编程 编辑:程序博客网 时间:2024/05/17 05:52
#include "connection.hpp"
#include <boost/lexical_cast.hpp>

using namespace sqlpp;

Connection::Connection():_is_open(false),_num_fields(0),_ptr_result(NULL)
{
}

Connection::Connection(const std::string& database, const std::string& server, const std::string& user, const std::string& password):_is_open(false),_num_fields(0),_ptr_result(NULL)
{
  try
    {
      connect(database,server,user,password);
    }
  catch(...)
    {
      throw;
    }
}

Connection::Connection(const char* database, const char* server, const char* user, const char* password):_is_open(false),_num_fields(0),_ptr_result(NULL)
{
  try
    {
      connect(database,server,user,password);
    }
  catch(...)
    {
      throw;
    }
}

Connection::~Connection()
{
  close();
  mysql_library_end();//finially library
}

void Connection::connect()throw(sqlerror)
{
  if ( mysql_init(&_mysql) == NULL)//mysql_library_init invoke by mysql_init because of single-thread mode
  {
    throw sqlerror("mysql_init","return null error");
  }

  //error occur
  if (!mysql_real_connect(&_mysql,_server.c_str(),_user.c_str(),_password.c_str(),_database.c_str(),0,NULL,0))
  {
    try
      {
    check_exception();
      }
    catch(...)
      {
    throw;
      }
  }
  else
  {
    _is_open = true;
  } 
}

void Connection::connect(const std::string& database,
             const std::string& server,
             const std::string& user,
             const std::string& password)throw(sqlerror)
{
  try
    {
      _database = database;
      _server   = server;
      _user     = user;
      _password = password;
      connect();
    }
  catch(...)
    {
      throw;
    }
}

void Connection::connect(const char* database, const char* server, const char* user, const char* password)throw(sqlerror)
{
  try
    {
      _database = database;
      _server   = server;
      _user     = user;
      _password = password;
      connect();
    }
  catch(...)
    {
      throw;
    }
}

bool Connection:: is_open(void)throw()
{
  return _is_open;
}

void Connection::close(void)throw()
{
  if (_is_open)
  {
    mysql_close(&_mysql);
  }
}

std::string Connection::get_client_info(void) throw(sqlerror)
{
  if (!_is_open)
  {
    try
      {
    connect();
      }
    catch(...)
      {
    throw;
      }
  }
  return std::string( mysql_get_client_info() ); 
}

unsigned long Connection::get_client_version(void) throw(sqlerror)
{
  if (!_is_open)
  {
    try
      {
    connect();
      }
    catch(...)
      {
    throw;
      }    
  }
  return  mysql_get_client_version();
}

std::string Connection::get_host_info(void) throw(sqlerror)
{
  if (!_is_open)
  {
    try
      {
    connect();
      }
    catch(...)
      {
    throw;
      }
  }
  return std::string( mysql_get_host_info(&_mysql)); 
}

unsigned int Connection::get_protocol_version(void) throw(sqlerror)
{
  if (!_is_open)
  {
    try
      {
    connect();
      }
    catch(...)
      {
    throw;
      }
  }
  return mysql_get_proto_info(&_mysql);
}

std::string Connection::get_server_info(void)throw(sqlerror)
{
  if (!_is_open)
  {
    try
      {
    connect();
      }
    catch(...)
      {
    throw;
      }
  }
  return std::string( mysql_get_server_info(&_mysql));
}

unsigned long Connection::get_server_version(void) throw(sqlerror)

  if (!_is_open)
  {
    try
      {
    connect();
      }
    catch(...)
      {
    throw;
      }
  }
  return mysql_get_server_version(&_mysql);
}

void Connection::check_exception(void) throw(sqlerror)
{
  std::string is_null( mysql_error(&_mysql) );
  if (is_null != "")
    {
      try
    {
      throw sqlerror( boost::lexical_cast<std::string>( mysql_errno(&_mysql) ),is_null);
    }
      //catch boost lexical cast error
      catch(const boost::bad_lexical_cast& e)
    {
      throw sqlerror(e.what(),is_null);
    }
    }
}

void Connection::download(Table& target) throw(sqlerror)
{
  if (!_is_open)
    {
      try
      {
      connect();
      }
      catch(...)
      {
      throw;
      }
    }
  std::string cmd = target.get_create_cmd();
 
  //error occur
  if (mysql_real_query(&_mysql,cmd.c_str(),cmd.length()))
  {
      try
      {
    check_exception();
      }
      catch(...)
     {
       throw;
     }
  }
 
  MYSQL_RES* ptr_result = mysql_use_result(&_mysql);
  //error occur
  if (ptr_result == NULL)
  {
      try
      {
    check_exception();
      }
      catch(...)
      {
    throw;
      }
 }

   // fill field message
   unsigned int field_nums = mysql_num_fields(ptr_result);//no error
   MYSQL_FIELD* ptr_fields = mysql_fetch_fields(ptr_result);// no error
   std::vector<std::string>& field_list = target.get_fields();
   for(int i=0; i<field_nums; i++)
   {
     field_list.push_back(std::string(ptr_fields[i].name));
   }
  
   //fill row message
   MYSQL_ROW row;
   typedef std::map<int, std::string> map_row;
   std::vector< map_row >& row_list = target.get_rows();
   map_row currt_row;
  
   while ((row = mysql_fetch_row(ptr_result)))
   {
     for (int k=0; k<field_list.size(); k++)
     {
       currt_row[k] = boost::lexical_cast<std::string>(row[k]);
     }
     row_list.push_back(currt_row);
   }
  
   mysql_free_result(ptr_result);// no error  
}

void Connection::online_start(std::map<int, std::string>& row, const std::string& cmd) throw(sqlerror)
{
  if (!_is_open)
    {
      try
    {
      connect();
    }
      catch(...)
    {
      throw;
    }
    }

  //return 0 is success,or failure
  if (mysql_real_query(&_mysql,cmd.c_str(),cmd.length()))
  {
    //check exception
    try
   {
     check_exception();
   }
    catch(...)
   {
     throw;
   }
  }

 _ptr_result = mysql_use_result(&_mysql);// no error

 MYSQL_ROW current_row = mysql_fetch_row(_ptr_result);
 //error possablie occur
 if (current_row == NULL)
 {
   //check exception
   try
   {
     check_exception();
   }
   catch(...)
   {
     throw;
   }
 }

 for (int k=0; k<_num_fields; k++)
   {
     row[k] = current_row[k];
   }

}

void Connection::online_next(std::map<int, std::string>& row) throw(sqlerror)
{

 MYSQL_ROW current_row = mysql_fetch_row(_ptr_result);

 //error passablie occur
 if (current_row == NULL)
   {
     try
       {
     check_exception();
       }
     catch(...)
       {
     throw;
       }
   }
 for (int k=0; k<_num_fields; k++)
   {
     row[k] = current_row[k];
   }
}

bool Connection::online_is_eof(void)throw()
{
  if (!mysql_eof(_ptr_result))// api no error
    {
      return false;
    }
  else
    {
       mysql_free_result(_ptr_result);// auto free result,api no error
    }
}

unsigned long Connection::exceute(const std::string& cmd)throw(sqlerror)
{
  if (!_is_open)
  {
      try
    {
      connect();
    }
      catch(...)
    {
      throw;
    }
  }

  if (mysql_real_query(&_mysql, cmd.c_str(), cmd.length()))//return 0 is successful
  {
      try
      {
    check_exception();
      }
      catch(...)
     {
       throw;
     }
  }

  return mysql_affected_rows(&_mysql);//no error
}

const std::string& Connection::get_user(void)const throw()
{
  return _user;
}

const std::string& Connection::get_database(void)const throw()
{
  return _database;
}

const std::string& Connection::get_server(void)const throw()
{
  return _server;
}

const std::string& Connection::get_password(void)const throw()
{
  return _password;
}

void Connection::set_user(const std::string& user)throw()
{
  _user = user;
}

void Connection::set_database(const std::string& database)throw()
{
  _database = database;
}

void Connection::set_server(const std::string& server)throw()
{
  _server = server;
}

void Connection::set_password(const std::string& password)throw()
{
  _password = password;
}
 
原创粉丝点击