Forums  +  C++ and Sockets  +  C++ and SQL: MySQL, sqlite, ODBC  +  Miscellaneous Projects
Logo
~Database~
~ C++ ~
~Contact~

Database.h

Go to the documentation of this file.
00001 /*
00002  **     Database.h
00003  **
00004  **     Published / author: 2005-08-12 / grymse@alhem.net
00005  **/
00006 
00007 /*
00008 Copyright (C) 2001-2006  Anders Hedstrom
00009 
00010 This program is made available under the terms of the GNU GPL.
00011 
00012 If you would like to use this program in a closed-source application,
00013 a separate license agreement is available. For information about 
00014 the closed-source license agreement for this program, please
00015 visit http://www.alhem.net/sqlwrapped/license.html and/or
00016 email license@alhem.net.
00017 
00018 This program is free software; you can redistribute it and/or
00019 modify it under the terms of the GNU General Public License
00020 as published by the Free Software Foundation; either version 2
00021 of the License, or (at your option) any later version.
00022 
00023 This program is distributed in the hope that it will be useful,
00024 but WITHOUT ANY WARRANTY; without even the implied warranty of
00025 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026 GNU General Public License for more details.
00027 
00028 You should have received a copy of the GNU General Public License
00029 along with this program; if not, write to the Free Software
00030 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00031 */
00032 
00033 #ifndef _DATABASE_H_SQLITE
00034 #define _DATABASE_H_SQLITE
00035 
00036 #ifdef _WIN32
00037 #include <windows.h>
00038 #else
00039 #include <pthread.h>
00040 #endif
00041 #include <string>
00042 #include <list>
00043 #ifdef WIN32
00044 typedef unsigned __int64 uint64_t;
00045 typedef __int64 int64_t;
00046 #else
00047 #include <stdint.h>
00048 #endif
00049 
00050 #ifdef SQLITEW_NAMESPACE
00051 namespace SQLITEW_NAMESPACE {
00052 #endif
00053 
00054 
00055 class IError;
00056 class Query;
00057 class Mutex;
00058 
00059 
00061 class Database 
00062 {
00063 public:
00066         class Mutex {
00067         public:
00068                 Mutex();
00069                 ~Mutex();
00070                 void Lock();
00071                 void Unlock();
00072         private:
00073 #ifdef _WIN32
00074                 HANDLE m_mutex;
00075 #else
00076                 pthread_mutex_t m_mutex;
00077 #endif
00078         };
00079 private:
00081         class Lock {
00082         public:
00083                 Lock(Mutex& mutex,bool use);
00084                 ~Lock();
00085         private:
00086                 Mutex& m_mutex;
00087                 bool m_b_use;
00088         };
00089 public:
00091         struct OPENDB {
00092                 OPENDB() : busy(false) {}
00093                 sqlite3 *db;
00094                 bool busy;
00095         };
00096         typedef std::list<OPENDB *> opendb_v;
00097 
00098 public:
00100         Database(const std::string& database,
00101                 IError * = NULL);
00102 
00104         Database(Mutex& ,const std::string& database,
00105                 IError * = NULL);
00106 
00107         virtual ~Database();
00108 
00110         bool Connected();
00111 
00112         void RegErrHandler(IError *);
00113         void error(Query&,const char *format, ...);
00114         void error(Query&,const std::string& );
00115 
00131         OPENDB *grabdb();
00132         void freedb(OPENDB *odb);
00133 
00135         std::string safestr(const std::string& );
00137         std::string xmlsafestr(const std::string& );
00138 
00140         int64_t a2bigint(const std::string& );
00142         uint64_t a2ubigint(const std::string& );
00143 
00144 private:
00145         Database(const Database& ) : m_mutex(m_mutex) {}
00146         Database& operator=(const Database& ) { return *this; }
00147         void error(const char *format, ...);
00148         //
00149         std::string database;
00150         opendb_v m_opendbs;
00151         IError *m_errhandler;
00152         bool m_embedded;
00153         Mutex& m_mutex;
00154         bool m_b_use_mutex;
00155 };
00156 
00157 
00158 #ifdef SQLITEW_NAMESPACE
00159 } // namespace SQLITEW_NAMESPACE {
00160 #endif
00161 
00162 #endif // _DATABASE_H
Page, code, and content Copyright (C) 2006 by Anders Hedström