2013-07-05 09:37:04 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-01-09 00:32:11 +01:00
|
|
|
* Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
|
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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-07-17 11:29:51 +02:00
|
|
|
#include "base/logger_fwd.h"
|
|
|
|
#include "base/objectlock.h"
|
2013-07-17 14:10:28 +02:00
|
|
|
#include "base/convert.h"
|
2013-07-22 14:51:04 +02:00
|
|
|
#include "base/utility.h"
|
2013-10-09 12:35:11 +02:00
|
|
|
#include "base/application.h"
|
2013-10-26 09:41:45 +02:00
|
|
|
#include "base/dynamictype.h"
|
2013-11-05 12:40:25 +01:00
|
|
|
#include "base/exception.h"
|
2014-02-17 16:34:18 +01:00
|
|
|
#include "base/statsfunction.h"
|
2013-09-25 10:41:59 +02:00
|
|
|
#include "db_ido/dbtype.h"
|
|
|
|
#include "db_ido/dbvalue.h"
|
|
|
|
#include "db_ido_mysql/idomysqlconnection.h"
|
2013-07-17 11:29:51 +02:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-02-17 16:34:18 +01:00
|
|
|
#define SCHEMA_VERSION "1.11.0"
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
REGISTER_TYPE(IdoMysqlConnection);
|
2014-02-17 16:34:18 +01:00
|
|
|
REGISTER_STATSFUNCTION(IdoMysqlConnectionStats, &IdoMysqlConnection::StatsFunc);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2014-02-17 16:34:18 +01:00
|
|
|
Value IdoMysqlConnection::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
|
|
|
|
{
|
2014-02-18 10:53:44 +01:00
|
|
|
Dictionary::Ptr nodes = make_shared<Dictionary>();
|
|
|
|
|
|
|
|
BOOST_FOREACH(const IdoMysqlConnection::Ptr& idomysqlconnection, DynamicType::GetObjects<IdoMysqlConnection>()) {
|
|
|
|
size_t items = idomysqlconnection->m_QueryQueue.GetLength();
|
|
|
|
|
|
|
|
Dictionary::Ptr stats = make_shared<Dictionary>();
|
|
|
|
stats->Set("version", SCHEMA_VERSION);
|
|
|
|
stats->Set("instance_name", idomysqlconnection->GetInstanceName());
|
|
|
|
stats->Set("query_queue_items", items);
|
|
|
|
|
|
|
|
nodes->Set(idomysqlconnection->GetName(), stats);
|
|
|
|
|
|
|
|
perfdata->Set("idomysqlconnection_" + idomysqlconnection->GetName() + "_query_queue_items", items);
|
|
|
|
}
|
|
|
|
|
|
|
|
status->Set("idomysqlconnection", nodes);
|
2014-02-17 16:34:18 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-26 09:41:45 +02:00
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
void IdoMysqlConnection::Start(void)
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DbConnection::Start();
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
m_Connected = false;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-11-05 13:59:30 +01:00
|
|
|
m_QueryQueue.SetExceptionCallback(boost::bind(&IdoMysqlConnection::ExceptionHandler, this, _1));
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
m_TxTimer = make_shared<Timer>();
|
2013-07-17 14:10:28 +02:00
|
|
|
m_TxTimer->SetInterval(5);
|
2013-09-25 10:41:59 +02:00
|
|
|
m_TxTimer->OnTimerExpired.connect(boost::bind(&IdoMysqlConnection::TxTimerHandler, this));
|
2013-07-17 14:10:28 +02:00
|
|
|
m_TxTimer->Start();
|
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
m_ReconnectTimer = make_shared<Timer>();
|
2013-07-19 10:18:47 +02:00
|
|
|
m_ReconnectTimer->SetInterval(10);
|
2013-09-25 10:41:59 +02:00
|
|
|
m_ReconnectTimer->OnTimerExpired.connect(boost::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
|
|
|
|
|
|
|
ASSERT(mysql_thread_safe());
|
|
|
|
}
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
void IdoMysqlConnection::Stop(void)
|
2013-07-19 10:18:47 +02:00
|
|
|
{
|
2013-10-30 13:32:01 +01:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::Disconnect, this));
|
|
|
|
m_QueryQueue.Join();
|
|
|
|
}
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
void IdoMysqlConnection::ExceptionHandler(boost::exception_ptr exp)
|
|
|
|
{
|
2013-11-20 21:55:14 +01:00
|
|
|
Log(LogCritical, "db_ido_mysql", "Exception during database operation: " + DiagnosticInformation(exp));
|
2013-11-05 12:40:25 +01:00
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
|
|
|
|
|
|
|
if (m_Connected) {
|
|
|
|
mysql_close(&m_Connection);
|
|
|
|
|
|
|
|
m_Connected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-30 13:32:01 +01:00
|
|
|
void IdoMysqlConnection::AssertOnWorkQueue(void)
|
|
|
|
{
|
2013-10-30 14:07:26 +01:00
|
|
|
ASSERT(boost::this_thread::get_id() == m_QueryQueue.GetThreadId());
|
2013-10-30 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoMysqlConnection::Disconnect(void)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2013-07-19 10:18:47 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
2013-07-22 12:08:49 +02:00
|
|
|
|
|
|
|
if (!m_Connected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Query("COMMIT");
|
2013-07-19 10:18:47 +02:00
|
|
|
mysql_close(&m_Connection);
|
2013-09-12 11:37:58 +02:00
|
|
|
|
|
|
|
m_Connected = false;
|
2013-07-19 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
void IdoMysqlConnection::TxTimerHandler(void)
|
2013-10-30 13:32:01 +01:00
|
|
|
{
|
2013-11-28 10:36:43 +01:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::NewTransaction, this), true);
|
2013-10-30 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IdoMysqlConnection::NewTransaction(void)
|
2013-07-19 10:18:47 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
|
|
|
|
|
|
|
if (!m_Connected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Query("COMMIT");
|
|
|
|
Query("BEGIN");
|
|
|
|
}
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
void IdoMysqlConnection::ReconnectTimerHandler(void)
|
2013-07-19 10:18:47 +02:00
|
|
|
{
|
2013-10-30 13:32:01 +01:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::Reconnect, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoMysqlConnection::Reconnect(void)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2013-11-19 07:49:41 +01:00
|
|
|
CONTEXT("Reconnecting to MySQL IDO database '" + GetName() + "'");
|
|
|
|
|
2014-02-12 14:49:39 +01:00
|
|
|
std::vector<DbObject::Ptr> active_dbobjs;
|
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2013-10-01 14:01:59 +02:00
|
|
|
bool reconnect = false;
|
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
if (m_Connected) {
|
|
|
|
/* Check if we're really still connected */
|
|
|
|
if (mysql_ping(&m_Connection) == 0)
|
|
|
|
return;
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
mysql_close(&m_Connection);
|
|
|
|
m_Connected = false;
|
2013-10-01 14:01:59 +02:00
|
|
|
reconnect = true;
|
2013-07-19 10:34:46 +02:00
|
|
|
}
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2013-11-20 09:32:30 +01:00
|
|
|
ClearIDCache();
|
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
String ihost, iuser, ipasswd, idb;
|
|
|
|
const char *host, *user , *passwd, *db;
|
|
|
|
long port;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
ihost = GetHost();
|
|
|
|
iuser = GetUser();
|
|
|
|
ipasswd = GetPassword();
|
|
|
|
idb = GetDatabase();
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
host = (!ihost.IsEmpty()) ? ihost.CStr() : NULL;
|
2013-10-26 09:41:45 +02:00
|
|
|
port = GetPort();
|
2013-07-19 10:34:46 +02:00
|
|
|
user = (!iuser.IsEmpty()) ? iuser.CStr() : NULL;
|
|
|
|
passwd = (!ipasswd.IsEmpty()) ? ipasswd.CStr() : NULL;
|
|
|
|
db = (!idb.IsEmpty()) ? idb.CStr() : NULL;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
if (!mysql_init(&m_Connection))
|
|
|
|
BOOST_THROW_EXCEPTION(std::bad_alloc());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
if (!mysql_real_connect(&m_Connection, host, user, passwd, db, port, NULL, CLIENT_FOUND_ROWS))
|
2013-07-19 10:34:46 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error(mysql_error(&m_Connection)));
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
m_Connected = true;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-09-27 15:30:17 +02:00
|
|
|
String dbVersionName = "idoutils";
|
2014-01-28 11:42:12 +01:00
|
|
|
IdoMysqlResult result = Query("SELECT version FROM " + GetTablePrefix() + "dbversion WHERE name='" + Escape(dbVersionName) + "'");
|
2013-09-27 15:30:17 +02:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
Dictionary::Ptr version_row = FetchRow(result);
|
|
|
|
|
|
|
|
if (!version_row)
|
2013-09-27 15:30:17 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Schema does not provide any valid version! Verify your schema installation."));
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
DiscardRows(result);
|
|
|
|
|
2013-09-27 15:30:17 +02:00
|
|
|
String version = version_row->Get("version");
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
if (Utility::CompareVersion(SCHEMA_VERSION, version) < 0) {
|
2013-09-27 15:30:17 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Schema version '" + version + "' does not match the required version '" +
|
2013-10-26 09:41:45 +02:00
|
|
|
SCHEMA_VERSION + "'! Please check the upgrade documentation."));
|
2013-09-27 15:30:17 +02:00
|
|
|
}
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
String instanceName = GetInstanceName();
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
result = Query("SELECT instance_id FROM " + GetTablePrefix() + "instances WHERE instance_name = '" + Escape(instanceName) + "'");
|
|
|
|
|
|
|
|
Dictionary::Ptr row = FetchRow(result);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
if (!row) {
|
2013-10-26 09:41:45 +02:00
|
|
|
Query("INSERT INTO " + GetTablePrefix() + "instances (instance_name, instance_description) VALUES ('" + Escape(instanceName) + "', '" + Escape(GetInstanceDescription()) + "')");
|
2013-08-02 08:56:36 +02:00
|
|
|
m_InstanceID = GetLastInsertID();
|
2013-07-19 10:34:46 +02:00
|
|
|
} else {
|
2014-01-28 11:42:12 +01:00
|
|
|
DiscardRows(result);
|
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
m_InstanceID = DbReference(row->Get("instance_id"));
|
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
std::ostringstream msgbuf;
|
2013-09-27 15:30:17 +02:00
|
|
|
msgbuf << "MySQL IDO instance id: " << static_cast<long>(m_InstanceID) << " (schema version: '" + version + "')";
|
2013-09-26 15:22:21 +02:00
|
|
|
Log(LogInformation, "db_ido_mysql", msgbuf.str());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-10-01 14:01:59 +02:00
|
|
|
/* set session time zone to utc */
|
|
|
|
Query("SET SESSION TIME_ZONE='+00: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 ("
|
2013-10-14 14:11:27 +02:00
|
|
|
+ Convert::ToString(static_cast<long>(m_InstanceID)) + ", NOW(), NOW(), 'icinga2 db_ido_mysql', '" + Escape(Application::GetVersion())
|
|
|
|
+ "', '" + (reconnect ? "RECONNECT" : "INITIAL") + "', NOW())");
|
2013-10-01 14:01:59 +02:00
|
|
|
|
|
|
|
/* clear config tables for the initial config dump */
|
2014-01-31 08:28:00 +01:00
|
|
|
PrepareDatabase();
|
2013-08-02 17:11:14 +02:00
|
|
|
|
2013-07-23 11:02:47 +02: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());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
while ((row = FetchRow(result))) {
|
2013-07-19 10:34:46 +02:00
|
|
|
DbType::Ptr dbtype = DbType::GetByID(row->Get("objecttype_id"));
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
if (!dbtype)
|
|
|
|
continue;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
DbObject::Ptr dbobj = dbtype->GetOrCreateObjectByName(row->Get("name1"), row->Get("name2"));
|
2013-08-02 08:56:36 +02:00
|
|
|
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-07-19 10:34:46 +02:00
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
Query("BEGIN");
|
|
|
|
}
|
2013-07-17 14:10:28 +02:00
|
|
|
|
2013-07-17 11:29:51 +02:00
|
|
|
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) {
|
|
|
|
Log(LogDebug, "db_ido", "Deactivate deleted object name1: '" + Convert::ToString(dbobj->GetName1() +
|
|
|
|
"' name2: '" + Convert::ToString(dbobj->GetName2() + "'.")));
|
|
|
|
DeactivateObject(dbobj);
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
void IdoMysqlConnection::ClearConfigTable(const String& table)
|
2013-08-02 17:11:14 +02:00
|
|
|
{
|
|
|
|
Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " + Convert::ToString(static_cast<long>(m_InstanceID)));
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2013-09-26 15:22:21 +02:00
|
|
|
Log(LogDebug, "db_ido_mysql", "Query: " + query);
|
2013-07-17 14:10:28 +02:00
|
|
|
|
2013-07-17 11:29:51 +02:00
|
|
|
if (mysql_query(&m_Connection, query.CStr()) != 0)
|
2013-11-05 12:40:25 +01:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
database_error()
|
|
|
|
<< errinfo_message(mysql_error(&m_Connection))
|
|
|
|
<< errinfo_database_query(query)
|
|
|
|
);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
m_AffectedRows = mysql_affected_rows(&m_Connection);
|
|
|
|
|
2013-11-28 12:12:10 +01:00
|
|
|
MYSQL_RES *result = mysql_use_result(&m_Connection);
|
2013-07-17 11:29:51 +02:00
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
if (mysql_field_count(&m_Connection) > 0)
|
2013-11-05 12:40:25 +01:00
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
database_error()
|
|
|
|
<< errinfo_message(mysql_error(&m_Connection))
|
|
|
|
<< errinfo_database_query(query)
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
return IdoMysqlResult(result, std::ptr_fun(mysql_free_result));
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 10:41:59 +02:00
|
|
|
DbReference IdoMysqlConnection::GetLastInsertID(void)
|
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
|
|
|
return DbReference(mysql_insert_id(&m_Connection));
|
|
|
|
}
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
int IdoMysqlConnection::GetAffectedRows(void)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2013-11-03 13:45:26 +01:00
|
|
|
size_t length = s.GetLength();
|
2013-07-17 11:29:51 +02:00
|
|
|
char *to = new char[s.GetLength() * 2 + 1];
|
|
|
|
|
|
|
|
mysql_real_escape_string(&m_Connection, to, s.CStr(), length);
|
|
|
|
|
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;
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
row = mysql_fetch_row(result.get());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
|
|
|
if (!row)
|
|
|
|
return Dictionary::Ptr();
|
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
lengths = mysql_fetch_lengths(result.get());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
|
|
|
if (!lengths)
|
|
|
|
return Dictionary::Ptr();
|
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
Dictionary::Ptr dict = make_shared<Dictionary>();
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2014-01-28 11:42:12 +01:00
|
|
|
mysql_field_seek(result.get(), 0);
|
|
|
|
for (field = mysql_fetch_field(result.get()), i = 0; field; field = 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
|
|
|
{
|
2013-07-17 14:10:28 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
2013-07-24 10:55:04 +02:00
|
|
|
InternalActivateObject(dbobj);
|
|
|
|
}
|
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
|
|
|
{
|
2013-07-22 13:44:08 +02:00
|
|
|
if (!m_Connected)
|
|
|
|
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()) {
|
2013-08-01 09:46:48 +02:00
|
|
|
qbuf << "INSERT INTO " + GetTablePrefix() + "objects (instance_id, objecttype_id, name1, name2, is_active) VALUES ("
|
2013-07-22 12:08:49 +02:00
|
|
|
<< static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", "
|
|
|
|
<< "'" << Escape(dbobj->GetName1()) << "', '" << Escape(dbobj->GetName2()) << "', 1)";
|
|
|
|
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);
|
2013-07-22 12:08:49 +02:00
|
|
|
Query(qbuf.str());
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
2013-07-19 10:18:47 +02:00
|
|
|
|
2013-07-22 13:44:08 +02:00
|
|
|
if (!m_Connected)
|
|
|
|
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);
|
2013-07-22 12:08:49 +02:00
|
|
|
Query(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-07-24 10:55:04 +02:00
|
|
|
/* caller must hold m_ConnectionMutex */
|
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;
|
|
|
|
}
|
2013-10-01 12:45:09 +02:00
|
|
|
if (key == "notification_id") {
|
2014-01-29 10:30:33 +01:00
|
|
|
*result = static_cast<long>(GetNotificationInsertID(value));
|
2013-10-01 12:45:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-22 12:08:49 +02:00
|
|
|
|
2013-08-02 15:20:28 +02:00
|
|
|
Value rawvalue = DbValue::ExtractValue(value);
|
|
|
|
|
|
|
|
if (rawvalue.IsObjectType<DynamicObject>()) {
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2013-08-02 15:20:28 +02:00
|
|
|
ASSERT(dbrefcol.IsValid());
|
|
|
|
} 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());
|
|
|
|
} else if (DbValue::IsTimestampNow(value)) {
|
|
|
|
*result = "NOW()";
|
|
|
|
} else {
|
2013-08-02 15:20:28 +02:00
|
|
|
*result = "'" + Escape(rawvalue) + "'";
|
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
|
|
|
{
|
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(&IdoMysqlConnection::InternalExecuteQuery, this, query, (DbQueryType *)NULL), true);
|
2013-10-30 13:32:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
void IdoMysqlConnection::InternalExecuteQuery(const DbQuery& query, DbQueryType *typeOverride)
|
2013-07-22 12:08:49 +02:00
|
|
|
{
|
2013-07-23 11:02:47 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
|
|
|
|
2013-10-29 16:08:52 +01:00
|
|
|
if ((query.Category & GetCategories()) == 0)
|
|
|
|
return;
|
|
|
|
|
2013-07-23 11:02:47 +02:00
|
|
|
if (!m_Connected)
|
|
|
|
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
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, query.WhereCriteria) {
|
|
|
|
if (!FieldToEscapedString(kv.first, kv.second, &value))
|
2013-08-02 08:56:36 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
type = typeOverride ? *typeOverride : query.Type;
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-08-02 10:11:48 +02:00
|
|
|
ASSERT(query.Object);
|
2013-08-02 08:56:36 +02:00
|
|
|
|
2013-08-02 15:45:50 +02:00
|
|
|
if (query.ConfigUpdate)
|
|
|
|
hasid = GetConfigUpdate(query.Object);
|
|
|
|
else if (query.StatusUpdate)
|
|
|
|
hasid = GetStatusUpdate(query.Object);
|
|
|
|
else
|
|
|
|
ASSERT(!"Invalid query flags.");
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
if (!hasid)
|
|
|
|
upsert = true;
|
|
|
|
|
|
|
|
type = DbQueryUpdate;
|
|
|
|
}
|
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:
|
|
|
|
ASSERT(!"Invalid query type.");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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;
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, query.Fields) {
|
|
|
|
Value value;
|
|
|
|
|
|
|
|
if (!FieldToEscapedString(kv.first, kv.second, &value))
|
2013-07-22 12:08:49 +02:00
|
|
|
return;
|
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
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
Query(qbuf.str());
|
2013-08-02 08:56:36 +02:00
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
if (upsert && GetAffectedRows() == 0) {
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
DbQueryType to = DbQueryInsert;
|
|
|
|
InternalExecuteQuery(query, &to);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-02 15:45:50 +02:00
|
|
|
if (query.Object) {
|
|
|
|
if (query.ConfigUpdate)
|
|
|
|
SetConfigUpdate(query.Object, true);
|
|
|
|
else if (query.StatusUpdate)
|
|
|
|
SetStatusUpdate(query.Object, true);
|
|
|
|
|
|
|
|
if (type == DbQueryInsert && query.ConfigUpdate)
|
|
|
|
SetInsertID(query.Object, GetLastInsertID());
|
|
|
|
}
|
2013-11-20 09:32:30 +01:00
|
|
|
|
2014-01-29 10:30:33 +01:00
|
|
|
if (type == DbQueryInsert && query.Table == "notifications" && query.NotificationObject) { // FIXME remove hardcoded table name
|
|
|
|
SetNotificationInsertID(query.NotificationObject, GetLastInsertID());
|
|
|
|
Log(LogDebug, "db_ido", "saving contactnotification notification_id=" + Convert::ToString(static_cast<long>(GetLastInsertID())));
|
2013-10-01 12:45:09 +02:00
|
|
|
}
|
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
|
|
|
{
|
2013-11-28 10:36:43 +01:00
|
|
|
m_QueryQueue.Enqueue(boost::bind(&IdoMysqlConnection::InternalCleanUpExecuteQuery, this, table, time_column, max_age), 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
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
|
|
|
|
|
|
|
if (!m_Connected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Query("DELETE FROM " + GetTablePrefix() + table + " WHERE instance_id = " +
|
2013-10-30 14:07:26 +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)
|
|
|
|
{
|
|
|
|
String query = "SELECT " + type->GetIDColumn() + " AS object_id, " + type->GetTable() + "_id FROM " + GetTablePrefix() + type->GetTable() + "s";
|
|
|
|
IdoMysqlResult result = Query(query);
|
|
|
|
|
|
|
|
Dictionary::Ptr row;
|
|
|
|
|
|
|
|
while ((row = FetchRow(result))) {
|
|
|
|
SetInsertID(type, DbReference(row->Get("object_id")), DbReference(row->Get(type->GetTable() + "_id")));
|
|
|
|
}
|
|
|
|
}
|