Logo
~Database~
~ C++ ~
~Contact~

Database Class Reference

#include <Database.h>

Collaboration diagram for Database:

Collaboration graph
List of all members.

Public Member Functions

 Database (const std::string &dsn, const std::string &u, const std::string &auth, IError *p=NULL)
 Connect with data source name, username and password.
 Database (const std::string &driverconnect, IError *p=NULL)
 Connect with connection string.
 Database (Mutex &, const std::string &dsn, const std::string &u, const std::string &auth, IError *p=NULL)
 Connect with mutex for threadsafe connection pool, data source name, username and password.
 Database (Mutex &, const std::string &driverconnect, IError *p=NULL)
 Connect with mutex for threadsafe connection pool, connection string.
virtual ~Database ()
 Destructor frees all connections in connection pool, and will warn if any of the connections are still busy.
Sessiongrabdb ()
 Get a free connection.
void freedb (Session *)
 Free a connection in use.
QueryGetQuery ()
 Get query class compatible with this Database implementation.
QueryGetQuery (const std::string &sql)
 Get query class compatible with this Database implementation.
short errcode ()
void GetErrorText (char *)
void SetEscapeChar (char)
char GetEscapeChar ()
void SetDecimalChar (char)
char GetDecimalChar ()
IErrorGetErrHandler ()
void error (const std::string &text)
std::string safestr (const std::string &)
std::string xmlsafestr (const std::string &)

Private Attributes

short m_driverconnect
std::string m_host
std::string m_user
std::string m_password
std::list< Session * > m_sessions
short m_errc
char m_escape
char m_decimal
IErrorm_errhandler
Mutexm_mutex
bool m_b_use_mutex

Classes

class  Lock
 Mutex helper class. More...
class  Mutex
 Mutex container class, used by Lock. More...

Detailed Description

Definition at line 38 of file Database.h.


Constructor & Destructor Documentation

Database::Database const std::string &  dsn,
const std::string &  u,
const std::string &  auth,
IError p = NULL
 

Connect with data source name, username and password.

Parameters:
dsn Data source name
u Username
auth Password
p Ptr to log class

Definition at line 43 of file Database.cpp.

00044 :m_driverconnect(0)
00045 ,m_host(dsn)
00046 ,m_user(u)
00047 ,m_password(auth)
00048 ,m_errc(0)
00049 ,m_escape('\'')
00050 ,m_decimal(',')
00051 ,m_errhandler(p)
00052 ,m_mutex(m_mutex)
00053 ,m_b_use_mutex(false)
00054 {
00055 }

Database::Database const std::string &  driverconnect,
IError p = NULL
 

Connect with connection string.

Parameters:
driverconnect ODBC driver connection string
p Ptr to log class

Definition at line 58 of file Database.cpp.

00059 :m_driverconnect(1)
00060 ,m_host(driverconnect)
00061 ,m_errc(0)
00062 ,m_escape('\'')
00063 ,m_decimal(',')
00064 ,m_errhandler(p)
00065 ,m_mutex(m_mutex)
00066 ,m_b_use_mutex(false)
00067 {
00068 }

Database::Database Mutex ,
const std::string &  dsn,
const std::string &  u,
const std::string &  auth,
IError p = NULL
 

Connect with mutex for threadsafe connection pool, data source name, username and password.

Parameters:
mutex Threadsafety mutex
dsn Data source name
u Username
auth Password
p Ptr to log class

Definition at line 71 of file Database.cpp.

00072 :m_driverconnect(0)
00073 ,m_host(dsn)
00074 ,m_user(u)
00075 ,m_password(auth)
00076 ,m_errc(0)
00077 ,m_escape('\'')
00078 ,m_decimal(',')
00079 ,m_errhandler(p)
00080 ,m_mutex(mutex)
00081 ,m_b_use_mutex(true)
00082 {
00083 }

Database::Database Mutex ,
const std::string &  driverconnect,
IError p = NULL
 

Connect with mutex for threadsafe connection pool, connection string.

Parameters:
mutex Threadsafety mutex
driverconnect ODBC driver connection string
p Ptr to log class

Definition at line 86 of file Database.cpp.

00087 :m_driverconnect(1)
00088 ,m_host(driverconnect)
00089 ,m_errc(0)
00090 ,m_escape('\'')
00091 ,m_decimal(',')
00092 ,m_errhandler(p)
00093 ,m_mutex(mutex)
00094 ,m_b_use_mutex(true)
00095 {
00096 }

Database::~Database  )  [virtual]
 

Destructor frees all connections in connection pool, and will warn if any of the connections are still busy.

Definition at line 99 of file Database.cpp.

References error(), and m_sessions.

00100 {
00101         for (std::list<Session *>::iterator it = m_sessions.begin(); it != m_sessions.end(); it++)
00102         {
00103                 if ((*it) -> IsBusy())
00104                 {
00105                         error("destroying Session object before Query object(s)\n");
00106                 }
00107                 Session *pSession = static_cast<Session*>(*it);
00108                 delete pSession;
00109         }
00110 }


Member Function Documentation

short Database::errcode  ) 
 

Definition at line 154 of file Database.cpp.

References m_errc.

00155 {
00156         return m_errc;
00157 }

void Database::error const std::string &  text  ) 
 

Definition at line 221 of file Database.cpp.

References m_errhandler.

Referenced by Session::Connect(), and ~Database().

00222 {
00223         if (m_errhandler)
00224         {
00225                 m_errhandler -> error(*this, text);
00226         }
00227 }

void Database::freedb Session  ) 
 

Free a connection in use.

Definition at line 147 of file Database.cpp.

References m_b_use_mutex, and m_mutex.

Referenced by Query::~Query().

00148 {
00149         Lock lck(m_mutex, m_b_use_mutex);
00150         odb -> SetBusy(false);
00151 }

char Database::GetDecimalChar  ) 
 

Definition at line 215 of file Database.cpp.

References m_decimal.

Referenced by Query::getstr().

00216 {
00217         return m_decimal;
00218 }

IError* Database::GetErrHandler  )  [inline]
 

Definition at line 114 of file Database.h.

Referenced by Query::error().

00114 { return m_errhandler; }

void Database::GetErrorText char *   ) 
 

Definition at line 160 of file Database.cpp.

References m_errc, and m_host.

00161 {
00162         short rc = m_errc;
00163 
00164         switch (rc)
00165         {
00166         case -1:
00167                 sprintf(dest, "%d: SQLAllocHandle(SQL_HANDLE_ENV) failed", rc);
00168                 break;
00169         case -2:
00170                 sprintf(dest, "%d: SQLSetEnvAttr() failed", rc);
00171                 break;
00172         case -3:
00173                 sprintf(dest, "%d: SQLAllocHandle(SQL_HANDLE_DBC) failed", rc);
00174                 break;
00175         case -4:
00176                 sprintf(dest, "%d: SQLConnect(%s) failed", rc, m_host.c_str());
00177                 break;
00178 
00179         default:
00180                 sprintf(dest, "%d: Undefined error code", rc);
00181         }
00182 }

char Database::GetEscapeChar  ) 
 

Definition at line 203 of file Database.cpp.

References m_escape.

Referenced by safestr().

00204 {
00205         return m_escape;
00206 }

Query * Database::GetQuery const std::string &  sql  ) 
 

Get query class compatible with this Database implementation.

Definition at line 191 of file Database.cpp.

00192 {
00193         return new Query(*this, sql);
00194 }

Query * Database::GetQuery  ) 
 

Get query class compatible with this Database implementation.

Definition at line 185 of file Database.cpp.

00186 {
00187         return new Query(*this);
00188 }

Session * Database::grabdb  ) 
 

Get a free connection.

Definition at line 113 of file Database.cpp.

References m_b_use_mutex, m_driverconnect, m_host, m_mutex, m_password, m_sessions, and m_user.

00114 {
00115         Lock lck(m_mutex, m_b_use_mutex);
00116         for (std::list<Session *>::iterator it = m_sessions.begin(); it != m_sessions.end(); it++)
00117         {
00118                 if (!(*it) -> IsBusy())
00119                 {
00120                         (*it) -> SetBusy(true);
00121                         return *it;
00122                 }
00123         }
00124         Session *tmp = new Session;
00125         int nResult;
00126         if (m_driverconnect)
00127         {
00128                 nResult = tmp -> Connect(*this, m_host);
00129         }
00130         else
00131         {
00132                 nResult = tmp -> Connect(*this, m_host,m_user,m_password);
00133         }
00134         if (nResult != 0)
00135         {
00136                 std::string strSqlState;
00137                 std::string strErrorMessage;
00138                 tmp -> GetLastError(strSqlState, strErrorMessage);
00139                 delete tmp;
00140                 throw strErrorMessage;
00141         }
00142         m_sessions.insert(m_sessions.end(), tmp);
00143         tmp -> SetBusy(true);
00144         return tmp;
00145 }

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

Definition at line 289 of file Database.cpp.

References GetEscapeChar().

00290 {
00291         std::string s2;
00292         for (size_t i = 0; i < s.size(); i++)
00293         {
00294                 switch (s[i])
00295                 {
00296                 case '\'':
00297                 case '\\':
00298                 case 34:
00299                         s2 += GetEscapeChar();
00300                 default:
00301                         s2 += s[i];
00302                 }
00303         }
00304         return s2;
00305 }

void Database::SetDecimalChar char   ) 
 

Definition at line 209 of file Database.cpp.

References m_decimal.

00210 {
00211         m_decimal = ch;
00212 }

void Database::SetEscapeChar char   ) 
 

Definition at line 197 of file Database.cpp.

References m_escape.

00198 {
00199         m_escape = ch;
00200 }

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

Definition at line 308 of file Database.cpp.

Referenced by Query::ReturnRowAsXML().

00309 {
00310         std::string str2;
00311 
00312         for (size_t i = 0; i < str.size(); i++)
00313         {
00314                 switch (str[i])
00315                 {
00316                 case '&':
00317                         str2 += "&amp;";
00318                         break;
00319                 case '<':
00320                         str2 += "&lt;";
00321                         break;
00322                 case '>':
00323                         str2 += "&gt;";
00324                         break;
00325                 case '"':
00326                         str2 += "&quot;";
00327                         break;
00328                 case '\'':
00329                         str2 += "&apos;";
00330                         break;
00331                 default:
00332                         str2 += str[i];
00333                 }
00334         }
00335         return str2;
00336 }


Member Data Documentation

bool Database::m_b_use_mutex [private]
 

Definition at line 131 of file Database.h.

Referenced by freedb(), and grabdb().

char Database::m_decimal [private]
 

Definition at line 128 of file Database.h.

Referenced by GetDecimalChar(), and SetDecimalChar().

short Database::m_driverconnect [private]
 

Definition at line 121 of file Database.h.

Referenced by grabdb().

short Database::m_errc [private]
 

Definition at line 126 of file Database.h.

Referenced by errcode(), and GetErrorText().

IError* Database::m_errhandler [private]
 

Definition at line 129 of file Database.h.

Referenced by error().

char Database::m_escape [private]
 

Definition at line 127 of file Database.h.

Referenced by GetEscapeChar(), and SetEscapeChar().

std::string Database::m_host [private]
 

Definition at line 122 of file Database.h.

Referenced by GetErrorText(), and grabdb().

Mutex& Database::m_mutex [private]
 

Definition at line 130 of file Database.h.

Referenced by freedb(), and grabdb().

std::string Database::m_password [private]
 

Definition at line 124 of file Database.h.

Referenced by grabdb().

std::list<Session *> Database::m_sessions [private]
 

Definition at line 125 of file Database.h.

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

std::string Database::m_user [private]
 

Definition at line 123 of file Database.h.

Referenced by grabdb().


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