UniSet  2.6.0
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 );
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 
117  virtual bool insert( const std::string& q ) override;
118  virtual double insert_id() override;
119 
120  virtual const std::string error() override;
121 
122  protected:
123 
124  bool wait( sqlite3_stmt* stmt, int result );
125  static bool checkResult( int rc );
126 
127  private:
128 
129  void makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize = true );
130  sqlite3* db;
131  // sqlite3_stmt* curStmt;
132 
133  std::string lastQ;
134  std::string lastE;
135  bool queryok; // успешность текущего запроса
136  bool connected;
137 
138  timeout_t opTimeout;
139  timeout_t opCheckPause;
140  };
141  // ----------------------------------------------------------------------------------
142 } // end of namespace uniset
143 // ----------------------------------------------------------------------------
144 #endif
145 // ----------------------------------------------------------------------------------