Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

cConnection.cpp

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /* POLiTe - Persistent Object Library Test                                    */
00004 /*                                        Ph.D. Thesis by Mgr. Michal Kopecky */
00005 /*                                                                            */
00006 /* Charles University Prague                                                  */
00007 /*                                                                            */
00008 /******************************************************************************/
00009 /*                                                                            */
00010 /* File name: ...                                                             */
00011 /* Module: ......                                                             */
00012 /*                                                                            */
00013 /******************************************************************************/
00014 
00015 // Standard Header(s)
00016 #include <malloc.h>
00017 #include <string.h>
00018 
00019 // Common POLiTe Header(s)
00020 #include <lDefs.h>
00021 #include <lTypes.h>
00022 #include <lTrace.h>
00023 #include <lCmds.h>
00024 
00025 // Own Header
00026 #include <cConnection.h>
00027 
00028 // Other POLiTe Header(s)
00029 #include <cDatabase.h>
00030 #include <cCursor.h>
00031 #include <cObjectBuffer.h>
00032 
00033 Connection::Connection(
00034                         Database *DB,
00035                         const char *UserName,
00036                         const char *Password,
00037                         const int LocHandle,
00038                         const int GlobHandle
00039                         )
00040 //Creates a new database connection
00041 {
00042   #ifdef C_CONNECTION_TRACE
00043          logmsg("Connection::Connection() invoked");
00044          #endif
00045   _Database = DB;
00046   StrCpy(_UserName = NULL, UserName);
00047   StrCpy(_Password = NULL, Password);
00048   _LocalHandle = LocHandle;
00049   _Handle = GlobHandle;
00050   _DefaultCursor = NULL;
00051   };
00052 
00053 Connection::~Connection()
00054 //Connection object destructor
00055 {
00056   #ifdef C_CONNECTION_TRACE
00057          logmsg("Connection::~Connection() invoked");
00058          #endif
00059   if (_Database != NULL) {
00060          if (_DefaultCursor != NULL)
00061             delete _DefaultCursor;
00062          Disconnect();
00063          };
00064   }
00065 
00066 bool Connection::Sql(const char * SqlCommand)
00067 {
00068         #ifdef C_CONNECTION_TRACE
00069                 logmsg("Connection::Sql(\"%s\") invoked", SqlCommand);
00070         #endif
00071         if (!ObjectCache.UpdateAll(this))
00072                 return(false);
00073         return(_Sql(SqlCommand));
00074 };
00075 
00076 bool Connection::Commit()
00077 //Commit database, after storing modified objects
00078 {
00079         #ifdef C_CONNECTION_TRACE
00080                 logmsg("Connection::Commit() invoked");
00081         #endif
00082         ObjectCache._ResetForUpdateFlags = true;
00083         // commit has to reset this _ForUpdate flag in object
00084         // because database locks on objects will be released
00085         if (!ObjectCache.UpdateAll(this))
00086         {
00087                 ObjectCache._ResetForUpdateFlags = false;
00088                 return(false);
00089         };
00090         ObjectCache._ResetForUpdateFlags = false;
00091         return(_Commit());
00092 };
00093 
00094 bool Connection::Rollback()
00095 //Rollback database, then destroy all copies in memory
00096 {
00097         #ifdef C_CONNECTION_TRACE
00098                 logmsg("Connection::Rollback() invoked");
00099         #endif
00100         if (!_Rollback())
00101                 return(false);
00102         return(ObjectCache._RemoveAll(this));
00103 };
00104 
00105 bool Connection::Savepoint(const char * const Name)
00106 //Create savepoint, after storing all modified objects
00107 {
00108         #ifdef C_CONNECTION_TRACE
00109                 logmsg("Connection::Savepoint(\"%s\") invoked",Name);
00110         #endif
00111         if (!ObjectCache.UpdateAll(this))
00112                 return(false);
00113         return(_Savepoint(Name));
00114 };
00115 
00116 bool Connection::RollbackToSavepoint(const char * const Name)
00117 //Rollback to savepoint, then destroy all data in memory
00118 {
00119         #ifdef C_CONNECTION_TRACE
00120                 logmsg("Connection::RollbackToSavepoint(\"%s\") invoked",Name);
00121         #endif
00122         if (!_RollbackToSavepoint(Name))
00123                 return(false);
00124         return(ObjectCache._RemoveAll(this));
00125 };
00126 
00127 bool Connection::Disconnect()
00128 //Commit work and disconnect from database
00129 {
00130         #ifdef C_CONNECTION_TRACE
00131                 logmsg("Connection::Disconnect() invoked");
00132         #endif
00133         _Database->_DisposeConnectionHandles(_LocalHandle,_Handle);
00134         _Database = NULL;
00135         StrFree(_UserName);
00136         StrFree(_Password);
00137         free(this);
00138         return(true);
00139 };
00140 
00141 bool Connection::Abort()
00142 //Rollback work and disconnect from database
00143 {
00144         #ifdef C_CONNECTION_TRACE
00145                 logmsg("Connection::Abort() invoked");
00146         #endif
00147         _Database->_DisposeConnectionHandles(_LocalHandle,_Handle);
00148         _Database = NULL;
00149         StrFree(_UserName);
00150         StrFree(_Password);
00151         free(this);
00152         return(true);
00153 };
00154 
00155 Connection &Connection::operator << (
00156                         const char * const SqlCommand
00157                         )
00158 {
00159         (void)Sql(SqlCommand);
00160         return *this;
00161 };
00162 
00163 Connection &Connection::operator << (
00164         const class Cmd &Command
00165         )
00166 {
00167         (void)Command.ExecuteCommand(*this);
00168         return(*this);
00169 };
00170 
00171 bool Connection::operator == (
00172         const class Connection &DbConn
00173         ) const
00174 //Tests, if the Database connections are the same
00175 {
00176         return (this == &DbConn);
00177 };
00178 
00179 bool Connection::operator != (
00180         const class Connection &DbConn
00181         ) const
00182 //Tests, if the Database connections are not the same
00183 {
00184         return (this != &DbConn);
00185 };
00186 
00187 bool Connection::_Open()
00188 {
00189         return _DefaultCursor->_Open();
00190 };
00191 
00192 bool Connection::_Prepare(const char *SqlCommand)
00193 {
00194         return _DefaultCursor->_Prepare(SqlCommand);
00195 };
00196 
00197 bool Connection::_PreExecBind(
00198         const char *const VarName,
00199         const void *VarAddr,
00200         const int VarLen,
00201         const char VarType,
00202         const short *VarInd
00203         )
00204 {
00205         return _DefaultCursor->_PreExecBind(VarName,VarAddr,VarLen,VarType,VarInd);
00206 };
00207 
00208 bool Connection::_Execute()
00209 {
00210         return _DefaultCursor->_Execute();
00211 };
00212 
00213 bool Connection::_PreFetchBind(
00214         const int Position,
00215         const void *VarAddr,
00216         const int VarLen,
00217         const char VarType,
00218         const short *VarInd
00219         )
00220 {
00221         return _DefaultCursor->_PreFetchBind(Position,VarAddr,VarLen,VarType,VarInd);
00222 };
00223 
00224 bool Connection::_FetchNext()
00225 {
00226         return _DefaultCursor->_FetchNext();
00227 };
00228 
00229 bool Connection::_FetchPrev()
00230 {
00231         return _DefaultCursor->_FetchPrev();
00232 };
00233 
00234 bool Connection::_Close()
00235 {
00236         return _DefaultCursor->_Close();
00237 };
00238 
00239 // Set corresponging patrs of attribute _Strategies
00240 // that holds all concurency strategies
00241 bool Connection::SetUpdatingStrategy(enum UpdatingStrategy anUpdatingStrategy)
00242 {
00243         #ifdef C_CONNECTION_TRACE
00244                 logmsg("bool Connection::SetUpdatingStrategy(...) invoked");
00245         #endif
00246         switch (anUpdatingStrategy)
00247         {
00248                 case US_Current:
00249                         break;
00250                 case US_Inherited:
00251                         _Strategies = _Strategies & ~US_MASK | (unsigned int)_Database->CurrentUpdatingStrategy();
00252                         break;
00253                 case US_Default:
00254                         _Strategies = _Strategies & ~US_MASK | DEFAULT_UPDATING_STRATEGY;
00255                         break;
00256                 default:
00257                         _Strategies = _Strategies & ~US_MASK | (unsigned int)anUpdatingStrategy;
00258         };
00259         #ifdef C_OBJREF_TRACE
00260                 logmsg("bool Connection::SetUpdatingStrategy(...) finished");
00261         #endif
00262         return true;
00263 };
00264 bool Connection::SetWaitingStrategy(enum WaitingStrategy aWaitingStrategy)
00265 {
00266         #ifdef C_CONNECTION_TRACE
00267                 logmsg("bool Connection::SetWaitingStrategy(...) invoked");
00268         #endif
00269         switch (aWaitingStrategy)
00270         {
00271                 case WS_Current:
00272                         break;
00273                 case WS_Inherited:
00274                         _Strategies = _Strategies & ~WS_MASK | (unsigned int)_Database->CurrentWaitingStrategy();
00275                         break;
00276                 case WS_Default:
00277                         _Strategies = _Strategies & ~WS_MASK | DEFAULT_WAITING_STRATEGY;
00278                         break;
00279                 default:
00280                         _Strategies = _Strategies & ~WS_MASK | (unsigned int)aWaitingStrategy;
00281         };
00282         #ifdef C_CONNECTION_TRACE
00283                 logmsg("bool Connection::SetWaitingStrategy(...) finished");
00284         #endif
00285         return true;
00286 };
00287 bool Connection::SetLockingStrategy(enum LockingStrategy aLockingStrategy)
00288 {
00289         #ifdef C_CONNECTION_TRACE
00290                 logmsg("bool Connection::SetLockingStrategy(...) invoked");
00291         #endif
00292         switch (aLockingStrategy)
00293         {
00294                 case LS_Current:
00295                         break;
00296                 case LS_Inherited:
00297                         _Strategies = _Strategies & ~LS_MASK | (unsigned int)_Database->CurrentLockingStrategy();
00298                         break;
00299                 case LS_Default:
00300                         _Strategies = _Strategies & ~LS_MASK | DEFAULT_LOCKING_STRATEGY;
00301                         break;
00302                 default:
00303                         _Strategies = _Strategies & ~LS_MASK | (unsigned int)aLockingStrategy;
00304         };
00305         #ifdef C_CONNECTION_TRACE
00306                 logmsg("bool Connection::SetLockingStrategy(...) finished");
00307         #endif
00308         return true;
00309 };
00310 bool Connection::SetReadingStrategy(enum ReadingStrategy aReadingStrategy)
00311 {
00312         #ifdef C_CONNECTION_TRACE
00313                 logmsg("bool Connection::SetReadingStrategy(...) invoked");
00314         #endif
00315         switch (aReadingStrategy)
00316         {
00317                 case RS_Current:
00318                         break;
00319                 case RS_Inherited:
00320                         _Strategies = _Strategies & ~RS_MASK | (unsigned int)_Database->CurrentReadingStrategy();
00321                         break;
00322                 case RS_Default:
00323                         _Strategies = _Strategies & ~RS_MASK | DEFAULT_READING_STRATEGY;
00324                         break;
00325                 default:
00326                         _Strategies = _Strategies & ~RS_MASK | (unsigned int)aReadingStrategy;
00327         };
00328         #ifdef C_CONNECTION_TRACE
00329                 logmsg("bool Connection::SetReadingStrategy(...) finished");
00330         #endif
00331         return true;
00332 };

Generated on Sun Jul 14 20:51:13 2002 for POLiTe by doxygen1.2.16