2013-07-05 09:37:04 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-10-18 09:27:04 +02:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
|
2013-07-05 09:37:04 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2015-03-28 12:22:40 +01:00
|
|
|
#include "db_ido_mysql/idomysqlconnection.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "db_ido_mysql/idomysqlconnection-ti.cpp"
|
2014-09-17 15:38:39 +02:00
|
|
|
#include "db_ido/dbtype.hpp"
|
|
|
|
#include "db_ido/dbvalue.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2017-05-15 15:51:39 +02:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/application.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
2013-07-17 11:29:51 +02:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2018-01-04 08:54:18 +01:00
|
|
|
#include <utility>
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
REGISTER_TYPE(IdoMysqlConnection);
|
2015-09-21 11:44:58 +02:00
|
|
|
REGISTER_STATSFUNCTION(IdoMysqlConnection, &IdoMysqlConnection::StatsFunc);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::OnConfigLoaded()
|
2016-06-14 08:19:13 +02:00
|
|
|
{
|
2016-06-16 14:21:29 +02:00
|
|
|
ObjectImpl<IdoMysqlConnection>::OnConfigLoaded();
|
|
|
|
|
2016-06-14 08:19:13 +02:00
|
|
|
m_QueryQueue.SetName("IdoMysqlConnection, " + GetName());
|
2018-01-01 15:21:14 +01:00
|
|
|
|
|
|
|
Library shimLibrary{"mysql_shim"};
|
|
|
|
|
|
|
|
auto create_mysql_shim = shimLibrary.GetSymbolAddress<create_mysql_shim_ptr>("create_mysql_shim");
|
|
|
|
|
|
|
|
m_Mysql.reset(create_mysql_shim());
|
|
|
|
|
|
|
|
std::swap(m_Library, shimLibrary);
|
2016-06-14 08:19:13 +02:00
|
|
|
}
|
|
|
|
|
2015-02-07 22:36:17 +01:00
|
|
|
void IdoMysqlConnection::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
DictionaryData nodes;
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const IdoMysqlConnection::Ptr& idomysqlconnection : ConfigType::GetObjectsByType<IdoMysqlConnection>()) {
|
2017-05-11 17:30:20 +02:00
|
|
|
size_t queryQueueItems = idomysqlconnection->m_QueryQueue.GetLength();
|
|
|
|
double queryQueueItemRate = idomysqlconnection->m_QueryQueue.GetTaskCount(60) / 60.0;
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
nodes.emplace_back(idomysqlconnection->GetName(), new Dictionary({
|
|
|
|
{ "version", idomysqlconnection->GetSchemaVersion() },
|
|
|
|
{ "instance_name", idomysqlconnection->GetInstanceName() },
|
|
|
|
{ "connected", idomysqlconnection->GetConnected() },
|
|
|
|
{ "query_queue_items", queryQueueItems },
|
|
|
|
{ "query_queue_item_rate", queryQueueItemRate }
|
|
|
|
}));
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2015-11-07 07:51:41 +01:00
|
|
|
perfdata->Add(new PerfdataValue("idomysqlconnection_" + idomysqlconnection->GetName() + "_queries_rate", idomysqlconnection->GetQueryCount(60) / 60.0));
|
|
|
|
perfdata->Add(new PerfdataValue("idomysqlconnection_" + idomysqlconnection->GetName() + "_queries_1min", idomysqlconnection->GetQueryCount(60)));
|
|
|
|
perfdata->Add(new PerfdataValue("idomysqlconnection_" + idomysqlconnection->GetName() + "_queries_5mins", idomysqlconnection->GetQueryCount(5 * 60)));
|
|
|
|
perfdata->Add(new PerfdataValue("idomysqlconnection_" + idomysqlconnection->GetName() + "_queries_15mins", idomysqlconnection->GetQueryCount(15 * 60)));
|
2017-05-11 17:30:20 +02:00
|
|
|
perfdata->Add(new PerfdataValue("idomysqlconnection_" + idomysqlconnection->GetName() + "_query_queue_items", queryQueueItems));
|
|
|
|
perfdata->Add(new PerfdataValue("idomysqlconnection_" + idomysqlconnection->GetName() + "_query_queue_item_rate", queryQueueItemRate));
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
status->Set("idomysqlconnection", new Dictionary(std::move(nodes)));
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::Resume()
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2014-05-09 13:02:30 +02:00
|
|
|
DbConnection::Resume();
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' resumed.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.SetExceptionCallback(std::bind(&IdoMysqlConnection::ExceptionHandler, this, _1));
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
m_TxTimer = new Timer();
|
2014-05-09 13:02:30 +02:00
|
|
|
m_TxTimer->SetInterval(1);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_TxTimer->OnTimerExpired.connect(std::bind(&IdoMysqlConnection::TxTimerHandler, this));
|
2013-07-17 14:10:28 +02:00
|
|
|
m_TxTimer->Start();
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
m_ReconnectTimer = new Timer();
|
2013-07-19 10:18:47 +02:00
|
|
|
m_ReconnectTimer->SetInterval(10);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_ReconnectTimer->OnTimerExpired.connect(std::bind(&IdoMysqlConnection::ReconnectTimerHandler, this));
|
2013-07-19 10:18:47 +02:00
|
|
|
m_ReconnectTimer->Start();
|
2013-10-30 13:32:01 +01:00
|
|
|
m_ReconnectTimer->Reschedule(0);
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
ASSERT(m_Mysql->thread_safe());
|
2013-07-19 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::Pause()
|
2013-07-19 10:18:47 +02:00
|
|
|
{
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' paused.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2014-05-09 13:02:30 +02:00
|
|
|
m_ReconnectTimer.reset();
|
|
|
|
|
2014-07-08 13:48:11 +02:00
|
|
|
DbConnection::Pause();
|
|
|
|
|
2017-02-22 16:32:17 +01:00
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Rescheduling disconnect task.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::Disconnect, this), PriorityHigh);
|
2013-10-30 13:32:01 +01:00
|
|
|
m_QueryQueue.Join();
|
|
|
|
}
|
|
|
|
|
2016-08-31 13:43:14 +02:00
|
|
|
void IdoMysqlConnection::ExceptionHandler(boost::exception_ptr exp)
|
2013-11-05 12:40:25 +01:00
|
|
|
{
|
2014-06-05 18:26:38 +02:00
|
|
|
Log(LogCritical, "IdoMysqlConnection", "Exception during database operation: Verify that your database is operational!");
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2018-01-04 08:54:18 +01:00
|
|
|
<< "Exception during database operation: " << DiagnosticInformation(std::move(exp));
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
if (GetConnected()) {
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->close(&m_Connection);
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2013-11-05 12:40:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::AssertOnWorkQueue()
|
2013-10-30 13:32:01 +01:00
|
|
|
{
|
2014-12-18 15:11:57 +01:00
|
|
|
ASSERT(m_QueryQueue.IsWorkerThread());
|
2013-10-30 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::Disconnect()
|
2013-10-30 13:32:01 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
if (!GetConnected())
|
2013-07-22 12:08:49 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
Query("COMMIT");
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->close(&m_Connection);
|
2013-09-12 11:37:58 +02:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2013-07-19 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::TxTimerHandler()
|
2013-10-30 13:32:01 +01:00
|
|
|
{
|
2015-02-04 15:25:10 +01:00
|
|
|
NewTransaction();
|
2013-10-30 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::NewTransaction()
|
2015-02-04 15:25:10 +01:00
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2017-02-22 16:32:17 +01:00
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling new transaction and finishing async queries.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalNewTransaction, this), PriorityHigh);
|
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::FinishAsyncQueries, this), PriorityHigh);
|
2015-02-04 15:25:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::InternalNewTransaction()
|
2013-07-19 10:18:47 +02:00
|
|
|
{
|
2015-03-13 20:23:14 +01:00
|
|
|
AssertOnWorkQueue();
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
if (!GetConnected())
|
2013-07-19 10:18:47 +02:00
|
|
|
return;
|
|
|
|
|
2015-03-14 07:58:55 +01:00
|
|
|
AsyncQuery("COMMIT");
|
|
|
|
AsyncQuery("BEGIN");
|
2013-07-19 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::ReconnectTimerHandler()
|
2013-07-19 10:18:47 +02:00
|
|
|
{
|
2017-02-22 16:32:17 +01:00
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling reconnect task.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::Reconnect, this), PriorityLow);
|
2013-10-30 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::Reconnect()
|
2013-10-30 13:32:01 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2015-12-10 16:54:43 +01:00
|
|
|
if (!IsActive())
|
|
|
|
return;
|
|
|
|
|
2013-11-19 07:49:41 +01:00
|
|
|
CONTEXT("Reconnecting to MySQL IDO database '" + GetName() + "'");
|
|
|
|
|
2015-12-15 16:50:17 +01:00
|
|
|
double startTime = Utility::GetTime();
|
2015-10-26 13:14:41 +01:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
SetShouldConnect(true);
|
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
bool reconnect = false;
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
if (GetConnected()) {
|
|
|
|
/* Check if we're really still connected */
|
2018-01-01 15:21:14 +01:00
|
|
|
if (m_Mysql->ping(&m_Connection) == 0)
|
2015-03-13 20:23:14 +01:00
|
|
|
return;
|
2013-10-01 14:01:59 +02:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->close(&m_Connection);
|
2015-03-13 20:23:14 +01:00
|
|
|
SetConnected(false);
|
|
|
|
reconnect = true;
|
|
|
|
}
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
ClearIDCache();
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
String ihost, isocket_path, iuser, ipasswd, idb;
|
2016-01-21 03:51:00 +01:00
|
|
|
String isslKey, isslCert, isslCa, isslCaPath, isslCipher;
|
2015-03-13 20:23:14 +01:00
|
|
|
const char *host, *socket_path, *user , *passwd, *db;
|
2016-01-21 03:51:00 +01:00
|
|
|
const char *sslKey, *sslCert, *sslCa, *sslCaPath, *sslCipher;
|
|
|
|
bool enableSsl;
|
2015-03-13 20:23:14 +01:00
|
|
|
long port;
|
2013-11-20 09:32:30 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
ihost = GetHost();
|
|
|
|
isocket_path = GetSocketPath();
|
|
|
|
iuser = GetUser();
|
|
|
|
ipasswd = GetPassword();
|
|
|
|
idb = GetDatabase();
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2016-01-21 03:51:00 +01:00
|
|
|
enableSsl = GetEnableSsl();
|
|
|
|
isslKey = GetSslKey();
|
|
|
|
isslCert = GetSslCert();
|
|
|
|
isslCa = GetSslCa();
|
|
|
|
isslCaPath = GetSslCapath();
|
|
|
|
isslCipher = GetSslCipher();
|
|
|
|
|
2017-12-14 15:37:20 +01:00
|
|
|
host = (!ihost.IsEmpty()) ? ihost.CStr() : nullptr;
|
2015-03-13 20:23:14 +01:00
|
|
|
port = GetPort();
|
2017-12-14 15:37:20 +01:00
|
|
|
socket_path = (!isocket_path.IsEmpty()) ? isocket_path.CStr() : nullptr;
|
|
|
|
user = (!iuser.IsEmpty()) ? iuser.CStr() : nullptr;
|
|
|
|
passwd = (!ipasswd.IsEmpty()) ? ipasswd.CStr() : nullptr;
|
|
|
|
db = (!idb.IsEmpty()) ? idb.CStr() : nullptr;
|
|
|
|
|
|
|
|
sslKey = (!isslKey.IsEmpty()) ? isslKey.CStr() : nullptr;
|
|
|
|
sslCert = (!isslCert.IsEmpty()) ? isslCert.CStr() : nullptr;
|
|
|
|
sslCa = (!isslCa.IsEmpty()) ? isslCa.CStr() : nullptr;
|
|
|
|
sslCaPath = (!isslCaPath.IsEmpty()) ? isslCaPath.CStr() : nullptr;
|
|
|
|
sslCipher = (!isslCipher.IsEmpty()) ? isslCipher.CStr() : nullptr;
|
2016-01-21 03:51:00 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
/* connection */
|
2018-01-01 15:21:14 +01:00
|
|
|
if (!m_Mysql->init(&m_Connection)) {
|
2015-03-13 20:23:14 +01:00
|
|
|
Log(LogCritical, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "mysql_init() failed: out of memory";
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::bad_alloc());
|
|
|
|
}
|
2014-06-05 18:26:38 +02:00
|
|
|
|
2016-01-21 03:51:00 +01:00
|
|
|
if (enableSsl)
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->ssl_set(&m_Connection, sslKey, sslCert, sslCa, sslCaPath, sslCipher);
|
2016-01-21 03:51:00 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
if (!m_Mysql->real_connect(&m_Connection, host, user, passwd, db, port, socket_path, CLIENT_FOUND_ROWS | CLIENT_MULTI_STATEMENTS)) {
|
2015-03-13 20:23:14 +01:00
|
|
|
Log(LogCritical, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Connection to database '" << db << "' with user '" << user << "' on '" << host << ":" << port
|
2018-01-01 15:21:14 +01:00
|
|
|
<< "' " << (enableSsl ? "(SSL enabled) " : "") << "failed: \"" << m_Mysql->error(&m_Connection) << "\"";
|
2014-06-05 18:26:38 +02:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error(m_Mysql->error(&m_Connection)));
|
2015-03-13 20:23:14 +01:00
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
SetConnected(true);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
IdoMysqlResult result = Query("SELECT @@global.max_allowed_packet AS max_allowed_packet");
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
Dictionary::Ptr row = FetchRow(result);
|
2013-09-27 15:30:17 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
if (row)
|
|
|
|
m_MaxPacketSize = row->Get("max_allowed_packet");
|
|
|
|
else
|
|
|
|
m_MaxPacketSize = 64 * 1024;
|
2014-01-28 11:42:12 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
DiscardRows(result);
|
2015-03-12 09:35:26 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
String dbVersionName = "idoutils";
|
|
|
|
result = Query("SELECT version FROM " + GetTablePrefix() + "dbversion WHERE name='" + Escape(dbVersionName) + "'");
|
2014-08-06 10:47:00 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
row = FetchRow(result);
|
2013-09-27 15:30:17 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
if (!row) {
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->close(&m_Connection);
|
2015-03-13 20:23:14 +01:00
|
|
|
SetConnected(false);
|
2014-01-28 11:42:12 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
Log(LogCritical, "IdoMysqlConnection", "Schema does not provide any valid version! Verify your schema installation.");
|
2013-09-27 15:30:17 +02:00
|
|
|
|
2017-03-28 10:16:07 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid schema."));
|
2015-03-13 20:23:14 +01:00
|
|
|
}
|
2015-03-12 09:39:53 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
DiscardRows(result);
|
2015-03-12 09:35:26 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
String version = row->Get("version");
|
2014-06-05 18:26:38 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
SetSchemaVersion(version);
|
2013-09-27 15:30:17 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
if (Utility::CompareVersion(IDO_COMPAT_SCHEMA_VERSION, version) < 0) {
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->close(&m_Connection);
|
2015-03-13 20:23:14 +01:00
|
|
|
SetConnected(false);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
Log(LogCritical, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Schema version '" << version << "' does not match the required version '"
|
|
|
|
<< IDO_COMPAT_SCHEMA_VERSION << "' (or newer)! Please check the upgrade documentation at "
|
|
|
|
<< "https://docs.icinga.com/icinga2/latest/doc/module/icinga2/chapter/upgrading-icinga-2#upgrading-mysql-db";
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2017-03-28 10:16:07 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Schema version mismatch."));
|
2015-03-13 20:23:14 +01:00
|
|
|
}
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
String instanceName = GetInstanceName();
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
result = Query("SELECT instance_id FROM " + GetTablePrefix() + "instances WHERE instance_name = '" + Escape(instanceName) + "'");
|
|
|
|
row = FetchRow(result);
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
if (!row) {
|
|
|
|
Query("INSERT INTO " + GetTablePrefix() + "instances (instance_name, instance_description) VALUES ('" + Escape(instanceName) + "', '" + Escape(GetInstanceDescription()) + "')");
|
|
|
|
m_InstanceID = GetLastInsertID();
|
|
|
|
} else {
|
|
|
|
m_InstanceID = DbReference(row->Get("instance_id"));
|
|
|
|
}
|
2014-01-28 11:42:12 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
DiscardRows(result);
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
Endpoint::Ptr my_endpoint = Endpoint::GetLocalEndpoint();
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
/* we have an endpoint in a cluster setup, so decide if we can proceed here */
|
|
|
|
if (my_endpoint && GetHAMode() == HARunOnce) {
|
|
|
|
/* get the current endpoint writing to programstatus table */
|
|
|
|
result = Query("SELECT UNIX_TIMESTAMP(status_update_time) AS status_update_time, endpoint_name FROM " +
|
2017-12-19 15:50:05 +01:00
|
|
|
GetTablePrefix() + "programstatus WHERE instance_id = " + Convert::ToString(m_InstanceID));
|
2015-03-13 20:23:14 +01:00
|
|
|
row = FetchRow(result);
|
|
|
|
DiscardRows(result);
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
String endpoint_name;
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
if (row)
|
|
|
|
endpoint_name = row->Get("endpoint_name");
|
|
|
|
else
|
|
|
|
Log(LogNotice, "IdoMysqlConnection", "Empty program status table");
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
/* if we did not write into the database earlier, another instance is active */
|
|
|
|
if (endpoint_name != my_endpoint->GetName()) {
|
|
|
|
double status_update_time;
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
if (row)
|
|
|
|
status_update_time = row->Get("status_update_time");
|
|
|
|
else
|
|
|
|
status_update_time = 0;
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
double status_update_age = Utility::GetTime() - status_update_time;
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
Log(LogNotice, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Last update by '" << endpoint_name << "' was " << status_update_age << "s ago.";
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
if (status_update_age < GetFailoverTimeout()) {
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->close(&m_Connection);
|
2015-03-13 20:23:14 +01:00
|
|
|
SetConnected(false);
|
|
|
|
SetShouldConnect(false);
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
return;
|
2014-08-13 20:30:28 +02:00
|
|
|
}
|
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
/* activate the IDO only, if we're authoritative in this zone */
|
|
|
|
if (IsPaused()) {
|
|
|
|
Log(LogNotice, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Local endpoint '" << my_endpoint->GetName() << "' is not authoritative, bailing out.";
|
2015-03-13 20:23:14 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->close(&m_Connection);
|
2015-03-13 20:23:14 +01:00
|
|
|
SetConnected(false);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2013-07-19 10:34:46 +02:00
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
Log(LogNotice, "IdoMysqlConnection", "Enabling IDO connection.");
|
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
Log(LogInformation, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "MySQL IDO instance id: " << static_cast<long>(m_InstanceID) << " (schema version: '" + version + "')";
|
2013-10-01 14:01:59 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
/* set session time zone to utc */
|
|
|
|
Query("SET SESSION TIME_ZONE='+00:00'");
|
2013-10-01 14:01:59 +02:00
|
|
|
|
2016-08-27 10:20:34 +02:00
|
|
|
Query("SET SESSION SQL_MODE='NO_AUTO_VALUE_ON_ZERO'");
|
|
|
|
|
2016-05-17 10:32:38 +02:00
|
|
|
Query("BEGIN");
|
|
|
|
|
2016-05-11 14:03:40 +02:00
|
|
|
/* update programstatus table */
|
|
|
|
UpdateProgramStatus();
|
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
/* record connection */
|
|
|
|
Query("INSERT INTO " + GetTablePrefix() + "conninfo " +
|
2017-12-19 15:50:05 +01:00
|
|
|
"(instance_id, connect_time, last_checkin_time, agent_name, agent_version, connect_type, data_start_time) VALUES ("
|
|
|
|
+ Convert::ToString(static_cast<long>(m_InstanceID)) + ", NOW(), NOW(), 'icinga2 db_ido_mysql', '" + Escape(Application::GetAppVersion())
|
|
|
|
+ "', '" + (reconnect ? "RECONNECT" : "INITIAL") + "', NOW())");
|
2013-08-02 17:11:14 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
/* clear config tables for the initial config dump */
|
|
|
|
PrepareDatabase();
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
std::ostringstream q1buf;
|
|
|
|
q1buf << "SELECT object_id, objecttype_id, name1, name2, is_active FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast<long>(m_InstanceID);
|
|
|
|
result = Query(q1buf.str());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
std::vector<DbObject::Ptr> activeDbObjs;
|
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
while ((row = FetchRow(result))) {
|
|
|
|
DbType::Ptr dbtype = DbType::GetByID(row->Get("objecttype_id"));
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
if (!dbtype)
|
|
|
|
continue;
|
2014-02-12 14:49:39 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2"));
|
|
|
|
SetObjectID(dbobj, DbReference(row->Get("object_id")));
|
2015-12-15 13:44:58 +01:00
|
|
|
bool active = row->Get("is_active");
|
|
|
|
SetObjectActive(dbobj, active);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (active)
|
2017-11-30 08:19:58 +01:00
|
|
|
activeDbObjs.emplace_back(std::move(dbobj));
|
2013-07-19 10:34:46 +02:00
|
|
|
}
|
2013-07-17 14:10:28 +02:00
|
|
|
|
2016-07-15 09:40:39 +02:00
|
|
|
SetIDCacheValid(true);
|
|
|
|
|
2016-08-13 22:54:22 +02:00
|
|
|
EnableActiveChangedHandler();
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const DbObject::Ptr& dbobj : activeDbObjs) {
|
2016-08-13 22:54:22 +02:00
|
|
|
if (dbobj->GetObject())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Log(LogNotice, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Deactivate deleted object name1: '" << dbobj->GetName1()
|
|
|
|
<< "' name2: '" << dbobj->GetName2() + "'.";
|
2016-08-13 22:54:22 +02:00
|
|
|
DeactivateObject(dbobj);
|
2014-02-12 14:49:39 +01:00
|
|
|
}
|
2015-10-26 13:14:41 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
UpdateAllObjects();
|
|
|
|
|
2017-02-22 16:32:17 +01:00
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling session table clear and finish connect task.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::ClearTablesBySession, this), PriorityLow);
|
2015-10-26 13:14:41 +01:00
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::FinishConnect, this, startTime), PriorityLow);
|
2015-12-16 08:49:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoMysqlConnection::FinishConnect(double startTime)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
if (!GetConnected())
|
|
|
|
return;
|
|
|
|
|
|
|
|
FinishAsyncQueries();
|
|
|
|
|
2015-12-15 16:50:17 +01:00
|
|
|
Log(LogInformation, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Finished reconnecting to MySQL IDO database in " << std::setw(2) << Utility::GetTime() - startTime << " second(s).";
|
2015-12-15 16:50:17 +01:00
|
|
|
|
2015-10-26 13:14:41 +01:00
|
|
|
Query("COMMIT");
|
|
|
|
Query("BEGIN");
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::ClearTablesBySession()
|
2015-10-26 13:14:41 +01:00
|
|
|
{
|
2016-08-14 22:24:51 +02:00
|
|
|
/* delete all comments and downtimes without current session token */
|
2016-08-01 17:54:03 +02:00
|
|
|
ClearTableBySession("comments");
|
|
|
|
ClearTableBySession("scheduleddowntime");
|
2016-07-21 17:37:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoMysqlConnection::ClearTableBySession(const String& table)
|
|
|
|
{
|
|
|
|
Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " +
|
2017-12-19 15:50:05 +01:00
|
|
|
Convert::ToString(static_cast<long>(m_InstanceID)) + " AND session_token <> " +
|
|
|
|
Convert::ToString(GetSessionToken()));
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
void IdoMysqlConnection::AsyncQuery(const String& query, const std::function<void (const IdoMysqlResult&)>& callback)
|
2015-03-13 20:23:14 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
IdoAsyncQuery aq;
|
|
|
|
aq.Query = query;
|
2017-02-22 16:32:31 +01:00
|
|
|
/* XXX: Important: The callback must not immediately execute a query, but enqueue it!
|
|
|
|
* See https://github.com/Icinga/icinga2/issues/4603 for details.
|
|
|
|
*/
|
2015-03-13 20:23:14 +01:00
|
|
|
aq.Callback = callback;
|
2017-11-30 08:19:58 +01:00
|
|
|
m_AsyncQueries.emplace_back(std::move(aq));
|
2016-07-13 14:44:53 +02:00
|
|
|
|
|
|
|
if (m_AsyncQueries.size() > 25000) {
|
|
|
|
FinishAsyncQueries();
|
|
|
|
InternalNewTransaction();
|
|
|
|
}
|
2015-03-13 20:23:14 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoMysqlConnection::FinishAsyncQueries()
|
2015-03-13 20:23:14 +01:00
|
|
|
{
|
|
|
|
std::vector<IdoAsyncQuery> queries;
|
|
|
|
m_AsyncQueries.swap(queries);
|
|
|
|
|
|
|
|
std::vector<IdoAsyncQuery>::size_type offset = 0;
|
|
|
|
|
|
|
|
while (offset < queries.size()) {
|
|
|
|
std::ostringstream querybuf;
|
|
|
|
|
|
|
|
std::vector<IdoAsyncQuery>::size_type count = 0;
|
|
|
|
size_t num_bytes = 0;
|
|
|
|
|
|
|
|
for (std::vector<IdoAsyncQuery>::size_type i = offset; i < queries.size(); i++) {
|
|
|
|
const IdoAsyncQuery& aq = queries[i];
|
|
|
|
|
|
|
|
size_t size_query = aq.Query.GetLength() + 1;
|
|
|
|
|
2018-08-08 12:11:37 +02:00
|
|
|
if (count > 0) {
|
|
|
|
if (num_bytes + size_query > m_MaxPacketSize - 512)
|
|
|
|
break;
|
2015-03-13 20:23:14 +01:00
|
|
|
|
|
|
|
querybuf << ";";
|
2018-08-08 12:11:37 +02:00
|
|
|
}
|
2015-03-13 20:23:14 +01:00
|
|
|
|
|
|
|
IncreaseQueryCount();
|
|
|
|
count++;
|
|
|
|
|
2015-11-05 15:43:09 +01:00
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Query: " << aq.Query;
|
2015-11-05 15:43:09 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
querybuf << aq.Query;
|
|
|
|
num_bytes += size_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
String query = querybuf.str();
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
if (m_Mysql->query(&m_Connection, query.CStr()) != 0) {
|
2015-03-13 20:23:14 +01:00
|
|
|
std::ostringstream msgbuf;
|
2018-01-01 15:21:14 +01:00
|
|
|
String message = m_Mysql->error(&m_Connection);
|
2015-03-13 20:23:14 +01:00
|
|
|
msgbuf << "Error \"" << message << "\" when executing query \"" << query << "\"";
|
|
|
|
Log(LogCritical, "IdoMysqlConnection", msgbuf.str());
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(
|
2017-12-19 15:50:05 +01:00
|
|
|
database_error()
|
2018-01-01 15:21:14 +01:00
|
|
|
<< errinfo_message(m_Mysql->error(&m_Connection))
|
2015-03-13 20:23:14 +01:00
|
|
|
<< errinfo_database_query(query)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<IdoAsyncQuery>::size_type i = offset; i < offset + count; i++) {
|
|
|
|
const IdoAsyncQuery& aq = queries[i];
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
MYSQL_RES *result = m_Mysql->store_result(&m_Connection);
|
2015-03-13 20:23:14 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
m_AffectedRows = m_Mysql->affected_rows(&m_Connection);
|
2016-11-25 15:43:54 +01:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
IdoMysqlResult iresult;
|
|
|
|
|
|
|
|
if (!result) {
|
2018-01-01 15:21:14 +01:00
|
|
|
if (m_Mysql->field_count(&m_Connection) > 0) {
|
2015-03-13 20:23:14 +01:00
|
|
|
std::ostringstream msgbuf;
|
2018-01-01 15:21:14 +01:00
|
|
|
String message = m_Mysql->error(&m_Connection);
|
2016-11-25 15:43:54 +01:00
|
|
|
msgbuf << "Error \"" << message << "\" when executing query \"" << aq.Query << "\"";
|
2015-03-13 20:23:14 +01:00
|
|
|
Log(LogCritical, "IdoMysqlConnection", msgbuf.str());
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(
|
2017-12-19 15:50:05 +01:00
|
|
|
database_error()
|
2018-01-01 15:21:14 +01:00
|
|
|
<< errinfo_message(m_Mysql->error(&m_Connection))
|
2015-03-13 20:23:14 +01:00
|
|
|
<< errinfo_database_query(query)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else
|
2018-01-01 15:21:14 +01:00
|
|
|
iresult = IdoMysqlResult(result, std::bind(&MysqlInterface::free_result, std::cref(m_Mysql), _1));
|
2015-03-13 20:23:14 +01:00
|
|
|
|
2016-11-25 15:43:54 +01:00
|
|
|
if (aq.Callback)
|
|
|
|
aq.Callback(iresult);
|
2015-03-13 20:23:14 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
if (m_Mysql->next_result(&m_Connection) > 0) {
|
2015-03-13 20:23:14 +01:00
|
|
|
std::ostringstream msgbuf;
|
2018-01-01 15:21:14 +01:00
|
|
|
String message = m_Mysql->error(&m_Connection);
|
2015-03-13 20:23:14 +01:00
|
|
|
msgbuf << "Error \"" << message << "\" when executing query \"" << query << "\"";
|
|
|
|
Log(LogCritical, "IdoMysqlConnection", msgbuf.str());
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(
|
2017-12-19 15:50:05 +01:00
|
|
|
database_error()
|
2018-01-01 15:21:14 +01:00
|
|
|
<< errinfo_message(m_Mysql->error(&m_Connection))
|
2015-03-13 20:23:14 +01:00
|
|
|
<< errinfo_database_query(query)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
IdoMysqlResult IdoMysqlConnection::Query(const String& query)
|
2013-07-17 11:29:51 +02:00
|
|
|
{
|
2013-10-30 13:32:01 +01:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
/* finish all async queries to maintain the right order for queries */
|
2015-12-14 10:33:43 +01:00
|
|
|
FinishAsyncQueries();
|
2015-03-13 20:23:14 +01:00
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Query: " << query;
|
2013-07-17 14:10:28 +02:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
IncreaseQueryCount();
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
if (m_Mysql->query(&m_Connection, query.CStr()) != 0) {
|
2014-06-05 18:26:38 +02:00
|
|
|
std::ostringstream msgbuf;
|
2018-01-01 15:21:14 +01:00
|
|
|
String message = m_Mysql->error(&m_Connection);
|
2014-06-05 18:26:38 +02:00
|
|
|
msgbuf << "Error \"" << message << "\" when executing query \"" << query << "\"";
|
|
|
|
Log(LogCritical, "IdoMysqlConnection", msgbuf.str());
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
BOOST_THROW_EXCEPTION(
|
2017-12-19 15:50:05 +01:00
|
|
|
database_error()
|
2018-01-01 15:21:14 +01:00
|
|
|
<< errinfo_message(m_Mysql->error(&m_Connection))
|
2013-11-05 12:40:25 +01:00
|
|
|
<< errinfo_database_query(query)
|
|
|
|
);
|
2014-06-05 18:26:38 +02:00
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
MYSQL_RES *result = m_Mysql->store_result(&m_Connection);
|
2014-01-31 08:28:00 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
m_AffectedRows = m_Mysql->affected_rows(&m_Connection);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
|
|
|
if (!result) {
|
2018-01-01 15:21:14 +01:00
|
|
|
if (m_Mysql->field_count(&m_Connection) > 0) {
|
2014-06-05 18:26:38 +02:00
|
|
|
std::ostringstream msgbuf;
|
2018-01-01 15:21:14 +01:00
|
|
|
String message = m_Mysql->error(&m_Connection);
|
2014-06-05 18:26:38 +02:00
|
|
|
msgbuf << "Error \"" << message << "\" when executing query \"" << query << "\"";
|
|
|
|
Log(LogCritical, "IdoMysqlConnection", msgbuf.str());
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
BOOST_THROW_EXCEPTION(
|
2017-12-19 15:50:05 +01:00
|
|
|
database_error()
|
2018-01-01 15:21:14 +01:00
|
|
|
<< errinfo_message(m_Mysql->error(&m_Connection))
|
2013-11-05 12:40:25 +01:00
|
|
|
<< errinfo_database_query(query)
|
|
|
|
);
|
2014-06-05 18:26:38 +02:00
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
return IdoMysqlResult();
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
return IdoMysqlResult(result, std::bind(&MysqlInterface::free_result, std::cref(m_Mysql), _1));
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
DbReference IdoMysqlConnection::GetLastInsertID()
|
2013-07-17 11:29:51 +02:00
|
|
|
{
|
2013-10-30 13:32:01 +01:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2018-01-04 09:43:49 +01:00
|
|
|
return {static_cast<long>(m_Mysql->insert_id(&m_Connection))};
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
int IdoMysqlConnection::GetAffectedRows()
|
2014-01-31 08:28:00 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
return m_AffectedRows;
|
|
|
|
}
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
String IdoMysqlConnection::Escape(const String& s)
|
2013-07-17 11:29:51 +02:00
|
|
|
{
|
2013-10-30 13:32:01 +01:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2015-12-10 12:25:46 +01:00
|
|
|
String utf8s = Utility::ValidateUTF8(s);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-12-10 12:25:46 +01:00
|
|
|
size_t length = utf8s.GetLength();
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *to = new char[utf8s.GetLength() * 2 + 1];
|
2015-12-10 12:25:46 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->real_escape_string(&m_Connection, to, utf8s.CStr(), length);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-30 14:34:54 +02:00
|
|
|
String result = String(to);
|
|
|
|
|
|
|
|
delete [] to;
|
|
|
|
|
|
|
|
return result;
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
Dictionary::Ptr IdoMysqlConnection::FetchRow(const IdoMysqlResult& result)
|
2013-07-17 11:29:51 +02:00
|
|
|
{
|
2013-10-30 13:32:01 +01:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2013-07-17 11:29:51 +02:00
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_FIELD *field;
|
|
|
|
unsigned long *lengths, i;
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
row = m_Mysql->fetch_row(result.get());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
|
|
|
if (!row)
|
2017-11-30 08:36:35 +01:00
|
|
|
return nullptr;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
lengths = m_Mysql->fetch_lengths(result.get());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
|
|
|
if (!lengths)
|
2017-11-30 08:36:35 +01:00
|
|
|
return nullptr;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr dict = new Dictionary();
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Mysql->field_seek(result.get(), 0);
|
|
|
|
for (field = m_Mysql->fetch_field(result.get()), i = 0; field; field = m_Mysql->fetch_field(result.get()), i++)
|
2013-11-22 10:13:42 +01:00
|
|
|
dict->Set(field->name, String(row[i], row[i] + lengths[i]));
|
2013-07-17 11:29:51 +02:00
|
|
|
|
|
|
|
return dict;
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
void IdoMysqlConnection::DiscardRows(const IdoMysqlResult& result)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr row;
|
|
|
|
|
|
|
|
while ((row = FetchRow(result)))
|
|
|
|
; /* empty loop body */
|
|
|
|
}
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
void IdoMysqlConnection::ActivateObject(const DbObject::Ptr& dbobj)
|
2013-07-22 12:08:49 +02:00
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2017-02-22 16:32:17 +01:00
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling object activation task for '" << dbobj->GetName1() << "!" << dbobj->GetName2() << "'.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalActivateObject, this, dbobj), PriorityLow);
|
2013-07-24 10:55:04 +02:00
|
|
|
}
|
2013-07-22 13:44:08 +02:00
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
void IdoMysqlConnection::InternalActivateObject(const DbObject::Ptr& dbobj)
|
2013-07-24 10:55:04 +02:00
|
|
|
{
|
2015-03-13 20:23:14 +01:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
if (!GetConnected())
|
2013-07-22 13:44:08 +02:00
|
|
|
return;
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
DbReference dbref = GetObjectID(dbobj);
|
2013-07-22 12:08:49 +02:00
|
|
|
std::ostringstream qbuf;
|
|
|
|
|
|
|
|
if (!dbref.IsValid()) {
|
2014-07-22 10:28:23 +02:00
|
|
|
if (!dbobj->GetName2().IsEmpty()) {
|
|
|
|
qbuf << "INSERT INTO " + GetTablePrefix() + "objects (instance_id, objecttype_id, name1, name2, is_active) VALUES ("
|
2017-12-19 15:50:05 +01:00
|
|
|
<< static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", "
|
|
|
|
<< "'" << Escape(dbobj->GetName1()) << "', '" << Escape(dbobj->GetName2()) << "', 1)";
|
2014-07-22 10:28:23 +02:00
|
|
|
} else {
|
|
|
|
qbuf << "INSERT INTO " + GetTablePrefix() + "objects (instance_id, objecttype_id, name1, is_active) VALUES ("
|
2017-12-19 15:50:05 +01:00
|
|
|
<< static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", "
|
|
|
|
<< "'" << Escape(dbobj->GetName1()) << "', 1)";
|
2014-07-22 10:28:23 +02:00
|
|
|
}
|
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
Query(qbuf.str());
|
2013-08-02 08:56:36 +02:00
|
|
|
SetObjectID(dbobj, GetLastInsertID());
|
2013-07-22 12:08:49 +02:00
|
|
|
} else {
|
2013-08-01 09:46:48 +02:00
|
|
|
qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 1 WHERE object_id = " << static_cast<long>(dbref);
|
2015-03-13 20:23:14 +01:00
|
|
|
AsyncQuery(qbuf.str());
|
2013-07-22 12:08:49 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-17 14:10:28 +02:00
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
void IdoMysqlConnection::DeactivateObject(const DbObject::Ptr& dbobj)
|
2013-07-22 12:08:49 +02:00
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2017-02-22 16:32:17 +01:00
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling object deactivation task for '" << dbobj->GetName1() << "!" << dbobj->GetName2() << "'.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalDeactivateObject, this, dbobj), PriorityLow);
|
2015-03-13 20:23:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoMysqlConnection::InternalDeactivateObject(const DbObject::Ptr& dbobj)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
if (!GetConnected())
|
2013-07-22 13:44:08 +02:00
|
|
|
return;
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
DbReference dbref = GetObjectID(dbobj);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
if (!dbref.IsValid())
|
|
|
|
return;
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
std::ostringstream qbuf;
|
2013-08-01 09:46:48 +02:00
|
|
|
qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 0 WHERE object_id = " << static_cast<long>(dbref);
|
2015-03-13 20:23:14 +01:00
|
|
|
AsyncQuery(qbuf.str());
|
2013-08-02 15:45:50 +02:00
|
|
|
|
|
|
|
/* Note that we're _NOT_ clearing the db refs via SetReference/SetConfigUpdate/SetStatusUpdate
|
|
|
|
* because the object is still in the database. */
|
2013-07-22 12:08:49 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
bool IdoMysqlConnection::FieldToEscapedString(const String& key, const Value& value, Value *result)
|
2013-07-22 12:08:49 +02:00
|
|
|
{
|
2013-07-23 11:02:47 +02:00
|
|
|
if (key == "instance_id") {
|
|
|
|
*result = static_cast<long>(m_InstanceID);
|
|
|
|
return true;
|
2015-12-15 11:55:21 +01:00
|
|
|
} else if (key == "session_token") {
|
2016-07-29 08:40:10 +02:00
|
|
|
*result = GetSessionToken();
|
2015-10-26 13:14:41 +01:00
|
|
|
return true;
|
2013-10-01 12:45:09 +02:00
|
|
|
}
|
2013-07-22 12:08:49 +02:00
|
|
|
|
2013-08-02 15:20:28 +02:00
|
|
|
Value rawvalue = DbValue::ExtractValue(value);
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
if (rawvalue.IsObjectType<ConfigObject>()) {
|
2013-08-02 15:20:28 +02:00
|
|
|
DbObject::Ptr dbobjcol = DbObject::GetOrCreateByObject(rawvalue);
|
2013-07-22 12:08:49 +02:00
|
|
|
|
|
|
|
if (!dbobjcol) {
|
|
|
|
*result = 0;
|
|
|
|
return true;
|
2013-07-17 14:10:28 +02:00
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2016-07-15 09:40:39 +02:00
|
|
|
if (!IsIDCacheValid())
|
|
|
|
return false;
|
|
|
|
|
2013-08-02 15:20:28 +02:00
|
|
|
DbReference dbrefcol;
|
2013-07-22 12:08:49 +02:00
|
|
|
|
2013-08-02 15:20:28 +02:00
|
|
|
if (DbValue::IsObjectInsertID(value)) {
|
|
|
|
dbrefcol = GetInsertID(dbobjcol);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-03-26 07:10:52 +01:00
|
|
|
if (!dbrefcol.IsValid())
|
|
|
|
return false;
|
2013-08-02 15:20:28 +02:00
|
|
|
} else {
|
2013-08-02 08:56:36 +02:00
|
|
|
dbrefcol = GetObjectID(dbobjcol);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-08-02 15:20:28 +02:00
|
|
|
if (!dbrefcol.IsValid()) {
|
|
|
|
InternalActivateObject(dbobjcol);
|
|
|
|
|
|
|
|
dbrefcol = GetObjectID(dbobjcol);
|
|
|
|
|
|
|
|
if (!dbrefcol.IsValid())
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-22 12:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*result = static_cast<long>(dbrefcol);
|
2013-07-23 11:02:47 +02:00
|
|
|
} else if (DbValue::IsTimestamp(value)) {
|
2013-08-02 17:11:14 +02:00
|
|
|
long ts = rawvalue;
|
2013-07-23 11:02:47 +02:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "FROM_UNIXTIME(" << ts << ")";
|
|
|
|
*result = Value(msgbuf.str());
|
2016-03-21 13:37:32 +01:00
|
|
|
} else if (DbValue::IsObjectInsertID(value)) {
|
2018-01-04 09:07:03 +01:00
|
|
|
auto id = static_cast<long>(rawvalue);
|
2016-03-21 13:37:32 +01:00
|
|
|
|
|
|
|
if (id <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*result = id;
|
|
|
|
return true;
|
2013-07-23 11:02:47 +02:00
|
|
|
} else {
|
2014-12-16 21:23:50 +01:00
|
|
|
Value fvalue;
|
|
|
|
|
|
|
|
if (rawvalue.IsBoolean())
|
2014-12-17 08:54:28 +01:00
|
|
|
fvalue = Convert::ToLong(rawvalue);
|
2014-12-16 21:23:50 +01:00
|
|
|
else
|
|
|
|
fvalue = rawvalue;
|
|
|
|
|
|
|
|
*result = "'" + Escape(fvalue) + "'";
|
2013-07-22 12:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
void IdoMysqlConnection::ExecuteQuery(const DbQuery& query)
|
2013-10-30 13:32:01 +01:00
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
ASSERT(query.Category != DbCatInvalid);
|
|
|
|
|
2017-02-22 16:32:17 +01:00
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling execute query task, type " << query.Type << ", table '" << query.Table << "'.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalExecuteQuery, this, query, -1), query.Priority, true);
|
2013-10-30 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-14 11:36:03 +01:00
|
|
|
void IdoMysqlConnection::ExecuteMultipleQueries(const std::vector<DbQuery>& queries)
|
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2016-04-21 09:17:40 +02:00
|
|
|
if (queries.empty())
|
|
|
|
return;
|
2015-12-15 15:29:48 +01:00
|
|
|
|
2017-02-22 16:32:17 +01:00
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling multiple execute query task, type " << queries[0].Type << ", table '" << queries[0].Table << "'.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalExecuteMultipleQueries, this, queries), queries[0].Priority, true);
|
2015-12-15 15:29:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IdoMysqlConnection::CanExecuteQuery(const DbQuery& query)
|
|
|
|
{
|
2016-07-15 09:40:39 +02:00
|
|
|
if (query.Object && !IsIDCacheValid())
|
|
|
|
return false;
|
|
|
|
|
2015-12-15 15:29:48 +01:00
|
|
|
if (query.WhereCriteria) {
|
|
|
|
ObjectLock olock(query.WhereCriteria);
|
|
|
|
Value value;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : query.WhereCriteria) {
|
2015-12-15 15:29:48 +01:00
|
|
|
if (!FieldToEscapedString(kv.first, kv.second, &value))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.Fields) {
|
|
|
|
ObjectLock olock(query.Fields);
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : query.Fields) {
|
2015-12-15 15:29:48 +01:00
|
|
|
Value value;
|
|
|
|
|
|
|
|
if (kv.second.IsEmpty() && !kv.second.IsString())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!FieldToEscapedString(kv.first, kv.second, &value))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoMysqlConnection::InternalExecuteMultipleQueries(const std::vector<DbQuery>& queries)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
2015-12-14 11:36:03 +01:00
|
|
|
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2015-12-16 11:29:06 +01:00
|
|
|
if (!GetConnected())
|
|
|
|
return;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const DbQuery& query : queries) {
|
2016-05-11 14:03:40 +02:00
|
|
|
ASSERT(query.Type == DbQueryNewTransaction || query.Category != DbCatInvalid);
|
2015-12-14 11:36:03 +01:00
|
|
|
|
2015-12-15 15:29:48 +01:00
|
|
|
if (!CanExecuteQuery(query)) {
|
2017-02-22 16:32:17 +01:00
|
|
|
|
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling multiple execute query task again: Cannot execute query now. Type '"
|
|
|
|
<< query.Type << "', table '" << query.Table << "', queue size: '" << GetPendingQueryCount() << "'.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalExecuteMultipleQueries, this, queries), query.Priority);
|
2015-12-16 13:57:29 +01:00
|
|
|
return;
|
2015-12-15 15:29:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const DbQuery& query : queries) {
|
2016-08-17 13:24:18 +02:00
|
|
|
InternalExecuteQuery(query);
|
2015-12-14 11:36:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 13:24:18 +02:00
|
|
|
void IdoMysqlConnection::InternalExecuteQuery(const DbQuery& query, int typeOverride)
|
2013-07-22 12:08:49 +02:00
|
|
|
{
|
2015-03-13 20:23:14 +01:00
|
|
|
AssertOnWorkQueue();
|
2013-07-23 11:02:47 +02:00
|
|
|
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2016-05-11 14:03:40 +02:00
|
|
|
if (!GetConnected())
|
2013-10-29 16:08:52 +01:00
|
|
|
return;
|
|
|
|
|
2016-05-11 14:03:40 +02:00
|
|
|
if (query.Type == DbQueryNewTransaction) {
|
|
|
|
InternalNewTransaction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-02 14:27:20 +02:00
|
|
|
/* check whether we're allowed to execute the query first */
|
2016-06-22 16:11:15 +02:00
|
|
|
if (GetCategoryFilter() != DbCatEverything && (query.Category & GetCategoryFilter()) == 0)
|
2013-07-23 11:02:47 +02:00
|
|
|
return;
|
|
|
|
|
2015-01-27 15:11:45 +01:00
|
|
|
if (query.Object && query.Object->GetObject()->GetExtension("agent_check").ToBool())
|
|
|
|
return;
|
|
|
|
|
2016-08-02 14:27:20 +02:00
|
|
|
/* check if there are missing object/insert ids and re-enqueue the query */
|
|
|
|
if (!CanExecuteQuery(query)) {
|
2017-02-22 16:32:17 +01:00
|
|
|
|
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling execute query task again: Cannot execute query now. Type '"
|
|
|
|
<< typeOverride << "', table '" << query.Table << "', queue size: '" << GetPendingQueryCount() << "'.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalExecuteQuery, this, query, typeOverride), query.Priority);
|
2016-08-02 14:27:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
std::ostringstream qbuf, where;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
if (query.WhereCriteria) {
|
|
|
|
where << " WHERE ";
|
|
|
|
|
|
|
|
ObjectLock olock(query.WhereCriteria);
|
|
|
|
Value value;
|
|
|
|
bool first = true;
|
2013-09-26 15:22:21 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : query.WhereCriteria) {
|
2015-03-26 07:10:52 +01:00
|
|
|
if (!FieldToEscapedString(kv.first, kv.second, &value)) {
|
2017-02-22 16:32:17 +01:00
|
|
|
|
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling execute query task again: Cannot execute query now. Type '"
|
|
|
|
<< typeOverride << "', table '" << query.Table << "', queue size: '" << GetPendingQueryCount() << "'.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalExecuteQuery, this, query, -1), query.Priority);
|
2013-08-02 08:56:36 +02:00
|
|
|
return;
|
2015-03-26 07:10:52 +01:00
|
|
|
}
|
2013-08-02 08:56:36 +02:00
|
|
|
|
|
|
|
if (!first)
|
2013-08-20 11:06:04 +02:00
|
|
|
where << " AND ";
|
2013-08-02 08:56:36 +02:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
where << kv.first << " = " << value;
|
2013-08-02 08:56:36 +02:00
|
|
|
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 13:24:18 +02:00
|
|
|
type = (typeOverride != -1) ? typeOverride : query.Type;
|
2014-01-31 08:28:00 +01:00
|
|
|
|
|
|
|
bool upsert = false;
|
|
|
|
|
|
|
|
if ((type & DbQueryInsert) && (type & DbQueryUpdate)) {
|
2013-09-26 15:22:21 +02:00
|
|
|
bool hasid = false;
|
2013-08-02 15:45:50 +02:00
|
|
|
|
2016-07-21 17:37:41 +02:00
|
|
|
if (query.Object) {
|
|
|
|
if (query.ConfigUpdate)
|
|
|
|
hasid = GetConfigUpdate(query.Object);
|
|
|
|
else if (query.StatusUpdate)
|
|
|
|
hasid = GetStatusUpdate(query.Object);
|
|
|
|
}
|
2013-08-02 15:45:50 +02:00
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
if (!hasid)
|
|
|
|
upsert = true;
|
|
|
|
|
|
|
|
type = DbQueryUpdate;
|
|
|
|
}
|
2013-08-02 08:56:36 +02:00
|
|
|
|
2016-08-17 13:24:18 +02:00
|
|
|
if ((type & DbQueryInsert) && (type & DbQueryDelete)) {
|
|
|
|
std::ostringstream qdel;
|
|
|
|
qdel << "DELETE FROM " << GetTablePrefix() << query.Table << where.str();
|
|
|
|
AsyncQuery(qdel.str());
|
|
|
|
|
|
|
|
type = DbQueryInsert;
|
|
|
|
}
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
switch (type) {
|
2013-07-22 12:08:49 +02:00
|
|
|
case DbQueryInsert:
|
2013-08-01 09:47:47 +02:00
|
|
|
qbuf << "INSERT INTO " << GetTablePrefix() << query.Table;
|
2013-07-22 12:08:49 +02:00
|
|
|
break;
|
|
|
|
case DbQueryUpdate:
|
2013-08-02 08:56:36 +02:00
|
|
|
qbuf << "UPDATE " << GetTablePrefix() << query.Table << " SET";
|
2013-07-22 12:08:49 +02:00
|
|
|
break;
|
|
|
|
case DbQueryDelete:
|
2013-08-01 09:47:47 +02:00
|
|
|
qbuf << "DELETE FROM " << GetTablePrefix() << query.Table;
|
2013-07-22 12:08:49 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-03-04 08:26:15 +01:00
|
|
|
VERIFY(!"Invalid query type.");
|
2013-07-22 12:08:49 +02:00
|
|
|
}
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
if (type == DbQueryInsert || type == DbQueryUpdate) {
|
2013-11-30 17:42:50 +01:00
|
|
|
std::ostringstream colbuf, valbuf;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2015-10-28 07:45:18 +01:00
|
|
|
if (type == DbQueryUpdate && query.Fields->GetLength() == 0)
|
|
|
|
return;
|
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
ObjectLock olock(query.Fields);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
bool first = true;
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : query.Fields) {
|
2013-11-30 17:42:50 +01:00
|
|
|
Value value;
|
|
|
|
|
2015-02-23 15:24:07 +01:00
|
|
|
if (kv.second.IsEmpty() && !kv.second.IsString())
|
2014-04-13 20:22:25 +02:00
|
|
|
continue;
|
|
|
|
|
2015-12-14 11:36:03 +01:00
|
|
|
if (!FieldToEscapedString(kv.first, kv.second, &value)) {
|
2017-02-22 16:32:17 +01:00
|
|
|
|
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Scheduling execute query task again: Cannot extract required INSERT/UPDATE fields, key '"
|
|
|
|
<< kv.first << "', val '" << kv.second << "', type " << typeOverride << ", table '" << query.Table << "'.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalExecuteQuery, this, query, -1), query.Priority);
|
2013-07-22 12:08:49 +02:00
|
|
|
return;
|
2015-12-14 11:36:03 +01:00
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
if (type == DbQueryInsert) {
|
2013-07-22 12:08:49 +02:00
|
|
|
if (!first) {
|
2013-11-30 17:42:50 +01:00
|
|
|
colbuf << ", ";
|
|
|
|
valbuf << ", ";
|
2013-07-22 12:08:49 +02:00
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
colbuf << kv.first;
|
|
|
|
valbuf << value;
|
2013-07-22 12:08:49 +02:00
|
|
|
} else {
|
|
|
|
if (!first)
|
|
|
|
qbuf << ", ";
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
qbuf << " " << kv.first << " = " << value;
|
2013-07-22 12:08:49 +02:00
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
if (type == DbQueryInsert)
|
2013-11-30 17:42:50 +01:00
|
|
|
qbuf << " (" << colbuf.str() << ") VALUES (" << valbuf.str() << ")";
|
2013-07-22 12:08:49 +02:00
|
|
|
}
|
2013-07-17 14:10:28 +02:00
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
if (type != DbQueryInsert)
|
|
|
|
qbuf << where.str();
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
AsyncQuery(qbuf.str(), std::bind(&IdoMysqlConnection::FinishExecuteQuery, this, query, type, upsert));
|
2015-03-13 20:23:14 +01:00
|
|
|
}
|
2013-08-02 08:56:36 +02:00
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
void IdoMysqlConnection::FinishExecuteQuery(const DbQuery& query, int type, bool upsert)
|
|
|
|
{
|
2014-01-31 08:28:00 +01:00
|
|
|
if (upsert && GetAffectedRows() == 0) {
|
2017-02-22 16:32:31 +01:00
|
|
|
|
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Rescheduling DELETE/INSERT query: Upsert UPDATE did not affect rows, type " << type << ", table '" << query.Table << "'.";
|
2017-02-22 16:32:31 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalExecuteQuery, this, query, DbQueryDelete | DbQueryInsert), query.Priority);
|
2014-01-31 08:28:00 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-11 16:35:58 +01:00
|
|
|
if (type == DbQueryInsert && query.Object) {
|
|
|
|
if (query.ConfigUpdate) {
|
|
|
|
SetInsertID(query.Object, GetLastInsertID());
|
2013-08-02 15:45:50 +02:00
|
|
|
SetConfigUpdate(query.Object, true);
|
2014-11-11 16:35:58 +01:00
|
|
|
} else if (query.StatusUpdate)
|
2013-08-02 15:45:50 +02:00
|
|
|
SetStatusUpdate(query.Object, true);
|
|
|
|
}
|
2013-11-20 09:32:30 +01:00
|
|
|
|
2016-03-21 13:37:32 +01:00
|
|
|
if (type == DbQueryInsert && query.Table == "notifications" && query.NotificationInsertID)
|
|
|
|
query.NotificationInsertID->SetValue(static_cast<long>(GetLastInsertID()));
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-10-30 14:07:26 +01:00
|
|
|
void IdoMysqlConnection::CleanUpExecuteQuery(const String& table, const String& time_column, double max_age)
|
2013-10-30 13:32:01 +01:00
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2017-02-22 16:32:17 +01:00
|
|
|
#ifdef I2_DEBUG /* I2_DEBUG */
|
|
|
|
Log(LogDebug, "IdoMysqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Rescheduling cleanup query for table '" << table << "' and column '"
|
|
|
|
<< time_column << "'. max_age is set to '" << max_age << "'.";
|
2017-02-22 16:32:17 +01:00
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoMysqlConnection::InternalCleanUpExecuteQuery, this, table, time_column, max_age), PriorityLow, true);
|
2013-10-30 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 14:07:26 +01:00
|
|
|
void IdoMysqlConnection::InternalCleanUpExecuteQuery(const String& table, const String& time_column, double max_age)
|
2013-09-26 15:22:21 +02:00
|
|
|
{
|
2015-03-13 20:23:14 +01:00
|
|
|
AssertOnWorkQueue();
|
2013-09-26 15:22:21 +02:00
|
|
|
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
if (!GetConnected())
|
2013-09-26 15:22:21 +02:00
|
|
|
return;
|
|
|
|
|
2015-03-13 20:23:14 +01:00
|
|
|
AsyncQuery("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " +
|
2017-12-19 15:50:05 +01:00
|
|
|
Convert::ToString(static_cast<long>(m_InstanceID)) + " AND " + time_column +
|
|
|
|
" < FROM_UNIXTIME(" + Convert::ToString(static_cast<long>(max_age)) + ")");
|
2013-09-26 15:22:21 +02:00
|
|
|
}
|
2014-01-31 08:28:00 +01:00
|
|
|
|
|
|
|
void IdoMysqlConnection::FillIDCache(const DbType::Ptr& type)
|
|
|
|
{
|
2016-08-14 22:24:51 +02:00
|
|
|
String query = "SELECT " + type->GetIDColumn() + " AS object_id, " + type->GetTable() + "_id, config_hash FROM " + GetTablePrefix() + type->GetTable() + "s";
|
2014-01-31 08:28:00 +01:00
|
|
|
IdoMysqlResult result = Query(query);
|
|
|
|
|
|
|
|
Dictionary::Ptr row;
|
|
|
|
|
|
|
|
while ((row = FetchRow(result))) {
|
2016-08-14 22:24:51 +02:00
|
|
|
DbReference dbref(row->Get("object_id"));
|
|
|
|
SetInsertID(type, dbref, DbReference(row->Get(type->GetTable() + "_id")));
|
|
|
|
SetConfigHash(type, dbref, row->Get("config_hash"));
|
2014-01-31 08:28:00 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-13 12:00:30 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
int IdoMysqlConnection::GetPendingQueryCount() const
|
2015-03-13 12:00:30 +01:00
|
|
|
{
|
|
|
|
return m_QueryQueue.GetLength();
|
|
|
|
}
|