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();
bool ping();
Query query(const std::string & str = "");
MYSQL & getDriver();
MYSQL * getDriver();
private:
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)
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 <iostream>
#include <mysqlxx/Types.h>
#include <mysqlxx/Row.h>
namespace mysqlxx
@ -80,6 +81,33 @@ struct EscapeManipResult
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:
void writeEscapedData(const char * data, size_t length)

View File

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

View File

@ -14,18 +14,18 @@ class Connection;
class ResultBase
{
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; }
unsigned getNumFields() { return num_fields; }
MYSQL_RES & getRes() { return res; }
MYSQL_RES * getRes() { return res; }
virtual ~ResultBase() {}
protected:
MYSQL_RES & res;
Connection & conn;
MYSQL_RES * res;
Connection * conn;
MYSQL_FIELDS fields;
unsigned num_fields;
};

View File

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

View File

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

View File

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

View File

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

View File

@ -5,10 +5,10 @@
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);
num_fields = mysql_num_fields(&res);
fields = mysql_fetch_fields(res);
num_fields = mysql_num_fields(res);
std::cerr << num_fields << std::endl;
std::cerr << fields[0].name << std::endl;

View File

@ -5,11 +5,11 @@
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));
checkError(conn.getDriver());
checkError(conn->getDriver());
}
}

View File

@ -5,15 +5,15 @@
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()
{
MYSQL_ROW row = mysql_fetch_row(&res);
MYSQL_ROW row = mysql_fetch_row(res);
if (!row)
checkError(conn.getDriver());
checkError(conn->getDriver());
return Row(row, this);
}