00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00073
00074
00075
00076
00077 char tblName_[IDENTIFIER_LENGTH];
00078 int tblID_;
00079 size_t length_;
00080 int numFlds_;
00081 void* chunkPtr_;
00082 void *curTuple_;
00083
00084 Predicate *pred_;
00085 ScanType scanType_;
00086
00087
00088
00089 TupleIterator *iter;
00090
00091 bool undoFlag;
00092
00093 public:
00094 FieldList fldList_;
00095 int numIndexes_;
00096 char** indexPtr_;
00097 IndexInfo **idxInfo;
00098 int useIndex_;
00099 bool isPlanCreated;
00100
00101 Database *db_;
00102 Database *sysDB_;
00103
00104
00105
00106 bool isIntUsedForNULL;
00107 int iNullInfo;
00108 char *cNullInfo;
00109 int iNotNullInfo;
00110 char *cNotNullInfo;
00111
00112 private:
00113
00114
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
00156 void setCondition(Condition *p)
00157 { isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
00158
00159
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