destructor is made to call table close which will delete the table handle
[csql.git] / include / TableImpl.h
blob88a2c694b14b0d4658ee40a03c6169a73e9c28bd
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; pred_ = p ; scanType_ = t; info = i; chunkPtr_ = cptr; procSlot =pslot; isBetween=between; isPointLook=isPtLook; }
65 ~TupleIterator()
67 if (bIter) delete bIter;
68 bIter = NULL;
70 bool isBetInvolved(){ return isBetween;}
71 void setBetInvolved(bool between){ isBetween=between;}
72 bool isPointLookInvolved(){return isPointLook;}
73 DbRetVal open();
74 void* next();
75 void* prev();//used only for tree iter during deleteTuple
76 void reset();
77 DbRetVal close();
80 class TableImpl:public Table
82 private:
85 LockManager *lMgr_;
86 Transaction **trans;
87 //This is pointer to the pointer stored in the
88 //Transaction manager.
89 //If the transaction commits/aborts this pointer changes
90 //and this will get that newly allocated transaction
92 char tblName_[IDENTIFIER_LENGTH];
93 int tblID_;
94 size_t length_; //length of the tuple
95 int numFlds_;
96 void* chunkPtr_;
97 void *curTuple_; //holds the current tuple ptr. moved during fetch() calls
99 Predicate *pred_;
100 ScanType scanType_;
101 //ChunkIterator *iter;
102 //BucketIter *bIter;
104 TupleIterator *iter;
106 bool undoFlag;
108 public:
109 FieldList fldList_;
110 List bindList_;
111 void **bindListArray_;
112 int numBindFlds_;
113 int numIndexes_;
114 char** indexPtr_; // array of index ptrs to the catalog table for the indexes of this table.
115 IndexInfo **idxInfo;
116 int useIndex_;//offet in the above array indexPtr_ for scan
117 bool isPlanCreated;
118 bool isPointLook;
119 bool isBetween;
120 Database *db_;
121 Database *sysDB_;
123 //Either one of the below is populated based on the no of fields and
124 //is used for tuple insertions
125 bool isIntUsedForNULL;
126 int iNullInfo;
127 char *cNullInfo;
128 int iNotNullInfo;
129 char *cNotNullInfo;
131 private:
133 //copy Values from binded buffer to tuple pointed by arg
134 DbRetVal copyValuesFromBindBuffer(void *tuple, bool isInsert=true);
135 DbRetVal copyValuesToBindBuffer(void *tuple);
136 void setNullBit(int fldpos);
137 void clearNullBit(int fldpos);
138 DbRetVal insertIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
139 DbRetVal updateIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
140 DbRetVal deleteIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
142 DbRetVal createPlan();
143 Chunk* getSystemTableChunk(CatalogTableID id)
145 return sysDB_->getSystemDatabaseChunk(id);
148 public:
149 TableImpl() { db_ = NULL; chunkPtr_ = NULL; iter = NULL;
150 idxInfo = NULL; indexPtr_ = NULL; scanType_ = unknownScan;
151 pred_ = NULL; useIndex_ = -1; numFlds_ = 0;
152 iNullInfo = 0; cNullInfo = NULL; isIntUsedForNULL = true;
153 iNotNullInfo = 0; cNotNullInfo = NULL; curTuple_ = NULL;
154 isPlanCreated = false; undoFlag = true;}
155 ~TableImpl();
157 void setDB(Database *db) { db_ = db; }
158 Database* getDB() { return db_;}
159 void setSystemDB(Database *db) { sysDB_ = db; }
160 void setLockManager(LockManager *lmgr) { lMgr_ = lmgr; }
161 void setTrans(Transaction **t) { trans = t; }
162 void setCurTuple(void *tuple){ curTuple_=tuple; }
163 DataType getFieldType(const char *name)
164 { return fldList_.getFieldType(name); }
165 int getFieldOffset(const char *name)
166 { return fldList_.getFieldOffset(name); }
167 size_t getFieldLength(const char *name)
168 { return fldList_.getFieldLength(name); }
170 DbRetVal getFieldInfo(const char *fieldName, FieldInfo *&info)
172 char tblName[IDENTIFIER_LENGTH];
173 char fldName[IDENTIFIER_LENGTH];
174 getTableNameAlone((char*)fieldName, tblName);
175 getFieldNameAlone((char*)fieldName, fldName);
176 if (0 == strcmp(tblName, "") || 0 ==strcmp(tblName, getName()))
177 return fldList_.getFieldInfo(fldName, info);
178 else
179 return ErrNotExists;
182 List getFieldNameList();
184 // search predicate
185 void setCondition(Condition *p)
186 { isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
188 //binding
189 DbRetVal bindFld(const char *name, void *val);
190 void *getBindFldAddr(const char *name);
191 int getFldPos(char *name);
192 DbRetVal markFldNull(const char *name);
193 DbRetVal markFldNull(int colpos);
194 bool isFldNull(const char *name);
195 bool isFldNull(int colpos);
197 void clearFldNull(const char *name);
198 void clearFldNull(int colpos);
199 void resetNullinfo();
200 DbRetVal insertTuple();
201 DbRetVal updateTuple();
203 DbRetVal deleteTuple();
204 int deleteWhere();
205 int truncate();
207 DbRetVal execute();
209 void* fetch();
210 void* fetch(DbRetVal &rv);
211 void* fetchNoBind();
212 void* fetchNoBind(DbRetVal &rv);
213 DbRetVal fetchAgg(const char *fldName, AggType aType, void *buf);
215 DbRetVal close();
216 DbRetVal closeScan();
219 long spaceUsed();
220 long numTuples();
221 int pagesUsed();
222 void printInfo();
223 void printPlan(int space);
225 DbRetVal lock(bool shared);
226 DbRetVal unlock();
228 DbRetVal setUndoLogging(bool flag) { undoFlag = flag; }
230 void printSQLIndexString();
232 DbRetVal optimize();
233 bool isTableInvolved(char *tblName);
234 bool pushPredicate(Predicate *pred);
235 void setPredicate(Predicate *pred);
236 ScanType getScanType() { return scanType_; }
237 bool hasIndex(char *fldName);
238 IndexType getIndexType(char *fldName, int* pos);
239 void addPredicate(char *fName, ComparisionOp op, void *buf);
241 char* getName() { return tblName_; }
242 void setTableInfo(char *name, int tblid, size_t length,
243 int numFld, int numIdx, void *chunk);
244 friend class DatabaseManagerImpl;
248 #endif