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

Database Class Reference

Connection information and pool. More...

#include <Database.h>

Collaboration diagram for Database:

Collaboration graph
List of all members.

Public Types

typedef std::list< OPENDB * > opendb_v

Public Member Functions

 Database (const std::string &database, IError *=NULL)
 Use file.
 Database (Mutex &, const std::string &database, IError *=NULL)
 Use file + thread safe.
virtual ~Database ()
bool Connected ()
 try to establish connection with given host
void RegErrHandler (IError *)
void error (Query &, const char *format,...)
void error (Query &, const std::string &)
OPENDBgrabdb ()
 Request a database connection.
void freedb (OPENDB *odb)
std::string safestr (const std::string &)
 Escape string - change all ' to ''.
std::string xmlsafestr (const std::string &)
 Make string xml safe.
int64_t a2bigint (const std::string &)
 Convert string to 64-bit integer.
uint64_t a2ubigint (const std::string &)
 Convert string to unsigned 64-bit integer.

Private Member Functions

 Database (const Database &)
Databaseoperator= (const Database &)
void error (const char *format,...)

Private Attributes

std::string database
opendb_v m_opendbs
IErrorm_errhandler
bool m_embedded
Mutexm_mutex
bool m_b_use_mutex

Classes

class  Lock
 Mutex helper class. More...
class  Mutex
 Mutex container class, used by Lock. More...
struct  OPENDB
 Connection pool struct. More...

Detailed Description

Connection information and pool.

Definition at line 61 of file Database.h.


Member Typedef Documentation

typedef std::list<OPENDB *> Database::opendb_v

Definition at line 96 of file Database.h.


Constructor & Destructor Documentation

Database::Database ( const std::string &  database,
IError = NULL 
)

Use file.

Definition at line 54 of file Database.cpp.

00055 :database(d)
00056 ,m_errhandler(e)
00057 ,m_embedded(true)
00058 ,m_mutex(m_mutex)
00059 ,m_b_use_mutex(false)
00060 {
00061 }

Database::Database ( Mutex ,
const std::string &  database,
IError = NULL 
)

Use file + thread safe.

Definition at line 64 of file Database.cpp.

00065 :database(d)
00066 ,m_errhandler(e)
00067 ,m_embedded(true)
00068 ,m_mutex(m)
00069 ,m_b_use_mutex(true)
00070 {
00071 }

Database::~Database (  )  [virtual]

Definition at line 74 of file Database.cpp.

References error(), and m_opendbs.

00075 {
00076         for (opendb_v::iterator it = m_opendbs.begin(); it != m_opendbs.end(); it++)
00077         {
00078                 OPENDB *p = *it;
00079                 sqlite3_close(p -> db);
00080         }
00081         while (m_opendbs.size())
00082         {
00083                 opendb_v::iterator it = m_opendbs.begin();
00084                 OPENDB *p = *it;
00085                 if (p -> busy)
00086                 {
00087                         error("destroying Database object before Query object");
00088                 }
00089                 delete p;
00090                 m_opendbs.erase(it);
00091         }
00092 }

Database::Database ( const Database  )  [inline, private]

Definition at line 145 of file Database.h.

00145 : m_mutex(m_mutex) {}


Member Function Documentation

bool Database::Connected (  ) 

try to establish connection with given host

Definition at line 200 of file Database.cpp.

References freedb(), and grabdb().

00201 {
00202         OPENDB *odb = grabdb();
00203         if (!odb)
00204         {
00205                 return false;
00206         }
00207         freedb(odb);
00208         return true;
00209 }

void Database::RegErrHandler ( IError  ) 

Definition at line 95 of file Database.cpp.

References m_errhandler.

00096 {
00097         m_errhandler = p;
00098 }

void Database::error ( Query ,
const char *  format,
  ... 
)

Definition at line 173 of file Database.cpp.

References m_errhandler.

Referenced by Query::error(), error(), Query::execute(), Query::fetch_row(), Query::get_result(), grabdb(), ~Database(), and Query::~Query().

00174 {
00175         if (m_errhandler)
00176         {
00177                 va_list ap;
00178                 char errstr[5000];
00179                 va_start(ap, format);
00180 #ifdef WIN32
00181                 vsprintf(errstr, format, ap);
00182 #else
00183                 vsnprintf(errstr, 5000, format, ap);
00184 #endif
00185                 va_end(ap);
00186                 m_errhandler -> error(*this, q, errstr);
00187         }
00188 }

void Database::error ( Query ,
const std::string &   
)

Definition at line 191 of file Database.cpp.

References error(), and m_errhandler.

00192 {
00193         if (m_errhandler)
00194         {
00195                 m_errhandler -> error(*this, q, msg);
00196         }
00197 }

Database::OPENDB * Database::grabdb (  ) 

Request a database connection.

The "grabdb" method is used by the Query class, so that each object instance of Query gets a unique database connection. I will reimplement your connection check logic in the Query class, as that's where the database connection is really used. It should be used something like this. { Query q(db); if (!q.Connected()) return false; q.execute("delete * from user"); // well maybe not }

When the Query object is deleted, then "freedb" is called - the database connection stays open in the m_opendbs vector. New Query objects can then reuse old connections.

Definition at line 101 of file Database.cpp.

References database, error(), m_b_use_mutex, m_mutex, and m_opendbs.

Referenced by Connected().

00102 {
00103         Lock lck(m_mutex, m_b_use_mutex);
00104         OPENDB *odb = NULL;
00105 
00106         for (opendb_v::iterator it = m_opendbs.begin(); it != m_opendbs.end(); it++)
00107         {
00108                 odb = *it;
00109                 if (!odb -> busy)
00110                 {
00111                         break;
00112                 }
00113                 else
00114                 {
00115                         odb = NULL;
00116                 }
00117         }
00118         if (!odb)
00119         {
00120                 odb = new OPENDB;
00121                 if (!odb)
00122                 {
00123                         error("grabdb: OPENDB struct couldn't be created");
00124                         return NULL;
00125                 }
00126                 int rc = sqlite3_open(database.c_str(), &odb -> db);
00127                 if (rc)
00128                 {
00129                         error("Can't open database: %s\n", sqlite3_errmsg(odb -> db));
00130                         sqlite3_close(odb -> db);
00131                         delete odb;
00132                         return NULL;
00133                 }
00134                 odb -> busy = true;
00135                 m_opendbs.push_back(odb);
00136         }
00137         else
00138         {
00139                 odb -> busy = true;
00140         }
00141         return odb;
00142 }

void Database::freedb ( OPENDB odb  ) 

Definition at line 145 of file Database.cpp.

References m_b_use_mutex, and m_mutex.

Referenced by Connected(), and Query::~Query().

00146 {
00147         Lock lck(m_mutex, m_b_use_mutex);
00148         if (odb)
00149         {
00150                 odb -> busy = false;
00151         }
00152 }

std::string Database::safestr ( const std::string &   ) 

Escape string - change all ' to ''.

Definition at line 271 of file Database.cpp.

00272 {
00273         std::string str2;
00274         for (size_t i = 0; i < str.size(); i++)
00275         {
00276                 switch (str[i])
00277                 {
00278                 case '\'':
00279                 case '\\':
00280                 case 34:
00281                         str2 += '\'';
00282                 default:
00283                         str2 += str[i];
00284                 }
00285         }
00286         return str2;
00287 }

std::string Database::xmlsafestr ( const std::string &   ) 

Make string xml safe.

Definition at line 290 of file Database.cpp.

00291 {
00292         std::string str2;
00293         for (size_t i = 0; i < str.size(); i++)
00294         {
00295                 switch (str[i])
00296                 {
00297                 case '&':
00298                         str2 += "&amp;";
00299                         break;
00300                 case '<':
00301                         str2 += "&lt;";
00302                         break;
00303                 case '>':
00304                         str2 += "&gt;";
00305                         break;
00306                 case '"':
00307                         str2 += "&quot;";
00308                         break;
00309                 case '\'':
00310                         str2 += "&apos;";
00311                         break;
00312                 default:
00313                         str2 += str[i];
00314                 }
00315         }
00316         return str2;
00317 }

int64_t Database::a2bigint ( const std::string &   ) 

Convert string to 64-bit integer.

Definition at line 320 of file Database.cpp.

00321 {
00322         int64_t val = 0;
00323         bool sign = false;
00324         size_t i = 0;
00325         if (str[i] == '-')
00326         {
00327                 sign = true;
00328                 i++;
00329         }
00330         for (; i < str.size(); i++)
00331         {
00332                 val = val * 10 + (str[i] - 48);
00333         }
00334         return sign ? -val : val;
00335 }

uint64_t Database::a2ubigint ( const std::string &   ) 

Convert string to unsigned 64-bit integer.

Definition at line 338 of file Database.cpp.

00339 {
00340         uint64_t val = 0;
00341         for (size_t i = 0; i < str.size(); i++)
00342         {
00343                 val = val * 10 + (str[i] - 48);
00344         }
00345         return val;
00346 }

Database& Database::operator= ( const Database  )  [inline, private]

Definition at line 146 of file Database.h.

00146 { return *this; }

void Database::error ( const char *  format,
  ... 
) [private]

Definition at line 155 of file Database.cpp.

References error(), and m_errhandler.

00156 {
00157         if (m_errhandler)
00158         {
00159                 va_list ap;
00160                 char errstr[5000];
00161                 va_start(ap, format);
00162 #ifdef WIN32
00163                 vsprintf(errstr, format, ap);
00164 #else
00165                 vsnprintf(errstr, 5000, format, ap);
00166 #endif
00167                 va_end(ap);
00168                 m_errhandler -> error(*this, errstr);
00169         }
00170 }


Member Data Documentation

std::string Database::database [private]

Definition at line 149 of file Database.h.

Referenced by grabdb().

Definition at line 150 of file Database.h.

Referenced by grabdb(), and ~Database().

Definition at line 151 of file Database.h.

Referenced by error(), and RegErrHandler().

bool Database::m_embedded [private]

Definition at line 152 of file Database.h.

Definition at line 153 of file Database.h.

Referenced by freedb(), and grabdb().

bool Database::m_b_use_mutex [private]

Definition at line 154 of file Database.h.

Referenced by freedb(), and grabdb().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2006 by Anders Hedström