UniSet  2.7.0
DBInterface.h
1 #ifndef DBInterface_H_
2 #define DBInterface_H_
3 // --------------------------------------------------------------------------
4 #include <string>
5 #include <deque>
6 #include <vector>
7 #include <unordered_map>
8 #include "UniSetTypes.h"
9 // --------------------------------------------------------------------------
10 namespace uniset
11 {
12  class DBResult;
13 
16  {
17  public:
18 
19  DBInterface() {}
20  virtual ~DBInterface() {}
21 
22  // Функция подключения к БД, параметры подключения зависят от типа БД
23  virtual bool connect( const std::string& param ) = 0;
24  virtual bool close() = 0;
25  virtual bool isConnection() const = 0;
26  virtual bool ping() const = 0; // проверка доступности БД
27 
28  virtual DBResult query( const std::string& q ) = 0;
29  virtual void cancel_query() {}; // попытка отменить текущий запрос
30  virtual const std::string lastQuery() = 0;
31  virtual bool insert( const std::string& q ) = 0;
32  virtual double insert_id() = 0;
33  virtual const std::string error() = 0;
34  };
35  // ----------------------------------------------------------------------------------
36  class DBNetInterface : public DBInterface
37  {
38  public:
39 
40  DBNetInterface() {}
41  virtual ~DBNetInterface() {}
42 
43  // Для сетевых БД параметры должны быть в формате user@host:pswd:dbname:port
44  virtual bool connect( const std::string& param );
45  virtual bool nconnect( const std::string& host, const std::string& user, const std::string& pswd,
46  const std::string& dbname, unsigned int port ) = 0;
47  };
48  // ----------------------------------------------------------------------------------
49  class DBRowIterator;
50 
51  class DBResult
52  {
53  public:
54 
55  DBResult() {}
56  virtual ~DBResult() {}
57 
58  typedef std::vector<std::string> COL;
59  typedef std::deque<COL> ROW;
60  typedef DBRowIterator iterator;
61 
62  ROW& row();
63  iterator begin();
64  iterator end();
65  operator bool() const;
66  size_t size() const;
67  bool empty() const;
68 
70  void setColName( int index, const std::string& name );
71 
73  int getColIndex( const std::string& name );
74 
75  // slow function
76  std::string getColName( int index );
77 
78  // ----------------------------------------------------------------------------
79  // COL
80  static int as_int( const DBResult::COL::iterator& it );
81  static double as_double(const DBResult::COL::iterator& it );
82  static std::string as_string( const DBResult::COL::iterator& it );
83  static size_t num_cols( const DBResult::iterator& it );
84  // ----------------------------------------------------------------------------
85 
86  protected:
87 
88  ROW row_;
89 
90  std::unordered_map<std::string, int> colname;
91  };
92  // ----------------------------------------------------------------------------------
94  public std::iterator<std::bidirectional_iterator_tag,
95  DBResult::ROW::value_type,
96  DBResult::ROW::difference_type,
97  DBResult::ROW::pointer,
98  DBResult::ROW::reference>
99  {
100 
101  public:
102 
103  std::string as_string( const char* name ) const;
104  std::string as_string( const std::string& name ) const;
105  int as_int( const std::string& name ) const;
106  double as_double( const std::string& name ) const;
107 
108  std::string as_string( int col ) const;
109  int as_int( int col ) const;
110  double as_double( int col ) const;
111 
112  size_t num_cols() const;
113 
114  typename DBRowIterator::pointer operator->();
115  typename DBRowIterator::reference operator*() const;
116 
117  DBRowIterator( const DBRowIterator& it );
118 
119  bool operator!=(DBRowIterator const& ) const;
120  bool operator==(DBRowIterator const& ) const;
121  DBRowIterator& operator+(int) noexcept;
122  DBRowIterator& operator+=(int) noexcept;
123  DBRowIterator& operator++() noexcept; // ++x
124  DBRowIterator operator++(int) noexcept; // x++
125  DBRowIterator& operator-(int) noexcept;
126  DBRowIterator operator--(int) noexcept; // x--
127  DBRowIterator& operator--() noexcept; // --x
128  DBRowIterator& operator-=(int) noexcept;
129 
130  private:
131  friend class DBResult;
132  DBRowIterator( DBResult& dbres, const DBResult::ROW::iterator& );
133 
134  DBResult& dbres;
135  DBResult::ROW::iterator it;
136  };
137 
138  // ----------------------------------------------------------------------------------
140  {
141  void operator()(DBInterface* p) const
142  {
143  try
144  {
145  delete p;
146  }
147  catch(...) {}
148  }
149  };
150  // ----------------------------------------------------------------------------------
151  // the types of the class factories
152  typedef std::shared_ptr<DBInterface> create_dbinterface_t();
153  // --------------------------------------------------------------------------
154 } // end of uniset namespace
155 // -------------------------------------------------------------------------
156 #endif // DBInterface_H_
157 // --------------------------------------------------------------------------
Definition: CallbackTimer.h:29
Definition: DBInterface.h:93
Definition: DBInterface.h:36
Definition: DBInterface.h:139
Definition: DBInterface.h:15
Definition: DBInterface.h:51