|
|
@@ -19,55 +19,40 @@ MySQLClientImpl::~MySQLClientImpl()
|
|
|
void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password)
|
|
|
{
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
- try
|
|
|
- {
|
|
|
- if (m_pConnection)
|
|
|
- Disconnect();
|
|
|
-
|
|
|
- std::stringstream ss;
|
|
|
- ss << "tcp://" << hostname << ":3306";
|
|
|
-
|
|
|
- m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(ss.str(), username, password));
|
|
|
- m_database = "";
|
|
|
- m_connected = true;
|
|
|
- }
|
|
|
- catch (sql::SQLException &e)
|
|
|
- {
|
|
|
- std::stringstream ss;
|
|
|
- ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
|
|
|
- Logging::Log(Logging::Severity::Error, ss.str());
|
|
|
- }
|
|
|
+ m_hostname = hostname;
|
|
|
+ m_username = username;
|
|
|
+ m_password = password;
|
|
|
+ m_database = "";
|
|
|
+ InternalConnect();
|
|
|
}
|
|
|
|
|
|
void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database)
|
|
|
{
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
- try
|
|
|
- {
|
|
|
- if (m_pConnection)
|
|
|
- Disconnect();
|
|
|
+ m_hostname = hostname;
|
|
|
+ m_username = username;
|
|
|
+ m_password = password;
|
|
|
+ m_database = database;
|
|
|
+ InternalConnect();
|
|
|
+}
|
|
|
|
|
|
- std::stringstream ss;
|
|
|
- ss << "tcp://" << hostname << ":3306/" << database;
|
|
|
+void MySQLClientImpl::Disconnect()
|
|
|
+{
|
|
|
+ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
+ InternalDisconnect();
|
|
|
+}
|
|
|
|
|
|
- m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(ss.str(), username, password));
|
|
|
- m_database = database;
|
|
|
- m_connected = true;
|
|
|
- }
|
|
|
- catch (sql::SQLException &e)
|
|
|
- {
|
|
|
- std::stringstream ss;
|
|
|
- ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
|
|
|
- Logging::Log(Logging::Severity::Error, ss.str());
|
|
|
- }
|
|
|
+void MySQLClientImpl::Reconnect()
|
|
|
+{
|
|
|
+ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
+ InternalDisconnect();
|
|
|
+ InternalConnect();
|
|
|
}
|
|
|
|
|
|
-void MySQLClientImpl::Disconnect()
|
|
|
+bool MySQLClientImpl::Connected() const
|
|
|
{
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
- m_pConnection.reset();
|
|
|
- m_database = "";
|
|
|
- m_connected = false;
|
|
|
+ return m_connected;
|
|
|
}
|
|
|
|
|
|
std::string MySQLClientImpl::Database() const
|
|
|
@@ -77,7 +62,7 @@ std::string MySQLClientImpl::Database() const
|
|
|
return m_database;
|
|
|
}
|
|
|
|
|
|
-void MySQLClientImpl::Execute(const std::string& query) const
|
|
|
+void MySQLClientImpl::Execute(const std::string& query)
|
|
|
{
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
if (!m_connected)
|
|
|
@@ -93,11 +78,17 @@ void MySQLClientImpl::Execute(const std::string& query) const
|
|
|
std::stringstream ss;
|
|
|
ss << "MySQLClientImpl::Execute() - Error: " << e.what() << std::endl;
|
|
|
ss << "Query: " << query << std::endl;
|
|
|
+ ss << "(MySQL error code: " << e.getErrorCode();
|
|
|
+ ss << ", SQLState: " << e.getSQLState() << " )" << std::endl;
|
|
|
Logging::Log(Logging::Severity::Error, ss.str());
|
|
|
+
|
|
|
+ int errorCode = e.getErrorCode();
|
|
|
+ if (errorCode == 2006 || errorCode == 2013)
|
|
|
+ InternalDisconnect();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::string& query) const
|
|
|
+std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::string& query)
|
|
|
{
|
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
if (!m_connected)
|
|
|
@@ -114,10 +105,48 @@ std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::str
|
|
|
std::stringstream ss;
|
|
|
ss << "MySQLClientImpl::ExecuteQuery() - Error: " << e.what() << std::endl;
|
|
|
ss << "Query: " << query << std::endl;
|
|
|
+ ss << "(MySQL error code: " << e.getErrorCode();
|
|
|
+ ss << ", SQLState: " << e.getSQLState() << " )" << std::endl;
|
|
|
Logging::Log(Logging::Severity::Error, ss.str());
|
|
|
+
|
|
|
+ int errorCode = e.getErrorCode();
|
|
|
+ if (errorCode == 2006 || errorCode == 2013)
|
|
|
+ InternalDisconnect();
|
|
|
}
|
|
|
|
|
|
return std::make_shared<MySQLResultSetImpl>(nullptr);
|
|
|
}
|
|
|
|
|
|
+void MySQLClientImpl::InternalConnect()
|
|
|
+{
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (m_pConnection)
|
|
|
+ Disconnect();
|
|
|
+
|
|
|
+ std::stringstream ss;
|
|
|
+ ss << "tcp://" << m_hostname << ":3306";
|
|
|
+
|
|
|
+ if (!m_database.empty())
|
|
|
+ ss << "/" << m_database;
|
|
|
+
|
|
|
+ m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(ss.str(), m_username, m_password));
|
|
|
+ m_connected = true;
|
|
|
+ }
|
|
|
+ catch (sql::SQLException &e)
|
|
|
+ {
|
|
|
+ std::stringstream ss;
|
|
|
+ ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Error, ss.str());
|
|
|
+ InternalDisconnect();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void MySQLClientImpl::InternalDisconnect()
|
|
|
+{
|
|
|
+ m_pConnection.reset();
|
|
|
+ m_database = "";
|
|
|
+ m_connected = false;
|
|
|
+}
|
|
|
+
|
|
|
} // namespace MySQL
|