UniSet  2.2.1
DBInterface.h
00001 #ifndef DBInterface_H_
00002 #define DBInterface_H_
00003 // --------------------------------------------------------------------------
00004 #include <string>
00005 #include <deque>
00006 #include <vector>
00007 #include "UniSetTypes.h"
00008 // --------------------------------------------------------------------------
00011 class DBResult;
00012 
00013 class DBInterface
00014 {
00015     public:
00016 
00017         DBInterface() {};
00018         virtual ~DBInterface() {};
00019 
00020         // Функция подключения к БД, параметры подключения зависят от типа БД
00021         virtual bool connect( const std::string& param ) = 0;
00022         virtual bool close() = 0;
00023         virtual bool isConnection() = 0;
00024         virtual bool ping() = 0; // проверка доступности БД
00025 
00026         virtual DBResult query( const std::string& q ) = 0;
00027         virtual const std::string lastQuery() = 0;
00028         virtual bool insert( const std::string& q ) = 0;
00029         virtual double insert_id() = 0;
00030         virtual const std::string error() = 0;
00031 };
00032 // ----------------------------------------------------------------------------------
00033 class DBNetInterface : public DBInterface
00034 {
00035     public:
00036 
00037         DBNetInterface() {};
00038         virtual ~DBNetInterface() {};
00039 
00040         // Для сетевых БД параметры должны быть в формате user@host:pswd:dbname
00041         virtual bool connect( const std::string& param );
00042         virtual bool nconnect( const std::string& host, const std::string& user, const std::string& pswd, const std::string& dbname ) = 0;
00043 };
00044 // ----------------------------------------------------------------------------------
00045 class DBResult
00046 {
00047     public:
00048 
00049         DBResult() {}
00050         virtual ~DBResult() {};
00051 
00052         typedef std::vector<std::string> COL;
00053         typedef std::deque<COL> ROW;
00054         typedef ROW::iterator iterator;
00055 
00056         ROW& row();
00057         iterator begin();
00058         iterator end();
00059         operator bool();
00060         size_t size();
00061         bool empty();
00062 
00063         // ----------------------------------------------------------------------------
00064         // ROW
00065         static int as_int( const DBResult::iterator& it, int col );
00066         static double as_double( const DBResult::iterator& it, int col );
00067         static std::string as_string( const DBResult::iterator& it, int col );
00068         // ----------------------------------------------------------------------------
00069         // COL
00070         static int as_int( const DBResult::COL::iterator& it );
00071         static double as_double(const  DBResult::COL::iterator& it );
00072         static std::string as_string( const DBResult::COL::iterator& it );
00073         static int num_cols( const DBResult::iterator& it );
00074         // ----------------------------------------------------------------------------
00075 
00076     protected:
00077 
00078         ROW row_;
00079 };
00080 // ----------------------------------------------------------------------------------
00081 struct DBInterfaceDeleter
00082 {
00083     void operator()(DBInterface* p) const
00084     {
00085         try{ delete p; } catch(...) {}
00086     }
00087 };
00088 // ----------------------------------------------------------------------------------
00089 // the types of the class factories
00090 typedef std::shared_ptr<DBInterface> create_dbinterface_t();
00091 // --------------------------------------------------------------------------
00092 #endif // DBInterface_H_
00093 // --------------------------------------------------------------------------