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 #ifndef __C_OBJECT_H__ 00016 #define __C_OBJECT_H__ 00017 00018 // Common POLiTe Header(s) 00019 #include <lStr.h> 00020 #include <lTrace.h> 00021 #include <lDefs.h> 00022 #include <lExceptions.h> 00023 #include <preproc.h> 00024 00025 // Parent Header(s) 00026 #include <cObjRef.h> 00027 00028 // Other POLiTe Header(s) 00029 #include <cDatabase.h> // Database class header 00030 #include <cConnection.h> // DatabaseConnection class header 00031 #include <cObjectIdentification.h> // ObjectIdentification class header 00032 #include <tProto.h> 00033 00034 class DLL_External Object : public ObjRef 00035 { 00036 #ifdef POLITE_TEST_FRIEND 00037 friend POLITE_TEST_FRIEND; 00038 #endif // POLITE_TEST_FRIEND 00039 00040 friend class ObjectBuffer; 00041 friend class RefBase; 00042 friend class ProtoBase; 00043 00044 protected: 00045 bool _FreeWasDone; 00046 //There was already called Free() methods on this object 00047 bool _ForUpdate; 00048 //Object was loaded using FOR UPDATE 00049 00050 protected: // member methods 00051 virtual bool _Free(); 00052 //Destroys an object in the memory. _Free is called by the 00053 //method Free, if all the tests passed. _Free is not visible 00054 //for the programmers of the target application. 00055 00056 public: 00057 Object(); // constructor 00058 00059 ~Object(); // destructor 00060 00061 static const char* RootClassName() {return NULL;}; 00062 static const char* BaseClassName() {return "Object";}; 00063 ABSTRACT_CLASS(Object); 00064 //static const char* ClassName() {return "Object";}; 00065 //static const Object * New() {return NULL;}; 00066 //virtual class ProtoBase *Prototype() const = 0 00067 PARENTS(NULL); 00068 //static const char* ParentClassNames() {return NULL;}; 00069 FROM(""); 00070 //static const char* From() {return "";}; 00071 WHERE(""); 00072 //static const char* Where() {return "";}; 00073 GROUP_BY(""); 00074 //static const char* GroupBy() {return "";}; 00075 HAVING(""); 00076 //static const char* Having() {return "";}; 00077 ORDER_BY(""); 00078 //static const char* OrderBy() {return "";}; 00079 MAPKEY_BEGIN 00080 MAPKEY_END; 00081 MAP_BEGIN 00082 MAP_END; 00083 00084 public: 00085 virtual class Object* IsInMemory() const 00086 //Access to the object with the tests whether the object 00087 //is loaded into the memory. It returns the pointer to 00088 //the object in the memory (if he is loaded in the memory) 00089 //or NULL otherwise. 00090 { 00091 return (Object*)this; 00092 //Object instances are in the memory 00093 //at least because the method is called on it 00094 }; 00095 00096 virtual class ObjectIdentification ObjectID() const; 00097 //Returns identification of 00098 //an object independent on the instance location 00099 //It consists of either memory pointer, 00100 //or couple of prototype an comma-separated list of primary key values 00101 00103 00104 virtual bool PostLoad() { return true;}; 00105 //Virtual method executed immediately after a database object 00106 //is loaded from the database. It may be overridden by programmer 00107 //to provide an additional functionality. 00108 00109 protected: 00110 00111 virtual bool IsDirty() const { return false; }; 00112 //Returns true, if an object is marked as dirty (e.g. the 00113 //object was changed in the memory) 00114 //Object is never dirty 00115 00116 virtual bool MarkAsDirty() { return true; }; 00117 //Marks a persistent object as Dirty. Only Dirty (changed) 00118 //objects are written to the database when flushing the buffer. 00119 //Object is never dirty 00120 00121 protected: 00122 virtual bool _MarkAsClean() { return true; }; 00123 //Marks a persistent object as Clean. Only Dirty (changed) 00124 //objects are written to the database when flushing the buffer. 00125 00126 virtual char *_KeyValues() const {return Prototype()->_KeyValues(*this);}; 00127 00128 virtual bool _Refresh(); 00129 // It copies an object from disk to the memory again if necessary 00130 // If not necessary, does nothing. 00131 00132 public: 00133 virtual bool Refresh(); 00134 // It copies an object from disk to the memory again. 00135 // Returns true, if it succeed. 00136 // Database objects are results of the complex queries 00137 // and thus can not be refreshed 00138 00139 public: 00140 virtual bool Update(); 00141 //Writes an object from the memory to the disk. 00142 00143 virtual bool Free(); 00144 //Tries to destroy an object in the memory only. 00145 //Succeeds if all the tests 00146 //(for example, if the object is not locked) 00147 //are passed successfully. 00148 00149 virtual bool Delete(); 00150 //Tries to destroy an object from the memory as well as 00151 //from the database. Succeeds if all the tests (for 00152 //example, if the object is not locked) are passed 00153 //successfully. 00154 00155 virtual class Object *MemoryLock() 00156 //Locks a persistent object. Multiple 00157 //locks can be placed on the object. The object can 00158 //be removed from memory only when all the locks are released. 00159 { 00160 return this; 00161 // DataabseObject is everytime in the memory, is bot buffered 00162 // via ObjectBuffer 00163 }; 00164 00165 virtual bool MemoryUnlock() 00166 //Unlocks a persistent object in memory. See MemoryLock. 00167 { 00168 return true; 00169 //No action is necessary on Object 00170 }; 00171 00172 virtual unsigned int MemoryLocked() const 00173 //Returns the number of memory locks on receiver or zero 00174 //if it is not locked. 00175 { 00176 return 0; 00177 // DataabseObject is never locked in memory 00178 }; 00179 00180 virtual bool IsTransient() const 00181 //Returns true if an instance of DatabaseObject is transient, 00182 //i.e. if it has no copy in database. This can happen if the 00183 //object was created as transient and was not made persistent 00184 //by calling BePersistent yet. 00185 { 00186 return true; 00187 // Object is never realy persistent 00188 }; 00189 00190 virtual bool IsPersistent() const 00191 //Returns true if the object is really persistent, otherwise 00192 //it returns false. This function returns the opposite value 00193 //to the method IsTransient. 00194 { 00195 return false; 00196 // Object is never realy persistent 00197 }; 00198 00199 public: 00200 virtual class RefBase BePersistent(class Connection *DbCon) 00201 //Causes an object to be persistent. 00202 //Persistent objects are created as transient (they exist only 00203 //in memory) and when it is needed (all attributes are set to 00204 //appropriate values), then they can be converted to true 00205 //persistent objects. An advantage is that transient objects do 00206 //not propagate into a database while persistent objects are 00207 //written (in spite of buffering mechanism) to the database 00208 //often. 00209 { 00210 return Address(); 00211 }; 00212 00213 virtual class RefBase Address() const; 00214 //Returns Database Pointer to self. 00215 00216 protected: 00217 virtual class RefBase _Virtualise(RefBase &DbPtr) const 00218 // Typecasts RefBase to class A to RefBase to 00219 // correct A's subclass B. Does nothing in other derived classes. 00220 { 00221 return DbPtr._Virtualise(); 00222 }; 00223 00224 virtual const char *_SelectKeyValues(const RefBase &DbPtr) const 00225 // Return this string from given object pointer 00226 { 00227 return DbPtr._ObjectIdentification._SelectKeyValues; 00228 }; 00229 00231 // Checks, if version of object in memory is the same as the version in the database 00232 // Applicable on PersistentObject class, predecessors return true 00233 00234 virtual bool _VersionsMatch() 00235 { 00236 return true; 00237 }; 00238 00239 }; 00240 00241 //ABSTRACT_CLASS_PROTOTYPE(Object); 00242 extern DLL_External Proto<Object> Object_class; 00243 00244 00245 #endif // __C_OBJECT_H__ 00246 00247