2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "db_ido_pgsql/idopgsqlconnection.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "db_ido_pgsql/idopgsqlconnection-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/context.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
2020-09-09 18:17:37 +02:00
|
|
|
#include "base/defer.hpp"
|
2018-01-04 08:54:18 +01:00
|
|
|
#include <utility>
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(IdoPgsqlConnection);
|
|
|
|
|
2015-09-21 11:44:58 +02:00
|
|
|
REGISTER_STATSFUNCTION(IdoPgsqlConnection, &IdoPgsqlConnection::StatsFunc);
|
2014-02-17 16:34:18 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
IdoPgsqlConnection::IdoPgsqlConnection()
|
2016-06-14 08:19:13 +02:00
|
|
|
{
|
|
|
|
m_QueryQueue.SetName("IdoPgsqlConnection, " + GetName());
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::OnConfigLoaded()
|
2016-06-14 08:19:13 +02:00
|
|
|
{
|
2016-06-16 14:21:29 +02:00
|
|
|
ObjectImpl<IdoPgsqlConnection>::OnConfigLoaded();
|
|
|
|
|
2016-06-14 08:19:13 +02:00
|
|
|
m_QueryQueue.SetName("IdoPgsqlConnection, " + GetName());
|
2018-01-01 15:21:14 +01:00
|
|
|
|
|
|
|
Library shimLibrary{"pgsql_shim"};
|
|
|
|
|
|
|
|
auto create_pgsql_shim = shimLibrary.GetSymbolAddress<create_pgsql_shim_ptr>("create_pgsql_shim");
|
|
|
|
|
|
|
|
m_Pgsql.reset(create_pgsql_shim());
|
|
|
|
|
|
|
|
std::swap(m_Library, shimLibrary);
|
2016-06-14 08:19:13 +02:00
|
|
|
}
|
2014-11-11 09:40:00 +01:00
|
|
|
|
2015-02-07 22:36:17 +01:00
|
|
|
void IdoPgsqlConnection::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 IdoPgsqlConnection::Ptr& idopgsqlconnection : ConfigType::GetObjectsByType<IdoPgsqlConnection>()) {
|
2017-05-11 17:30:20 +02:00
|
|
|
size_t queryQueueItems = idopgsqlconnection->m_QueryQueue.GetLength();
|
|
|
|
double queryQueueItemRate = idopgsqlconnection->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(idopgsqlconnection->GetName(), new Dictionary({
|
|
|
|
{ "version", idopgsqlconnection->GetSchemaVersion() },
|
|
|
|
{ "instance_name", idopgsqlconnection->GetInstanceName() },
|
|
|
|
{ "connected", idopgsqlconnection->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("idopgsqlconnection_" + idopgsqlconnection->GetName() + "_queries_rate", idopgsqlconnection->GetQueryCount(60) / 60.0));
|
|
|
|
perfdata->Add(new PerfdataValue("idopgsqlconnection_" + idopgsqlconnection->GetName() + "_queries_1min", idopgsqlconnection->GetQueryCount(60)));
|
|
|
|
perfdata->Add(new PerfdataValue("idopgsqlconnection_" + idopgsqlconnection->GetName() + "_queries_5mins", idopgsqlconnection->GetQueryCount(5 * 60)));
|
|
|
|
perfdata->Add(new PerfdataValue("idopgsqlconnection_" + idopgsqlconnection->GetName() + "_queries_15mins", idopgsqlconnection->GetQueryCount(15 * 60)));
|
2017-05-11 17:30:20 +02:00
|
|
|
perfdata->Add(new PerfdataValue("idopgsqlconnection_" + idopgsqlconnection->GetName() + "_query_queue_items", queryQueueItems));
|
|
|
|
perfdata->Add(new PerfdataValue("idopgsqlconnection_" + idopgsqlconnection->GetName() + "_query_queue_item_rate", queryQueueItemRate));
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
status->Set("idopgsqlconnection", new Dictionary(std::move(nodes)));
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::Resume()
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "IdoPgsqlConnection")
|
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-10-30 15:32:33 +01:00
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.SetExceptionCallback(std::bind(&IdoPgsqlConnection::ExceptionHandler, this, _1));
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
/* Immediately try to connect on Resume() without timer. */
|
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::Reconnect, this), PriorityImmediate);
|
|
|
|
|
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(&IdoPgsqlConnection::TxTimerHandler, this));
|
2013-10-30 15:32:33 +01:00
|
|
|
m_TxTimer->Start();
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
m_ReconnectTimer = new Timer();
|
2013-10-30 15:32:33 +01:00
|
|
|
m_ReconnectTimer->SetInterval(10);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_ReconnectTimer->OnTimerExpired.connect(std::bind(&IdoPgsqlConnection::ReconnectTimerHandler, this));
|
2013-10-30 15:32:33 +01:00
|
|
|
m_ReconnectTimer->Start();
|
2019-03-29 15:53:56 +01:00
|
|
|
|
|
|
|
/* Start with queries after connect. */
|
|
|
|
DbConnection::Resume();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
ASSERT(m_Pgsql->isthreadsafe());
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::Pause()
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
2014-07-08 13:48:11 +02:00
|
|
|
DbConnection::Pause();
|
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
m_ReconnectTimer.reset();
|
|
|
|
|
2019-02-20 17:10:46 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::Disconnect, this), PriorityLow);
|
2019-03-29 15:53:56 +01:00
|
|
|
|
|
|
|
/* Work on remaining tasks but never delete the threads, for HA resuming later. */
|
2013-10-30 15:32:33 +01:00
|
|
|
m_QueryQueue.Join();
|
2019-02-20 17:10:46 +01:00
|
|
|
|
|
|
|
Log(LogInformation, "IdoPgsqlConnection")
|
|
|
|
<< "'" << GetName() << "' paused.";
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2016-08-31 13:43:14 +02:00
|
|
|
void IdoPgsqlConnection::ExceptionHandler(boost::exception_ptr exp)
|
2013-11-05 12:40:25 +01:00
|
|
|
{
|
2014-06-05 18:26:38 +02:00
|
|
|
Log(LogWarning, "IdoPgsqlConnection", "Exception during database operation: Verify that your database is operational!");
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "IdoPgsqlConnection")
|
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-04-04 01:03:39 +02:00
|
|
|
if (GetConnected()) {
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Pgsql->finish(m_Connection);
|
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 IdoPgsqlConnection::AssertOnWorkQueue()
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
2014-12-18 15:11:57 +01:00
|
|
|
ASSERT(m_QueryQueue.IsWorkerThread());
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::Disconnect()
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
if (!GetConnected())
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2013-10-30 15:32:33 +01:00
|
|
|
Query("COMMIT");
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Pgsql->finish(m_Connection);
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2019-03-29 15:53:56 +01:00
|
|
|
|
|
|
|
Log(LogInformation, "IdoPgsqlConnection")
|
|
|
|
<< "Disconnected from '" << GetName() << "' database '" << GetDatabase() << "'.";
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::TxTimerHandler()
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
2015-02-04 15:25:10 +01:00
|
|
|
NewTransaction();
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::NewTransaction()
|
2015-02-04 15:25:10 +01:00
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::InternalNewTransaction, this), PriorityNormal, true);
|
2015-02-04 15:25:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::InternalNewTransaction()
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
2015-04-04 01:03:39 +02:00
|
|
|
AssertOnWorkQueue();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
if (!GetConnected())
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(2);
|
2013-10-30 15:32:33 +01:00
|
|
|
Query("COMMIT");
|
|
|
|
Query("BEGIN");
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::ReconnectTimerHandler()
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
2019-03-29 15:53:56 +01:00
|
|
|
/* Only allow Reconnect events with high priority. */
|
2019-02-20 17:10:46 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::Reconnect, this), PriorityHigh);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::Reconnect()
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2013-11-19 07:49:41 +01:00
|
|
|
CONTEXT("Reconnecting to PostgreSQL IDO database '" + GetName() + "'");
|
|
|
|
|
2015-12-15 16:50:17 +01:00
|
|
|
double startTime = Utility::GetTime();
|
2015-10-26 13:14:41 +01:00
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
SetShouldConnect(true);
|
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
bool reconnect = false;
|
2014-02-12 14:49:39 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (GetConnected()) {
|
|
|
|
/* Check if we're really still connected */
|
|
|
|
try {
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2015-12-15 13:44:58 +01:00
|
|
|
Query("SELECT 1");
|
|
|
|
return;
|
|
|
|
} catch (const std::exception&) {
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Pgsql->finish(m_Connection);
|
2015-12-15 13:44:58 +01:00
|
|
|
SetConnected(false);
|
|
|
|
reconnect = true;
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
2015-12-15 13:44:58 +01:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
ClearIDCache();
|
2013-11-20 09:32:30 +01:00
|
|
|
|
2018-04-18 15:30:06 +02:00
|
|
|
String host = GetHost();
|
|
|
|
String port = GetPort();
|
|
|
|
String user = GetUser();
|
|
|
|
String password = GetPassword();
|
|
|
|
String database = GetDatabase();
|
|
|
|
|
|
|
|
String sslMode = GetSslMode();
|
|
|
|
String sslKey = GetSslKey();
|
|
|
|
String sslCert = GetSslCert();
|
|
|
|
String sslCa = GetSslCa();
|
|
|
|
|
|
|
|
String conninfo;
|
|
|
|
|
|
|
|
if (!host.IsEmpty())
|
|
|
|
conninfo += " host=" + host;
|
|
|
|
if (!port.IsEmpty())
|
|
|
|
conninfo += " port=" + port;
|
|
|
|
if (!user.IsEmpty())
|
|
|
|
conninfo += " user=" + user;
|
|
|
|
if (!password.IsEmpty())
|
|
|
|
conninfo += " password=" + password;
|
|
|
|
if (!database.IsEmpty())
|
|
|
|
conninfo += " dbname=" + database;
|
|
|
|
|
|
|
|
if (!sslMode.IsEmpty())
|
|
|
|
conninfo += " sslmode=" + sslMode;
|
|
|
|
if (!sslKey.IsEmpty())
|
|
|
|
conninfo += " sslkey=" + sslKey;
|
|
|
|
if (!sslCert.IsEmpty())
|
|
|
|
conninfo += " sslcert=" + sslCert;
|
|
|
|
if (!sslCa.IsEmpty())
|
|
|
|
conninfo += " sslrootcert=" + sslCa;
|
|
|
|
|
|
|
|
/* connection */
|
|
|
|
m_Connection = m_Pgsql->connectdb(conninfo.CStr());
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (!m_Connection)
|
|
|
|
return;
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
if (m_Pgsql->status(m_Connection) != CONNECTION_OK) {
|
|
|
|
String message = m_Pgsql->errorMessage(m_Connection);
|
|
|
|
m_Pgsql->finish(m_Connection);
|
2015-12-15 13:44:58 +01:00
|
|
|
SetConnected(false);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection")
|
2018-04-18 15:30:06 +02:00
|
|
|
<< "Connection to database '" << database << "' with user '" << user << "' on '" << host << ":" << port
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "' failed: \"" << message << "\"";
|
2014-06-05 18:26:38 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error(message));
|
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
SetConnected(true);
|
2015-04-04 01:03:39 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
IdoPgsqlResult result;
|
2015-09-05 14:08:35 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
String dbVersionName = "idoutils";
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2015-12-15 13:44:58 +01:00
|
|
|
result = Query("SELECT version FROM " + GetTablePrefix() + "dbversion WHERE name=E'" + Escape(dbVersionName) + "'");
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
Dictionary::Ptr row = FetchRow(result, 0);
|
2014-01-28 11:42:12 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (!row) {
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Pgsql->finish(m_Connection);
|
2015-12-15 13:44:58 +01:00
|
|
|
SetConnected(false);
|
2015-03-05 15:57:46 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection", "Schema does not provide any valid version! Verify your schema installation.");
|
2014-08-06 10:47:00 +02:00
|
|
|
|
2017-03-28 10:16:07 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid schema."));
|
2015-12-15 13:44:58 +01:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
String version = row->Get("version");
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
SetSchemaVersion(version);
|
2015-03-12 09:39:53 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (Utility::CompareVersion(IDO_COMPAT_SCHEMA_VERSION, version) < 0) {
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Pgsql->finish(m_Connection);
|
2015-12-15 13:44:58 +01:00
|
|
|
SetConnected(false);
|
2015-03-05 15:57:46 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection")
|
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 "
|
2019-08-27 16:46:51 +02:00
|
|
|
<< "https://icinga.com/docs/icinga2/latest/doc/16-upgrading-icinga-2/#upgrading-postgresql-db";
|
2014-06-05 18:26:38 +02:00
|
|
|
|
2017-03-28 10:16:07 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Schema version mismatch."));
|
2015-12-15 13:44:58 +01:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
String instanceName = GetInstanceName();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2015-12-15 13:44:58 +01:00
|
|
|
result = Query("SELECT instance_id FROM " + GetTablePrefix() + "instances WHERE instance_name = E'" + Escape(instanceName) + "'");
|
|
|
|
row = FetchRow(result, 0);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (!row) {
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2015-12-15 13:44:58 +01:00
|
|
|
Query("INSERT INTO " + GetTablePrefix() + "instances (instance_name, instance_description) VALUES (E'" + Escape(instanceName) + "', E'" + Escape(GetInstanceDescription()) + "')");
|
|
|
|
m_InstanceID = GetSequenceValue(GetTablePrefix() + "instances", "instance_id");
|
|
|
|
} else {
|
|
|
|
m_InstanceID = DbReference(row->Get("instance_id"));
|
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
Endpoint::Ptr my_endpoint = Endpoint::GetLocalEndpoint();
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-12-15 13:44:58 +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 */
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2015-12-15 13:44:58 +01:00
|
|
|
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-12-15 13:44:58 +01:00
|
|
|
row = FetchRow(result, 0);
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
String endpoint_name;
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (row)
|
|
|
|
endpoint_name = row->Get("endpoint_name");
|
|
|
|
else
|
|
|
|
Log(LogNotice, "IdoPgsqlConnection", "Empty program status table");
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-12-15 13:44:58 +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-12-15 13:44:58 +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
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
double now = Utility::GetTime();
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
double status_update_age = now - status_update_time;
|
|
|
|
double failoverTimeout = GetFailoverTimeout();
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (status_update_age < GetFailoverTimeout()) {
|
2019-03-29 15:53:56 +01:00
|
|
|
Log(LogInformation, "IdoPgsqlConnection")
|
|
|
|
<< "Last update by endpoint '" << endpoint_name << "' was "
|
|
|
|
<< status_update_age << "s ago (< failover timeout of " << failoverTimeout << "s). Retrying.";
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Pgsql->finish(m_Connection);
|
2015-12-15 13:44:58 +01:00
|
|
|
SetConnected(false);
|
|
|
|
SetShouldConnect(false);
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
/* activate the IDO only, if we're authoritative in this zone */
|
|
|
|
if (IsPaused()) {
|
|
|
|
Log(LogNotice, "IdoPgsqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Local endpoint '" << my_endpoint->GetName() << "' is not authoritative, bailing out.";
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
m_Pgsql->finish(m_Connection);
|
2015-12-15 13:44:58 +01:00
|
|
|
SetConnected(false);
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
return;
|
2014-08-13 20:30:28 +02:00
|
|
|
}
|
2019-03-29 15:53:56 +01:00
|
|
|
|
|
|
|
SetLastFailover(now);
|
|
|
|
|
|
|
|
Log(LogInformation, "IdoPgsqlConnection")
|
|
|
|
<< "Last update by endpoint '" << endpoint_name << "' was "
|
|
|
|
<< status_update_age << "s ago. Taking over '" << GetName() << "' in HA zone '" << Zone::GetLocalZone()->GetName() << "'.";
|
2014-08-13 20:30:28 +02:00
|
|
|
}
|
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
Log(LogNotice, "IdoPgsqlConnection", "Enabling IDO connection.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(LogInformation, "IdoPgsqlConnection")
|
2018-04-18 15:30:06 +02:00
|
|
|
<< "PGSQL IDO instance id: " << static_cast<long>(m_InstanceID) << " (schema version: '" + version + "')"
|
|
|
|
<< (!sslMode.IsEmpty() ? ", sslmode='" + sslMode + "'" : "");
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2016-05-17 10:32:38 +02:00
|
|
|
Query("BEGIN");
|
|
|
|
|
2016-05-11 14:03:40 +02:00
|
|
|
/* update programstatus table */
|
|
|
|
UpdateProgramStatus();
|
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
/* record connection */
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2015-12-15 13:44:58 +01:00
|
|
|
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(), E'icinga2 db_ido_pgsql', E'" + Escape(Application::GetAppVersion())
|
|
|
|
+ "', E'" + (reconnect ? "RECONNECT" : "INITIAL") + "', NOW())");
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
/* clear config tables for the initial config dump */
|
|
|
|
PrepareDatabase();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +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);
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2015-12-15 13:44:58 +01:00
|
|
|
result = Query(q1buf.str());
|
2014-01-28 11:42:12 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
std::vector<DbObject::Ptr> activeDbObjs;
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
int index = 0;
|
|
|
|
while ((row = FetchRow(result, index))) {
|
|
|
|
index++;
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
DbType::Ptr dbtype = DbType::GetByID(row->Get("objecttype_id"));
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (!dbtype)
|
|
|
|
continue;
|
2014-02-12 14:49:39 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2"));
|
|
|
|
SetObjectID(dbobj, DbReference(row->Get("object_id")));
|
|
|
|
bool active = row->Get("is_active");
|
|
|
|
SetObjectActive(dbobj, active);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (active)
|
|
|
|
activeDbObjs.push_back(dbobj);
|
2013-10-30 15:32:33 +01: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, "IdoPgsqlConnection")
|
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();
|
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::ClearTablesBySession, this), PriorityNormal);
|
2015-10-26 13:14:41 +01:00
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::FinishConnect, this, startTime), PriorityNormal);
|
2015-12-16 08:49:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::FinishConnect(double startTime)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
if (!GetConnected())
|
|
|
|
return;
|
|
|
|
|
2015-12-15 16:50:17 +01:00
|
|
|
Log(LogInformation, "IdoPgsqlConnection")
|
2019-03-29 15:53:56 +01:00
|
|
|
<< "Finished reconnecting to '" << GetName() << "' database '" << GetDatabase() << "' in "
|
|
|
|
<< std::setw(2) << Utility::GetTime() - startTime << " second(s).";
|
2015-12-15 16:50:17 +01:00
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(2);
|
2015-10-26 13:14:41 +01:00
|
|
|
Query("COMMIT");
|
|
|
|
Query("BEGIN");
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void IdoPgsqlConnection::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 IdoPgsqlConnection::ClearTableBySession(const String& table)
|
|
|
|
{
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2016-07-21 17:37:41 +02:00
|
|
|
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-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
IdoPgsqlResult IdoPgsqlConnection::Query(const String& query)
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
Defer decreaseQueries ([this]() { DecreasePendingQueries(1); });
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "IdoPgsqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Query: " << query;
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
IncreaseQueryCount();
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
PGresult *result = m_Pgsql->exec(m_Connection, query.CStr());
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-06-05 18:26:38 +02:00
|
|
|
if (!result) {
|
2018-01-01 15:21:14 +01:00
|
|
|
String message = m_Pgsql->errorMessage(m_Connection);
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Error \"" << message << "\" when executing query \"" << query << "\"";
|
2014-06-05 18:26:38 +02:00
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
BOOST_THROW_EXCEPTION(
|
2017-12-19 15:50:05 +01:00
|
|
|
database_error()
|
2014-08-17 17:57:20 +02:00
|
|
|
<< errinfo_message(message)
|
|
|
|
<< errinfo_database_query(query)
|
2013-11-05 12:40:25 +01:00
|
|
|
);
|
2014-06-05 18:26:38 +02:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
char *rowCount = m_Pgsql->cmdTuples(result);
|
2014-02-10 10:25:12 +01:00
|
|
|
m_AffectedRows = atoi(rowCount);
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
if (m_Pgsql->resultStatus(result) == PGRES_COMMAND_OK) {
|
|
|
|
m_Pgsql->clear(result);
|
2014-01-28 11:42:12 +01:00
|
|
|
return IdoPgsqlResult();
|
2014-09-12 17:51:59 +02:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
if (m_Pgsql->resultStatus(result) != PGRES_TUPLES_OK) {
|
|
|
|
String message = m_Pgsql->resultErrorMessage(result);
|
|
|
|
m_Pgsql->clear(result);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Error \"" << message << "\" when executing query \"" << query << "\"";
|
2014-06-05 18:26:38 +02:00
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
BOOST_THROW_EXCEPTION(
|
2017-12-19 15:50:05 +01:00
|
|
|
database_error()
|
2014-08-17 17:57:20 +02:00
|
|
|
<< errinfo_message(message)
|
|
|
|
<< errinfo_database_query(query)
|
2013-11-05 12:40:25 +01:00
|
|
|
);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
return IdoPgsqlResult(result, std::bind(&PgsqlInterface::clear, std::cref(m_Pgsql), _1));
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DbReference IdoPgsqlConnection::GetSequenceValue(const String& table, const String& column)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2014-06-25 09:47:20 +02:00
|
|
|
IdoPgsqlResult result = Query("SELECT CURRVAL(pg_get_serial_sequence(E'" + Escape(table) + "', E'" + Escape(column) + "')) AS id");
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
Dictionary::Ptr row = FetchRow(result, 0);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
ASSERT(row);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "IdoPgsqlConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Sequence Value: " << row->Get("id");
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2018-01-04 09:28:24 +01:00
|
|
|
return {Convert::ToLong(row->Get("id"))};
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
int IdoPgsqlConnection::GetAffectedRows()
|
2014-01-31 08:28:00 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
return m_AffectedRows;
|
|
|
|
}
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
String IdoPgsqlConnection::Escape(const String& s)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2015-12-10 12:25:46 +01:00
|
|
|
String utf8s = Utility::ValidateUTF8(s);
|
2013-10-30 15:32:33 +01: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_Pgsql->escapeStringConn(m_Connection, to, utf8s.CStr(), length, nullptr);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
String result = String(to);
|
|
|
|
|
|
|
|
delete [] to;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
Dictionary::Ptr IdoPgsqlConnection::FetchRow(const IdoPgsqlResult& result, int row)
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
if (row >= m_Pgsql->ntuples(result.get()))
|
2017-11-30 08:36:35 +01:00
|
|
|
return nullptr;
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
int columns = m_Pgsql->nfields(result.get());
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
DictionaryData dict;
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
for (int column = 0; column < columns; column++) {
|
|
|
|
Value value;
|
|
|
|
|
2018-01-01 15:21:14 +01:00
|
|
|
if (!m_Pgsql->getisnull(result.get(), row, column))
|
|
|
|
value = m_Pgsql->getvalue(result.get(), row, column);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
dict.emplace_back(m_Pgsql->fname(result.get(), column), value);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Dictionary(std::move(dict));
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::ActivateObject(const DbObject::Ptr& dbobj)
|
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::InternalActivateObject, this, dbobj), PriorityNormal);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::InternalActivateObject(const DbObject::Ptr& dbobj)
|
|
|
|
{
|
2015-04-04 01:03:39 +02:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
if (!GetConnected())
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
DbReference dbref = GetObjectID(dbobj);
|
|
|
|
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() << ", "
|
|
|
|
<< "E'" << Escape(dbobj->GetName1()) << "', E'" << 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() << ", "
|
|
|
|
<< "E'" << Escape(dbobj->GetName1()) << "', 1)";
|
2014-07-22 10:28:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2013-10-30 15:32:33 +01:00
|
|
|
Query(qbuf.str());
|
|
|
|
SetObjectID(dbobj, GetSequenceValue(GetTablePrefix() + "objects", "object_id"));
|
|
|
|
} else {
|
|
|
|
qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 1 WHERE object_id = " << static_cast<long>(dbref);
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2013-10-30 15:32:33 +01:00
|
|
|
Query(qbuf.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::DeactivateObject(const DbObject::Ptr& dbobj)
|
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::InternalDeactivateObject, this, dbobj), PriorityNormal);
|
2015-04-04 01:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::InternalDeactivateObject(const DbObject::Ptr& dbobj)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
if (!GetConnected())
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
DbReference dbref = GetObjectID(dbobj);
|
|
|
|
|
|
|
|
if (!dbref.IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::ostringstream qbuf;
|
|
|
|
qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 0 WHERE object_id = " << static_cast<long>(dbref);
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2013-10-30 15:32:33 +01:00
|
|
|
Query(qbuf.str());
|
|
|
|
|
|
|
|
/* Note that we're _NOT_ clearing the db refs via SetReference/SetConfigUpdate/SetStatusUpdate
|
|
|
|
* because the object is still in the database. */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IdoPgsqlConnection::FieldToEscapedString(const String& key, const Value& value, Value *result)
|
|
|
|
{
|
|
|
|
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-29 22:27:09 +01:00
|
|
|
return true;
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Value rawvalue = DbValue::ExtractValue(value);
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
if (rawvalue.IsObjectType<ConfigObject>()) {
|
2013-10-30 15:32:33 +01:00
|
|
|
DbObject::Ptr dbobjcol = DbObject::GetOrCreateByObject(rawvalue);
|
|
|
|
|
|
|
|
if (!dbobjcol) {
|
|
|
|
*result = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-15 09:40:39 +02:00
|
|
|
if (!IsIDCacheValid())
|
|
|
|
return false;
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
DbReference dbrefcol;
|
|
|
|
|
|
|
|
if (DbValue::IsObjectInsertID(value)) {
|
|
|
|
dbrefcol = GetInsertID(dbobjcol);
|
|
|
|
|
2016-04-20 18:21:39 +02:00
|
|
|
if (!dbrefcol.IsValid())
|
|
|
|
return false;
|
2013-10-30 15:32:33 +01:00
|
|
|
} else {
|
|
|
|
dbrefcol = GetObjectID(dbobjcol);
|
|
|
|
|
|
|
|
if (!dbrefcol.IsValid()) {
|
|
|
|
InternalActivateObject(dbobjcol);
|
|
|
|
|
|
|
|
dbrefcol = GetObjectID(dbobjcol);
|
|
|
|
|
|
|
|
if (!dbrefcol.IsValid())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = static_cast<long>(dbrefcol);
|
|
|
|
} else if (DbValue::IsTimestamp(value)) {
|
|
|
|
long ts = rawvalue;
|
|
|
|
std::ostringstream msgbuf;
|
2017-01-11 17:34:09 +01:00
|
|
|
msgbuf << "TO_TIMESTAMP(" << ts << ") AT TIME ZONE 'UTC'";
|
2013-10-30 15:32:33 +01:00
|
|
|
*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-10-30 15:32:33 +01: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 = "E'" + Escape(fvalue) + "'";
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::ExecuteQuery(const DbQuery& query)
|
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
ASSERT(query.Category != DbCatInvalid);
|
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::InternalExecuteQuery, this, query, -1), query.Priority, true);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2015-12-14 11:36:03 +01:00
|
|
|
void IdoPgsqlConnection::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
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(queries.size());
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::InternalExecuteMultipleQueries, this, queries), queries[0].Priority, true);
|
2015-12-15 15:29:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IdoPgsqlConnection::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 IdoPgsqlConnection::InternalExecuteMultipleQueries(const std::vector<DbQuery>& queries)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
2015-12-14 11:36:03 +01:00
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
if (IsPaused()) {
|
|
|
|
DecreasePendingQueries(queries.size());
|
2018-10-30 13:17:28 +01:00
|
|
|
return;
|
2020-09-09 18:17:37 +02:00
|
|
|
}
|
2018-10-30 13:17:28 +01:00
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
if (!GetConnected()) {
|
|
|
|
DecreasePendingQueries(queries.size());
|
2015-12-16 11:29:06 +01:00
|
|
|
return;
|
2020-09-09 18:17:37 +02:00
|
|
|
}
|
2015-12-16 11:29:06 +01:00
|
|
|
|
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-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::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 IdoPgsqlConnection::InternalExecuteQuery(const DbQuery& query, int typeOverride)
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
2015-04-04 01:03:39 +02:00
|
|
|
AssertOnWorkQueue();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
if (IsPaused()) {
|
|
|
|
DecreasePendingQueries(1);
|
2018-10-30 13:17:28 +01:00
|
|
|
return;
|
2020-09-09 18:17:37 +02:00
|
|
|
}
|
2018-10-30 13:17:28 +01:00
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
if (!GetConnected()) {
|
|
|
|
DecreasePendingQueries(1);
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
2020-09-09 18:17:37 +02:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2016-05-11 14:03:40 +02:00
|
|
|
if (query.Type == DbQueryNewTransaction) {
|
2020-09-09 18:17:37 +02:00
|
|
|
DecreasePendingQueries(1);
|
2016-05-11 14:03:40 +02:00
|
|
|
InternalNewTransaction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-02 14:27:20 +02:00
|
|
|
/* check whether we're allowed to execute the query first */
|
2020-09-09 18:17:37 +02:00
|
|
|
if (GetCategoryFilter() != DbCatEverything && (query.Category & GetCategoryFilter()) == 0) {
|
|
|
|
DecreasePendingQueries(1);
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
2020-09-09 18:17:37 +02:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
if (query.Object && query.Object->GetObject()->GetExtension("agent_check").ToBool()) {
|
|
|
|
DecreasePendingQueries(1);
|
2015-01-27 15:11:45 +01:00
|
|
|
return;
|
2020-09-09 18:17:37 +02:00
|
|
|
}
|
2015-01-27 15:11:45 +01:00
|
|
|
|
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-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::InternalExecuteQuery, this, query, typeOverride), query.Priority);
|
2016-08-02 14:27:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
std::ostringstream qbuf, where;
|
|
|
|
int type;
|
|
|
|
|
|
|
|
if (query.WhereCriteria) {
|
|
|
|
where << " WHERE ";
|
|
|
|
|
|
|
|
ObjectLock olock(query.WhereCriteria);
|
|
|
|
Value value;
|
|
|
|
bool first = true;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : query.WhereCriteria) {
|
2015-12-14 11:36:03 +01:00
|
|
|
if (!FieldToEscapedString(kv.first, kv.second, &value)) {
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::InternalExecuteQuery, this, query, -1), query.Priority);
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
2015-12-14 11:36:03 +01:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
if (!first)
|
|
|
|
where << " AND ";
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
where << kv.first << " = " << value;
|
2013-10-30 15:32:33 +01: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;
|
|
|
|
|
2014-02-05 14:58:54 +01:00
|
|
|
if ((type & DbQueryInsert) && (type & DbQueryUpdate)) {
|
2013-10-30 15:32:33 +01:00
|
|
|
bool hasid = false;
|
|
|
|
|
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-10-30 15:32:33 +01:00
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
if (!hasid)
|
|
|
|
upsert = true;
|
|
|
|
|
|
|
|
type = DbQueryUpdate;
|
|
|
|
}
|
2013-10-30 15:32:33 +01: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();
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2016-08-17 13:24:18 +02:00
|
|
|
Query(qdel.str());
|
|
|
|
|
|
|
|
type = DbQueryInsert;
|
|
|
|
}
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
switch (type) {
|
|
|
|
case DbQueryInsert:
|
|
|
|
qbuf << "INSERT INTO " << GetTablePrefix() << query.Table;
|
|
|
|
break;
|
|
|
|
case DbQueryUpdate:
|
|
|
|
qbuf << "UPDATE " << GetTablePrefix() << query.Table << " SET";
|
|
|
|
break;
|
|
|
|
case DbQueryDelete:
|
|
|
|
qbuf << "DELETE FROM " << GetTablePrefix() << query.Table;
|
|
|
|
break;
|
|
|
|
default:
|
2015-03-04 08:26:15 +01:00
|
|
|
VERIFY(!"Invalid query type.");
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == DbQueryInsert || type == DbQueryUpdate) {
|
2013-11-30 17:42:50 +01:00
|
|
|
std::ostringstream colbuf, valbuf;
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-10-28 07:45:18 +01:00
|
|
|
if (type == DbQueryUpdate && query.Fields->GetLength() == 0)
|
|
|
|
return;
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
ObjectLock olock(query.Fields);
|
|
|
|
|
|
|
|
Value value;
|
|
|
|
bool first = true;
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : query.Fields) {
|
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-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::InternalExecuteQuery, this, query, -1), query.Priority);
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
2015-12-14 11:36:03 +01:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
if (type == DbQueryInsert) {
|
|
|
|
if (!first) {
|
2013-11-30 17:42:50 +01:00
|
|
|
colbuf << ", ";
|
|
|
|
valbuf << ", ";
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
colbuf << kv.first;
|
|
|
|
valbuf << value;
|
2013-10-30 15:32:33 +01:00
|
|
|
} else {
|
|
|
|
if (!first)
|
|
|
|
qbuf << ", ";
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
qbuf << " " << kv.first << " = " << value;
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == DbQueryInsert)
|
2013-11-30 17:42:50 +01:00
|
|
|
qbuf << " (" << colbuf.str() << ") VALUES (" << valbuf.str() << ")";
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type != DbQueryInsert)
|
|
|
|
qbuf << where.str();
|
|
|
|
|
|
|
|
Query(qbuf.str());
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
if (upsert && GetAffectedRows() == 0) {
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2016-08-17 13:24:18 +02:00
|
|
|
InternalExecuteQuery(query, DbQueryDelete | DbQueryInsert);
|
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) {
|
2013-10-30 15:32:33 +01:00
|
|
|
String idField = query.IdColumn;
|
|
|
|
|
|
|
|
if (idField.IsEmpty())
|
|
|
|
idField = query.Table.SubStr(0, query.Table.GetLength() - 1) + "_id";
|
|
|
|
|
|
|
|
SetInsertID(query.Object, GetSequenceValue(GetTablePrefix() + query.Table, idField));
|
2014-11-11 16:35:58 +01:00
|
|
|
|
|
|
|
SetConfigUpdate(query.Object, true);
|
|
|
|
} else if (query.StatusUpdate)
|
|
|
|
SetStatusUpdate(query.Object, true);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
2014-01-29 10:30:33 +01:00
|
|
|
|
2016-03-21 13:37:32 +01:00
|
|
|
if (type == DbQueryInsert && query.Table == "notifications" && query.NotificationInsertID) {
|
|
|
|
DbReference seqval = GetSequenceValue(GetTablePrefix() + query.Table, "notification_id");
|
|
|
|
query.NotificationInsertID->SetValue(static_cast<long>(seqval));
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::CleanUpExecuteQuery(const String& table, const String& time_column, double max_age)
|
|
|
|
{
|
2018-10-30 13:17:28 +01:00
|
|
|
if (IsPaused())
|
|
|
|
return;
|
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_QueryQueue.Enqueue(std::bind(&IdoPgsqlConnection::InternalCleanUpExecuteQuery, this, table, time_column, max_age), PriorityLow, true);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::InternalCleanUpExecuteQuery(const String& table, const String& time_column, double max_age)
|
|
|
|
{
|
2015-04-04 01:03:39 +02:00
|
|
|
AssertOnWorkQueue();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2020-09-09 18:17:37 +02:00
|
|
|
if (!GetConnected()) {
|
|
|
|
DecreasePendingQueries(1);
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
2020-09-09 18:17:37 +02:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
Query("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 +
|
2018-09-17 09:51:20 +02:00
|
|
|
" < TO_TIMESTAMP(" + Convert::ToString(static_cast<long>(max_age)) + ") AT TIME ZONE 'UTC'");
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
2014-01-31 08:28:00 +01:00
|
|
|
|
|
|
|
void IdoPgsqlConnection::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";
|
2020-09-09 18:17:37 +02:00
|
|
|
IncreasePendingQueries(1);
|
2014-01-31 08:28:00 +01:00
|
|
|
IdoPgsqlResult result = Query(query);
|
|
|
|
|
|
|
|
Dictionary::Ptr row;
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
while ((row = FetchRow(result, index))) {
|
|
|
|
index++;
|
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 IdoPgsqlConnection::GetPendingQueryCount() const
|
2015-03-13 12:00:30 +01:00
|
|
|
{
|
|
|
|
return m_QueryQueue.GetLength();
|
|
|
|
}
|