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

cObject.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 // Common POLiTe Header(s)
00016 #include <lTypes.h>
00017 
00018 // Own Header
00019 #include <cObject.h>                    // Database Object class header
00020 
00021 // Other POLiTe Header(s)
00022 #include <cObjectBuffer.h>                      // Object Buffer
00023 
00025 
00026 // The Object is the common ancestor of all classes for objects retrieved
00027 // from the database. Object can represent any projection onto some attributes
00028 // of join of any number of database tables. Instances retrieved from database are not
00029 // persistent. They can be changed, but changes will not be propagated into database.
00030 // Classes derived by the user from this class can be used to access data, resulted
00031 // from any SQL select statement.
00032 //
00033 
00034 PROTOTYPE(Object);
00035 //Proto<Object> Object_class;
00036 
00038 // Object constructor:
00039 // Create the Object object with the appropriate (NULL) initialization.
00040 
00041 Object::Object()
00042 {
00043         #ifdef C_OBJECT_TRACE
00044                 logmsg("Object::Object() invoked");
00045         #endif
00046 
00047         _FreeWasDone = false;
00048         _ForUpdate = false;
00049 
00050         #ifdef C_OBJECT_TRACE
00051                 logmsg("Object::Object() finished");
00052         #endif
00053 }
00054 
00056 // Object destructor:
00057 // Delete the Object object with the appropriate deletion.
00058 
00059 // done
00060 Object::~Object()
00061 {
00062         #ifdef C_OBJECT_TRACE
00063                 logmsg("Object::~DatabaseObject() invoked");
00064         #endif
00065 
00066         _Free();
00067 
00068         #ifdef C_OBJECT_TRACE
00069                 logmsg("Object::~DatabaseObject() finished");
00070         #endif
00071 }
00072 
00073 bool Object::Free()
00074 //Tries to destroy an object in the memory only.
00075 //Succeeds if all the tests
00076 //(for example, if the object is not locked)
00077 //are passed successfully.
00078 {
00079         delete this;
00080         return true;
00081 };
00082 
00083 bool Object::_Free()
00084         //Destroys an object in the memory. _Free is called by the
00085         //method Free, if all the tests passed. _Free is not visible
00086         //for the programmers of the target application.
00087 {
00088         if (!_FreeWasDone) {
00089                 _FreeWasDone = true;
00090                 if (IsPersistent()) {
00091                         if (IsDirty())
00092                                 Update();
00093                         if (MemoryLocked() > 0)
00094                                 throw ObjLibException_MemoryLock();
00095                         ObjectCache.RemoveObject(this,Connection());
00096                         };
00097                 };
00098         return true;
00099 };
00100 
00102 //Tries to destroy an object from the memory as well as
00103 //from the database. Succeeds if all the tests (for
00104 //example, if the object is not locked) are passed successfully.
00105 
00106 bool Object::Delete()
00107 {
00108         bool retval = true;
00109 
00110         #ifdef C_OBJECT_TRACE
00111         logmsg("bool Object::Delete() invoked");
00112         #endif
00113 
00114         if (IsPersistent())
00115         {
00116                 if (MemoryLocked())
00117                         throw ObjLibException_MemoryLock();
00118                 Prototype()->_ExportKeyAttributes(*this);
00119                 Prototype()->_DeleteAll(this);
00120         }; // Object is deleted from the database
00121 
00122         _MarkAsClean();
00123         retval = retval && Free();
00124 
00125         #ifdef C_OBJECT_TRACE
00126         logmsg("bool Object::Delete() finished");
00127         #endif
00128 
00129         return retval;
00130 }
00131 
00132 bool Object::Update()
00133 {
00134         bool retval = true;
00135 
00136         #ifdef C_OBJECT_TRACE
00137                 logmsg("bool Object::Update() invoked");
00138         #endif
00139 
00140         if (IsPersistent() && IsDirty()) {
00141                 #ifdef C_OBJECT_TRACE
00142                         logmsg("Object is dirty");
00143                 #endif
00144 
00145                 //Prototype()->_ExportAttributes(*this);
00146                 retval = Prototype()->_UpdateAll(this);
00147                 //_MarkAsClean();
00148         };
00149 
00150         #ifdef C_OBJECT_TRACE
00151                 logmsg("bool Object::Update() finished");
00152         #endif
00153 
00154         return retval;
00155 };
00156 
00158 //It copies an object from disk to the memory again. Returns true, if it succeed.
00159 
00160 bool Object::Refresh()
00161 {
00162         bool retval;
00163         bool do_refresh;
00164 
00165         #ifdef C_OBJECT_TRACE
00166                 logmsg("bool Object::Refresh() invoked");
00167         #endif
00168 
00169         if (IsTransient())
00170                 // Transient instances need not to be refreshed
00171                 do_refresh = false;
00172         else
00173                 do_refresh = true;
00174 
00175         if (do_refresh)
00176         {
00177                 retval = Prototype()->_ExportKeyAttributes(*this);
00178                 retval = retval && Prototype()->_Load(*this);
00179         }
00180         else
00181                 retval = true;
00182 
00183         #ifdef C_OBJECT_TRACE
00184                 logmsg("bool Object::Refresh() finished");
00185         #endif
00186 
00187         return (retval);
00188 };
00189 
00190 // refresh object only if necessary
00191 bool Object::_Refresh()
00192 {
00193         bool retval;
00194         bool do_refresh;
00195 
00196         #ifdef C_OBJECT_TRACE
00197                 logmsg("bool Object::_Refresh() invoked");
00198         #endif
00199 
00200         if (IsTransient())
00201                 // Transient instances need not to be refreshed
00202                 do_refresh = false;
00203         else if (Prototype()->IsObject())
00204                 // Object descendants cannot be refreshed
00205                 do_refresh = false;
00206         else if (_ForUpdate)
00207                 // Instances loaded for update need not to be refreshed
00208                 do_refresh = false;
00209         else if ((CurrentLockingStrategy()==LS_Exclusive) && !_ForUpdate)
00210                 // Instances that should be locked in database and are not must be reloaded 
00211                 do_refresh = true;
00212         else if (CurrentReadingStrategy()==RS_Database)
00213                 // Instance should be reloaded
00214                 do_refresh = true;
00215         else if (CurrentReadingStrategy()==RS_Timestamp)
00216         {
00217                 // check version of the instance with version in the database
00218                 do_refresh = !_VersionsMatch();
00219         }
00220         else
00221                 do_refresh = false;
00222 
00223         if (do_refresh)
00224         {
00225                 retval = Prototype()->_ExportKeyAttributes(*this);
00226                 retval = retval && Prototype()->_Load(*this);
00227         }
00228         else
00229                 retval = true;
00230 
00231         #ifdef C_OBJECT_TRACE
00232                 logmsg("bool Object::_Refresh() finished");
00233         #endif
00234 
00235         return (retval);
00236 };
00237 
00239 //Returns pointer to the unique identification of an object in a database where
00240 //it is stored. It means either true OID (longint) or a tuple of values of primary key.
00241 
00242 ObjectIdentification Object::ObjectID() const
00243 {
00244         #ifdef C_OBJECT_TRACE
00245         logmsg("ObjectIdentification Object::ObjectID()"
00246                          " invoked and finished");
00247         #endif
00248 
00249         if (IsTransient())
00250                 return ObjectIdentification(this);
00251         else
00252                 return ObjectIdentification(Prototype(),_KeyValues());
00253 }
00254 
00256 //Returns Database Pointer to self.
00257 
00258 RefBase Object::Address() const
00259 {
00260         if (IsPersistent())
00261                 return RefBase(Connection(),Prototype(),Prototype()->_KeyValues(*this),_Strategies);
00262         else
00263                 return RefBase(this);
00264 };
00265 

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