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