ClickHouse/libs/libmysqlxx/src/Query.cpp

97 lines
1.8 KiB
C++
Raw Normal View History

#include <mysql/mysql.h>
2011-03-03 19:57:34 +00:00
#include <mysqlxx/Connection.h>
#include <mysqlxx/Query.h>
namespace mysqlxx
{
2011-03-10 20:31:02 +00:00
Query::Query(Connection * conn_, const std::string & query_string) : std::ostream(0), conn(conn_)
2011-03-03 19:57:34 +00:00
{
/// Важно в случае, если Query используется не из того же потока, что Connection.
mysql_thread_init();
2011-03-10 20:31:02 +00:00
init(&query_buf);
if (!query_string.empty())
{
query_buf.str(query_string);
seekp(0, std::ios::end);
}
imbue(std::locale::classic());
2011-03-03 19:57:34 +00:00
}
2011-03-16 20:33:39 +00:00
Query::Query(const Query & other) : std::ostream(0), conn(other.conn)
2011-03-03 19:57:34 +00:00
{
/// Важно в случае, если Query используется не из того же потока, что Connection.
mysql_thread_init();
2011-03-16 20:33:39 +00:00
init(&query_buf);
imbue(std::locale::classic());
*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)
{
2011-03-10 20:31:02 +00:00
conn = other.conn;
seekp(0);
clear();
2011-03-16 20:33:39 +00:00
*this << other.str();
2011-03-05 20:47:46 +00:00
return *this;
}
Query::~Query()
{
mysql_thread_end();
}
2011-03-03 19:57:34 +00:00
void Query::reset()
{
2011-03-10 20:31:02 +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
{
2011-03-10 20:31:02 +00:00
std::string query_string = query_buf.str();
2011-03-09 20:11:29 +00:00
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()
{
2011-03-15 20:56:42 +00:00
executeImpl();
2011-03-09 20:11:29 +00:00
MYSQL_RES * res = mysql_use_result(conn->getDriver());
2011-03-03 19:57:34 +00:00
if (!res)
2011-03-09 20:11:29 +00:00
onError(conn->getDriver());
2011-03-03 19:57:34 +00:00
2011-03-18 20:26:54 +00:00
return UseQueryResult(res, conn, this);
2011-03-03 19:57:34 +00:00
}
StoreQueryResult Query::store()
{
2011-03-15 20:56:42 +00:00
executeImpl();
2011-03-09 20:11:29 +00:00
MYSQL_RES * res = mysql_store_result(conn->getDriver());
2011-03-03 19:57:34 +00:00
if (!res)
2011-03-09 20:11:29 +00:00
checkError(conn->getDriver());
2011-03-03 19:57:34 +00:00
2011-03-18 20:26: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()
{
executeImpl();
}
2011-03-05 16:56:30 +00:00
UInt64 Query::insertID()
{
2011-03-09 20:11:29 +00:00
return mysql_insert_id(conn->getDriver());
2011-03-05 16:56:30 +00:00
}
2011-03-03 19:57:34 +00:00
}