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

cPersistentObject.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 
00016 //      PersistentObject is represented in a
00017 //      relational database by a set of tables.
00018 //  To the root class of the hierarchy tree is assigned a table
00019 //  with columns associated to the attributes of this class.
00020 //  Each descendant class is represented by a table which columns
00021 //      correspond only to the added attributes.
00022 //  Attributes of one object then appear in several tables.
00023 //  From this follows that a object must be     constructed
00024 //  by multiple joins of tables associated with all predecessors
00025 //      in the hierarchy tree.
00026 //  To be able to join all pieces of one object,
00027 //      each table must contain an additional column OID.
00028 
00029 // Own Header
00030 #include <cPersistentObject.h>
00031 
00032 // Other POLiTe Header(s)
00033 #include <tProto.h>
00034 #include <cRefBase.h>
00035 
00036 PROTOTYPE(PersistentObject);
00037 //Proto<PersistentObject> PersistentObject_class;
00038 
00040 //PersistentObject constructor
00041 
00042 PersistentObject::PersistentObject()
00043 {
00044         #ifdef C_PERSISTENTOBJECT_TRACE
00045         logmsg("PersistentObject::PersistentObject() invoked");
00046         #endif
00047 
00048         _SerialNumber = 0;
00049         _OID = -1;  // non-existent OID
00050 
00051 // OidBasedPersistentObjects
00052 // The class is similar to PersistentObject with following differences:
00053 // Object has special primary key called OID (Object Identification).
00054 // OID is unique in the database.
00055 // It supports specialisation hierarchy of classes, parent data are stored separately in other tables
00056 // and handled by predecessors.
00057 
00058         #ifdef C_PERSISTENTOBJECT_TRACE
00059         logmsg("PersistentObject::PersistentObject() finished");
00060         #endif
00061 }
00062 
00064 //PersistentObject destructor
00065 
00066 PersistentObject::~PersistentObject()
00067 {
00068         #ifdef C_PERSISTENTOBJECT_TRACE
00069                 logmsg("PersistentObject::~PersistentObject() invoked");
00070         #endif
00071 
00072         _Free();
00073 
00074         #ifdef C_PERSISTENTOBJECT_TRACE
00075                 logmsg("PersistentObject::~PersistentObject() finished");
00076         #endif
00077 }
00078 
00080 //Causes an object to be persistent.
00081 //Persistent objects are  created as transient (they exist only
00082 //in memory) and when it is needed (all attributes are set to
00083 //appropriate values), then they  can be converted to true
00084 //persistent objects. An advantage is that transient objects do
00085 //not propagate into a database while persistent objects are
00086 //written (in spite of buffering mechanism) to the database
00087 //often.
00088 
00089 class RefBase PersistentObject::BePersistent(
00090         class Connection *DbCon
00091         )
00092 {
00093         RefBase retval;
00094 
00095         #ifdef C_PERSISTENTOBJECT_TRACE
00096                 logmsg("int PersistentObject::BePersistent() invoked");
00097         #endif
00098 
00099         if (IsPersistent())
00100                 retval = Address();
00101         else {
00102                 if (DbCon == NULL)
00103                         throw ObjLibException_ConnectionError();
00104                 // SELECT NEW OID
00105                 DbCon->_NextOID(_OID,_SerialNumber);
00106                 retval = DatabaseObject::BePersistent(DbCon);
00107                 };
00108 
00109         #ifdef C_PERSISTENTOBJECT_TRACE
00110                 logmsg("int PersistentObject::BePersistent() finished");
00111         #endif
00112 
00113         return retval;
00114         };
00115 
00116 
00117 bool PersistentObject::Update()
00118 {
00119         bool retval = true;
00120         class Connection *DbCon;
00121 
00122         #ifdef C_PERSISTENTOBJECT_TRACE
00123                 logmsg("bool PersistentObject::Update() invoked");
00124         #endif
00125 
00126         if (IsPersistent() && IsDirty())
00127         {
00128                 #ifdef C_PERSISTENTOBJECT_TRACE
00129                         logmsg("PersistentObject is dirty");
00130                 #endif
00131                 if ((DbCon = Connection()) == NULL)
00132                         throw ObjLibException_ConnectionError();
00133                 // SELECT NEW SERIAL FOR THIS OID
00134                 DbCon->_NextSN(_OID,_SerialNumber);
00135                 retval = DatabaseObject::Update();
00136         };
00137 
00138         #ifdef C_PERSISTENTOBJECT_TRACE
00139                 logmsg("bool PersistentObject::Update() finished");
00140         #endif
00141 
00142         return retval;
00143         };
00144 
00146 // Checks, if version of object in memory is the same as the version in the database
00147 // Applicable on PersistentObject class, predecessors return true
00148 bool PersistentObject::_VersionsMatch()
00149 {
00150         if (IsTransient())
00151                         return true;
00152         // check version in the database with current version in itself
00153         long dbVersion;
00154         bool okay;
00155         char *oidStr = LongToStr(_OID);
00156         char *sqlcmd = NULL;
00157         StrCat(sqlcmd,2,"SELECT VERSION FROM " PERSISTENT_OBJECT_TABLE " WHERE OID = ",oidStr);
00158         StrFree(oidStr);
00159         okay = _Connection->_Open()
00160                 && _Connection->_Prepare(sqlcmd)
00161                 && _Connection->_Execute()
00162                 && _Connection->_PreFetchBind(
00163                         1,
00164                         (void *)&dbVersion,
00165                         4,
00166                         TYPE_INT
00167                         )
00168                 && _Connection->_FetchNext()
00169                 && _Connection->_Close();
00170         return okay && (dbVersion==_SerialNumber);
00171 };
00172 

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