include/TableImpl.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #ifndef TABLE_IMPL_H
00017 #define TABLE_IMPL_H
00018 #include<os.h>
00019 #include<DataType.h>
00020 #include<Transaction.h>
00021 #include<Database.h>
00022 #include<Index.h>
00023 #include<CatalogTables.h>
00024 #include<Info.h>
00025 #include<Debug.h>
00026 #include<DatabaseManagerImpl.h>
00027 #include<Predicate.h>
00028 enum ScanType
00029 {
00030     fullTableScan = 0,
00031     hashIndexScan,
00032     treeIndexScan,
00033     unknownScan
00034 };
00035 
00036 class Predicate;
00037 
00038 class TupleIterator
00039 {
00040     Predicate *pred_;
00041     ScanType scanType_;
00042     ChunkIterator *cIter;
00043     BucketIter *bIter;
00044     IndexInfo *info;
00045     void *chunkPtr_;
00046     int procSlot;
00047 
00048     TupleIterator(){}
00049     public:
00050     
00051     TupleIterator(Predicate *p, ScanType t, IndexInfo *i, void *cptr, int pslot)
00052     { bIter = NULL; pred_ = p ; scanType_ = t; info = i; chunkPtr_ = cptr; procSlot =pslot;}
00053     
00054     ~TupleIterator() 
00055         { 
00056         if (bIter) delete bIter; 
00057         bIter = NULL; 
00058     }
00059     
00060     DbRetVal open();
00061     void* next();
00062     DbRetVal close();
00063 
00064 };
00065 class TableImpl:public Table
00066 {
00067     private:
00068 
00069 
00070     LockManager *lMgr_;
00071     Transaction **trans;
00072     //This is pointer to the pointer stored in the
00073     //Transaction manager.
00074     //If the transaction commits/aborts this pointer changes
00075     //and this will get that newly allocated transaction
00076 
00077     char tblName_[IDENTIFIER_LENGTH];
00078     int tblID_;
00079     size_t length_; //length of the tuple
00080     int numFlds_;
00081     void* chunkPtr_;
00082     void *curTuple_; //holds the current tuple ptr. moved during fetch() calls
00083 
00084     Predicate *pred_;
00085     ScanType scanType_;
00086     //ChunkIterator *iter;
00087     //BucketIter *bIter;
00088 
00089     TupleIterator *iter;
00090 
00091     bool undoFlag;
00092 
00093     public:
00094     FieldList fldList_;
00095     int numIndexes_;
00096     char** indexPtr_; // array of index ptrs to the catalog table for the indexes of this table.
00097     IndexInfo **idxInfo;
00098     int useIndex_;//offet in the above array indexPtr_ for scan
00099     bool isPlanCreated;
00100 
00101     Database *db_;
00102     Database *sysDB_;
00103 
00104     //Either one of the below is populated based on the no of fields and 
00105     //is used for tuple insertions
00106     bool isIntUsedForNULL;
00107     int iNullInfo;
00108     char *cNullInfo;
00109     int iNotNullInfo;
00110     char *cNotNullInfo;
00111 
00112     private:
00113 
00114     //copy Values from binded buffer to tuple pointed by arg
00115     DbRetVal copyValuesFromBindBuffer(void *tuple, bool isInsert=true);
00116     DbRetVal copyValuesToBindBuffer(void *tuple);
00117     void setNullBit(int fldpos);
00118 
00119     DbRetVal insertIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
00120     DbRetVal updateIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
00121     DbRetVal deleteIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
00122 
00123     DbRetVal createPlan();
00124     Chunk* getSystemTableChunk(CatalogTableID id)
00125     {
00126         return sysDB_->getSystemDatabaseChunk(id);
00127     }
00128 
00129     public:
00130     TableImpl() { db_ = NULL; chunkPtr_ = NULL; iter = NULL;
00131         idxInfo = NULL; indexPtr_ = NULL; scanType_ = unknownScan; 
00132         pred_ = NULL; useIndex_ = -1; numFlds_ = 0;
00133         iNullInfo = 0; cNullInfo = NULL; isIntUsedForNULL = true; 
00134         iNotNullInfo = 0; cNotNullInfo = NULL;
00135         isPlanCreated = false; undoFlag = true;}
00136     ~TableImpl();
00137 
00138     void setDB(Database *db) { db_ = db; }
00139     void setSystemDB(Database *db) { sysDB_ = db; }
00140     void setLockManager(LockManager *lmgr) { lMgr_ = lmgr; }
00141     void setTrans(Transaction **t) { trans = t; }
00142 
00143     DataType getFieldType(const char *name)
00144         { return fldList_.getFieldType(name);   }
00145     int getFieldOffset(const char *name)
00146         { return fldList_.getFieldOffset(name); }
00147     size_t getFieldLength(const char *name)
00148         { return fldList_.getFieldLength(name); }
00149 
00150     DbRetVal getFieldInfo(const char *fieldName,  FieldInfo *&info)
00151         { return fldList_.getFieldInfo(fieldName, info); }
00152 
00153     List getFieldNameList();
00154 
00155     // search predicate
00156      void setCondition(Condition *p) 
00157      { isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
00158 
00159     //binding
00160     DbRetVal bindFld(const char *name, void *val);
00161 
00162     void markFldNull(const char *name);
00163     void markFldNull(int colpos);
00164     bool isFldNull(const char *name);
00165     bool isFldNull(int colpos);
00166 
00167 
00168     void clearFldNull(const char *name);
00169     void clearFldNull(int colpos);
00170 
00171 
00172     DbRetVal insertTuple();
00173     DbRetVal updateTuple();
00174 
00175     DbRetVal deleteTuple();
00176     int deleteWhere();
00177     int truncate();
00178 
00179     DbRetVal execute();
00180 
00181     void* fetch();
00182     void* fetch(DbRetVal &rv);
00183     void* fetchNoBind();
00184     void* fetchNoBind(DbRetVal &rv);
00185 
00186     DbRetVal close();
00187 
00188 
00189     long spaceUsed();
00190     long numTuples();
00191     int pagesUsed();
00192     void printInfo();
00193 
00194     DbRetVal lock(bool shared);
00195     DbRetVal unlock();
00196 
00197     DbRetVal setUndoLogging(bool flag) { undoFlag = flag; }
00198 
00199     void printSQLIndexString();
00200     char* getName() { return tblName_; }
00201     void setTableInfo(char *name, int tblid, size_t  length,
00202                        int numFld, int numIdx, void *chunk);
00203     friend class DatabaseManagerImpl;
00204 };
00205 
00206 
00207 #endif

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7