2013-07-05 09:37:04 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* 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-07-17 11:29:51 +02:00
|
|
|
#include "ido/dbtype.h"
|
2013-07-23 11:02:47 +02:00
|
|
|
#include "ido/dbvalue.h"
|
2013-08-16 11:15:05 +02:00
|
|
|
#include "ido_mysql/idomysqldbconnection.h"
|
2013-07-17 11:29:51 +02:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-08-16 11:15:05 +02:00
|
|
|
REGISTER_TYPE(IdoMysqlDbConnection);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void IdoMysqlDbConnection::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-07-17 14:10:28 +02:00
|
|
|
m_TxTimer = boost::make_shared<Timer>();
|
|
|
|
m_TxTimer->SetInterval(5);
|
2013-08-16 11:15:05 +02:00
|
|
|
m_TxTimer->OnTimerExpired.connect(boost::bind(&IdoMysqlDbConnection::TxTimerHandler, this));
|
2013-07-17 14:10:28 +02:00
|
|
|
m_TxTimer->Start();
|
|
|
|
|
2013-07-19 10:18:47 +02:00
|
|
|
m_ReconnectTimer = boost::make_shared<Timer>();
|
|
|
|
m_ReconnectTimer->SetInterval(10);
|
2013-08-16 11:15:05 +02:00
|
|
|
m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&IdoMysqlDbConnection::ReconnectTimerHandler, this));
|
2013-07-19 10:18:47 +02:00
|
|
|
m_ReconnectTimer->Start();
|
|
|
|
|
|
|
|
ASSERT(mysql_thread_safe());
|
|
|
|
}
|
|
|
|
|
2013-08-16 11:15:05 +02:00
|
|
|
void IdoMysqlDbConnection::Stop(void)
|
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-08-16 11:15:05 +02:00
|
|
|
void IdoMysqlDbConnection::TxTimerHandler(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-08-16 11:15:05 +02:00
|
|
|
void IdoMysqlDbConnection::ReconnectTimerHandler(void)
|
2013-07-19 10:18:47 +02:00
|
|
|
{
|
2013-07-19 10:34:46 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
2013-07-19 10:18:47 +02:00
|
|
|
|
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-07-19 10:18:47 +02:00
|
|
|
|
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-07-19 10:34:46 +02:00
|
|
|
ihost = m_Host;
|
|
|
|
iuser = m_User;
|
|
|
|
ipasswd = m_Password;
|
|
|
|
idb = m_Database;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
host = (!ihost.IsEmpty()) ? ihost.CStr() : NULL;
|
|
|
|
port = m_Port;
|
|
|
|
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
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
if (!mysql_real_connect(&m_Connection, host, user, passwd, db, port, NULL, 0))
|
|
|
|
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-07-19 10:34:46 +02:00
|
|
|
String instanceName = "default";
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
if (!m_InstanceName.IsEmpty())
|
|
|
|
instanceName = m_InstanceName;
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-08-01 09:46:48 +02:00
|
|
|
Array::Ptr rows = Query("SELECT instance_id FROM " + GetTablePrefix() + "instances WHERE instance_name = '" + Escape(instanceName) + "'");
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
if (rows->GetLength() == 0) {
|
2013-08-01 09:46:48 +02:00
|
|
|
Query("INSERT INTO " + GetTablePrefix() + "instances (instance_name, instance_description) VALUES ('" + Escape(instanceName) + "', '" + m_InstanceDescription + "')");
|
2013-08-02 08:56:36 +02:00
|
|
|
m_InstanceID = GetLastInsertID();
|
2013-07-19 10:34:46 +02:00
|
|
|
} else {
|
|
|
|
Dictionary::Ptr row = rows->Get(0);
|
|
|
|
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;
|
|
|
|
msgbuf << "MySQL IDO instance id: " << static_cast<long>(m_InstanceID);
|
|
|
|
Log(LogInformation, "ido_mysql", msgbuf.str());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-08-02 17:11:14 +02:00
|
|
|
ClearConfigTables();
|
|
|
|
|
2013-08-01 09:46:48 +02:00
|
|
|
Query("UPDATE " + GetTablePrefix() + "objects SET is_active = 0");
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-23 11:02:47 +02:00
|
|
|
std::ostringstream q1buf;
|
2013-08-01 09:46:48 +02:00
|
|
|
q1buf << "SELECT object_id, objecttype_id, name1, name2 FROM " + GetTablePrefix() + "objects WHERE instance_id = " << static_cast<long>(m_InstanceID);
|
2013-07-23 11:02:47 +02:00
|
|
|
rows = Query(q1buf.str());
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-19 10:34:46 +02:00
|
|
|
ObjectLock olock(rows);
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& row, rows) {
|
|
|
|
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-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();
|
|
|
|
}
|
|
|
|
|
2013-08-16 11:15:05 +02:00
|
|
|
void IdoMysqlDbConnection::ClearConfigTables(void)
|
2013-08-02 17:11:14 +02:00
|
|
|
{
|
|
|
|
/* TODO make hardcoded table names modular */
|
|
|
|
ClearConfigTable("commands");
|
|
|
|
ClearConfigTable("contact_addresses");
|
|
|
|
ClearConfigTable("contact_notificationcommands");
|
|
|
|
ClearConfigTable("contactgroup_members");
|
|
|
|
ClearConfigTable("contactgroups");
|
|
|
|
ClearConfigTable("contacts");
|
|
|
|
ClearConfigTable("customvariables");
|
|
|
|
ClearConfigTable("host_contactgroups");
|
|
|
|
ClearConfigTable("host_contacts");
|
|
|
|
ClearConfigTable("host_parenthosts");
|
|
|
|
ClearConfigTable("hostdependencies");
|
|
|
|
ClearConfigTable("hostgroup_members");
|
|
|
|
ClearConfigTable("hostgroups");
|
|
|
|
ClearConfigTable("hosts");
|
|
|
|
ClearConfigTable("service_contactgroups");
|
|
|
|
ClearConfigTable("service_contacts");
|
|
|
|
ClearConfigTable("servicedependencies");
|
|
|
|
ClearConfigTable("servicegroup_members");
|
|
|
|
ClearConfigTable("servicegroups");
|
|
|
|
ClearConfigTable("services");
|
|
|
|
ClearConfigTable("timeperiod_timeranges");
|
|
|
|
ClearConfigTable("timeperiods");
|
|
|
|
}
|
|
|
|
|
2013-08-16 11:15:05 +02:00
|
|
|
void IdoMysqlDbConnection::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)));
|
|
|
|
}
|
|
|
|
|
2013-08-16 11:15:05 +02:00
|
|
|
Array::Ptr IdoMysqlDbConnection::Query(const String& query)
|
2013-07-17 11:29:51 +02:00
|
|
|
{
|
2013-07-17 14:10:28 +02:00
|
|
|
Log(LogDebug, "ido_mysql", "Query: " + query);
|
|
|
|
|
2013-07-17 11:29:51 +02:00
|
|
|
if (mysql_query(&m_Connection, query.CStr()) != 0)
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error(mysql_error(&m_Connection)));
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_store_result(&m_Connection);
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
if (mysql_field_count(&m_Connection) > 0)
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error(mysql_error(&m_Connection)));
|
|
|
|
|
|
|
|
return Array::Ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
Array::Ptr rows = boost::make_shared<Array>();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
Dictionary::Ptr row = FetchRow(result);
|
|
|
|
|
|
|
|
if (!row)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rows->Add(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql_free_result(result);
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
2013-08-16 11:15:05 +02:00
|
|
|
DbReference IdoMysqlDbConnection::GetLastInsertID(void)
|
2013-07-17 11:29:51 +02:00
|
|
|
{
|
|
|
|
return DbReference(mysql_insert_id(&m_Connection));
|
|
|
|
}
|
|
|
|
|
2013-08-16 11:15:05 +02:00
|
|
|
String IdoMysqlDbConnection::Escape(const String& s)
|
2013-07-17 11:29:51 +02:00
|
|
|
{
|
|
|
|
ssize_t length = s.GetLength();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-08-16 11:15:05 +02:00
|
|
|
Dictionary::Ptr IdoMysqlDbConnection::FetchRow(MYSQL_RES *result)
|
2013-07-17 11:29:51 +02:00
|
|
|
{
|
|
|
|
MYSQL_ROW row;
|
|
|
|
MYSQL_FIELD *field;
|
|
|
|
unsigned long *lengths, i;
|
|
|
|
|
|
|
|
row = mysql_fetch_row(result);
|
|
|
|
|
|
|
|
if (!row)
|
|
|
|
return Dictionary::Ptr();
|
|
|
|
|
|
|
|
lengths = mysql_fetch_lengths(result);
|
|
|
|
|
|
|
|
if (!lengths)
|
|
|
|
return Dictionary::Ptr();
|
|
|
|
|
|
|
|
Dictionary::Ptr dict = boost::make_shared<Dictionary>();
|
|
|
|
|
|
|
|
mysql_field_seek(result, 0);
|
|
|
|
for (field = mysql_fetch_field(result), i = 0; field; field = mysql_fetch_field(result), i++) {
|
|
|
|
Value value;
|
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
if (field)
|
2013-07-17 11:29:51 +02:00
|
|
|
value = String(row[i], row[i] + lengths[i]);
|
|
|
|
|
|
|
|
dict->Set(field->name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dict;
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
2013-08-16 11:15:05 +02:00
|
|
|
void IdoMysqlDbConnection::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-08-16 11:15:05 +02:00
|
|
|
void IdoMysqlDbConnection::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-08-16 11:15:05 +02:00
|
|
|
void IdoMysqlDbConnection::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-08-16 11:15:05 +02:00
|
|
|
bool IdoMysqlDbConnection::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-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-08-16 11:15:05 +02:00
|
|
|
void IdoMysqlDbConnection::ExecuteQuery(const DbQuery& query)
|
2013-07-22 12:08:49 +02:00
|
|
|
{
|
2013-07-23 11:02:47 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ConnectionMutex);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
String key;
|
|
|
|
Value value;
|
|
|
|
bool first = true;
|
|
|
|
BOOST_FOREACH(boost::tie(key, value), query.WhereCriteria) {
|
|
|
|
if (!FieldToEscapedString(key, value, &value))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!first)
|
2013-08-20 11:06:04 +02:00
|
|
|
where << " AND ";
|
2013-08-02 08:56:36 +02:00
|
|
|
|
|
|
|
where << key << " = " << value;
|
|
|
|
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((query.Type & DbQueryInsert) && (query.Type & DbQueryUpdate)) {
|
2013-08-02 15:45:50 +02:00
|
|
|
bool hasid;
|
|
|
|
|
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.");
|
|
|
|
|
|
|
|
if (hasid)
|
2013-08-02 08:56:36 +02:00
|
|
|
type = DbQueryUpdate;
|
|
|
|
else {
|
|
|
|
if (query.WhereCriteria)
|
|
|
|
Query("DELETE FROM " + GetTablePrefix() + query.Table + where.str());
|
2013-07-22 12:08:49 +02:00
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
type = DbQueryInsert;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
type = query.Type;
|
|
|
|
|
|
|
|
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-07-17 14:10:28 +02:00
|
|
|
String cols;
|
|
|
|
String values;
|
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-17 14:10:28 +02:00
|
|
|
String key;
|
|
|
|
Value value;
|
2013-07-22 12:08:49 +02:00
|
|
|
bool first = true;
|
|
|
|
BOOST_FOREACH(boost::tie(key, value), query.Fields) {
|
2013-07-23 11:02:47 +02:00
|
|
|
if (!FieldToEscapedString(key, value, &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) {
|
|
|
|
cols += ", ";
|
|
|
|
values += ", ";
|
|
|
|
}
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
cols += key;
|
2013-07-23 11:02:47 +02:00
|
|
|
values += Convert::ToString(value);
|
2013-07-22 12:08:49 +02:00
|
|
|
} else {
|
|
|
|
if (!first)
|
|
|
|
qbuf << ", ";
|
2013-07-17 11:29:51 +02:00
|
|
|
|
2013-07-23 11:02:47 +02:00
|
|
|
qbuf << " " << key << " = " << 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-07-22 12:08:49 +02:00
|
|
|
qbuf << " (" << cols << ") VALUES (" << values << ")";
|
|
|
|
}
|
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
|
|
|
|
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-07-17 11:29:51 +02:00
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
|
|
|
void IdoMysqlDbConnection::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
|
|
|
|
{
|
|
|
|
DbConnection::InternalSerialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
|
|
|
bag->Set("host", m_Host);
|
|
|
|
bag->Set("port", m_Port);
|
|
|
|
bag->Set("user", m_User);
|
|
|
|
bag->Set("password", m_Password);
|
|
|
|
bag->Set("database", m_Database);
|
|
|
|
bag->Set("instance_name", m_InstanceName);
|
|
|
|
bag->Set("instance_description", m_InstanceDescription);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdoMysqlDbConnection::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
|
|
|
|
{
|
|
|
|
DbConnection::InternalDeserialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
|
|
|
m_Host = bag->Get("host");
|
|
|
|
m_Port = bag->Get("port");
|
|
|
|
m_User = bag->Get("user");
|
|
|
|
m_Password = bag->Get("password");
|
|
|
|
m_Database = bag->Get("database");
|
|
|
|
m_InstanceName = bag->Get("instance_name");
|
|
|
|
m_InstanceDescription = bag->Get("instance_description");
|
|
|
|
}
|
|
|
|
}
|