
|
Database Class Reference#include <Database.h>
Collaboration diagram for Database:
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.
|
Session * | grabdb () |
| Get a free connection.
|
void | freedb (Session *) |
| Free a connection in use.
|
Query * | GetQuery () |
| Get query class compatible with this Database implementation.
|
Query * | GetQuery (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 () |
IError * | GetErrHandler () |
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 |
IError * | m_errhandler |
Mutex & | m_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.
|
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.
|
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 |
( |
|
) |
|
|
void Database::error |
( |
const std::string & |
text |
) |
|
|
void Database::freedb |
( |
Session * |
|
) |
|
|
char Database::GetDecimalChar |
( |
|
) |
|
|
IError* Database::GetErrHandler |
( |
|
) |
[inline] |
|
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 |
( |
|
) |
|
|
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 }
|
|
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 |
|
) |
|
|
void Database::SetEscapeChar |
( |
char |
|
) |
|
|
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 += "&";
00318 break;
00319 case '<':
00320 str2 += "<";
00321 break;
00322 case '>':
00323 str2 += ">";
00324 break;
00325 case '"':
00326 str2 += """;
00327 break;
00328 case '\'':
00329 str2 += "'";
00330 break;
00331 default:
00332 str2 += str[i];
00333 }
00334 }
00335 return str2;
00336 }
|
Member Data Documentation
The documentation for this class was generated from the following files:
|