Commit 04cf4bc4 authored by Vinogradov Aleksei's avatar Vinogradov Aleksei Committed by Pavel Vainerman

вынес интерфейс БД в абстрактный класс и добавил методы для динамической загрузки библиотек

parent 2f3f3f30
......@@ -297,7 +297,7 @@ void DBServer_MySQL::initDBServer()
<< " pingTime=" << PingTime
<< " ReconnectTime=" << ReconnectTime << endl;
if( !db->connect(dbnode, user, password, dbname) )
if( !db->nconnect(dbnode, user, password, dbname) )
{
// ostringstream err;
dbcrit << myname
......
......@@ -45,7 +45,7 @@ MySQLInterface::~MySQLInterface()
}
// -----------------------------------------------------------------------------------------
bool MySQLInterface::connect( const string& host, const string& user, const string& pswd, const string& dbname)
bool MySQLInterface::nconnect( const string& host, const string& user, const string& pswd, const string& dbname)
{
if( !mysql_real_connect(mysql, host.c_str(), user.c_str(), pswd.c_str(), dbname.c_str(), 0, NULL, 0) )
{
......@@ -76,24 +76,25 @@ bool MySQLInterface::insert( const string& q )
return true;
}
// -----------------------------------------------------------------------------------------
MySQLResult MySQLInterface::query( const std::string& q )
DBResult MySQLInterface::query( const std::string& q )
{
if( !mysql )
return MySQLResult();
return DBResult();
if( mysql_query(mysql, q.c_str()) )
{
cerr << error() << endl;
return MySQLResult();
return DBResult();
}
lastQ = q;
MYSQL_RES* res = mysql_store_result(mysql); // _use_result - некорректно работает с _num_rows
if( !res || mysql_num_rows(res) == 0 )
return MySQLResult();
return MySQLResult(res, true);
return DBResult();
DBResult dbres;
makeResult(dbres, res, true);
return dbres;
}
// -----------------------------------------------------------------------------------------
bool MySQLInterface::query_ok( const string& q )
......@@ -129,7 +130,7 @@ const string MySQLInterface::lastQuery()
return lastQ;
}
// -----------------------------------------------------------------------------------------
int MySQLInterface::insert_id()
double MySQLInterface::insert_id()
{
if( !mysql )
return 0;
......@@ -173,71 +174,32 @@ string MySQLInterface::addslashes( const string& str )
return tmp.str();
}
// -----------------------------------------------------------------------------------------
int MySQLResult::num_cols( const MySQLResult::iterator& it )
{
return it->size();
}
// -----------------------------------------------------------------------------------------
int MySQLResult::as_int( const MySQLResult::iterator& it, int col )
{
// if( col<0 || col >it->size() )
// return 0;
return uni_atoi( (*it)[col] );
}
// -----------------------------------------------------------------------------------------
double as_double( const MySQLResult::iterator& it, int col )
{
return atof( ((*it)[col]).c_str() );
}
// -----------------------------------------------------------------------------------------
string MySQLResult::as_string( const MySQLResult::iterator& it, int col )
void MySQLInterface::makeResult(DBResult& dbres, MYSQL_RES* myres, bool finalize )
{
return ((*it)[col]);
}
// -----------------------------------------------------------------------------------------
int MySQLResult::as_int( const MySQLResult::COL::iterator& it )
{
return uni_atoi( (*it) );
}
// -----------------------------------------------------------------------------------------
double MySQLResult::as_double( const MySQLResult::COL::iterator& it )
{
return atof( (*it).c_str() );
}
// -----------------------------------------------------------------------------------------
std::string MySQLResult::as_string( const MySQLResult::COL::iterator& it )
{
return (*it);
}
// -----------------------------------------------------------------------------------------
#if 0
MySQLResult::COL get_col( MySQLResult::ROW::iterator& it )
{
return (*it);
}
#endif
// -----------------------------------------------------------------------------------------
MySQLResult::~MySQLResult()
{
}
// -----------------------------------------------------------------------------------------
MySQLResult::MySQLResult( MYSQL_RES* myres, bool finalize )
{
MYSQL_ROW row;
MYSQL_ROW mysql_row;
unsigned int nfields = mysql_num_fields(myres);
while( (row = mysql_fetch_row(myres)) )
while( (mysql_row = mysql_fetch_row(myres)) )
{
COL c;
DBResult::COL c;
for( unsigned int i = 0; i < nfields; i++ )
c.push_back( (row[i] != 0 ? string(row[i]) : "") );
c.push_back( (mysql_row[i] != 0 ? string(mysql_row[i]) : "") );
res.push_back(c);
dbres.row().push_back(c);
}
if( finalize )
mysql_free_result(myres);
}
// -----------------------------------------------------------------------------------------
extern "C" DBInterface* create_mysqlinterface()
{
return new MySQLInterface();
}
// -----------------------------------------------------------------------------------------
extern "C" void destroy_mysqlinterface(DBInterface* p)
{
delete p;
}
// -----------------------------------------------------------------------------------------
......@@ -31,29 +31,28 @@
//#warning Для использования mysql_create нужен define USE_OLD_FUNCTIONS
//#define USE_OLD_FUNCTIONS
#include <mysql/mysql.h>
#include <DBInterface.h>
// ----------------------------------------------------------------------------
class MySQLResult;
// ----------------------------------------------------------------------------
class MySQLInterface
class MySQLInterface : public DBNetInterface
{
public:
MySQLInterface();
~MySQLInterface();
// MySQLResult listFields( const std::string& table, const std::string& wild );
// DBResult listFields( const std::string& table, const std::string& wild );
bool connect( const std::string& host, const std::string& user, const std::string& pswd,
const std::string& dbname);
bool close();
bool nconnect( const std::string& host, const std::string& user, const std::string& pswd,
const std::string& dbname) override;
bool close() override;
bool query_ok( const std::string& q );
// \param finalize - освободить буфер после запроса
MySQLResult query( const std::string& q );
DBResult query( const std::string& q ) override;
const std::string lastQuery();
bool insert( const std::string& q );
const std::string lastQuery() override;
bool insert( const std::string& q ) override;
std::string addslashes(const std::string& str);
......@@ -61,14 +60,14 @@ class MySQLInterface
проверка связи с БД.
в случае отсутсвия попытка восстановить...
*/
bool ping();
bool ping() override;
/*! связь с БД установлена (была) */
bool isConnection();
bool isConnection() override;
int insert_id();
double insert_id() override;
const std::string error();
const std::string error() override;
// *******************
const char* gethostinfo();
......@@ -76,63 +75,10 @@ class MySQLInterface
private:
void makeResult(DBResult& dbres, MYSQL_RES* r, bool finalize = true );
MYSQL* mysql;
std::string lastQ;
bool connected;
};
// ----------------------------------------------------------------------------------
class MySQLResult
{
public:
MySQLResult() {}
MySQLResult( MYSQL_RES* r, bool finalize = true );
~MySQLResult();
typedef std::vector<std::string> COL;
typedef std::deque<COL> ROW;
typedef ROW::iterator iterator;
inline iterator begin()
{
return res.begin();
}
inline iterator end()
{
return res.end();
}
inline operator bool()
{
return !res.empty();
}
inline size_t size()
{
return res.size();
}
inline bool empty()
{
return res.empty();
}
// ----------------------------------------------------------------------------
// ROW
static int as_int( const MySQLResult::iterator&, int col );
static double as_double( const MySQLResult::iterator&, int col );
static std::string as_string( const MySQLResult::iterator&, int col );
// ----------------------------------------------------------------------------
// COL
static int num_cols( const MySQLResult::iterator& );
static int as_int( const MySQLResult::COL::iterator& );
static double as_double(const MySQLResult::COL::iterator& );
static std::string as_string( const MySQLResult::COL::iterator& );
// ----------------------------------------------------------------------------
protected:
ROW res;
};
// ----------------------------------------------------------------------------------
#endif
......@@ -17,7 +17,7 @@ int main(int argc, char** argv)
{
MySQLInterface db;
if( !db.connect("localhost", "dbadmin", "dbadmin", dbname) )
if( !db.nconnect("localhost", "dbadmin", "dbadmin", dbname) )
{
cerr << "db connect error: " << db.error() << endl;
return 1;
......@@ -27,7 +27,7 @@ int main(int argc, char** argv)
stringstream q;
q << "SELECT * from main_history";
MySQLResult r = db.query(q.str());
DBResult r = db.query(q.str());
if( !r )
{
......@@ -35,13 +35,13 @@ int main(int argc, char** argv)
return 1;
}
for( MySQLResult::iterator it = r.begin(); it != r.end(); it++ )
for( DBResult::iterator it = r.begin(); it != r.end(); it++ )
{
cout << "ROW: ";
MySQLResult::COL col(*it);
DBResult::COL col(*it);
for( MySQLResult::COL::iterator cit = it->begin(); cit != it->end(); cit++ )
cout << MySQLResult::as_string(cit) << "(" << MySQLResult::as_double(cit) << ") | ";
for( DBResult::COL::iterator cit = it->begin(); cit != it->end(); cit++ )
cout << DBResult::as_string(cit) << "(" << DBResult::as_double(cit) << ") | ";
cout << endl;
}
......
......@@ -273,7 +273,7 @@ void DBServer_PostgreSQL::initDBServer()
<< " pingTime=" << PingTime
<< " ReconnectTime=" << ReconnectTime << endl;
if( !db->connect(dbnode, user, password, dbname) )
if( !db->nconnect(dbnode, user, password, dbname) )
{
dbwarn << myname << "(init): DB connection error: " << db->error() << endl;
askTimer(DBServer_PostgreSQL::ReconnectTimer, ReconnectTime);
......
......@@ -28,7 +28,7 @@ bool PostgreSQLInterface::ping()
return db && db->is_open();
}
// -----------------------------------------------------------------------------------------
bool PostgreSQLInterface::connect( const string& host, const string& user, const string& pswd, const string& dbname)
bool PostgreSQLInterface::nconnect( const string& host, const string& user, const string& pswd, const string& dbname)
{
if( db )
return true;
......@@ -105,10 +105,10 @@ bool PostgreSQLInterface::insertAndSaveRowid( const string& q )
return false;
}
// -----------------------------------------------------------------------------------------
PostgreSQLResult PostgreSQLInterface::query( const string& q )
DBResult PostgreSQLInterface::query( const string& q )
{
if( !db )
return PostgreSQLResult();
return DBResult();
try
{
......@@ -116,17 +116,19 @@ PostgreSQLResult PostgreSQLInterface::query( const string& q )
/* Execute SQL query */
result res( n.exec(q) );
return PostgreSQLResult(res);
DBResult dbres;
makeResult(dbres, res);
return dbres;
}
catch( const std::exception& e )
{
lastE = string(e.what());
}
return PostgreSQLResult();
return DBResult();
}
// -----------------------------------------------------------------------------------------
string PostgreSQLInterface::error()
const string PostgreSQLInterface::error()
{
return lastE;
}
......@@ -152,65 +154,26 @@ bool PostgreSQLInterface::isConnection()
return (db && db->is_open());
}
// -----------------------------------------------------------------------------------------
int PostgreSQLResult::num_cols( const PostgreSQLResult::iterator& it )
{
return it->size();
}
// -----------------------------------------------------------------------------------------
int PostgreSQLResult::as_int( const PostgreSQLResult::iterator& it, int col )
{
// if( col<0 || col >it->size() )
// return 0;
return uni_atoi( (*it)[col] );
}
// -----------------------------------------------------------------------------------------
double PostgreSQLResult::as_double( const PostgreSQLResult::iterator& it, int col )
{
return atof( ((*it)[col]).c_str() );
}
// -----------------------------------------------------------------------------------------
std::string PostgreSQLResult::as_string( const PostgreSQLResult::iterator& it, int col )
{
return ((*it)[col]);
}
// -----------------------------------------------------------------------------------------
int PostgreSQLResult::as_int( const PostgreSQLResult::COL::iterator& it )
{
return uni_atoi( (*it) );
}
// -----------------------------------------------------------------------------------------
double PostgreSQLResult::as_double( const PostgreSQLResult::COL::iterator& it )
{
return atof( (*it).c_str() );
}
// -----------------------------------------------------------------------------------------
std::string PostgreSQLResult::as_string( const PostgreSQLResult::COL::iterator& it )
{
return (*it);
}
// -----------------------------------------------------------------------------------------
#if 0
PostgreSQLResult::COL PostgreSQLResult::get_col( const PostgreSQLResult::ROW::iterator& it )
{
return (*it);
}
#endif
// -----------------------------------------------------------------------------------------
PostgreSQLResult::~PostgreSQLResult()
{
}
// -----------------------------------------------------------------------------------------
PostgreSQLResult::PostgreSQLResult( const pqxx::result& res )
void PostgreSQLInterface::makeResult(DBResult& dbres, const pqxx::result& res )
{
for( result::const_iterator c = res.begin(); c != res.end(); ++c )
{
COL col;
DBResult::COL col;
for( pqxx::result::tuple::size_type i = 0; i < c.size(); i++ )
col.push_back( c[i].as<string>() );
row.push_back(col);
dbres.row().push_back(col);
}
}
// -----------------------------------------------------------------------------------------
extern "C" DBInterface* create_postgresqlinterface()
{
return new PostgreSQLInterface();
}
// -----------------------------------------------------------------------------------------
extern "C" void destroy_postgresqlinterface(DBInterface* p)
{
delete p;
}
// -----------------------------------------------------------------------------------------
......@@ -8,94 +8,40 @@
#include <iostream>
#include <pqxx/pqxx>
#include <PassiveTimer.h>
#include <DBInterface.h>
// ----------------------------------------------------------------------------
class PostgreSQLResult;
// ----------------------------------------------------------------------------
class PostgreSQLInterface
class PostgreSQLInterface : public DBNetInterface
{
public:
PostgreSQLInterface();
~PostgreSQLInterface();
bool connect( const std::string& host, const std::string& user, const std::string& pswd, const std::string& dbname );
bool close();
bool isConnection();
bool ping(); // проверка доступности БД
bool nconnect( const std::string& host, const std::string& user, const std::string& pswd, const std::string& dbname ) override;
bool close() override;
bool isConnection() override;
bool ping() override; // проверка доступности БД
PostgreSQLResult query( const std::string& q );
const std::string lastQuery();
DBResult query( const std::string& q ) override;
const std::string lastQuery() override;
bool insert( const std::string& q );
bool insert( const std::string& q ) override;
bool insertAndSaveRowid( const std::string& q );
double insert_id();
double insert_id() override;
void save_inserted_id( const pqxx::result& res );
std::string error();
const std::string error() override;
protected:
private:
void makeResult(DBResult& dbres, const pqxx::result& res );
std::shared_ptr<pqxx::connection> db;
std::string lastQ;
std::string lastE;
double last_inserted_id;
};
// ----------------------------------------------------------------------------------
class PostgreSQLResult
{
public:
PostgreSQLResult() {}
explicit PostgreSQLResult( const pqxx::result& res );
~PostgreSQLResult();
typedef std::vector<std::string> COL;
typedef std::list<COL> ROW;
typedef ROW::iterator iterator;
inline iterator begin()
{
return row.begin();
}
inline iterator end()
{
return row.end();
}
inline operator bool()
{
return !row.empty();
}
inline int size()
{
return row.size();
}
inline bool empty()
{
return row.empty();
}
// ----------------------------------------------------------------------------
// ROW
static int as_int( const PostgreSQLResult::iterator&, int col );
static double as_double( const PostgreSQLResult::iterator&, int col );
static std::string as_string( const PostgreSQLResult::iterator&, int col );
// ----------------------------------------------------------------------------
// COL
static int num_cols( const PostgreSQLResult::iterator& );
static int as_int( const PostgreSQLResult::COL::iterator& );
static double as_double( const PostgreSQLResult::COL::iterator& );
static std::string as_string(const PostgreSQLResult::COL::iterator& );
//----------------------------------------------------------------------------
protected:
ROW row;
};
// ----------------------------------------------------------------------------
#endif
// ----------------------------------------------------------------------------------
......@@ -17,7 +17,7 @@ int main(int argc, char** argv)
{
PostgreSQLInterface db;
if( !db.connect("localhost", "dbadmin", "dbadmin", dbname) )
if( !db.nconnect("localhost", "dbadmin", "dbadmin", dbname) )
{
cerr << "db connect error: " << db.error() << endl;
return 1;
......@@ -27,7 +27,7 @@ int main(int argc, char** argv)
stringstream q;
q << "SELECT * from main_history";
PostgreSQLResult r = db.query(q.str());
DBResult r = db.query(q.str());
if( !r )
{
......@@ -35,13 +35,13 @@ int main(int argc, char** argv)
return 1;
}
for( PostgreSQLResult::iterator it = r.begin(); it != r.end(); it++ )
for( DBResult::iterator it = r.begin(); it != r.end(); it++ )
{
cout << "ROW: ";
PostgreSQLResult::COL col(*it);
DBResult::COL col(*it);
for( PostgreSQLResult::COL::iterator cit = it->begin(); cit != it->end(); cit++ )
cout << PostgreSQLResult::as_string(cit) << "(" << PostgreSQLResult::as_double(cit) << ") | ";
for( DBResult::COL::iterator cit = it->begin(); cit != it->end(); cit++ )
cout << DBResult::as_string(cit) << "(" << DBResult::as_double(cit) << ") | ";
cout << endl;
}
......
......@@ -52,6 +52,20 @@ bool SQLiteInterface::ping()
return db && ( sqlite3_db_status(db, 0, NULL, NULL, 0) == SQLITE_OK );
}
// -----------------------------------------------------------------------------------------
bool SQLiteInterface::connect( const std::string& param )
{
std::string dbfile;
std::string::size_type pos = param.find_first_of(":");
if( pos != std::string::npos )
{
dbfile = param.substr(0, pos);
std::string create_str = param.substr(pos + 1, std::string::npos);
if( create_str == "true" )
return connect( dbfile, true );
}
return connect( dbfile, false );
}
// -----------------------------------------------------------------------------------------
bool SQLiteInterface::connect( const string& dbfile, bool create )
{
// т.к. sqlite3 по умолчанию, создаёт файл при открытии, то проверим "сами"
......@@ -133,10 +147,10 @@ bool SQLiteInterface::checkResult( int rc )
return true;
}
// -----------------------------------------------------------------------------------------
SQLiteResult SQLiteInterface::query( const string& q )
DBResult SQLiteInterface::query( const string& q )
{
if( !db )
return SQLiteResult();
return DBResult();
// char* errmsg = 0;
sqlite3_stmt* pStmt;
......@@ -149,12 +163,14 @@ SQLiteResult SQLiteInterface::query( const string& q )
{
sqlite3_finalize(pStmt);
queryok = false;
return SQLiteResult();
return DBResult();
}
lastQ = q;
queryok = true;
return SQLiteResult(pStmt, true);
DBResult dbres;
makeResult(dbres, pStmt, true);
return dbres;
}
// -----------------------------------------------------------------------------------------
bool SQLiteInterface::wait( sqlite3_stmt* stmt, int result )
......@@ -175,7 +191,7 @@ bool SQLiteInterface::wait( sqlite3_stmt* stmt, int result )
return false;
}
// -----------------------------------------------------------------------------------------
string SQLiteInterface::error()
const string SQLiteInterface::error()
{
if( db )
lastE = sqlite3_errmsg(db);
......@@ -188,7 +204,7 @@ const string SQLiteInterface::lastQuery()
return lastQ;
}
// -----------------------------------------------------------------------------------------
int SQLiteInterface::insert_id()
double SQLiteInterface::insert_id()
{
if( !db )
return 0;
......@@ -201,56 +217,7 @@ bool SQLiteInterface::isConnection()
return connected;
}
// -----------------------------------------------------------------------------------------
int SQLiteResult::num_cols( const SQLiteResult::iterator& it )
{
return it->size();
}
// -----------------------------------------------------------------------------------------
int SQLiteResult::as_int( const SQLiteResult::iterator& it, int col )
{
// if( col<0 || col >it->size() )
// return 0;
return uni_atoi( (*it)[col] );
}
// -----------------------------------------------------------------------------------------
double SQLiteResult::as_double( const SQLiteResult::iterator& it, int col )
{
return atof( ((*it)[col]).c_str() );
}
// -----------------------------------------------------------------------------------------
string SQLiteResult::as_string( const SQLiteResult::iterator& it, int col )
{
return ((*it)[col]);
}
// -----------------------------------------------------------------------------------------
int SQLiteResult::as_int( const SQLiteResult::COL::iterator& it )
{
return uni_atoi( (*it) );
}
// -----------------------------------------------------------------------------------------
double SQLiteResult::as_double( const SQLiteResult::COL::iterator& it )
{
return atof( (*it).c_str() );
}
// -----------------------------------------------------------------------------------------
std::string SQLiteResult::as_string( const SQLiteResult::COL::iterator& it )
{
return (*it);
}
// -----------------------------------------------------------------------------------------
#if 0
SQLiteResult::COL SQLiteResult::get_col( SQLiteResult::ROW::iterator& it )
{
return (*it);
}
#endif
// -----------------------------------------------------------------------------------------
SQLiteResult::~SQLiteResult()
{
}
// -----------------------------------------------------------------------------------------
SQLiteResult::SQLiteResult( sqlite3_stmt* s, bool finalize )
void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize )
{
do
{
......@@ -264,7 +231,7 @@ SQLiteResult::SQLiteResult( sqlite3_stmt* s, bool finalize )
return;
}
COL c;
DBResult::COL c;
for( int i = 0; i < n; i++ )
{
......@@ -276,7 +243,7 @@ SQLiteResult::SQLiteResult( sqlite3_stmt* s, bool finalize )
c.push_back("");
}
res.push_back(c);
dbres.row().push_back(c);
}
while( sqlite3_step(s) == SQLITE_ROW );
......@@ -284,3 +251,13 @@ SQLiteResult::SQLiteResult( sqlite3_stmt* s, bool finalize )
sqlite3_finalize(s);
}
// -----------------------------------------------------------------------------------------
extern "C" DBInterface* create_sqliteinterface()
{
return new SQLiteInterface();
}
// -----------------------------------------------------------------------------------------
extern "C" void destroy_sqliteinterface(DBInterface* p)
{
delete p;
}
// -----------------------------------------------------------------------------------------
......@@ -30,6 +30,7 @@
#include <iostream>
#include <sqlite3.h>
#include "PassiveTimer.h"
#include <DBInterface.h>
// ----------------------------------------------------------------------------
/*! \page SQLiteIntarface Интерфейс к SQLite
......@@ -47,19 +48,19 @@
stringstream q;
q << "SELECT * from main_history";
SQLiteResult r = db.query(q.str());
DBResult r = db.query(q.str());
if( !r )
{
cerr << "db connect error: " << db.error() << endl;
return 1;
}
for( SQLiteResult::iterator it=r.begin(); it!=r.end(); it++ )
for( DBResult::iterator it=r.begin(); it!=r.end(); it++ )
{
cout << "ROW: ";
SQLiteResult::COL col(*it);
for( SQLiteResult::COL::iterator cit = it->begin(); cit!=it->end(); cit++ )
cout << SQLiteResult::as_string(cit) << "(" << SQLiteResult::as_double(cit) << ") | ";
DBResult::COL col(*it);
for( DBResult::COL::iterator cit = it->begin(); cit!=it->end(); cit++ )
cout << DBResult::as_string(cit) << "(" << DBResult::as_double(cit) << ") | ";
cout << endl;
}
......@@ -81,19 +82,18 @@
// PRAGMA journal_mode = MEMORY
// При этом конечно есть риск потерять данные при выключении..
// ----------------------------------------------------------------------------
class SQLiteResult;
// ----------------------------------------------------------------------------
class SQLiteInterface
class SQLiteInterface : public DBInterface
{
public:
SQLiteInterface();
~SQLiteInterface();
bool connect( const std::string& dbfile, bool create = false );
bool close();
bool isConnection();
bool ping(); // проверка доступности БД
bool connect( const std::string& param ) override;
bool connect( const std::string& dbfile, bool create );
bool close() override;
bool isConnection() override;
bool ping() override; // проверка доступности БД
void setOperationTimeout( timeout_t msec );
inline timeout_t getOperationTimeout()
......@@ -110,13 +110,13 @@ class SQLiteInterface
return opCheckPause;
}
SQLiteResult query( const std::string& q );
const std::string lastQuery();
DBResult query( const std::string& q ) override;
const std::string lastQuery() override;
bool insert( const std::string& q );
int insert_id();
bool insert( const std::string& q ) override;
double insert_id() override;
std::string error();
const std::string error() override;
protected:
......@@ -125,6 +125,7 @@ class SQLiteInterface
private:
void makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize = true );
sqlite3* db;
// sqlite3_stmt* curStmt;
......@@ -136,59 +137,6 @@ class SQLiteInterface
timeout_t opTimeout;
timeout_t opCheckPause;
};
// ----------------------------------------------------------------------------------
class SQLiteResult
{
public:
SQLiteResult() {}
SQLiteResult( sqlite3_stmt* s, bool finalize = true );
~SQLiteResult();
typedef std::vector<std::string> COL;
typedef std::deque<COL> ROW;
typedef ROW::iterator iterator;
inline iterator begin()
{
return res.begin();
}
inline iterator end()
{
return res.end();
}
inline operator bool()
{
return !res.empty();
}
inline int size()
{
return res.size();
}
inline bool empty()
{
return res.empty();
}
// ----------------------------------------------------------------------------
static int num_cols( const SQLiteResult::iterator& );
// ROW
static int as_int( const SQLiteResult::iterator&, int col );
static double as_double( const SQLiteResult::iterator&, int col );
static std::string as_string( const SQLiteResult::iterator&, int col );
// ----------------------------------------------------------------------------
// COL
static int as_int( const SQLiteResult::COL::iterator& );
static double as_double( const SQLiteResult::COL::iterator& );
static std::string as_string( const SQLiteResult::COL::iterator& );
protected:
ROW res;
};
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
#endif
......
......@@ -21,7 +21,7 @@ int main(int argc, char** argv)
stringstream q;
q << "SELECT * from main_history";
SQLiteResult r = db.query(q.str());
DBResult r = db.query(q.str());
if( !r )
{
......@@ -29,13 +29,13 @@ int main(int argc, char** argv)
return 1;
}
for( SQLiteResult::iterator it = r.begin(); it != r.end(); it++ )
for( DBResult::iterator it = r.begin(); it != r.end(); it++ )
{
cout << "ROW: ";
SQLiteResult::COL col(*it);
DBResult::COL col(*it);
for( SQLiteResult::COL::iterator cit = it->begin(); cit != it->end(); cit++ )
cout << SQLiteResult::as_string(cit) << "(" << SQLiteResult::as_double(cit) << ") | ";
for( DBResult::COL::iterator cit = it->begin(); cit != it->end(); cit++ )
cout << DBResult::as_string(cit) << "(" << DBResult::as_double(cit) << ") | ";
cout << endl;
}
......
#ifndef DBInterface_H_
#define DBInterface_H_
// --------------------------------------------------------------------------
#include <string>
#include <deque>
#include <vector>
#include "UniSetTypes.h"
// --------------------------------------------------------------------------
/*! Асбстрактный класс для доступа к БД
*/
class DBResult;
class DBInterface
{
public:
DBInterface(){};
virtual ~DBInterface(){};
// Функция подключения к БД, параметры подключения зависят от типа БД
virtual bool connect( const std::string& param ) = 0;
virtual bool close() = 0;
virtual bool isConnection() = 0;
virtual bool ping() = 0; // проверка доступности БД
virtual DBResult query( const std::string& q ) = 0;
virtual const std::string lastQuery() = 0;
virtual bool insert( const std::string& q ) = 0;
virtual double insert_id() = 0;
virtual const std::string error() = 0;
};
// ----------------------------------------------------------------------------------
class DBNetInterface : public DBInterface
{
public:
DBNetInterface(){};
virtual ~DBNetInterface(){};
// Для сетевых БД параметры должны быть в формате user@host:pswd:dbname
virtual bool connect( const std::string& param );
virtual bool nconnect( const std::string& host, const std::string& user, const std::string& pswd, const std::string& dbname ) = 0;
};
// ----------------------------------------------------------------------------------
class DBResult
{
public:
DBResult() {}
virtual ~DBResult(){};
typedef std::vector<std::string> COL;
typedef std::deque<COL> ROW;
typedef ROW::iterator iterator;
ROW& row();
iterator begin();
iterator end();
operator bool();
size_t size();
bool empty();
// ----------------------------------------------------------------------------
// ROW
static int as_int( const DBResult::iterator& it, int col );
static double as_double( const DBResult::iterator& it, int col );
static std::string as_string( const DBResult::iterator& it, int col );
// ----------------------------------------------------------------------------
// COL
static int as_int( const DBResult::COL::iterator& it );
static double as_double(const DBResult::COL::iterator& it );
static std::string as_string( const DBResult::COL::iterator& it );
static int num_cols( const DBResult::iterator& it );
// ----------------------------------------------------------------------------
protected:
ROW row_;
};
// the types of the class factories
typedef DBInterface* create_dbinterface_t();
typedef void destroy_dbinterface_t(DBInterface*);
// --------------------------------------------------------------------------
#endif // DBInterface_H_
// --------------------------------------------------------------------------
#include "DBInterface.h"
// ------------------------------------------------------------------------------------------
bool DBNetInterface::connect( const std::string& param )
{
std::string host;
std::string user;
std::string pswd;
std::string dbname;
std::string::size_type pos = param.find_first_of("@");
std::string::size_type prev = 0;
if( pos != std::string::npos )
user = param.substr(prev, pos);
prev = pos + 1;
pos = param.find_first_of(":", prev);
if( pos != std::string::npos )
host = param.substr(prev, pos);
prev = pos + 1;
pos = param.find_first_of(":", prev);
if( pos != std::string::npos )
pswd = param.substr(prev, pos);
prev = pos + 1;
pos = param.find_first_of(":", prev);
if( pos != std::string::npos )
dbname = param.substr(prev, pos);
return nconnect( host, user, pswd, dbname );
}
//--------------------------------------------------------------------------------------------
DBResult::ROW& DBResult::row()
{
return row_;
}
// ----------------------------------------------------------------------------
DBResult::iterator DBResult::begin()
{
return row_.begin();
}
// ----------------------------------------------------------------------------
DBResult::iterator DBResult::end()
{
return row_.end();
}
// ----------------------------------------------------------------------------
DBResult::operator bool()
{
return !row_.empty();
}
// ----------------------------------------------------------------------------
size_t DBResult::size()
{
return row_.size();
}
// ----------------------------------------------------------------------------
bool DBResult::empty()
{
return row_.empty();
}
// ----------------------------------------------------------------------------
int DBResult::as_int( const DBResult::iterator& it, int col )
{
return UniSetTypes::uni_atoi( (*it)[col] );
}
// ----------------------------------------------------------------------------
double DBResult::as_double( const DBResult::iterator& it, int col )
{
return atof( ((*it)[col]).c_str() );
}
// ----------------------------------------------------------------------------
std::string DBResult::as_string( const DBResult::iterator& it, int col )
{
return ((*it)[col]);
}
// ----------------------------------------------------------------------------
int DBResult::num_cols( const DBResult::iterator& it )
{
return it->size();
}
// ----------------------------------------------------------------------------
int DBResult::as_int( const DBResult::COL::iterator& it )
{
return UniSetTypes::uni_atoi( (*it) );
}
// ----------------------------------------------------------------------------
double DBResult::as_double(const DBResult::COL::iterator& it )
{
return atof( (*it).c_str() );
}
// ----------------------------------------------------------------------------
std::string DBResult::as_string( const DBResult::COL::iterator& it )
{
return (*it);
}
// ----------------------------------------------------------------------------
#if 0
DBResult::COL DBResult::get_col( DBResult::iterator& it )
{
return (*it);
}
#endif
\ No newline at end of file
......@@ -4,7 +4,7 @@
noinst_LTLIBRARIES = libServices.la
libServices_la_CPPFLAGS = $(SIGC_CFLAGS)
libServices_la_LIBADD = $(SIGC_LIBS)
libServices_la_SOURCES = DBServer.cc
libServices_la_SOURCES = DBServer.cc DBInterface.cc
local-clean:
rm -rf *iSK.cc
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment