Logo
~Sockets~
~Examples~
~Contact~


Database.cpp

Go to the documentation of this file.
00001 // Database.cpp
00002 /*
00003 Copyright (C) 2001-2005  Anders Hedstrom
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (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 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <sys/types.h>
00023 namespace SQLITE {
00024 #include <sqlite/sqlite.h>
00025 }
00026 // STL include begin
00027 #ifdef WIN32
00028 #pragma warning(push, 3)
00029 #endif
00030 //
00031 #include <vector>
00032 #include <string>
00033 //
00034 #ifdef WIN32
00035 #pragma warning(pop)
00036 #endif
00037 // STL include end
00038 using std::string; // fix broken include files
00039 
00040 #include "Session.h"
00041 #include "Database.h"
00042 
00043 
00044 #define DEB(x) x
00045 
00046 
00047 Database::Database(const string &f)
00048 :filename(new char[f.size() + 1])
00049 ,mode(0)
00050 {
00051         errc = 0;
00052         m_escape = '\\';
00053 
00054         strcpy(filename,f.c_str());
00055         
00056         freedb(grabdb());               // open one connection
00057 }
00058 
00059 Database::Database(char *f,int m)
00060 :filename(new char[strlen(f) + 1])
00061 ,mode(m)
00062 {
00063         errc = 0;
00064         m_escape = '\\';
00065 
00066         strcpy(filename,f);
00067         
00068         freedb(grabdb());               // open one connection
00069 }
00070 
00071 Database::~Database()
00072 {
00073         sessions_t::iterator it;
00074         if (filename)
00075                 delete filename;
00076         for (it = m_sessions.begin(); it != m_sessions.end(); it++)
00077         {
00078                 Session *p = *it;
00079                 p -> Disconnect();
00080         }
00081         while (m_sessions.size())
00082         {
00083                 it = m_sessions.begin();
00084                 if ((*it) -> IsBusy())
00085                 {
00086                         fprintf(stderr,"destroying Database object before Connect object(s)\n");
00087                 }
00088                 delete *it;
00089                 m_sessions.erase(it);
00090         }
00091 }
00092 
00093 Session *Database::grabdb()
00094 {
00095         sessions_t::iterator it;
00096         for (it = m_sessions.begin(); it != m_sessions.end(); it++)
00097         {
00098                 if (!(*it) -> IsBusy())
00099                 {
00100                         (*it) -> SetBusy(true);
00101                         return *it;
00102                 }
00103         }
00104         Session *tmp = new Session();
00105         m_sessions.insert(m_sessions.end(), tmp);
00106 DEB(    printf("number of Database sessions: %d\n",m_sessions.size());)
00107         tmp -> Connect(filename, mode);
00108         tmp -> SetBusy(true);
00109         return tmp;
00110 }
00111 
00112 void Database::freedb(Session *odb)
00113 {
00114         odb -> SetBusy(false);
00115 }
00116 
00117 short Database::errcode()
00118 {
00119         return errc;
00120 }
00121 
00122 void Database::debug(short val)
00123 {
00124         _debug = val;
00125 }
00126 
00127 short Database::debug()
00128 {
00129         return _debug;
00130 }
00131 
00132 
00133 Query *Database::GetQuery()
00134 {
00135         return new Query( this );
00136 }
00137 
00138 
00139 Query *Database::GetQuery(const string &sql)
00140 {
00141         return new Query( this, sql.c_str() );
00142 }
00143 
00144 
00145 void Database::SetEscapeChar(char ch)
00146 {
00147         m_escape = ch;
00148 }
00149 
00150 char Database::GetEscapeChar()
00151 {
00152         return m_decimal;
00153 }
00154 
00155 
00156 void Database::SetDecimalChar(char ch)
00157 {
00158         m_decimal = ch;
00159 }
00160 
00161 char Database::GetDecimalChar()
00162 {
00163         return m_decimal;
00164 }
00165 
00166 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4