⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connection.cc

📁 有关MYSQL的开源码
💻 CC
字号:
#include "mysqlcppapi/Connection.h"#include "mysqlcppapi/query/Query.h"#include "mysqlcppapi/row/Row.h"namespace mysqlcppapi{namespace // unnamed{  // util functions to make std::string safe in presence of NULL  // Returns std::string contents, or null pointer if string is empty.  // Pointer is invalidated if std::string modifed.  inline const char* string2charptr(const std::string& s)  {    return s.size() ? s.c_str() : 0;  }  // Returns a std::string from a possibly null pointer.  inline std::string charptr2string(const char* p)  {    return p ? p : std::string();  }}  // namespaceConnection::Connection(){  m_connection_data->host = "localhost";  m_connection_data->timeout = 60;}Connection::~Connection(){}bool Connection::is_connected() const{  return m_connection_data->is_connected;}/*Connection::type_sharedptr_connection Connection::cobj(){  return m_sharedptr_connection;}*/st_mysql_options Connection::get_options() const{  return m_sharedptr_connection->options;}std::string Connection::host_info(){  return std::string(mysql_get_host_info(m_sharedptr_connection.obj()));}int Connection::proto_info(){  return mysql_get_proto_info(m_sharedptr_connection.obj());}std::string Connection::server_info(){  return std::string(mysql_get_server_info(m_sharedptr_connection.obj()));}std::string Connection::stat(){  return std::string(mysql_stat(m_sharedptr_connection.obj()));}int Connection::affected_rows(){  return mysql_affected_rows(m_sharedptr_connection.obj());}int Connection::insert_id(){ return mysql_insert_id(m_sharedptr_connection.obj());}    Query Connection::create_Query(){  return Query(*this);}void Connection::close(){  check_connection_is_open();  m_sharedptr_connection.clear(); // Calls mysql_close()  m_connection_data->is_connected = false;}/*bool Connection::connected() const{  return m_bConnected;}*/bool Connection::success() const{  return m_connection_data->success;}void Connection::lock() throw(ex_Locked){  if(m_connection_data->locked)    throw ex_Locked();  m_connection_data->locked = true;} void Connection::unlock(){  m_connection_data->locked = false;}/*void Connection::purge(MYSQL *m){  mysql_close(m_sharedptr_connection.obj());}*/                   std::string Connection::error(){  return charptr2string(mysql_error(m_sharedptr_connection.obj()));}int Connection::errnum(){  return mysql_errno(m_sharedptr_connection.obj());}int Connection::refresh(unsigned int refresh_options){  return mysql_refresh (m_sharedptr_connection.obj(), refresh_options);}int Connection::ping(){  return ::mysql_ping(m_sharedptr_connection.obj());}int Connection::kill(unsigned long pid){  return ::mysql_kill(m_sharedptr_connection.obj(), pid);}std::string Connection::get_client_info(){  return charptr2string(mysql_get_client_info());}bool Connection::reload(){  check_connection_is_open();    const bool suc = !mysql_reload(m_sharedptr_connection.obj());  if (!suc)    throw ex_BadQuery(error());    return suc;}bool Connection::shutdown (){  check_connection_is_open();  #if MYSQL_VERSION_ID >= 40103  if (::mysql_shutdown(m_sharedptr_connection.obj(), SHUTDOWN_DEFAULT))#else  if (::mysql_shutdown(m_sharedptr_connection.obj()))#endif    throw ex_BadQuery(error());  return true;}  void Connection::connect(){  if(is_connected())    throw ex_base("The Connection is already open.");  lock();  const char* pchUnixSocket = string2charptr(m_connection_data->unix_socket);  m_sharedptr_connection->options.connect_timeout = m_connection_data->timeout;  //std::cout << "Connection::connect() connecting: host=" << m_connection_data->host << " user=" << m_connection_data->user << " Password=" << m_connection_data->password << std::endl;   //std::cout << "Connection::connect() connecting: host=" << m_connection_data->host << " user=" << m_connection_data->user << " Password=" << m_connection_data->password << " uiport=" << m_connection_data->port << " unixsocket=" << pchUnixSocket << " uiclientflags=" << m_connection_data->client_flags << " instance=" << m_sharedptr_connection.obj() << std::endl;    //The MYSQL* in m_sharedptr_connection has already been allocated. mysql_real_connect will initialize it:  const MYSQL* pMySQL = ::mysql_real_connect(m_sharedptr_connection.obj(), m_connection_data->host.c_str(), m_connection_data->user.c_str(), m_connection_data->password.c_str(), "", m_connection_data->port, pchUnixSocket, m_connection_data->client_flags);  const std::string errmsg = error();   unlock();  if(!pMySQL)    throw ex_BadQuery("mysql_real_connect() failed: " + errmsg);  m_connection_data->is_connected = true;}Connection::type_vecStrings Connection::get_DatabaseNames(const std::string& strPattern /* = "" */){  check_connection_is_open();    type_vecStrings vecResult;  const char* pchPattern = string2charptr(strPattern);    if (MYSQL_RES* pMySQL_Res = ::mysql_list_dbs(m_sharedptr_connection.obj(), pchPattern))  {    Result_Store result(pMySQL_Res);    pMySQL_Res = 0; //Result frees it.    for(Result_Store::size_type i = 0; i < result.size(); i++)    {      Row rowResult = result.fetch_row();      if(rowResult.size())      {        const std::string& strName = rowResult[0];        vecResult.push_back(strName);      }    }  }  return vecResult;}Connection::type_vecStrings  Connection::get_TableNames(const std::string& strPattern /* = "" */){  check_connection_is_open();    type_vecStrings vecResult;  const char* pchPattern = string2charptr(strPattern);  if (MYSQL_RES* pMySQL_Res = ::mysql_list_tables(m_sharedptr_connection.obj(), pchPattern))  {    Result_Store result(pMySQL_Res);    pMySQL_Res = 0; //Result frees it.    for(Result_Store::size_type i = 0; i < result.size(); i++)    {      Row rowResult = result.fetch_row();      if(rowResult.size())      {        const std::string& strName = rowResult[0];        vecResult.push_back(strName);      }    }  }  return vecResult;}void Connection::select_database(const std::string& strDatabaseName){  check_connection_is_open();      const int iError = ::mysql_select_db(m_sharedptr_connection.obj(), strDatabaseName.c_str());  if(iError)    throw ex_BadQuery("mysql_select_db() failed");}std::string Connection::info (){  check_connection_is_open();    return charptr2string(mysql_info(m_sharedptr_connection.obj()));}void Connection::query(const std::string& strQuery){  m_connection_data->success = false;  lock();  check_connection_is_open();       m_connection_data->success = !(::mysql_query(m_sharedptr_connection.obj(), strQuery.c_str()));   unlock();    if (!(m_connection_data->success))    throw ex_BadQuery(error());}  Result_NoData Connection::execute(const std::string& strQuery){  query(strQuery);    return Result_NoData(*this);}Result_Store Connection::store(const std::string& strQuery){  query(strQuery);  return Result_Store(::mysql_store_result(m_sharedptr_connection.obj()));}  Result_Use Connection::use(const std::string& strQuery){  query(strQuery);  return Result_Use(::mysql_use_result(m_sharedptr_connection.obj()), *this);}int Connection::read_options(enum mysql_option option, const std::string& arg){  check_connection_is_open();    return ::mysql_options(m_sharedptr_connection.obj(), option, arg.c_str());}void Connection::set_Host(const std::string& strVal){    m_connection_data->host = strVal;}void Connection::set_User(const std::string& strVal){    m_connection_data->user = strVal;}void Connection::set_Password(const std::string& strVal){    m_connection_data->password = strVal;}void Connection::set_Port(unsigned int uiVal){    m_connection_data->port = uiVal;}void Connection::set_UnixSocket(const std::string& strVal){    m_connection_data->unix_socket = strVal;}void Connection::set_ClientFlags(unsigned int uiVal){    m_connection_data->client_flags = uiVal;}void Connection::set_Timeout(unsigned int uiVal){  m_connection_data->timeout = uiVal;}bool Connection::create_database(const std::string& strName){  Result_NoData res = execute( "CREATE DATABASE " + strName );  return !(res.get_succeeded());}bool Connection::drop_database(const std::string& strName){  Result_NoData res = execute( "DROP DATABASE " + strName );  return !(res.get_succeeded());}unsigned long Connection::get_last_auto_increment(){  check_connection_is_open();    return mysql_insert_id(m_sharedptr_connection.obj());}Fields Connection::get_fields(const std::string& strTableName, const std::string& strPattern /*= ""*/){  check_connection_is_open();    const char* pchPattern = string2charptr(strPattern); //0 means 'all fields'      MYSQL_RES* pMySqlRes = ::mysql_list_fields(m_sharedptr_connection.obj(), strTableName.c_str(), pchPattern);  return  Result_Store(pMySqlRes, true).get_fields(); //true = full field info.}void Connection::check_connection_is_open() throw(ex_base){  if(!is_connected())    throw ex_base("The Connection is not open.");}  } //namespace mysqlcppapi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -