Varchar Implementation part 1
[csql.git] / include / TableImpl.h
bloba2b95ee130b3f5b3ddd806f5fc76bb381c24ddb3
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 char *keyBuffer;
56 char *keyPtr;
57 ComparisionOp op;
58 void *chunkPtr_;
59 int procSlot;
60 bool isBetween;
61 bool isPointLook;
62 bool shouldNullCheck;
63 bool isClosed;
64 TupleIterator() { }
65 public:
66 TupleIterator(Predicate *p, ScanType t, IndexInfo *i, void *cptr, int pslot,bool between , bool isPtLook,bool nullflag)
67 { bIter = new BucketIter();
68 cIter = new ChunkIterator();;
69 tIter = new TreeIter();
70 keyBuffer = NULL;
71 pred_ = p ; scanType_ = t; info = i; chunkPtr_ = cptr;
72 isClosed =true;
73 procSlot =pslot; isBetween=between; isPointLook=isPtLook; shouldNullCheck=nullflag;
75 DbRetVal setPlan();
77 ~TupleIterator()
79 if (bIter) { delete bIter; bIter = NULL; }
80 if (cIter) { delete cIter; cIter = NULL; }
81 if (tIter) { delete tIter; tIter = NULL; }
82 if (keyBuffer) { ::free(keyBuffer); keyBuffer = NULL; }
84 bool isIterClosed() { return isClosed; }
85 bool isBetInvolved(){ return isBetween;}
86 void setBetInvolved(bool between){ isBetween=between;}
87 bool isPointLookInvolved(){return isPointLook;}
88 DbRetVal open();
89 void* next();
90 void* prev();//used only for tree iter during deleteTuple
91 void reset();
92 DbRetVal close();
94 class TableImpl:public Table
96 private:
97 LockManager *lMgr_;
98 Transaction **trans;
99 //This is pointer to the pointer stored in the
100 //Transaction manager.
101 //If the transaction commits/aborts this pointer changes
102 //and this will get that newly allocated transaction
104 char tblName_[IDENTIFIER_LENGTH];
105 int tblID_;
106 size_t length_; //length of the tuple
107 int numFlds_;
108 void* chunkPtr_;
109 void* vcChunkPtr_;
110 void *curTuple_; //holds the current tuple ptr. moved during fetch() calls
112 Predicate *pred_;
113 List predList;
114 ScanType scanType_;
115 //ChunkIterator *iter;
116 //BucketIter *bIter;
118 TupleIterator *iter;
120 bool loadFlag;
121 char aliasName[IDENTIFIER_LENGTH];
123 public:
124 FieldList fldList_;
125 List bindList_;
126 void **bindListArray_;
127 int numBindFlds_;
128 int numIndexes_;
129 int numFkRelation_;
130 char** indexPtr_; // array of index ptrs to the catalog table for the indexes of this table.
131 IndexInfo **idxInfo;
132 int useIndex_;//offet in the above array indexPtr_ for scan
133 bool isPlanCreated;
134 bool isPointLook;
135 bool isBetween;
136 Database *db_;
137 Database *sysDB_;
138 void *ptrToAuto;
139 //Either one of the below is populated based on the no of fields and
140 //is used for tuple insertions
141 bool isIntUsedForNULL;
142 int iNullInfo;
143 char *cNullInfo;
144 int iNotNullInfo;
145 char *cNotNullInfo;
146 //Table *fkTbl;
147 List tblList;
148 List tblFkList;
149 bool isFkTbl;
150 bool isPkTbl;
151 bool shouldNullSearch;
152 private:
154 //copy Values from binded buffer to tuple pointed by arg
155 DbRetVal copyValuesFromBindBuffer(void *tuple, bool isInsert=true);
156 DbRetVal copyValuesToBindBuffer(void *tuple);
157 void setNullBit(int fldpos);
158 void clearNullBit(int fldpos);
159 DbRetVal insertIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
160 DbRetVal updateIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
161 DbRetVal deleteIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
162 bool isFKTable(){return isFkTbl;};
163 DbRetVal createPlan();
164 Chunk* getSystemTableChunk(CatalogTableID id)
166 return sysDB_->getSystemDatabaseChunk(id);
168 DbRetVal trySharedLock(void *curTuple, Transaction **trans);
169 DbRetVal tryExclusiveLock(void *curTuple, Transaction **trans);
172 public:
173 TableImpl() { db_ = NULL; chunkPtr_ = NULL; vcChunkPtr_=NULL; iter = NULL;
174 idxInfo = NULL; indexPtr_ = NULL; scanType_ = unknownScan;
175 pred_ = NULL; useIndex_ = -1; numFlds_ = 0; bindListArray_ = NULL;
176 iNullInfo = 0; cNullInfo = NULL; isIntUsedForNULL = true;
177 iNotNullInfo = 0; cNotNullInfo = NULL; curTuple_ = NULL;
178 isPlanCreated = false; loadFlag = false;isFkTbl=false;isPkTbl=false;numFkRelation_=0; shouldNullSearch = false;}
179 ~TableImpl();
181 void setDB(Database *db) { db_ = db; }
182 Database* getDB() { return db_;}
183 void setSystemDB(Database *db) { sysDB_ = db; }
184 void setLockManager(LockManager *lmgr) { lMgr_ = lmgr; }
185 void setTrans(Transaction **t) { trans = t; }
186 inline void setCurTuple(void *tuple){ curTuple_=tuple; }
187 DataType getFieldType(const char *name)
188 { return fldList_.getFieldType(name); }
189 int getFieldOffset(const char *name)
190 { return fldList_.getFieldOffset(name); }
191 size_t getFieldLength(const char *name)
192 { return fldList_.getFieldLength(name); }
193 int getNumFields() { return numFlds_; }
194 void fillFieldInfo(int pos, void* val)
195 { return fldList_.fillFieldInfo(pos,val); }
197 DbRetVal getFieldInfo(const char *fieldName, FieldInfo *&info)
199 char tblName[IDENTIFIER_LENGTH];
200 char fldName[IDENTIFIER_LENGTH];
201 getTableNameAlone((char*)fieldName, tblName);
202 getFieldNameAlone((char*)fieldName, fldName);
203 if (0 == strcmp(tblName, "") || 0 ==strcmp(tblName, getName()) ||
204 0 == strcmp(tblName, getAliasName()))
205 return fldList_.getFieldInfo(fldName, info);
206 else
207 return ErrNotExists;
210 List getFieldNameList();
212 // search predicate
213 void setCondition(Condition *p);
214 //{ isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
216 //binding
217 DbRetVal bindFld(const char *name, void *val);
218 void *getBindFldAddr(const char *name);
219 int getFldPos(char *name);
220 DbRetVal markFldNull(const char *name);
221 DbRetVal markFldNull(int colpos);
222 bool isFldNull(const char *name);
223 bool isFldNull(int colpos);
225 void clearFldNull(const char *name);
226 void clearFldNull(int colpos);
227 void resetNullinfo();
228 DbRetVal insertTuple();
229 DbRetVal updateTuple();
231 DbRetVal deleteTuple();
232 int deleteWhere();
233 int truncate();
235 DbRetVal execute();
236 bool isPkTableHasRecord(char *name,TableImpl *fkTbl,bool isInsert);
237 bool isFkTableHasRecord(char *name,TableImpl *fkTbl);
238 void* fetch();
239 void* fetch(DbRetVal &rv);
240 void* fetchNoBind();
241 void* fetchNoBind(DbRetVal &rv);
242 DbRetVal fetchAgg(const char *fldName, AggType aType, void *buf, bool &noRec);
244 DbRetVal close();
245 DbRetVal closeScan();
248 long spaceUsed();
249 long numTuples();
250 int pagesUsed();
251 void printInfo();
252 void printPlan(int space);
254 DbRetVal lock(bool shared);
255 DbRetVal unlock();
257 DbRetVal setUndoLogging(bool flag) { loadFlag = flag; return OK;}
259 void printSQLIndexString(FILE *fp, int fd);
260 void printSQLForeignString();
261 DbRetVal optimize();
262 bool isTableInvolved(char *tblName);
263 bool pushPredicate(Predicate *pred);
264 void setPredicate(Predicate *pred);
265 ScanType getScanType() { return scanType_; }
266 bool hasIndex(char *fldName);
267 IndexType getIndexType(char *fldName, int* pos);
268 void addPredicate(char *fName, ComparisionOp op, void *buf);
269 DbRetVal compact();
270 DbRetVal compactIndexNode( void *indexPtr);
271 char* getName() { return tblName_; }
272 char* getAliasName() { return aliasName; }
273 void setAliasName(char *name);
274 void setTableInfo(char *name, int tblid, size_t length,
275 int numFld, int numIdx, void *chunk, void *vcchunk);
276 void setLoading(bool flag) { loadFlag = flag; }
277 friend class DatabaseManagerImpl;
281 #endif