2013-10-30 15:32:33 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2013-10-30 15:32:33 +01: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 11:04:42 +01:00
|
|
|
#include "db_ido_pgsql/idopgsqlconnection.hpp"
|
|
|
|
#include "db_ido_pgsql/idopgsqlconnection.tcpp"
|
2014-09-17 15:38:39 +02:00
|
|
|
#include "db_ido/dbtype.hpp"
|
|
|
|
#include "db_ido/dbvalue.hpp"
|
|
|
|
#include "icinga/perfdatavalue.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"
|
|
|
|
#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"
|
2013-10-30 15:32:33 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(IdoPgsqlConnection);
|
|
|
|
|
2014-02-17 16:34:18 +01:00
|
|
|
REGISTER_STATSFUNCTION(IdoPgsqlConnectionStats, &IdoPgsqlConnection::StatsFunc);
|
|
|
|
|
2015-01-09 11:23:29 +01:00
|
|
|
IdoPgsqlConnection::IdoPgsqlConnection(void)
|
2015-04-04 01:03:39 +02:00
|
|
|
: m_QueryQueue(500000)
|
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
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr nodes = new Dictionary();
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
BOOST_FOREACH(const IdoPgsqlConnection::Ptr& idopgsqlconnection, ConfigType::GetObjectsByType<IdoPgsqlConnection>()) {
|
2014-02-18 10:53:44 +01:00
|
|
|
size_t items = idopgsqlconnection->m_QueryQueue.GetLength();
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr stats = new Dictionary();
|
2015-03-12 09:39:53 +01:00
|
|
|
stats->Set("version", idopgsqlconnection->GetSchemaVersion());
|
2014-02-18 10:53:44 +01:00
|
|
|
stats->Set("instance_name", idopgsqlconnection->GetInstanceName());
|
|
|
|
stats->Set("query_queue_items", items);
|
|
|
|
|
|
|
|
nodes->Set(idopgsqlconnection->GetName(), stats);
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue("idopgsqlconnection_" + idopgsqlconnection->GetName() + "_query_queue_items", items));
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status->Set("idopgsqlconnection", nodes);
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-05-09 13:02:30 +02:00
|
|
|
void IdoPgsqlConnection::Resume(void)
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
2014-05-09 13:02:30 +02:00
|
|
|
DbConnection::Resume();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2013-11-05 13:59:30 +01:00
|
|
|
m_QueryQueue.SetExceptionCallback(boost::bind(&IdoPgsqlConnection::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);
|
2013-10-30 15:32:33 +01:00
|
|
|
m_TxTimer->OnTimerExpired.connect(boost::bind(&IdoPgsqlConnection::TxTimerHandler, this));
|
|
|
|
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);
|
|
|
|
m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&IdoPgsqlConnection::ReconnectTimerHandler, this));
|
|
|
|
m_ReconnectTimer->Start();
|
|
|
|
m_ReconnectTimer->Reschedule(0);
|
|
|
|
|
|
|
|
ASSERT(PQisthreadsafe());
|
|
|
|
}
|
|
|
|
|
2014-05-09 13:02:30 +02:00
|
|
|
void IdoPgsqlConnection::Pause(void)
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
2014-05-09 13:02:30 +02:00
|
|
|
m_ReconnectTimer.reset();
|
|
|
|
|
2014-07-08 13:48:11 +02:00
|
|
|
DbConnection::Pause();
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::Disconnect, this));
|
|
|
|
m_QueryQueue.Join();
|
|
|
|
}
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
void IdoPgsqlConnection::ExceptionHandler(boost::exception_ptr exp)
|
|
|
|
{
|
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")
|
|
|
|
<< "Exception during database operation: " << DiagnosticInformation(exp);
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
if (GetConnected()) {
|
2013-11-05 12:40:25 +01:00
|
|
|
PQfinish(m_Connection);
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2013-11-05 12:40:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
void IdoPgsqlConnection::AssertOnWorkQueue(void)
|
|
|
|
{
|
2014-12-18 15:11:57 +01:00
|
|
|
ASSERT(m_QueryQueue.IsWorkerThread());
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::Disconnect(void)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
if (!GetConnected())
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
Query("COMMIT");
|
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
PQfinish(m_Connection);
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::TxTimerHandler(void)
|
|
|
|
{
|
2015-02-04 15:25:10 +01:00
|
|
|
NewTransaction();
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::NewTransaction(void)
|
2015-02-04 15:25:10 +01:00
|
|
|
{
|
2015-02-17 12:40:19 +01:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::InternalNewTransaction, this), true);
|
2015-02-04 15:25:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::InternalNewTransaction(void)
|
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;
|
|
|
|
|
|
|
|
Query("COMMIT");
|
|
|
|
Query("BEGIN");
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::ReconnectTimerHandler(void)
|
|
|
|
{
|
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::Reconnect, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::Reconnect(void)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2013-11-19 07:49:41 +01:00
|
|
|
CONTEXT("Reconnecting to PostgreSQL IDO database '" + GetName() + "'");
|
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
SetShouldConnect(true);
|
|
|
|
|
2014-02-12 14:49:39 +01:00
|
|
|
std::vector<DbObject::Ptr> active_dbobjs;
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
bool reconnect = false;
|
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
if (GetConnected()) {
|
2013-10-30 15:32:33 +01:00
|
|
|
/* Check if we're really still connected */
|
|
|
|
try {
|
|
|
|
Query("SELECT 1");
|
|
|
|
return;
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
PQfinish(m_Connection);
|
2015-04-04 01:03:39 +02:00
|
|
|
SetConnected(false);
|
2013-10-30 15:32:33 +01:00
|
|
|
reconnect = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 09:32:30 +01:00
|
|
|
ClearIDCache();
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
String ihost, iport, iuser, ipasswd, idb;
|
|
|
|
const char *host, *port, *user , *passwd, *db;
|
|
|
|
|
|
|
|
ihost = GetHost();
|
|
|
|
iport = GetPort();
|
|
|
|
iuser = GetUser();
|
|
|
|
ipasswd = GetPassword();
|
|
|
|
idb = GetDatabase();
|
|
|
|
|
|
|
|
host = (!ihost.IsEmpty()) ? ihost.CStr() : NULL;
|
|
|
|
port = (!iport.IsEmpty()) ? iport.CStr() : NULL;
|
|
|
|
user = (!iuser.IsEmpty()) ? iuser.CStr() : NULL;
|
|
|
|
passwd = (!ipasswd.IsEmpty()) ? ipasswd.CStr() : NULL;
|
|
|
|
db = (!idb.IsEmpty()) ? idb.CStr() : NULL;
|
|
|
|
|
|
|
|
m_Connection = PQsetdbLogin(host, port, NULL, NULL, db, user, passwd);
|
|
|
|
|
|
|
|
if (!m_Connection)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (PQstatus(m_Connection) != CONNECTION_OK) {
|
|
|
|
String message = PQerrorMessage(m_Connection);
|
|
|
|
PQfinish(m_Connection);
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection")
|
|
|
|
<< "Connection to database '" << db << "' with user '" << user << "' on '" << host << ":" << port
|
2014-06-05 18:26:38 +02:00
|
|
|
<< "' failed: \"" << message << "\"";
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error(message));
|
|
|
|
}
|
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
SetConnected(true);
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
String dbVersionName = "idoutils";
|
2014-06-25 09:47:20 +02:00
|
|
|
IdoPgsqlResult result = Query("SELECT version FROM " + GetTablePrefix() + "dbversion WHERE name=E'" + Escape(dbVersionName) + "'");
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-08-13 20:30:28 +02:00
|
|
|
Dictionary::Ptr row = FetchRow(result, 0);
|
2014-01-28 11:42:12 +01:00
|
|
|
|
2014-08-13 20:30:28 +02:00
|
|
|
if (!row) {
|
2015-03-05 15:57:46 +01:00
|
|
|
PQfinish(m_Connection);
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2015-03-05 15:57:46 +01:00
|
|
|
|
2014-06-05 18:26:38 +02:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection", "Schema does not provide any valid version! Verify your schema installation.");
|
2014-08-06 10:47:00 +02:00
|
|
|
|
2015-03-12 11:43:04 +01:00
|
|
|
Application::Exit(EXIT_FAILURE);
|
2014-06-05 18:26:38 +02:00
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-08-13 20:30:28 +02:00
|
|
|
String version = row->Get("version");
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
SetSchemaVersion(version);
|
|
|
|
|
2015-03-12 09:35:26 +01:00
|
|
|
if (Utility::CompareVersion(IDO_COMPAT_SCHEMA_VERSION, version) < 0) {
|
2015-03-05 15:57:46 +01:00
|
|
|
PQfinish(m_Connection);
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2015-03-05 15:57:46 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection")
|
|
|
|
<< "Schema version '" << version << "' does not match the required version '"
|
2015-03-12 09:35:26 +01:00
|
|
|
<< IDO_COMPAT_SCHEMA_VERSION << "' (or newer)! Please check the upgrade documentation.";
|
2014-06-05 18:26:38 +02:00
|
|
|
|
2015-03-12 11:43:04 +01:00
|
|
|
Application::Exit(EXIT_FAILURE);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String instanceName = GetInstanceName();
|
|
|
|
|
2014-06-25 09:47:20 +02:00
|
|
|
result = Query("SELECT instance_id FROM " + GetTablePrefix() + "instances WHERE instance_name = E'" + Escape(instanceName) + "'");
|
2014-08-13 20:30:28 +02:00
|
|
|
row = FetchRow(result, 0);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
if (!row) {
|
2014-06-25 09:47:20 +02:00
|
|
|
Query("INSERT INTO " + GetTablePrefix() + "instances (instance_name, instance_description) VALUES (E'" + Escape(instanceName) + "', E'" + Escape(GetInstanceDescription()) + "')");
|
2013-10-30 15:32:33 +01:00
|
|
|
m_InstanceID = GetSequenceValue(GetTablePrefix() + "instances", "instance_id");
|
|
|
|
} else {
|
|
|
|
m_InstanceID = DbReference(row->Get("instance_id"));
|
|
|
|
}
|
|
|
|
|
2014-08-13 20:30:28 +02:00
|
|
|
Endpoint::Ptr my_endpoint = Endpoint::GetLocalEndpoint();
|
|
|
|
|
|
|
|
/* 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 " +
|
|
|
|
GetTablePrefix() + "programstatus WHERE instance_id = " + Convert::ToString(m_InstanceID));
|
|
|
|
row = FetchRow(result, 0);
|
|
|
|
|
|
|
|
String endpoint_name;
|
|
|
|
|
|
|
|
if (row)
|
|
|
|
endpoint_name = row->Get("endpoint_name");
|
|
|
|
else
|
|
|
|
Log(LogNotice, "IdoPgsqlConnection", "Empty program status table");
|
|
|
|
|
|
|
|
/* if we did not write into the database earlier, another instance is active */
|
|
|
|
if (endpoint_name != my_endpoint->GetName()) {
|
|
|
|
double status_update_time;
|
|
|
|
|
|
|
|
if (row)
|
|
|
|
status_update_time = row->Get("status_update_time");
|
|
|
|
else
|
|
|
|
status_update_time = 0;
|
|
|
|
|
|
|
|
double status_update_age = Utility::GetTime() - status_update_time;
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "IdoPgsqlConnection")
|
|
|
|
<< "Last update by '" << endpoint_name << "' was " << status_update_age << "s ago.";
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2014-08-15 17:13:32 +02:00
|
|
|
if (status_update_age < GetFailoverTimeout()) {
|
2014-08-13 20:30:28 +02:00
|
|
|
PQfinish(m_Connection);
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2015-04-04 01:03:39 +02:00
|
|
|
SetShouldConnect(false);
|
2014-08-13 20:30:28 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* activate the IDO only, if we're authoritative in this zone */
|
|
|
|
if (IsPaused()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "IdoPgsqlConnection")
|
|
|
|
<< "Local endpoint '" << my_endpoint->GetName() << "' is not authoritative, bailing out.";
|
2014-08-13 20:30:28 +02:00
|
|
|
|
|
|
|
PQfinish(m_Connection);
|
2015-03-12 09:39:53 +01:00
|
|
|
SetConnected(false);
|
2014-08-13 20:30:28 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(LogNotice, "IdoPgsqlConnection", "Enabling IDO connection.");
|
|
|
|
}
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "IdoPgsqlConnection")
|
|
|
|
<< "pgSQL IDO instance id: " << static_cast<long>(m_InstanceID) << " (schema version: '" + version + "')";
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
/* record connection */
|
|
|
|
Query("INSERT INTO " + GetTablePrefix() + "conninfo " +
|
|
|
|
"(instance_id, connect_time, last_checkin_time, agent_name, agent_version, connect_type, data_start_time) VALUES ("
|
2014-06-25 09:47:20 +02:00
|
|
|
+ Convert::ToString(static_cast<long>(m_InstanceID)) + ", NOW(), NOW(), E'icinga2 db_ido_pgsql', E'" + Escape(Application::GetVersion())
|
|
|
|
+ "', E'" + (reconnect ? "RECONNECT" : "INITIAL") + "', NOW())");
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
/* clear config tables for the initial config dump */
|
2014-01-31 08:28:00 +01:00
|
|
|
PrepareDatabase();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
std::ostringstream q1buf;
|
2013-11-22 10:13:42 +01:00
|
|
|
q1buf << "SELECT object_id, objecttype_id, name1, name2, is_active FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast<long>(m_InstanceID);
|
2014-01-28 11:42:12 +01:00
|
|
|
result = Query(q1buf.str());
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
while ((row = FetchRow(result, index))) {
|
|
|
|
index++;
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
DbType::Ptr dbtype = DbType::GetByID(row->Get("objecttype_id"));
|
|
|
|
|
|
|
|
if (!dbtype)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2"));
|
|
|
|
SetObjectID(dbobj, DbReference(row->Get("object_id")));
|
2013-11-22 10:13:42 +01:00
|
|
|
SetObjectActive(dbobj, row->Get("is_active"));
|
2014-02-12 14:49:39 +01:00
|
|
|
|
|
|
|
if (GetObjectActive(dbobj))
|
|
|
|
active_dbobjs.push_back(dbobj);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Query("BEGIN");
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateAllObjects();
|
2014-02-12 14:49:39 +01:00
|
|
|
|
|
|
|
/* deactivate all deleted configuration objects */
|
|
|
|
BOOST_FOREACH(const DbObject::Ptr& dbobj, active_dbobjs) {
|
|
|
|
if (dbobj->GetObject() == NULL) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "IdoPgsqlConnection")
|
|
|
|
<< "Deactivate deleted object name1: '" << dbobj->GetName1()
|
|
|
|
<< "' name2: '" << dbobj->GetName2() + "'.";
|
2014-02-12 14:49:39 +01:00
|
|
|
DeactivateObject(dbobj);
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::ClearConfigTable(const String& table)
|
|
|
|
{
|
|
|
|
Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " + Convert::ToString(static_cast<long>(m_InstanceID)));
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
IdoPgsqlResult IdoPgsqlConnection::Query(const String& query)
|
2013-10-30 15:32:33 +01:00
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "IdoPgsqlConnection")
|
|
|
|
<< "Query: " << query;
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
IncreaseQueryCount();
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
PGresult *result = PQexec(m_Connection, query.CStr());
|
|
|
|
|
2014-06-05 18:26:38 +02:00
|
|
|
if (!result) {
|
|
|
|
String message = PQerrorMessage(m_Connection);
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection")
|
|
|
|
<< "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(
|
|
|
|
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
|
|
|
|
2014-02-10 10:25:12 +01:00
|
|
|
char *rowCount = PQcmdTuples(result);
|
|
|
|
m_AffectedRows = atoi(rowCount);
|
|
|
|
|
2014-09-12 17:51:59 +02:00
|
|
|
if (PQresultStatus(result) == PGRES_COMMAND_OK) {
|
|
|
|
PQclear(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
|
|
|
|
|
|
|
if (PQresultStatus(result) != PGRES_TUPLES_OK) {
|
|
|
|
String message = PQresultErrorMessage(result);
|
|
|
|
PQclear(result);
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "IdoPgsqlConnection")
|
|
|
|
<< "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(
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
return IdoPgsqlResult(result, std::ptr_fun(PQclear));
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DbReference IdoPgsqlConnection::GetSequenceValue(const String& table, const String& column)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
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")
|
|
|
|
<< "Sequence Value: " << row->Get("id");
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
return DbReference(Convert::ToLong(row->Get("id")));
|
|
|
|
}
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
int IdoPgsqlConnection::GetAffectedRows(void)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
return m_AffectedRows;
|
|
|
|
}
|
|
|
|
|
2013-10-30 15:32:33 +01:00
|
|
|
String IdoPgsqlConnection::Escape(const String& s)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
size_t length = s.GetLength();
|
2013-10-30 15:32:33 +01:00
|
|
|
char *to = new char[s.GetLength() * 2 + 1];
|
|
|
|
|
|
|
|
PQescapeStringConn(m_Connection, to, s.CStr(), length, NULL);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
if (row >= PQntuples(result.get()))
|
2013-10-30 15:32:33 +01:00
|
|
|
return Dictionary::Ptr();
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
int columns = PQnfields(result.get());
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr dict = new Dictionary();
|
2013-10-30 15:32:33 +01:00
|
|
|
|
|
|
|
for (int column = 0; column < columns; column++) {
|
|
|
|
Value value;
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
if (!PQgetisnull(result.get(), row, column))
|
|
|
|
value = PQgetvalue(result.get(), row, column);
|
2013-10-30 15:32:33 +01:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
dict->Set(PQfname(result.get(), column), value);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::ActivateObject(const DbObject::Ptr& dbobj)
|
|
|
|
{
|
2015-04-04 01:03:39 +02:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::InternalActivateObject, this, dbobj));
|
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 ("
|
|
|
|
<< static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", "
|
|
|
|
<< "E'" << Escape(dbobj->GetName1()) << "', E'" << Escape(dbobj->GetName2()) << "', 1)";
|
|
|
|
} else {
|
|
|
|
qbuf << "INSERT INTO " + GetTablePrefix() + "objects (instance_id, objecttype_id, name1, is_active) VALUES ("
|
|
|
|
<< static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", "
|
|
|
|
<< "E'" << Escape(dbobj->GetName1()) << "', 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);
|
|
|
|
Query(qbuf.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::DeactivateObject(const DbObject::Ptr& dbobj)
|
|
|
|
{
|
2015-04-04 01:03:39 +02:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::InternalDeactivateObject, this, dbobj));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (key == "notification_id") {
|
2014-01-29 10:30:33 +01:00
|
|
|
*result = static_cast<long>(GetNotificationInsertID(value));
|
2013-10-30 15:32:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
DbReference dbrefcol;
|
|
|
|
|
|
|
|
if (DbValue::IsObjectInsertID(value)) {
|
|
|
|
dbrefcol = GetInsertID(dbobjcol);
|
|
|
|
|
|
|
|
ASSERT(dbrefcol.IsValid());
|
|
|
|
} 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;
|
|
|
|
msgbuf << "TO_TIMESTAMP(" << ts << ")";
|
|
|
|
*result = Value(msgbuf.str());
|
|
|
|
} else if (DbValue::IsTimestampNow(value)) {
|
|
|
|
*result = "NOW()";
|
|
|
|
} 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)
|
|
|
|
{
|
2013-11-05 12:40:25 +01:00
|
|
|
ASSERT(query.Category != DbCatInvalid);
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::InternalExecuteQuery, this, query, (DbQueryType *)NULL), true);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
void IdoPgsqlConnection::InternalExecuteQuery(const DbQuery& query, DbQueryType *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
|
|
|
|
|
|
|
if ((query.Category & GetCategories()) == 0)
|
|
|
|
return;
|
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
if (!GetConnected())
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
|
|
|
|
2015-01-27 15:11:45 +01:00
|
|
|
if (query.Object && query.Object->GetObject()->GetExtension("agent_check").ToBool())
|
|
|
|
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;
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, query.WhereCriteria) {
|
|
|
|
if (!FieldToEscapedString(kv.first, kv.second, &value))
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
type = typeOverride ? *typeOverride : query.Type;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
ASSERT(query.Object);
|
|
|
|
|
|
|
|
if (query.ConfigUpdate)
|
|
|
|
hasid = GetConfigUpdate(query.Object);
|
|
|
|
else if (query.StatusUpdate)
|
|
|
|
hasid = GetStatusUpdate(query.Object);
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
if (!hasid)
|
|
|
|
upsert = true;
|
|
|
|
|
|
|
|
type = DbQueryUpdate;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
ObjectLock olock(query.Fields);
|
|
|
|
|
|
|
|
Value value;
|
|
|
|
bool first = true;
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(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;
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
if (!FieldToEscapedString(kv.first, kv.second, &value))
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
DbQueryType to = DbQueryInsert;
|
|
|
|
InternalExecuteQuery(query, &to);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (type == DbQueryInsert && query.Table == "notifications" && query.NotificationObject) { // FIXME remove hardcoded table name
|
|
|
|
String idField = "notification_id";
|
2014-10-19 17:52:17 +02:00
|
|
|
DbReference seqval = GetSequenceValue(GetTablePrefix() + query.Table, idField);
|
|
|
|
SetNotificationInsertID(query.NotificationObject, seqval);
|
|
|
|
Log(LogDebug, "IdoPgsqlConnection")
|
|
|
|
<< "saving contactnotification notification_id=" << Convert::ToString(seqval);
|
2013-10-30 15:32:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoPgsqlConnection::CleanUpExecuteQuery(const String& table, const String& time_column, double max_age)
|
|
|
|
{
|
2013-11-28 10:36:43 +01:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoPgsqlConnection::InternalCleanUpExecuteQuery, this, table, time_column, max_age), 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
|
|
|
|
2015-04-04 01:03:39 +02:00
|
|
|
if (!GetConnected())
|
2013-10-30 15:32:33 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " +
|
|
|
|
Convert::ToString(static_cast<long>(m_InstanceID)) + " AND " + time_column +
|
|
|
|
" < TO_TIMESTAMP(" + Convert::ToString(static_cast<long>(max_age)) + ")");
|
|
|
|
}
|
2014-01-31 08:28:00 +01:00
|
|
|
|
|
|
|
void IdoPgsqlConnection::FillIDCache(const DbType::Ptr& type)
|
|
|
|
{
|
|
|
|
String query = "SELECT " + type->GetIDColumn() + " AS object_id, " + type->GetTable() + "_id FROM " + GetTablePrefix() + type->GetTable() + "s";
|
|
|
|
IdoPgsqlResult result = Query(query);
|
|
|
|
|
|
|
|
Dictionary::Ptr row;
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
while ((row = FetchRow(result, index))) {
|
|
|
|
index++;
|
|
|
|
SetInsertID(type, DbReference(row->Get("object_id")), DbReference(row->Get(type->GetTable() + "_id")));
|
|
|
|
}
|
|
|
|
}
|
2015-03-13 12:00:30 +01:00
|
|
|
|
|
|
|
int IdoPgsqlConnection::GetPendingQueryCount(void) const
|
|
|
|
{
|
|
|
|
return m_QueryQueue.GetLength();
|
|
|
|
}
|