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-03-08 21:04:10 +00:00
|
|
|
Query::Query(Connection * conn_, const std::string & query_string) : std::ostream(nullptr), 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
|
|
|
init(&query_buf);
|
2011-03-10 20:31:02 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!query_string.empty())
|
|
|
|
{
|
|
|
|
query_buf.str(query_string);
|
|
|
|
seekp(0, std::ios::end);
|
|
|
|
}
|
2011-03-10 20:31:02 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
imbue(std::locale::classic());
|
2011-03-03 19:57:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 21:04:10 +00:00
|
|
|
Query::Query(const Query & other) : std::ostream(nullptr), 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
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
init(&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
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
seekp(0);
|
|
|
|
clear();
|
|
|
|
*this << 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()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
seekp(0);
|
|
|
|
clear();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
StoreQueryResult Query::store()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
executeImpl();
|
|
|
|
MYSQL_RES * res = mysql_store_result(conn->getDriver());
|
|
|
|
if (!res)
|
|
|
|
checkError(conn->getDriver());
|
2011-03-03 19:57:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return StoreQueryResult(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
|
|
|
}
|