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 
00007 /*
00008 Copyright (C) 2001  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
00034 #define _DATABASE_H
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 MYSQLW_NAMESPACE
00051 namespace MYSQLW_NAMESPACE {
00052 #endif
00053 
00054 class IError;
00055 class Query;
00056 class Mutex;
00057 
00058 
00060 class Database 
00061 {
00064 public:
00065         class Mutex {
00066         public:
00067                 Mutex();
00068                 ~Mutex();
00069                 void Lock();
00070                 void Unlock();
00071         private:
00072 #ifdef _WIN32
00073                 HANDLE m_mutex;
00074 #else
00075                 pthread_mutex_t m_mutex;
00076 #endif
00077         };
00079 private:
00080         class Lock {
00081         public:
00082                 Lock(Mutex& mutex,bool use);
00083                 ~Lock();
00084         private:
00085                 Mutex& m_mutex;
00086                 bool m_b_use;
00087         };
00088 public:
00090         struct OPENDB {
00091                 OPENDB() : busy(false) {}
00092                 MYSQL mysql;
00093                 bool busy;
00094         };
00095         typedef std::list<OPENDB *> opendb_v;
00096 
00097 public:
00099         Database(const std::string& database,
00100                 IError * = NULL);
00101 
00103         Database(Mutex& ,const std::string& database,
00104                 IError * = NULL);
00105 
00107         Database(const std::string& host,
00108                 const std::string& user,
00109                 const std::string& password = "",
00110                 const std::string& database = "",
00111                 IError * = NULL);
00112 
00114         Database(Mutex& ,const std::string& host,
00115                 const std::string& user,
00116                 const std::string& password = "",
00117                 const std::string& database = "",
00118                 IError * = NULL);
00119 
00120         virtual ~Database();
00121 
00123         virtual void OnMyInit(OPENDB *);
00124 
00126         bool Connected();
00127 
00128         void RegErrHandler(IError *);
00129         void error(Query&,const char *format, ...);
00130 
00146         OPENDB *grabdb();
00147         void freedb(OPENDB *odb);
00148 
00149         // utility
00150         std::string safestr(const std::string& );
00151         std::string unsafestr(const std::string& );
00152         std::string xmlsafestr(const std::string& );
00153 
00154         int64_t a2bigint(const std::string& );
00155         uint64_t a2ubigint(const std::string& );
00156 
00157 private:
00158         Database(const Database& ) : m_mutex(m_mutex) {}
00159         Database& operator=(const Database& ) { return *this; }
00160         void error(const char *format, ...);
00161         //
00162         std::string host;
00163         std::string user;
00164         std::string password;
00165         std::string database;
00166         opendb_v m_opendbs;
00167         IError *m_errhandler;
00168         bool m_embedded;
00169         Mutex& m_mutex;
00170         bool m_b_use_mutex;
00171 };
00172 
00173 #ifdef MYSQLW_NAMESPACE
00174 } //namespace MYSQLW_NAMESPACE {
00175 #endif
00176 
00177 
00178 #endif // _DATABASE_H
Page, code, and content Copyright (C) 2006 by Anders Hedström