table類--初步修改cpp
#include "table.hpp"
#include <boost/lexical_cast.hpp>
using namespace sqlpp;
Table::Table(const std::string& cmd, Connection& conn)throw():_create_cmd(cmd),_conn(conn),_num_fields(0),_ptr_result(NULL)
{
}
Table::Table(Connection& conn)throw():_conn(conn),_num_fields(0),_ptr_result(NULL)
{
}
Table::~Table()
{
}
void Table::download(const std::string& cmd)throw(sqlerror)
{
_create_cmd = cmd;
try
{
download();
}
catch(...)
{
throw;
}
}
void Table::download(void) throw(sqlerror)
{
if (!_conn.is_open())
{
try
{
_conn.connect();
}
catch(...)
{
throw;
}
}
std::string cmd = get_create_cmd();
//error occur
if (mysql_real_query(_conn.get_mysql_handle(), cmd.c_str(), cmd.length()))
{
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
MYSQL_RES* ptr_result = mysql_use_result(_conn.get_mysql_handle());
//error occur
if (ptr_result == NULL)
{
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
_fields.clear();
// fill field message
unsigned int field_nums = mysql_num_fields(ptr_result);//no error
MYSQL_FIELD* ptr_fields = mysql_fetch_fields(ptr_result);// no error
for(int i=0; i<field_nums; i++)
{
_fields.push_back(std::string(ptr_fields[i].name));
}
_rows.clear();
_rows_string.clear();
//fill row message
MYSQL_ROW row;
row_digit currt_row;
_is_row_digit = true;
while ((row = mysql_fetch_row(ptr_result)))
{
for (int k=0; k<_fields.size(); k++)
{
currt_row[k] = boost::lexical_cast<std::string>(row[k]);
}
_rows.push_back(currt_row);
}
mysql_free_result(ptr_result);// no error
}
void Table::online_start(Table::row_digit& row) throw(sqlerror)
{
if (!_conn.is_open())
{
try
{
_conn.connect();
}
catch(...)
{
throw;
}
}
//return 0 is success,or failure
if (mysql_real_query(_conn.get_mysql_handle(), _create_cmd.c_str(), _create_cmd.length()))
{
//check exception
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
_ptr_result = mysql_use_result(_conn.get_mysql_handle());// no error
MYSQL_ROW current_row = mysql_fetch_row(_ptr_result);
//error possablie occur
if (current_row == NULL)
{
//check exception
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
for (int k=0; k<_num_fields; k++)
{
row[k] = current_row[k];
}
}
void Table::online_next(Table::row_digit& row) throw(sqlerror)
{
MYSQL_ROW current_row = mysql_fetch_row(_ptr_result);
//error passablie occur
if (current_row == NULL)
{
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
for (int k=0; k<_num_fields; k++)
{
row[k] = current_row[k];
}
}
bool Table::online_is_eof(void)throw()
{
if (!mysql_eof(_ptr_result))// api no error
{
return false;
}
else
{
mysql_free_result(_ptr_result);
_ptr_result = NULL;
_num_fields = 0;
return true;
}
}
Table::field_list& Table::get_fields(void) throw()
{
return _fields;
}
const std::string& Table::get_create_cmd(void)const throw()
{
return _create_cmd;
}
const std::vector<std::string>& Table::get_update_cmds(void)const throw()
{
return _update_cmds;
}
void Table::trans_to_digit(void)throw()
{
_is_row_digit = true;
_rows.clear();
_rows.resize(_rows_string.size());
row_digit row;
while (!_rows_string.empty())
{
for (int k=0; k<_fields.size(); k++)
{
row[k] = _rows_string.front()[_fields[k]];
}
_rows_string.erase(_rows_string.begin());
_rows.push_back(row);
}
}
void Table::trans_to_string(void)throw()
{
_is_row_digit = false;
_rows_string.clear();
//for save time
_rows_string.resize(_rows.size());
row_string row;
//assign a row,delete it,for save space
while (!_rows.empty())
{
for (int k=0; k<_fields.size(); k++)
{
row[_fields[k]] = _rows.front()[k];
}
_rows.erase(_rows.begin());
_rows_string.push_back(row);
}
}
#include <boost/lexical_cast.hpp>
using namespace sqlpp;
Table::Table(const std::string& cmd, Connection& conn)throw():_create_cmd(cmd),_conn(conn),_num_fields(0),_ptr_result(NULL)
{
}
Table::Table(Connection& conn)throw():_conn(conn),_num_fields(0),_ptr_result(NULL)
{
}
Table::~Table()
{
}
void Table::download(const std::string& cmd)throw(sqlerror)
{
_create_cmd = cmd;
try
{
download();
}
catch(...)
{
throw;
}
}
void Table::download(void) throw(sqlerror)
{
if (!_conn.is_open())
{
try
{
_conn.connect();
}
catch(...)
{
throw;
}
}
std::string cmd = get_create_cmd();
//error occur
if (mysql_real_query(_conn.get_mysql_handle(), cmd.c_str(), cmd.length()))
{
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
MYSQL_RES* ptr_result = mysql_use_result(_conn.get_mysql_handle());
//error occur
if (ptr_result == NULL)
{
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
_fields.clear();
// fill field message
unsigned int field_nums = mysql_num_fields(ptr_result);//no error
MYSQL_FIELD* ptr_fields = mysql_fetch_fields(ptr_result);// no error
for(int i=0; i<field_nums; i++)
{
_fields.push_back(std::string(ptr_fields[i].name));
}
_rows.clear();
_rows_string.clear();
//fill row message
MYSQL_ROW row;
row_digit currt_row;
_is_row_digit = true;
while ((row = mysql_fetch_row(ptr_result)))
{
for (int k=0; k<_fields.size(); k++)
{
currt_row[k] = boost::lexical_cast<std::string>(row[k]);
}
_rows.push_back(currt_row);
}
mysql_free_result(ptr_result);// no error
}
void Table::online_start(Table::row_digit& row) throw(sqlerror)
{
if (!_conn.is_open())
{
try
{
_conn.connect();
}
catch(...)
{
throw;
}
}
//return 0 is success,or failure
if (mysql_real_query(_conn.get_mysql_handle(), _create_cmd.c_str(), _create_cmd.length()))
{
//check exception
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
_ptr_result = mysql_use_result(_conn.get_mysql_handle());// no error
MYSQL_ROW current_row = mysql_fetch_row(_ptr_result);
//error possablie occur
if (current_row == NULL)
{
//check exception
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
for (int k=0; k<_num_fields; k++)
{
row[k] = current_row[k];
}
}
void Table::online_next(Table::row_digit& row) throw(sqlerror)
{
MYSQL_ROW current_row = mysql_fetch_row(_ptr_result);
//error passablie occur
if (current_row == NULL)
{
try
{
_conn.check_exception();
}
catch(...)
{
throw;
}
}
for (int k=0; k<_num_fields; k++)
{
row[k] = current_row[k];
}
}
bool Table::online_is_eof(void)throw()
{
if (!mysql_eof(_ptr_result))// api no error
{
return false;
}
else
{
mysql_free_result(_ptr_result);
_ptr_result = NULL;
_num_fields = 0;
return true;
}
}
Table::field_list& Table::get_fields(void) throw()
{
return _fields;
}
const std::string& Table::get_create_cmd(void)const throw()
{
return _create_cmd;
}
const std::vector<std::string>& Table::get_update_cmds(void)const throw()
{
return _update_cmds;
}
void Table::trans_to_digit(void)throw()
{
_is_row_digit = true;
_rows.clear();
_rows.resize(_rows_string.size());
row_digit row;
while (!_rows_string.empty())
{
for (int k=0; k<_fields.size(); k++)
{
row[k] = _rows_string.front()[_fields[k]];
}
_rows_string.erase(_rows_string.begin());
_rows.push_back(row);
}
}
void Table::trans_to_string(void)throw()
{
_is_row_digit = false;
_rows_string.clear();
//for save time
_rows_string.resize(_rows.size());
row_string row;
//assign a row,delete it,for save space
while (!_rows.empty())
{
for (int k=0; k<_fields.size(); k++)
{
row[_fields[k]] = _rows.front()[k];
}
_rows.erase(_rows.begin());
_rows_string.push_back(row);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.