UniSet  2.24.2
SQLiteInterface.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
20 //----------------------------------------------------------------------------
21 #ifndef SQLiteInterface_H_
22 #define SQLiteInterface_H_
23 // ---------------------------------------------------------------------------
24 #include <string>
25 #include <deque>
26 #include <vector>
27 #include <iostream>
28 #include <sqlite3.h>
29 #include "PassiveTimer.h"
30 #include <DBInterface.h>
31 // -------------------------------------------------------------------------
32 namespace uniset
33 {
34  // ----------------------------------------------------------------------------
79  // ----------------------------------------------------------------------------
80  // Памятка:
81  // Включение режима для журнала - "вести в памяти" (чтобы поберечь CompactFlash)
82  // PRAGMA journal_mode = MEMORY
83  // При этом конечно есть риск потерять данные при выключении..
84  // ----------------------------------------------------------------------------
86  public DBInterface
87  {
88  public:
89 
91  ~SQLiteInterface();
92 
93  virtual bool connect( const std::string& param ) override;
94  bool connect(const std::string& dbfile, bool create, int extra_sqlite_flags = 0 );
95  virtual bool close() override;
96  virtual bool isConnection() const override;
97  virtual bool ping() const override;
98 
99  void setOperationTimeout( timeout_t msec );
100  inline timeout_t getOperationTimeout()
101  {
102  return opTimeout;
103  }
104 
105  inline void setOperationCheckPause( timeout_t msec )
106  {
107  opCheckPause = msec;
108  }
109  inline timeout_t getOperationCheckPause()
110  {
111  return opCheckPause;
112  }
113 
114  virtual DBResult query( const std::string& q ) override;
115  virtual const std::string lastQuery() override;
116  bool lastQueryOK() const;
117 
118  virtual bool insert( const std::string& q ) override;
119  virtual double insert_id() override;
120 
121  virtual const std::string error() override;
122 
123  protected:
124 
125  bool wait( sqlite3_stmt* stmt, int result );
126  static bool checkResult( int rc );
127 
128  private:
129 
130  DBResult makeResult( sqlite3_stmt* s, bool finalize = true );
131  sqlite3* db;
132  // sqlite3_stmt* curStmt;
133 
134  std::string lastQ;
135  std::string lastE;
136  bool queryok; // успешность текущего запроса
137  bool connected;
138 
139  timeout_t opTimeout;
140  timeout_t opCheckPause;
141  };
142  // ----------------------------------------------------------------------------------
143 } // end of namespace uniset
144 // ----------------------------------------------------------------------------
145 #endif
146 // ----------------------------------------------------------------------------------
Definition: DBInterface.h:16
Definition: DBInterface.h:52
Definition: SQLiteInterface.h:87
Definition: CommonEventLoop.h:15