UniSet  1.7.0
SQLiteInterface.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00023 //----------------------------------------------------------------------------
00024 #ifndef SQLiteInterface_H_
00025 #define SQLiteInterface_H_
00026 // ---------------------------------------------------------------------------
00027 #include <string>
00028 #include <list>
00029 #include <vector>
00030 #include <iostream>
00031 #include <sqlite3.h>
00032 #include "PassiveTimer.h"
00033 // ----------------------------------------------------------------------------
00034 class SQLiteResult;
00035 // ----------------------------------------------------------------------------
00080 // ----------------------------------------------------------------------------
00081 // Памятка:
00082 // Включение режима для журнала - "вести в памяти" (чтобы поберечь CompactFlash)
00083 // PRAGMA journal_mode = MEMORY
00084 //
00085 // ----------------------------------------------------------------------------
00086 class SQLiteInterface
00087 {
00088     public:
00089     
00090         SQLiteInterface();
00091         ~SQLiteInterface();
00092 
00093         bool connect( const std::string& dbfile, bool create = false );
00094         bool close();
00095         bool isConnection();
00096         bool ping(); // проверка доступности БД
00097             
00098         void setOperationTimeout( timeout_t msec );
00099         inline timeout_t getOperationTimeout(){ return opTimeout; }
00100 
00101         inline void setOperationCheckPause( timeout_t msec ){ opCheckPause = msec; }
00102         inline timeout_t getOperationCheckPause(){ return opCheckPause; }
00103 
00104         SQLiteResult query( const std::string& q );
00105         const std::string lastQuery();
00106 
00107         bool insert( const std::string& q );
00108         int insert_id();
00109 
00110         std::string error();
00111 
00112     protected:
00113 
00114         bool wait( sqlite3_stmt* stmt, int result );
00115         static bool checkResult( int rc );
00116 
00117     private:
00118     
00119         sqlite3* db;
00120         // sqlite3_stmt* curStmt;
00121         
00122         std::string lastQ;
00123         std::string lastE;
00124         bool queryok;   // успешность текущего запроса
00125         bool connected;
00126         
00127         timeout_t opTimeout;
00128         timeout_t opCheckPause;
00129 };
00130 // ----------------------------------------------------------------------------------
00131 class SQLiteResult
00132 {
00133     public:
00134         SQLiteResult(){}
00135         SQLiteResult( sqlite3_stmt* s, bool finalize=true );
00136         ~SQLiteResult();
00137 
00138         typedef std::vector<std::string> COL;
00139         typedef std::list<COL> ROW;
00140 
00141         typedef ROW::iterator iterator;
00142     
00143         inline iterator begin(){ return res.begin(); }
00144         inline iterator end(){ return res.end(); }
00145 
00146         inline operator bool(){ return !res.empty(); }
00147 
00148         inline int size(){ return res.size(); }
00149         inline bool empty(){ return res.empty(); }
00150 
00151     protected:
00152 
00153         ROW res;
00154 };
00155 // ----------------------------------------------------------------------------
00156 int num_cols( SQLiteResult::iterator& );
00157 // ROW
00158 int as_int( SQLiteResult::iterator&, int col );
00159 double as_double( SQLiteResult::iterator&, int col );
00160 std::string as_text( SQLiteResult::iterator&, int col );
00161 // ----------------------------------------------------------------------------
00162 // COL
00163 int as_int( SQLiteResult::COL::iterator& );
00164 double as_double( SQLiteResult::COL::iterator& );
00165 std::string as_string( SQLiteResult::COL::iterator& );
00166 // ----------------------------------------------------------------------------
00167 #endif
00168 // ----------------------------------------------------------------------------------