connection類--初步修改cpp

#include "connection.hpp"
#include <boost/lexical_cast.hpp>

using namespace sqlpp;

Connection::Connection():_is_open(false)
{
}

Connection::Connection(const std::string& database, const std::string& server, const std::string& user, const std::string& password):_is_open(false)
{
  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)
{
  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);
    }
    }
}

MYSQL* Connection::get_mysql_handle(void)throw()
{
  return &_mysql;
}

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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章