home *** CD-ROM | disk | FTP | other *** search
- // =================================================================
- // Dbase.cpp
- // =================================================================
- // Harold Kasperink / John Dekker
- // Dr. Dobb's Journal 1997
- // =================================================================
- // Database class
- // =================================================================
- #include "dbarray.h"
- #include "database.h"
- #include "oracle.h"
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::CDbase
- ////////////////////////////////////////////////////////////////////
- CDbase::CDbase()
- {
- m_bConnected = FALSE;
- m_bContinue = TRUE;
- m_pszUsr = 0;
- m_pszPsswd = 0;
- m_pszDb = 0;
- // Lock mutexes
- m_mtxStartSql.Lock();
- m_mtxEndSql.Lock();
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::~CDbase
- ////////////////////////////////////////////////////////////////////
- CDbase::~CDbase()
- {
- // Set flag to stop
- m_mtxInuse.Lock();
- m_bContinue = FALSE;
- m_pCommand = 0;
-
- // Unlock mutexes
- m_mtxEndSql.Unlock();
- m_mtxStartSql.Unlock();
-
- // Wait until thread is ready
- Join();
- DeleteConnectInfo();
-
- m_mtxStartSql.Unlock();
- m_mtxInuse.Unlock();
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::Do
- ////////////////////////////////////////////////////////////////////
- // Do Database command
- ////////////////////////////////////////////////////////////////////
- long CDbase::Do(CDbCommand &command)
- {
- // Lock do command
- m_mtxDoCmd.Lock();
-
- // Set command
- m_pCommand = &command;
-
- // Unlock, means thread will perform command
- m_mtxStartSql.Unlock();
-
- // Lock, means thread completed command
- m_mtxEndSql.Lock();
- long lSql = m_lSql;
-
- // Unlock do command
- m_mtxDoCmd.Unlock();
-
- return lSql;
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::Process
- ////////////////////////////////////////////////////////////////////
- // Process = thread loop
- ////////////////////////////////////////////////////////////////////
- void CDbase::Process()
- {
- while (m_bContinue) {
- // Wait for command
- m_mtxStartSql.Lock();
-
- if (m_pCommand != 0) {
- // Connect if not connected yet
- if (!m_bConnected)
- m_lSql = Connect();
-
- // Execute command
- if (m_lSql == 0)
- m_lSql = m_pCommand->Execute();
-
- // Ready with command execution
- m_mtxEndSql.Unlock();
- }
- }
- Disconnect();
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::Trylock
- ////////////////////////////////////////////////////////////////////
- // Check if this object is inuse, and if not
- // reserve it for caller
- ////////////////////////////////////////////////////////////////////
- boolean CDbase::TryLock()
- {
- if (m_bContinue)
- return m_mtxInuse.TryLock();
-
- return FALSE;
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::Trylock
- ////////////////////////////////////////////////////////////////////
- // Set lock
- ////////////////////////////////////////////////////////////////////
- void CDbase::Lock()
- {
- m_mtxInuse.Lock();
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::Unlock
- ////////////////////////////////////////////////////////////////////
- // Release lock
- ////////////////////////////////////////////////////////////////////
- void CDbase::Unlock()
- {
- m_mtxInuse.Unlock();
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::ConnectInfo
- ////////////////////////////////////////////////////////////////////
- // Set connect information strings
- ////////////////////////////////////////////////////////////////////
- void CDbase::ConnectInfo(const char *szUsr, const char *szPsswd, const char *szDB)
- {
- DeleteConnectInfo();
-
- if (szUsr != 0)
- m_pszUsr = strdup(szUsr);
-
- if (szPsswd != 0)
- m_pszPsswd = strdup(szPsswd);
-
- if (szDB != 0)
- m_pszDb = strdup(szDB);
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::DeleteConnectInfo
- ////////////////////////////////////////////////////////////////////
- // free connect information strings
- ////////////////////////////////////////////////////////////////////
- void CDbase::DeleteConnectInfo()
- {
- if (m_pszUsr != 0)
- delete m_pszUsr;
-
- if (m_pszPsswd != 0)
- delete m_pszPsswd;
-
- if (m_pszDb != 0)
- delete m_pszDb;
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::Connect
- ////////////////////////////////////////////////////////////////////
- long CDbase::Connect()
- {
- if (m_bConnected)
- return 0;
-
- CDbConnect lConnect(*this, m_pszUsr);
- lConnect.Do();
-
- return 1;
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::Disconnect
- ////////////////////////////////////////////////////////////////////
- void CDbase::Disconnect()
- {
- CDbDisConnect lDisconnect(*this);
- lDisconnect.Do();
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::Commit
- ////////////////////////////////////////////////////////////////////
- void CDbase::Commit(boolean bUnlock)
- {
- CDbCommit lCommit(*this, bUnlock);
- lCommit.Do();
- }
-
- ////////////////////////////////////////////////////////////////////
- // CDbase::Rollback
- ////////////////////////////////////////////////////////////////////
- void CDbase::Rollback(boolean bUnlock)
- {
- CDbRollback lRollback(*this, bUnlock);
- lRollback.Do();
- }
-
-