UniSet  2.6.0
SQLiteInterface.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // --------------------------------------------------------------------------
00020 //----------------------------------------------------------------------------
00021 #ifndef SQLiteInterface_H_
00022 #define SQLiteInterface_H_
00023 // ---------------------------------------------------------------------------
00024 #include <string>
00025 #include <deque>
00026 #include <vector>
00027 #include <iostream>
00028 #include <sqlite3.h>
00029 #include "PassiveTimer.h"
00030 #include <DBInterface.h>
00031 // -------------------------------------------------------------------------
00032 namespace uniset
00033 {
00034 // ----------------------------------------------------------------------------
00079 // ----------------------------------------------------------------------------
00080 // Памятка:
00081 // Включение режима для журнала - "вести в памяти" (чтобы поберечь CompactFlash)
00082 // PRAGMA journal_mode = MEMORY
00083 // При этом конечно есть риск потерять данные при выключении..
00084 // ----------------------------------------------------------------------------
00085 class SQLiteInterface:
00086     public DBInterface
00087 {
00088     public:
00089 
00090         SQLiteInterface();
00091         ~SQLiteInterface();
00092 
00093         virtual bool connect( const std::string& param ) override;
00094         bool connect( const std::string& dbfile, bool create );
00095         virtual bool close() override;
00096         virtual bool isConnection() const override;
00097         virtual bool ping() const override;
00098 
00099         void setOperationTimeout( timeout_t msec );
00100         inline timeout_t getOperationTimeout()
00101         {
00102             return opTimeout;
00103         }
00104 
00105         inline void setOperationCheckPause( timeout_t msec )
00106         {
00107             opCheckPause = msec;
00108         }
00109         inline timeout_t getOperationCheckPause()
00110         {
00111             return opCheckPause;
00112         }
00113 
00114         virtual DBResult query( const std::string& q ) override;
00115         virtual const std::string lastQuery() override;
00116 
00117         virtual bool insert( const std::string& q ) override;
00118         virtual double insert_id() override;
00119 
00120         virtual const std::string error() override;
00121 
00122     protected:
00123 
00124         bool wait( sqlite3_stmt* stmt, int result );
00125         static bool checkResult( int rc );
00126 
00127     private:
00128 
00129         void makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize = true );
00130         sqlite3* db;
00131         // sqlite3_stmt* curStmt;
00132 
00133         std::string lastQ;
00134         std::string lastE;
00135         bool queryok;    // успешность текущего запроса
00136         bool connected;
00137 
00138         timeout_t opTimeout;
00139         timeout_t opCheckPause;
00140 };
00141 // ----------------------------------------------------------------------------------
00142 } // end of namespace uniset
00143 // ----------------------------------------------------------------------------
00144 #endif
00145 // ----------------------------------------------------------------------------------