SqlClient.cpp
Go to the documentation of this file.
00001 
00011 // worldlib
00012 #include "worldlib/remote/SqlClient.h"
00013 
00014 // ROS
00015 #include <ros/ros.h>
00016 
00017 using namespace std;
00018 using namespace rail::spatial_temporal_learning::worldlib::remote;
00019 
00020 SqlClient::SqlClient(const SqlClient &client)
00021     : Client(client.getHost(), client.getPort()),
00022       user_(client.getUser()), password_(client.getPassword()), database_(client.getDatabase())
00023 {
00024   connection_ = NULL;
00025 
00026   // check if a connection was made
00027   if (client.connected())
00028   {
00029     this->connect();
00030   }
00031 }
00032 
00033 SqlClient::SqlClient(const string &host, const uint16_t port, const string &user, const string &password,
00034     const string &database) : Client(host, port), user_(user), password_(password), database_(database)
00035 {
00036   connection_ = NULL;
00037   connected_ = false;
00038 }
00039 
00040 SqlClient::~SqlClient()
00041 {
00042   // check for an existing connection
00043   this->disconnect();
00044 }
00045 
00046 const string &SqlClient::getUser() const
00047 {
00048   return user_;
00049 }
00050 
00051 const string &SqlClient::getPassword() const
00052 {
00053   return password_;
00054 }
00055 
00056 const string &SqlClient::getDatabase() const
00057 {
00058   return database_;
00059 }
00060 
00061 bool SqlClient::connected() const
00062 {
00063   return connection_ != NULL && connected_;
00064 }
00065 
00066 bool SqlClient::connect()
00067 {
00068   // check for an existing connection
00069   this->disconnect();
00070 
00071   // setup the client connection
00072   connection_ = mysql_init(NULL);
00073   connected_ = mysql_real_connect(connection_, this->getHost().c_str(), user_.c_str(), password_.c_str(),
00074                                   database_.c_str(), this->getPort(), NULL, 0);
00075   if (!connected_)
00076   {
00077     this->printSqlError();
00078   }
00079 
00080   return this->connected();
00081 }
00082 
00083 void SqlClient::disconnect()
00084 {
00085   // check for an existing connection
00086   if (connection_ != NULL)
00087   {
00088     if (this->connected())
00089     {
00090       mysql_close(connection_);
00091     }
00092     connection_ = NULL;
00093     connected_ = false;
00094   }
00095 }
00096 
00097 MYSQL_RES *SqlClient::query(const string &query) const
00098 {
00099   if (this->connected())
00100   {
00101     if (mysql_query(connection_, query.c_str()) == 0)
00102     {
00103       // parse and get it
00104       return mysql_use_result(connection_);
00105     } else
00106     {
00107       this->printSqlError();
00108     }
00109   } else
00110   {
00111     ROS_WARN("MySQL attempted to make a query while it was not connected.");
00112   }
00113 
00114   // something went wrong
00115   return NULL;
00116 }
00117 
00118 void SqlClient::printSqlError() const
00119 {
00120   ROS_ERROR("MySQL Error: %s", mysql_error(connection_));
00121 }


worldlib
Author(s): Russell Toris
autogenerated on Fri Feb 12 2016 00:24:19