From b50c931d6eddb9493c494bc09900c79893b0bf1a Mon Sep 17 00:00:00 2001 From: Machiavelli Date: Sun, 19 Sep 2010 12:36:48 +0200 Subject: Core/DBLayer: Move QueryHolder and BasicStatement operations to their own file and remove SQLOperation.cpp from revision control --HG-- branch : trunk --- src/server/shared/Database/AdhocStatement.cpp | 57 ++++++ src/server/shared/Database/AdhocStatement.h | 42 +++++ src/server/shared/Database/DatabaseWorkerPool.h | 2 + src/server/shared/Database/QueryHolder.cpp | 189 ++++++++++++++++++++ src/server/shared/Database/QueryHolder.h | 58 ++++++ src/server/shared/Database/SQLOperation.cpp | 226 ------------------------ src/server/shared/Database/SQLOperation.h | 57 +----- 7 files changed, 349 insertions(+), 282 deletions(-) create mode 100644 src/server/shared/Database/AdhocStatement.cpp create mode 100644 src/server/shared/Database/AdhocStatement.h create mode 100644 src/server/shared/Database/QueryHolder.cpp create mode 100644 src/server/shared/Database/QueryHolder.h delete mode 100644 src/server/shared/Database/SQLOperation.cpp (limited to 'src') diff --git a/src/server/shared/Database/AdhocStatement.cpp b/src/server/shared/Database/AdhocStatement.cpp new file mode 100644 index 00000000000..b2e99f6d25c --- /dev/null +++ b/src/server/shared/Database/AdhocStatement.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2008-2010 Trinity + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "AdhocStatement.h" +#include "MySQLConnection.h" + +/*! Basic, ad-hoc queries. */ +BasicStatementTask::BasicStatementTask(const char* sql) : +m_has_result(false) +{ + m_sql = strdup(sql); +} + +BasicStatementTask::BasicStatementTask(const char* sql, QueryResultFuture result) : +m_has_result(true), +m_result(result) +{ + m_sql = strdup(sql); +} + +BasicStatementTask::~BasicStatementTask() +{ + free((void*)m_sql); +} + +bool BasicStatementTask::Execute() +{ + if (m_has_result) + { + ResultSet* result = m_conn->Query(m_sql); + if (!result || !result->GetRowCount()) + { + m_result.set(QueryResult(NULL)); + return false; + } + result->NextRow(); + m_result.set(QueryResult(result)); + return true; + } + + return m_conn->Execute(m_sql); +} diff --git a/src/server/shared/Database/AdhocStatement.h b/src/server/shared/Database/AdhocStatement.h new file mode 100644 index 00000000000..ad580f1fbab --- /dev/null +++ b/src/server/shared/Database/AdhocStatement.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2008-2010 Trinity + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _ADHOCSTATEMENT_H +#define _ADHOCSTATEMENT_H + +#include +#include "SQLOperation.h" + +typedef ACE_Future QueryResultFuture; +/*! Raw, ad-hoc query. */ +class BasicStatementTask : public SQLOperation +{ + public: + BasicStatementTask(const char* sql); + BasicStatementTask(const char* sql, QueryResultFuture result); + ~BasicStatementTask(); + + bool Execute(); + + private: + const char* m_sql; //- Raw query to be executed + bool m_has_result; + QueryResultFuture m_result; +}; + +#endif \ No newline at end of file diff --git a/src/server/shared/Database/DatabaseWorkerPool.h b/src/server/shared/Database/DatabaseWorkerPool.h index e0735a83fba..2d55a6f1b6b 100644 --- a/src/server/shared/Database/DatabaseWorkerPool.h +++ b/src/server/shared/Database/DatabaseWorkerPool.h @@ -30,6 +30,8 @@ #include "PreparedStatement.h" #include "Log.h" #include "QueryResult.h" +#include "QueryHolder.h" +#include "AdhocStatement.h" enum MySQLThreadBundle { diff --git a/src/server/shared/Database/QueryHolder.cpp b/src/server/shared/Database/QueryHolder.cpp new file mode 100644 index 00000000000..dfb5792cc9d --- /dev/null +++ b/src/server/shared/Database/QueryHolder.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2008-2010 Trinity + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "QueryHolder.h" +#include "PreparedStatement.h" +#include "MySQLConnection.h" +#include "Log.h" + +bool SQLQueryHolder::SetQuery(size_t index, const char *sql) +{ + if (m_queries.size() <= index) + { + sLog.outError("Query index (%zu) out of range (size: %u) for query: %s", index, (uint32)m_queries.size(), sql); + return false; + } + + /// not executed yet, just stored (it's not called a holder for nothing) + SQLElementData element; + element.type = SQL_ELEMENT_RAW; + element.element.query = strdup(sql); + + SQLResultSetUnion result; + result.qresult = NULL; + + m_queries[index] = SQLResultPair(element, result); + return true; +} + +bool SQLQueryHolder::SetPQuery(size_t index, const char *format, ...) +{ + if (!format) + { + sLog.outError("Query (index: %zu) is empty.",index); + return false; + } + + va_list ap; + char szQuery [MAX_QUERY_LEN]; + va_start(ap, format); + int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap); + va_end(ap); + + if (res == -1) + { + sLog.outError("SQL Query truncated (and not execute) for format: %s",format); + return false; + } + + return SetQuery(index, szQuery); +} + +QueryResult SQLQueryHolder::GetResult(size_t index) +{ + // Don't call to this function if the index is of an ad-hoc statement + if (index < m_queries.size()) + { + /// the query strings are freed on the first GetResult or in the destructor + if (SQLElementData* data = &m_queries[index].first) + { + free((void*)(const_cast(data->element.query))); + data->element.query = NULL; + } + + ResultSet* result = m_queries[index].second.qresult; + if (!result || !result->GetRowCount()) + return QueryResult(NULL); + + result->NextRow(); + return QueryResult(result); + } + else + return QueryResult(NULL); +} + +PreparedQueryResult SQLQueryHolder::GetPreparedResult(size_t index) +{ + // Don't call to this function if the index is of a prepared statement + if (index < m_queries.size()) + { + /// the query strings are freed on the first GetResult or in the destructor + if (SQLElementData* data = &m_queries[index].first) + { + delete data->element.stmt; + data->element.stmt = NULL; + } + + PreparedResultSet* result = m_queries[index].second.presult; + if (!result || !result->GetRowCount()) + return PreparedQueryResult(NULL); + + result->NextRow(); + return PreparedQueryResult(result); + } + else + return PreparedQueryResult(NULL); +} + +void SQLQueryHolder::SetResult(size_t index, ResultSet* result) +{ + /// store the result in the holder + if (index < m_queries.size()) + m_queries[index].second.qresult = result; +} + +void SQLQueryHolder::SetPreparedResult(size_t index, PreparedResultSet* result) +{ + /// store the result in the holder + if (index < m_queries.size()) + m_queries[index].second.presult = result; +} + +SQLQueryHolder::~SQLQueryHolder() +{ + for (size_t i = 0; i < m_queries.size(); i++) + { + /// if the result was never used, free the resources + /// results used already (getresult called) are expected to be deleted + if (SQLElementData* data = &m_queries[i].first) + { + switch (data->type) + { + case SQL_ELEMENT_RAW: + free((void*)(const_cast(data->element.query))); + break; + case SQL_ELEMENT_PREPARED: + delete data->element.stmt; + break; + } + } + } +} + +void SQLQueryHolder::SetSize(size_t size) +{ + /// to optimize push_back, reserve the number of queries about to be executed + m_queries.resize(size); +} + +bool SQLQueryHolderTask::Execute() +{ + if (!m_holder) + return false; + + /// we can do this, we are friends + std::vector &queries = m_holder->m_queries; + + for (size_t i = 0; i < queries.size(); i++) + { + /// execute all queries in the holder and pass the results + if (SQLElementData* data = &queries[i].first) + { + switch (data->type) + { + case SQL_ELEMENT_RAW: + { + char const *sql = data->element.query; + if (sql) + m_holder->SetResult(i, m_conn->Query(sql)); + break; + } + case SQL_ELEMENT_PREPARED: + { + PreparedStatement* stmt = data->element.stmt; + if (stmt) + m_holder->SetPreparedResult(i, m_conn->Query(stmt)); + break; + } + } + } + } + + m_result.set(m_holder); + return true; +} diff --git a/src/server/shared/Database/QueryHolder.h b/src/server/shared/Database/QueryHolder.h new file mode 100644 index 00000000000..e43a8275554 --- /dev/null +++ b/src/server/shared/Database/QueryHolder.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2008-2010 Trinity + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _QUERYHOLDER_H +#define _QUERYHOLDER_H + +#include + +class SQLQueryHolder +{ + friend class SQLQueryHolderTask; + private: + typedef std::pair SQLResultPair; + std::vector m_queries; + public: + SQLQueryHolder() {} + ~SQLQueryHolder(); + bool SetQuery(size_t index, const char *sql); + bool SetPQuery(size_t index, const char *format, ...) ATTR_PRINTF(3,4); + bool SetPreparedQuery(size_t index, PreparedStatement* stmt); + void SetSize(size_t size); + QueryResult GetResult(size_t index); + PreparedQueryResult GetPreparedResult(size_t index); + void SetResult(size_t index, ResultSet* result); + void SetPreparedResult(size_t index, PreparedResultSet* result); +}; + +typedef ACE_Future QueryResultHolderFuture; + +class SQLQueryHolderTask : public SQLOperation +{ + private: + SQLQueryHolder * m_holder; + QueryResultHolderFuture m_result; + + public: + SQLQueryHolderTask(SQLQueryHolder *holder, QueryResultHolderFuture res) + : m_holder(holder), m_result(res){}; + bool Execute(); + +}; + +#endif \ No newline at end of file diff --git a/src/server/shared/Database/SQLOperation.cpp b/src/server/shared/Database/SQLOperation.cpp deleted file mode 100644 index 7ae690d2053..00000000000 --- a/src/server/shared/Database/SQLOperation.cpp +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Copyright (C) 2008-2010 Trinity - * - * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "Common.h" -#include "SQLOperation.h" -#include "MySQLConnection.h" -#include "Log.h" - -/*! Basic, ad-hoc queries. */ -BasicStatementTask::BasicStatementTask(const char* sql) : -m_has_result(false) -{ - m_sql = strdup(sql); -} - -BasicStatementTask::BasicStatementTask(const char* sql, QueryResultFuture result) : -m_has_result(true), -m_result(result) -{ - m_sql = strdup(sql); -} - -BasicStatementTask::~BasicStatementTask() -{ - free((void*)m_sql); -} - -bool BasicStatementTask::Execute() -{ - if (m_has_result) - { - ResultSet* result = m_conn->Query(m_sql); - if (!result || !result->GetRowCount()) - { - m_result.set(QueryResult(NULL)); - return false; - } - result->NextRow(); - m_result.set(QueryResult(result)); - return true; - } - - return m_conn->Execute(m_sql); -} - -bool SQLQueryHolder::SetQuery(size_t index, const char *sql) -{ - if (m_queries.size() <= index) - { - sLog.outError("Query index (%zu) out of range (size: %u) for query: %s", index, (uint32)m_queries.size(), sql); - return false; - } - - /// not executed yet, just stored (it's not called a holder for nothing) - SQLElementData element; - element.type = SQL_ELEMENT_RAW; - element.element.query = strdup(sql); - - SQLResultSetUnion result; - result.qresult = NULL; - - m_queries[index] = SQLResultPair(element, result); - return true; -} - -bool SQLQueryHolder::SetPQuery(size_t index, const char *format, ...) -{ - if (!format) - { - sLog.outError("Query (index: %zu) is empty.",index); - return false; - } - - va_list ap; - char szQuery [MAX_QUERY_LEN]; - va_start(ap, format); - int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap); - va_end(ap); - - if (res==-1) - { - sLog.outError("SQL Query truncated (and not execute) for format: %s",format); - return false; - } - - return SetQuery(index, szQuery); -} - -QueryResult SQLQueryHolder::GetResult(size_t index) -{ - // Don't call to this function if the index is of a prepared statement - if (index < m_queries.size()) - { - /// the query strings are freed on the first GetResult or in the destructor - if (SQLElementData* data = &m_queries[index].first) - { - free((void*)(const_cast(data->element.query))); - data->element.query = NULL; - } - - ResultSet* result = m_queries[index].second.qresult; - if (!result || !result->GetRowCount()) - return QueryResult(NULL); - - result->NextRow(); - return QueryResult(result); - } - else - return QueryResult(NULL); -} - -PreparedQueryResult SQLQueryHolder::GetPreparedResult(size_t index) -{ - // Don't call to this function if the index is of a prepared statement - if (index < m_queries.size()) - { - /// the query strings are freed on the first GetResult or in the destructor - if (SQLElementData* data = &m_queries[index].first) - { - delete data->element.stmt; - data->element.stmt = NULL; - } - - PreparedResultSet* result = m_queries[index].second.presult; - if (!result || !result->GetRowCount()) - return PreparedQueryResult(NULL); - - result->NextRow(); - return PreparedQueryResult(result); - } - else - return PreparedQueryResult(NULL); -} - -void SQLQueryHolder::SetResult(size_t index, ResultSet* result) -{ - /// store the result in the holder - if (index < m_queries.size()) - m_queries[index].second.qresult = result; -} - -void SQLQueryHolder::SetPreparedResult(size_t index, PreparedResultSet* result) -{ - /// store the result in the holder - if (index < m_queries.size()) - m_queries[index].second.presult = result; -} - -SQLQueryHolder::~SQLQueryHolder() -{ - for (size_t i = 0; i < m_queries.size(); i++) - { - /// if the result was never used, free the resources - /// results used already (getresult called) are expected to be deleted - if (SQLElementData* data = &m_queries[i].first) - { - switch (data->type) - { - case SQL_ELEMENT_RAW: - free((void*)(const_cast(data->element.query))); - break; - case SQL_ELEMENT_PREPARED: - delete data->element.stmt; - break; - } - } - } -} - -void SQLQueryHolder::SetSize(size_t size) -{ - /// to optimize push_back, reserve the number of queries about to be executed - m_queries.resize(size); -} - -bool SQLQueryHolderTask::Execute() -{ - if (!m_holder) - return false; - - /// we can do this, we are friends - std::vector &queries = m_holder->m_queries; - - for (size_t i = 0; i < queries.size(); i++) - { - /// execute all queries in the holder and pass the results - if (SQLElementData* data = &queries[i].first) - { - switch (data->type) - { - case SQL_ELEMENT_RAW: - { - char const *sql = data->element.query; - if (sql) - m_holder->SetResult(i, m_conn->Query(sql)); - break; - } - case SQL_ELEMENT_PREPARED: - { - PreparedStatement* stmt = data->element.stmt; - if (stmt) - m_holder->SetPreparedResult(i, m_conn->Query(stmt)); - break; - } - } - } - } - - m_result.set(m_holder); - return true; -} diff --git a/src/server/shared/Database/SQLOperation.h b/src/server/shared/Database/SQLOperation.h index e5d7f7c7180..77f73a95a82 100644 --- a/src/server/shared/Database/SQLOperation.h +++ b/src/server/shared/Database/SQLOperation.h @@ -22,8 +22,7 @@ #include #include -#include "Common.h" -#include "Callback.h" +#include "QueryResult.h" //- Forward declare (don't include header to prevent circular includes) class PreparedStatement; @@ -73,58 +72,4 @@ class SQLOperation : public ACE_Method_Request MySQLConnection* m_conn; }; -typedef ACE_Future QueryResultFuture; -/*! Raw, ad-hoc query. */ -class BasicStatementTask : public SQLOperation -{ - public: - BasicStatementTask(const char* sql); - BasicStatementTask(const char* sql, QueryResultFuture result); - ~BasicStatementTask(); - - bool Execute(); - - private: - const char* m_sql; //- Raw query to be executed - bool m_has_result; - QueryResultFuture m_result; -}; - - - -class SQLQueryHolder -{ - friend class SQLQueryHolderTask; - private: - typedef std::pair SQLResultPair; - std::vector m_queries; - public: - SQLQueryHolder() {} - ~SQLQueryHolder(); - bool SetQuery(size_t index, const char *sql); - bool SetPQuery(size_t index, const char *format, ...) ATTR_PRINTF(3,4); - bool SetPreparedQuery(size_t index, PreparedStatement* stmt); - void SetSize(size_t size); - QueryResult GetResult(size_t index); - PreparedQueryResult GetPreparedResult(size_t index); - void SetResult(size_t index, ResultSet* result); - void SetPreparedResult(size_t index, PreparedResultSet* result); -}; - -typedef ACE_Future QueryResultHolderFuture; - -class SQLQueryHolderTask : public SQLOperation -{ - private: - SQLQueryHolder * m_holder; - QueryResultHolderFuture m_result; - - public: - SQLQueryHolderTask(SQLQueryHolder *holder, QueryResultHolderFuture res) - : m_holder(holder), m_result(res){}; - bool Execute(); - -}; - - #endif -- cgit v1.2.3