submitting patch from enterprise version
[csql.git] / include / TableImpl.h
blob4270931e7276b55948c89feed8a6c630a49bdf44
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #ifndef TABLE_IMPL_H
17 #define TABLE_IMPL_H
18 #include<os.h>
19 #include<DataType.h>
20 #include<Transaction.h>
21 #include<Database.h>
22 #include<Index.h>
23 #include<CatalogTables.h>
24 #include<Info.h>
25 #include<Debug.h>
26 #include<DatabaseManagerImpl.h>
27 #include<Predicate.h>
28 #include<AggTableImpl.h>//for AggType
29 #ifndef SCANTYPE
30 enum ScanType
32 fullTableScan = 0,
33 hashIndexScan,
34 treeIndexScan,
35 unknownScan
37 #define SCANTYPE
38 #endif
40 static char ScanTypeNames[][10] =
42 "TableScan", "HashScan", "TreeScan", "Invalid"
45 class Predicate;
47 class TupleIterator
49 Predicate *pred_;
50 ScanType scanType_;
51 ChunkIterator *cIter;
52 BucketIter *bIter;
53 TreeIter *tIter;
54 IndexInfo *info;
55 void *chunkPtr_;
56 int procSlot;
57 bool isBetween;
58 bool isPointLook;
60 TupleIterator() { }
61 public:
62 TupleIterator(Predicate *p, ScanType t, IndexInfo *i, void *cptr, int pslot,bool between , bool isPtLook)
63 { bIter = NULL; cIter = NULL; tIter = NULL;
64 pred_ = p ; scanType_ = t; info = i; chunkPtr_ = cptr;
65 procSlot =pslot; isBetween=between; isPointLook=isPtLook;
68 ~TupleIterator()
70 if (bIter) { delete bIter; bIter = NULL; }
71 if (cIter) { delete cIter; cIter = NULL; }
72 if (tIter) { delete tIter; tIter = NULL; }
74 bool isBetInvolved(){ return isBetween;}
75 void setBetInvolved(bool between){ isBetween=between;}
76 bool isPointLookInvolved(){return isPointLook;}
77 DbRetVal open();
78 void* next();
79 void* prev();//used only for tree iter during deleteTuple
80 void reset();
81 DbRetVal close();
84 class TableImpl:public Table
86 private:
89 LockManager *lMgr_;
90 Transaction **trans;
91 //This is pointer to the pointer stored in the
92 //Transaction manager.
93 //If the transaction commits/aborts this pointer changes
94 //and this will get that newly allocated transaction
96 char tblName_[IDENTIFIER_LENGTH];
97 int tblID_;
98 size_t length_; //length of the tuple
99 int numFlds_;
100 void* chunkPtr_;
101 void *curTuple_; //holds the current tuple ptr. moved during fetch() calls
103 Predicate *pred_;
104 ScanType scanType_;
105 //ChunkIterator *iter;
106 //BucketIter *bIter;
108 TupleIterator *iter;
110 bool loadFlag;
112 public:
113 FieldList fldList_;
114 List bindList_;
115 void **bindListArray_;
116 int numBindFlds_;
117 int numIndexes_;
118 char** indexPtr_; // array of index ptrs to the catalog table for the indexes of this table.
119 IndexInfo **idxInfo;
120 int useIndex_;//offet in the above array indexPtr_ for scan
121 bool isPlanCreated;
122 bool isPointLook;
123 bool isBetween;
124 Database *db_;
125 Database *sysDB_;
126 void *ptrToAuto;
127 //Either one of the below is populated based on the no of fields and
128 //is used for tuple insertions
129 bool isIntUsedForNULL;
130 int iNullInfo;
131 char *cNullInfo;
132 int iNotNullInfo;
133 char *cNotNullInfo;
135 private:
137 //copy Values from binded buffer to tuple pointed by arg
138 DbRetVal copyValuesFromBindBuffer(void *tuple, bool isInsert=true);
139 DbRetVal copyValuesToBindBuffer(void *tuple);
140 void setNullBit(int fldpos);
141 void clearNullBit(int fldpos);
142 DbRetVal insertIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple, bool loadFlag=0);
143 DbRetVal updateIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
144 DbRetVal deleteIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
146 DbRetVal createPlan();
147 Chunk* getSystemTableChunk(CatalogTableID id)
149 return sysDB_->getSystemDatabaseChunk(id);
152 public:
153 TableImpl() { db_ = NULL; chunkPtr_ = NULL; iter = NULL;
154 idxInfo = NULL; indexPtr_ = NULL; scanType_ = unknownScan;
155 pred_ = NULL; useIndex_ = -1; numFlds_ = 0; bindListArray_ = NULL;
156 iNullInfo = 0; cNullInfo = NULL; isIntUsedForNULL = true;
157 iNotNullInfo = 0; cNotNullInfo = NULL; curTuple_ = NULL;
158 isPlanCreated = false; loadFlag = false; ptrToAuto=NULL;}
159 ~TableImpl();
161 void setDB(Database *db) { db_ = db; }
162 Database* getDB() { return db_;}
163 void setSystemDB(Database *db) { sysDB_ = db; }
164 void setLockManager(LockManager *lmgr) { lMgr_ = lmgr; }
165 void setTrans(Transaction **t) { trans = t; }
166 inline void setCurTuple(void *tuple){ curTuple_=tuple; }
167 DataType getFieldType(const char *name)
168 { return fldList_.getFieldType(name); }
169 int getFieldOffset(const char *name)
170 { return fldList_.getFieldOffset(name); }
171 size_t getFieldLength(const char *name)
172 { return fldList_.getFieldLength(name); }
174 DbRetVal getFieldInfo(const char *fieldName, FieldInfo *&info)
176 char tblName[IDENTIFIER_LENGTH];
177 char fldName[IDENTIFIER_LENGTH];
178 getTableNameAlone((char*)fieldName, tblName);
179 getFieldNameAlone((char*)fieldName, fldName);
180 if (0 == strcmp(tblName, "") || 0 ==strcmp(tblName, getName()))
181 return fldList_.getFieldInfo(fldName, info);
182 else
183 return ErrNotExists;
186 List getFieldNameList();
188 // search predicate
189 void setCondition(Condition *p)
190 { isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
192 //binding
193 DbRetVal bindFld(const char *name, void *val);
194 void *getBindFldAddr(const char *name);
195 int getFldPos(char *name);
196 DbRetVal markFldNull(const char *name);
197 DbRetVal markFldNull(int colpos);
198 bool isFldNull(const char *name);
199 bool isFldNull(int colpos);
201 void clearFldNull(const char *name);
202 void clearFldNull(int colpos);
203 void resetNullinfo();
204 DbRetVal insertTuple();
205 DbRetVal updateTuple();
207 DbRetVal deleteTuple();
208 int deleteWhere();
209 int truncate();
211 DbRetVal execute();
213 void* fetch();
214 void* fetch(DbRetVal &rv);
215 void* fetchNoBind();
216 void* fetchNoBind(DbRetVal &rv);
217 DbRetVal fetchAgg(const char *fldName, AggType aType, void *buf);
219 DbRetVal close();
220 DbRetVal closeScan();
223 long spaceUsed();
224 long numTuples();
225 int pagesUsed();
226 void printInfo();
227 void printPlan(int space);
229 DbRetVal lock(bool shared);
230 DbRetVal unlock();
232 DbRetVal setUndoLogging(bool flag) { loadFlag = flag; }
234 void printSQLIndexString();
236 DbRetVal optimize();
237 bool isTableInvolved(char *tblName);
238 bool pushPredicate(Predicate *pred);
239 void setPredicate(Predicate *pred);
240 ScanType getScanType() { return scanType_; }
241 bool hasIndex(char *fldName);
242 IndexType getIndexType(char *fldName, int* pos);
243 void addPredicate(char *fName, ComparisionOp op, void *buf);
245 char* getName() { return tblName_; }
246 void setTableInfo(char *name, int tblid, size_t length,
247 int numFld, int numIdx, void *chunk);
248 void setLoading(bool flag) { loadFlag = flag; }
249 friend class DatabaseManagerImpl;
253 #endif