UniSet  2.6.0
DBInterface.h
1 #ifndef DBInterface_H_
2 #define DBInterface_H_
3 // --------------------------------------------------------------------------
4 #include <string>
5 #include <deque>
6 #include <vector>
7 #include "UniSetTypes.h"
8 // --------------------------------------------------------------------------
9 namespace uniset
10 {
11  class DBResult;
12 
15  {
16  public:
17 
18  DBInterface() {};
19  virtual ~DBInterface() {};
20 
21  // Функция подключения к БД, параметры подключения зависят от типа БД
22  virtual bool connect( const std::string& param ) = 0;
23  virtual bool close() = 0;
24  virtual bool isConnection() const = 0;
25  virtual bool ping() const = 0; // проверка доступности БД
26 
27  virtual DBResult query( const std::string& q ) = 0;
28  virtual const std::string lastQuery() = 0;
29  virtual bool insert( const std::string& q ) = 0;
30  virtual double insert_id() = 0;
31  virtual const std::string error() = 0;
32  };
33  // ----------------------------------------------------------------------------------
34  class DBNetInterface : public DBInterface
35  {
36  public:
37 
38  DBNetInterface() {};
39  virtual ~DBNetInterface() {};
40 
41  // Для сетевых БД параметры должны быть в формате user@host:pswd:dbname:port
42  virtual bool connect( const std::string& param );
43  virtual bool nconnect( const std::string& host, const std::string& user, const std::string& pswd,
44  const std::string& dbname, unsigned int port ) = 0;
45  };
46  // ----------------------------------------------------------------------------------
47  class DBResult
48  {
49  public:
50 
51  DBResult() {}
52  virtual ~DBResult() {};
53 
54  typedef std::vector<std::string> COL;
55  typedef std::deque<COL> ROW;
56  typedef ROW::iterator iterator;
57 
58  ROW& row();
59  iterator begin();
60  iterator end();
61  operator bool() const;
62  size_t size() const;
63  bool empty() const;
64 
65  // ----------------------------------------------------------------------------
66  // ROW
67  static int as_int( const DBResult::iterator& it, int col );
68  static double as_double( const DBResult::iterator& it, int col );
69  static std::string as_string( const DBResult::iterator& it, int col );
70  // ----------------------------------------------------------------------------
71  // COL
72  static int as_int( const DBResult::COL::iterator& it );
73  static double as_double(const DBResult::COL::iterator& it );
74  static std::string as_string( const DBResult::COL::iterator& it );
75  static size_t num_cols( const DBResult::iterator& it );
76  // ----------------------------------------------------------------------------
77 
78  protected:
79 
80  ROW row_;
81  };
82  // ----------------------------------------------------------------------------------
84  {
85  void operator()(DBInterface* p) const
86  {
87  try
88  {
89  delete p;
90  }
91  catch(...) {}
92  }
93  };
94  // ----------------------------------------------------------------------------------
95  // the types of the class factories
96  typedef std::shared_ptr<DBInterface> create_dbinterface_t();
97  // --------------------------------------------------------------------------
98 } // end of uniset namespace
99 // -------------------------------------------------------------------------
100 #endif // DBInterface_H_
101 // --------------------------------------------------------------------------