2019-09-24 22:28:59 +00:00
|
|
|
#if __has_include(<mysql.h>)
|
2021-02-27 08:18:28 +00:00
|
|
|
#include <errmsg.h>
|
2019-09-24 22:28:59 +00:00
|
|
|
#include <mysql.h>
|
2018-07-25 11:29:26 +00:00
|
|
|
#else
|
2021-06-21 19:23:13 +00:00
|
|
|
#include <mysql/errmsg.h> //Y_IGNORE
|
2017-01-24 21:07:08 +00:00
|
|
|
#include <mysql/mysql.h>
|
2018-07-25 11:29:26 +00:00
|
|
|
#endif
|
2017-04-19 00:25:57 +00:00
|
|
|
|
2021-02-27 08:18:28 +00:00
|
|
|
#include <Poco/Logger.h>
|
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
#include <mysqlxx/Connection.h>
|
|
|
|
#include <mysqlxx/Query.h>
|
2021-02-27 08:18:28 +00:00
|
|
|
#include <mysqlxx/Types.h>
|
2011-03-03 19:57:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace mysqlxx
|
|
|
|
{
|
|
|
|
|
2020-05-09 23:31:44 +00:00
|
|
|
Query::Query(Connection * conn_, const std::string & query_string) : conn(conn_)
|
2011-03-03 19:57:34 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Важно в случае, если Query используется не из того же потока, что Connection.
|
|
|
|
mysql_thread_init();
|
2016-12-12 06:45:46 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!query_string.empty())
|
2020-05-09 23:31:44 +00:00
|
|
|
query_buf << query_string;
|
2011-03-10 20:31:02 +00:00
|
|
|
|
2020-05-09 23:31:44 +00:00
|
|
|
query_buf.imbue(std::locale::classic());
|
2011-03-03 19:57:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 23:31:44 +00:00
|
|
|
Query::Query(const Query & other) : conn(other.conn)
|
2011-03-03 19:57:34 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Важно в случае, если Query используется не из того же потока, что Connection.
|
|
|
|
mysql_thread_init();
|
2016-12-12 06:45:46 +00:00
|
|
|
|
2020-05-09 23:31:44 +00:00
|
|
|
query_buf.imbue(std::locale::classic());
|
2011-03-16 20:33:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
*this << other.str();
|
2011-03-03 19:57:34 +00:00
|
|
|
}
|
|
|
|
|
2011-03-05 20:47:46 +00:00
|
|
|
Query & Query::operator= (const Query & other)
|
|
|
|
{
|
2019-04-22 16:07:09 +00:00
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
conn = other.conn;
|
2011-03-10 20:31:02 +00:00
|
|
|
|
2020-05-09 23:31:44 +00:00
|
|
|
query_buf.str(other.str());
|
2016-12-12 06:45:46 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return *this;
|
2011-03-05 20:47:46 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 19:14:06 +00:00
|
|
|
Query::~Query()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
mysql_thread_end();
|
2011-11-30 19:14:06 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
void Query::reset()
|
|
|
|
{
|
2020-05-09 23:31:44 +00:00
|
|
|
query_buf.str({});
|
2011-03-03 19:57:34 +00:00
|
|
|
}
|
|
|
|
|
2011-03-15 20:56:42 +00:00
|
|
|
void Query::executeImpl()
|
2011-03-03 19:57:34 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string query_string = query_buf.str();
|
2021-02-27 08:18:28 +00:00
|
|
|
|
|
|
|
MYSQL* mysql_driver = conn->getDriver();
|
|
|
|
|
|
|
|
auto & logger = Poco::Logger::get("mysqlxx::Query");
|
|
|
|
logger.trace("Running MySQL query using connection %lu", mysql_thread_id(mysql_driver));
|
|
|
|
if (mysql_real_query(mysql_driver, query_string.data(), query_string.size()))
|
|
|
|
{
|
|
|
|
const auto err_no = mysql_errno(mysql_driver);
|
|
|
|
switch (err_no)
|
|
|
|
{
|
|
|
|
case CR_SERVER_GONE_ERROR:
|
|
|
|
[[fallthrough]];
|
|
|
|
case CR_SERVER_LOST:
|
|
|
|
throw ConnectionLost(errorMessage(mysql_driver), err_no);
|
|
|
|
default:
|
2021-09-11 09:12:29 +00:00
|
|
|
/// Add query to the exception message, since it may differs from the user input query.
|
|
|
|
/// (also you can use this and create query with an error to see what query ClickHouse created)
|
2021-09-18 12:46:48 +00:00
|
|
|
throw BadQuery(errorMessage(mysql_driver) + " (query: " + query_string + ")", err_no);
|
2021-02-27 08:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-03 19:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UseQueryResult Query::use()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
executeImpl();
|
|
|
|
MYSQL_RES * res = mysql_use_result(conn->getDriver());
|
|
|
|
if (!res)
|
|
|
|
onError(conn->getDriver());
|
2011-03-03 19:57:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return UseQueryResult(res, conn, this);
|
2011-03-03 19:57:34 +00:00
|
|
|
}
|
|
|
|
|
2011-03-15 20:56:42 +00:00
|
|
|
void Query::execute()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
executeImpl();
|
2011-03-15 20:56:42 +00:00
|
|
|
}
|
|
|
|
|
2011-03-05 16:56:30 +00:00
|
|
|
UInt64 Query::insertID()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return mysql_insert_id(conn->getDriver());
|
2011-03-05 16:56:30 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
}
|