2019-09-24 22:28:59 +00:00
|
|
|
#if __has_include(<mysql.h>)
|
|
|
|
#include <mysql.h>
|
2018-07-25 11:29:26 +00:00
|
|
|
#else
|
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
|
|
|
|
2011-03-03 19:57:34 +00:00
|
|
|
#include <mysqlxx/Connection.h>
|
|
|
|
#include <mysqlxx/Query.h>
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
if (mysql_real_query(conn->getDriver(), query_string.data(), query_string.size()))
|
|
|
|
throw BadQuery(errorMessage(conn->getDriver()), mysql_errno(conn->getDriver()));
|
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
|
|
|
}
|