Better code

This commit is contained in:
Alexey Milovidov 2020-05-10 02:31:44 +03:00
parent eeaab4ba11
commit 715b6f3617
4 changed files with 30 additions and 29 deletions

View File

@ -11,29 +11,23 @@
namespace mysqlxx
{
Query::Query(Connection * conn_, const std::string & query_string) : std::ostream(nullptr), conn(conn_)
Query::Query(Connection * conn_, const std::string & query_string) : conn(conn_)
{
/// Важно в случае, если Query используется не из того же потока, что Connection.
mysql_thread_init();
init(&query_buf);
if (!query_string.empty())
{
query_buf.str(query_string);
seekp(0, std::ios::end);
}
query_buf << query_string;
imbue(std::locale::classic());
query_buf.imbue(std::locale::classic());
}
Query::Query(const Query & other) : std::ostream(nullptr), conn(other.conn)
Query::Query(const Query & other) : conn(other.conn)
{
/// Важно в случае, если Query используется не из того же потока, что Connection.
mysql_thread_init();
init(&query_buf);
imbue(std::locale::classic());
query_buf.imbue(std::locale::classic());
*this << other.str();
}
@ -45,9 +39,7 @@ Query & Query::operator= (const Query & other)
conn = other.conn;
seekp(0);
clear();
*this << other.str();
query_buf.str(other.str());
return *this;
}
@ -59,9 +51,7 @@ Query::~Query()
void Query::reset()
{
seekp(0);
clear();
query_buf.str("");
query_buf.str({});
}
void Query::executeImpl()

View File

@ -1,7 +1,6 @@
#pragma once
#include <sstream>
#include <ostream>
#include <mysqlxx/UseQueryResult.h>
#include <mysqlxx/StoreQueryResult.h>
@ -28,7 +27,7 @@ namespace mysqlxx
*
* Внимание! Один объект запроса можно использовать только из одного потока.
*/
class Query : public std::ostream
class Query
{
public:
Query(Connection * conn_, const std::string & query_string = "");
@ -64,9 +63,21 @@ public:
return query_buf.str();
}
auto rdbuf() const
{
return query_buf.rdbuf();
}
template <typename T>
inline Query & operator<< (T && x)
{
query_buf << std::forward<T>(x);
return *this;
}
private:
Connection * conn;
std::stringbuf query_buf;
std::ostringstream query_buf;
void executeImpl();
};

View File

@ -22,11 +22,11 @@ class ResultBase
public:
ResultBase(MYSQL_RES * res_, Connection * conn_, const Query * query_);
Connection * getConnection() { return conn; }
MYSQL_FIELDS getFields() { return fields; }
unsigned getNumFields() { return num_fields; }
MYSQL_RES * getRes() { return res; }
const Query * getQuery() const { return query; }
Connection * getConnection() { return conn; }
MYSQL_FIELDS getFields() { return fields; }
unsigned getNumFields() { return num_fields; }
MYSQL_RES * getRes() { return res; }
const Query * getQuery() const { return query; }
virtual ~ResultBase();

View File

@ -14,16 +14,16 @@ namespace mysqlxx
StoreQueryResult::StoreQueryResult(MYSQL_RES * res_, Connection * conn_, const Query * query_) : ResultBase(res_, conn_, query_)
{
UInt64 rows = mysql_num_rows(res);
UInt32 fields = getNumFields();
UInt32 num_fields = getNumFields();
reserve(rows);
lengths.resize(rows * fields);
lengths.resize(rows * num_fields);
for (UInt64 i = 0; MYSQL_ROW row = mysql_fetch_row(res); ++i)
{
MYSQL_LENGTHS lengths_for_row = mysql_fetch_lengths(res);
memcpy(&lengths[i * fields], lengths_for_row, sizeof(lengths[0]) * fields);
memcpy(&lengths[i * num_fields], lengths_for_row, sizeof(lengths[0]) * num_fields);
push_back(Row(row, this, &lengths[i * fields]));
push_back(Row(row, this, &lengths[i * num_fields]));
}
checkError(conn->getDriver());
}