mysqlxx: development.

This commit is contained in:
Alexey Milovidov 2011-03-09 20:11:29 +00:00
parent 6635e81bc1
commit acbab22bb2
13 changed files with 69 additions and 41 deletions

View File

@ -49,7 +49,7 @@ public:
void disconnect(); void disconnect();
bool ping(); bool ping();
Query query(const std::string & str = ""); Query query(const std::string & str = "");
MYSQL & getDriver(); MYSQL * getDriver();
private: private:
MYSQL driver; MYSQL driver;

View File

@ -26,17 +26,17 @@ struct BadQuery : public Exception
}; };
inline void checkError(MYSQL & driver) inline void checkError(MYSQL * driver)
{ {
unsigned num = mysql_errno(&driver); unsigned num = mysql_errno(driver);
if (num) if (num)
throw Exception(mysql_error(&driver), num); throw Exception(mysql_error(driver), num);
} }
inline void onError(MYSQL & driver) inline void onError(MYSQL * driver)
{ {
throw Exception(mysql_error(&driver), mysql_errno(&driver)); throw Exception(mysql_error(driver), mysql_errno(driver));
} }
} }

View File

@ -4,6 +4,7 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <mysqlxx/Types.h> #include <mysqlxx/Types.h>
#include <mysqlxx/Row.h>
namespace mysqlxx namespace mysqlxx
@ -80,6 +81,33 @@ struct EscapeManipResult
return ostr << value; return ostr << value;
} }
std::ostream & operator<< (const String & string)
{
writeEscapedData(string.data(), string.size());
return ostr;
}
std::ostream & operator<< (const Row & row)
{
for (size_t i = 0; i < row.size(); ++i)
{
if (i != 0)
ostr << '\t';
if (row[i].isNull())
{
ostr << "\\N";
continue;
}
(*this) << row[i];
}
return ostr;
}
private: private:
void writeEscapedData(const char * data, size_t length) void writeEscapedData(const char * data, size_t length)

View File

@ -13,7 +13,7 @@ namespace mysqlxx
class Query class Query
{ {
public: public:
Query(Connection & conn_, const std::string & query_string); Query(Connection * conn_, const std::string & query_string);
Query(const Query & other); Query(const Query & other);
Query & operator= (const Query & other); Query & operator= (const Query & other);
@ -45,7 +45,7 @@ public:
} }
private: private:
Connection & conn; Connection * conn;
std::stringstream query_stream; std::stringstream query_stream;
}; };

View File

@ -14,18 +14,18 @@ class Connection;
class ResultBase class ResultBase
{ {
public: public:
ResultBase(MYSQL_RES & res_, Connection & conn_); ResultBase(MYSQL_RES * res_, Connection * conn_);
Connection & getConnection() { return conn; } Connection * getConnection() { return conn; }
MYSQL_FIELDS getFields() { return fields; } MYSQL_FIELDS getFields() { return fields; }
unsigned getNumFields() { return num_fields; } unsigned getNumFields() { return num_fields; }
MYSQL_RES & getRes() { return res; } MYSQL_RES * getRes() { return res; }
virtual ~ResultBase() {} virtual ~ResultBase() {}
protected: protected:
MYSQL_RES & res; MYSQL_RES * res;
Connection & conn; Connection * conn;
MYSQL_FIELDS fields; MYSQL_FIELDS fields;
unsigned num_fields; unsigned num_fields;
}; };

View File

@ -5,7 +5,6 @@
#include <mysqlxx/String.h> #include <mysqlxx/String.h>
#include <mysqlxx/ResultBase.h> #include <mysqlxx/ResultBase.h>
#include <iostream>
namespace mysqlxx namespace mysqlxx
{ {
@ -29,18 +28,16 @@ public:
Row(MYSQL_ROW row_, ResultBase * res_) Row(MYSQL_ROW row_, ResultBase * res_)
: row(row_), res(res_) : row(row_), res(res_)
{ {
lengths = mysql_fetch_lengths(&res->getRes()); lengths = mysql_fetch_lengths(res->getRes());
} }
String operator[] (int n) const String operator[] (int n) const
{ {
std::cerr << lengths[0] << std::endl;
return String(row[n], lengths[n]); return String(row[n], lengths[n]);
} }
String operator[] (const char * name) const String operator[] (const char * name) const
{ {
std::cerr << "???" << std::endl;
unsigned n = res->getNumFields(); unsigned n = res->getNumFields();
MYSQL_FIELDS fields = res->getFields(); MYSQL_FIELDS fields = res->getFields();
@ -56,6 +53,9 @@ public:
return operator[](n); return operator[](n);
} }
size_t size() const { return res->getNumFields(); }
bool empty() const { return row == NULL; }
operator private_bool_type() const { return row == NULL ? NULL : &Row::row; } operator private_bool_type() const { return row == NULL ? NULL : &Row::row; }
private: private:

View File

@ -15,7 +15,7 @@ class Connection;
class StoreQueryResult : public std::vector<Row>, public ResultBase class StoreQueryResult : public std::vector<Row>, public ResultBase
{ {
public: public:
StoreQueryResult(MYSQL_RES & res_, Connection & conn_); StoreQueryResult(MYSQL_RES * res_, Connection * conn_);
size_t num_rows() const { return size(); } size_t num_rows() const { return size(); }
}; };

View File

@ -13,7 +13,7 @@ class Connection;
class UseQueryResult : public ResultBase class UseQueryResult : public ResultBase
{ {
public: public:
UseQueryResult(MYSQL_RES & res_, Connection & conn_); UseQueryResult(MYSQL_RES * res_, Connection * conn_);
Row fetch_row(); Row fetch_row();
}; };

View File

@ -66,12 +66,12 @@ bool Connection::ping()
Query Connection::query(const std::string & str) Query Connection::query(const std::string & str)
{ {
return Query(*this, str); return Query(this, str);
} }
MYSQL & Connection::getDriver() MYSQL * Connection::getDriver()
{ {
return driver; return &driver;
} }
} }

View File

@ -5,7 +5,7 @@
namespace mysqlxx namespace mysqlxx
{ {
Query::Query(Connection & conn_, const std::string & query_string) : conn(conn_) Query::Query(Connection * conn_, const std::string & query_string) : conn(conn_)
{ {
query_stream << query_string; query_stream << query_string;
} }
@ -29,33 +29,33 @@ void Query::reset()
void Query::execute() void Query::execute()
{ {
std::string query_string = query_stream.str(); std::string query_string = query_stream.str();
if (mysql_real_query(&conn.getDriver(), query_string.data(), query_string.size())) if (mysql_real_query(conn->getDriver(), query_string.data(), query_string.size()))
throw BadQuery(mysql_error(&conn.getDriver()), mysql_errno(&conn.getDriver())); throw BadQuery(mysql_error(conn->getDriver()), mysql_errno(conn->getDriver()));
} }
UseQueryResult Query::use() UseQueryResult Query::use()
{ {
execute(); execute();
MYSQL_RES * res = mysql_use_result(&conn.getDriver()); MYSQL_RES * res = mysql_use_result(conn->getDriver());
if (!res) if (!res)
onError(conn.getDriver()); onError(conn->getDriver());
return UseQueryResult(*res, conn); return UseQueryResult(res, conn);
} }
StoreQueryResult Query::store() StoreQueryResult Query::store()
{ {
execute(); execute();
MYSQL_RES * res = mysql_store_result(&conn.getDriver()); MYSQL_RES * res = mysql_store_result(conn->getDriver());
if (!res) if (!res)
checkError(conn.getDriver()); checkError(conn->getDriver());
return StoreQueryResult(*res, conn); return StoreQueryResult(res, conn);
} }
UInt64 Query::insertID() UInt64 Query::insertID()
{ {
return mysql_insert_id(&conn.getDriver()); return mysql_insert_id(conn->getDriver());
} }
} }

View File

@ -5,10 +5,10 @@
namespace mysqlxx namespace mysqlxx
{ {
ResultBase::ResultBase(MYSQL_RES & res_, Connection & conn_) : res(res_), conn(conn_) ResultBase::ResultBase(MYSQL_RES * res_, Connection * conn_) : res(res_), conn(conn_)
{ {
fields = mysql_fetch_fields(&res); fields = mysql_fetch_fields(res);
num_fields = mysql_num_fields(&res); num_fields = mysql_num_fields(res);
std::cerr << num_fields << std::endl; std::cerr << num_fields << std::endl;
std::cerr << fields[0].name << std::endl; std::cerr << fields[0].name << std::endl;

View File

@ -5,11 +5,11 @@
namespace mysqlxx namespace mysqlxx
{ {
StoreQueryResult::StoreQueryResult(MYSQL_RES & res_, Connection & conn_) : ResultBase(res_, conn_) StoreQueryResult::StoreQueryResult(MYSQL_RES * res_, Connection * conn_) : ResultBase(res_, conn_)
{ {
while (MYSQL_ROW row = mysql_fetch_row(&res)) while (MYSQL_ROW row = mysql_fetch_row(res))
push_back(Row(row, this)); push_back(Row(row, this));
checkError(conn.getDriver()); checkError(conn->getDriver());
} }
} }

View File

@ -5,15 +5,15 @@
namespace mysqlxx namespace mysqlxx
{ {
UseQueryResult::UseQueryResult(MYSQL_RES & res_, Connection & conn_) : ResultBase(res_, conn_) UseQueryResult::UseQueryResult(MYSQL_RES * res_, Connection * conn_) : ResultBase(res_, conn_)
{ {
} }
Row UseQueryResult::fetch_row() Row UseQueryResult::fetch_row()
{ {
MYSQL_ROW row = mysql_fetch_row(&res); MYSQL_ROW row = mysql_fetch_row(res);
if (!row) if (!row)
checkError(conn.getDriver()); checkError(conn->getDriver());
return Row(row, this); return Row(row, this);
} }