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

cProtoBase.h

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 #ifndef __C_PROTOBASE_H__
00016 #define __C_PROTOBASE_H__
00017 
00018 // Standard Header(s)
00019 #include <stdio.h>
00020 #include <malloc.h>
00021 #include <fstream.h>
00022 
00023 // Common POLiTe Header(s)
00024 #include <lDefs.h>
00025 
00026 // Parent Header(s)
00027 #include <cQueRefProto.h>
00028 
00029 class DLL_External ProtoBase : public QueRefProto
00030 {
00031 #ifdef POLITE_TEST_FRIEND
00032         friend POLITE_TEST_FRIEND;
00033 #endif // POLITE_TEST_FRIEND
00034 
00035 friend class RefBase;
00036 friend class ClassRegister;
00037 friend class Object;
00038 friend class DatabaseObject;
00039 friend class ObjectBuffer;
00040 friend class ResultBase;
00041 friend class ChainedRelationBase;
00042 
00043 protected:
00044         static class ProtoBase *_root;
00045         // root of the search tree of all prototypes
00046         static class ProtoBase *_first;
00047         // first element of the list of all prototypes
00048         // having both tree and list reduces time
00049         // for searching prototypes and eliminate the recurse in some algorithms
00050         class ProtoBase *_left, *_right, *_next;
00051         bool _initialised;
00052         bool _processed;
00053         bool _isObject, _isImmutableObject, _isDatabaseObject, _isPersistentObject, _isRootClass;
00054         //Flags  that distinguish different families of objects
00055         //If isRootClass==true then it is one of four basic prototypes
00056         char *_condition;
00057         struct
00058         {
00059                 int KeyCount;
00060                 // Boundary of key members at the begining of the datatabase
00061                 // keys keep positions 0..KeyCount-1
00062                 int Count;
00063                 // Count of botk key and non-key elements in the member database _sqlVars
00064                 int SpecCount;
00065                 // Boundary of additional columns
00066                 // needed for virtualisation of resulted pointers
00067                 // used only for Proto<PersistentObject>
00068                 // which selects also Class Name in addition to OID
00069                 void *Arr;
00070         } _sqlVars;
00071         // Pointer to the database of member attributes
00072         // Array of structures, defined in the class implementation
00073         class ProtoBase *_rootPrototype;
00074         // determines if the object is derived from
00075         // Object, ImmutableObject, Object or DatabaseObject
00076         int _parentPrototypeCount;
00077         // Number of direct predecessors
00078         class ProtoBase **_parentPrototype;
00079         // Array of direct predecessors
00080         int _fullParentPrototypeCount;
00081         // Number of all predecessors including this class
00082         class ProtoBase **_fullParentPrototype;
00083         // Array of all predecessors
00084         char *_select;
00085         char *_fullSelect;
00086         char *_keySelect;
00087         char *_ptrSelect;
00088         char *_into;
00089         //char *_fullInto;
00090         //char *_keyInto;
00091         char *_fullFrom;
00092         char *_where;
00093         char *_fullWhere;
00094         char *_keyWhere;
00095         char *_groupBy;
00096         char *_having;
00097         char *_stmtSelect;
00098         char *_stmtInsert;
00099         char *_stmtDelete;
00100         char *_stmtUpdate;
00101         static void _ResetProcessed(ProtoBase * const _p);
00102         virtual bool _KeyValues(char *&s);
00103         virtual char * _KeyValues(const class Object &o);
00104         //methods return comma-separated list of encoded values of key attributes
00105         virtual bool _ExportAttributes(const class Object &o);
00106         //method copies attributes of an object into prototype's buffers
00107         virtual bool _ExportKeyAttributes(const class Object &o);
00108         //method copies only key attributes of an object into prototype's buffers
00109         virtual bool _ExportKeyAttributes(const class RefBase &x);
00110         //method copies key attributes from reference into prototype's buffers
00111         virtual bool _ImportAttributes(class Object &o);
00112         //method copies attributes of an object from prototype's buffers
00113         virtual bool _ImportKeyAttributes(class RefBase &x);
00114         //method copies key attributes of an object from prototype's buffers into reference
00115         void _Initialise();
00116         void _InitialiseParentPrototype();
00117         void _InitialiseFullParentPrototype(class ProtoBase *_p);
00118         void _InitialiseSqlVar();
00119         void _InitialiseSelect();
00120         void _InitialiseFullSelect();
00121         void _InitialiseKeySelect();
00122         void _InitialisePtrSelect();
00123         void _InitialiseInto();
00124         //void _InitialiseFullInto();
00125         //void _InitialiseKeyInto();
00126         void _InitialiseFullFrom();
00127         void _InitialiseWhere();
00128         void _InitialiseFullWhere();
00129         void _InitialiseKeyWhere();
00130         void _InitialiseGroupBy();
00131         void _InitialiseHaving();
00132         void _InitialiseStmtSelect();
00133         void _InitialiseStmtInsert();
00134         void _InitialiseStmtDelete();
00135         void _InitialiseStmtUpdate();
00136 
00137         char * _StmtSelectPtr(
00138         // Method creates a SQL SELECT command
00139         // that selects only primary key
00140         // plus special columns like ClassName
00141         // and adds additional condition.
00142         // Wanted result :
00143         // SELECT - FROM - WHERE - GROUP BY - HAVING - ORDER BY
00144                 const QueRefProto &Q,
00145                 const int i
00146                 );
00147 
00148         char * _StmtSelectPtr(
00149         // Method creates a SQL SELECT command
00150         // that selects only primary key
00151         // plus special columns like ClassName
00152         // and adds additional condition.
00153         // Wanted result :
00154                 const char *const _where_clause,
00155                 const char *const _order_by_clause,
00156                 const char *const _select_clause,
00157                 const char *const _from_clause,
00158                 const int i
00159                 );
00160 
00161         bool _BindPtr(
00162         // Method binds all columns of _StmtSelectPtr's statement
00163                 class Cursor *C
00164                 );
00165 
00166         ProtoBase *_PtrPrototype();
00167         // Method returns last fetched class name for PersistentObject descendants
00168 
00169 public:
00170         virtual char* _Where() const {return StrCpy(_condition);};
00171         virtual char* _OrderBy() const {return StrCpy(OrderBy());};
00172         static void Register(class ProtoBase *_p, const char * const _n);
00173         //static void ResetProcessed() {_ResetProcessed(_root);};
00174         static class ProtoBase *Find(const char * const _n);
00175         virtual class ResultBase * ExecuteQuery(
00176                 const QueRefProto &Q,
00177                 class Connection *DbCon,
00178                 const int i,
00179                 class ResultBase * QR
00180                 );
00181         bool operator==(class ProtoBase &_p) {return (this == &_p);};
00182         bool operator!=(class ProtoBase &_p) {return (this != &_p);};
00183         bool operator<(class ProtoBase &_p);
00184         bool operator>(class ProtoBase &_p) {return (_p < *this);};
00185         bool operator<=(class ProtoBase &_p) {return ((this == &_p) || (*this < _p));};
00186         bool operator>=(class ProtoBase &_p) {return ((this == &_p) || (_p < *this));};
00187         ProtoBase() :
00188                 _left(NULL),
00189                 _right(NULL),
00190                 _next(NULL),
00191                 _initialised(false),
00192                 _processed(false),
00193                 _isObject(false),
00194                 _isImmutableObject(false),
00195                 _isDatabaseObject(false),
00196                 _isPersistentObject(false),
00197                 _isRootClass(false),
00198                 _condition(NULL),
00199                 _rootPrototype(NULL),
00200                 _parentPrototypeCount(0),
00201                 _parentPrototype(NULL),
00202                 _fullParentPrototypeCount(0),
00203                 _fullParentPrototype(NULL),
00204                 _select(NULL),
00205                 _fullSelect(NULL),
00206                 _keySelect(NULL),
00207                 _ptrSelect(NULL),
00208                 _into(NULL),
00209                 //_fullInto(NULL),
00210                 //_keyInto(NULL),
00211                 _fullFrom(NULL),
00212                 _where(NULL),
00213                 _fullWhere(NULL),
00214                 _keyWhere(NULL),
00215                 _stmtSelect(NULL),
00216                 _stmtInsert(NULL),
00217                 _stmtDelete(NULL),
00218                 _stmtUpdate(NULL)
00219         {
00220                 _sqlVars.KeyCount = 0;
00221                 _sqlVars.Count = 0;
00222                 _sqlVars.Arr = NULL;
00223         };
00224         ~ProtoBase();
00225         virtual bool IsObject() {_Initialise(); return _isObject;};
00226         virtual bool IsImmutableObject() {_Initialise(); return _isImmutableObject;};
00227         virtual bool IsDatabaseObject() {_Initialise(); return _isDatabaseObject;};
00228         virtual bool IsPersistentObject() {_Initialise(); return _isPersistentObject;};
00229         virtual const char* ClassName() = 0;
00230         virtual const char* BaseClassName() {return NULL;};
00231         virtual const char* RootClassName() = 0;
00232         virtual const char* ParentClassNames() = 0;
00233         virtual const char* Select() {_Initialise(); return _select;};
00234         virtual const char* Into() {_Initialise(); return _into;};
00235         virtual const char* From() = 0;
00236         virtual const char* Where() {_Initialise(); return _where;};
00237         virtual const char* GroupBy() {_Initialise(); return _groupBy;};
00238         virtual const char* Having() {_Initialise(); return _having;};
00239         virtual const char* OrderBy() const = 0;
00240         virtual const char* FullSelect() {_Initialise(); return _fullSelect;};
00241         virtual const char* FullFrom() {_Initialise(); return _fullFrom;};
00242         virtual char* FullFrom(int k);
00243         virtual const char* FullWhere() {_Initialise(); return _fullWhere;};
00244         virtual char* FullWhere(int k);
00245         virtual const char* KeySelect() {_Initialise(); return _keySelect;};
00246         //virtual const char* KeyInto() {_Initialise(); return _keyInto;};
00247         virtual const char* KeyWhere() {_Initialise(); return _keyWhere;};
00248         //virtual const char* FullInto() {_Initialise(); return _fullInto;};
00249         virtual const char* KeyFrom() {return _rootPrototype->From();};
00250         virtual class ProtoBase * RootPrototype() const {return _rootPrototype;};
00251         virtual int ParentPrototypeCount() {_Initialise(); return _parentPrototypeCount;};
00252         virtual class ProtoBase * ParentPrototype(const int i);
00253         virtual int FullParentPrototypeCount() {_Initialise(); return _fullParentPrototypeCount;};
00254         virtual class ProtoBase * FullParentPrototype(const int i);
00255         virtual class Object *New() const = 0;
00256         //Constructs a new empty transient variant of a corresponding persistent object type
00257         virtual class RefBase *NewRef() = 0;
00258         //Constructs a new empty reference to corresponding type
00259         //
00260         // Follows methods needed for translation Object syntax <-> SQL syntax
00261         //
00262         const char* ColumnTable(const char* const col);
00263         // Returns a table name containing the given column
00264 
00265 protected:
00266         virtual char *_RealiasT(
00267                 char * &fragment,
00268                 const char *table,
00269                 const int i,
00270                 const int j
00271                 );
00272         // replaces all occurences of table. with proper alias T$i_j
00273         virtual char *_RealiasT(
00274                 char * &fragment,
00275                 const int i
00276                 );
00277         // replaces all occurences of all tables. with proper alias T$i_j
00278         virtual void _MapKey(int i, const char *&attr, unsigned char Object::*&mem, const char *&col, char &type, unsigned int &len, bool &rw) = 0;
00279         virtual void _Map(int i, const char *&attr, unsigned char Object::*&mem, const char *&col, char &type, unsigned int &len, bool &rw) = 0;
00280         virtual const char *_ProtoTWhere() = 0;
00281         virtual const char *_ProtoTGroupBy() = 0;
00282         virtual const char *_ProtoTHaving() = 0;
00283         bool _InsertAll(class Object *target);
00284         //Inserts complete instance
00285         bool _Insert(class Connection *aDbConn);
00286         //Inserts piece of an instance
00287         bool _UpdateAll(class Object *target);
00288         //Writes complete object from the memory to the database.
00289         bool _Update(class Connection *aDbConn);
00290         //Writes piece of an object from the memory to the database
00291         bool _DeleteAll(class RefBase *target);
00292         //Deletes an object from the database.
00293         bool _DeleteAll(class Object *target);
00294         //Deletes an object from the database.
00295         bool _Delete(class Connection * aDbConn);
00296         //Deletes piece of an object from the database.
00297         bool ProtoBase::_Load(
00298                 class Object &o
00299                 );
00300         //Loads one instance into memory
00301 
00302 public:
00304         // Method lock a table assigned to target class in desired mode
00305         // the method works for really persistent objects
00306         bool ProtoBase::LockTable(
00307                 class Connection *aDbCon,
00308                 enum LockingStrategy aLockingStrategy = LS_Default,
00309                 enum WaitingStrategy aWaitingStrategy = WS_Default
00310                 );
00311 
00312         bool WriteDDL(ofstream &S, class Database &Db);
00313         // generates DDL stements nedded for the library to run
00314         char *ProtoBase::ColumnTypeDDL(int i, class Database &Db);
00315         // returns correct database column type for i-th attribute
00316 
00317 protected:
00318         char *_TranslateToSql(char * (&dst), const char * const src);
00319         char *_TranslateToSql(char * (&src));
00320         // replaces C++ operator and names by relational ones
00321         class Query &_TranslateToSql(class Query &Q);
00322         // replaces C++ operators in both where and oreder-by part
00323         ProtoBase *_gcs(ProtoBase &X);
00324         // greatest common successor of *this and X
00325         // (one of potentially many of them)
00326 };
00327 
00328 
00329 #endif //__GC__PROTOBASE_H__

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