Fix for Bug : 2410115
[csql.git] / include / TableImpl.h
blob3b6a1bab097fba36dae1e40911be8748cef3cf74
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 enum ScanType
30 fullTableScan = 0,
31 hashIndexScan,
32 treeIndexScan,
33 unknownScan
36 static char ScanTypeNames[][10] =
38 "TableScan", "HashScan", "TreeScan", "Invalid"
41 class Predicate;
43 class TupleIterator
45 Predicate *pred_;
46 ScanType scanType_;
47 ChunkIterator *cIter;
48 BucketIter *bIter;
49 TreeIter *tIter;
50 IndexInfo *info;
51 void *chunkPtr_;
52 int procSlot;
53 bool isBetween;
54 TupleIterator(){}
55 public:
57 TupleIterator(Predicate *p, ScanType t, IndexInfo *i, void *cptr, int pslot,bool between)
58 { bIter = NULL; pred_ = p ; scanType_ = t; info = i; chunkPtr_ = cptr; procSlot =pslot; isBetween=between;}
60 ~TupleIterator()
62 if (bIter) delete bIter;
63 bIter = NULL;
65 bool isBetInvolved(){ return isBetween;}
66 void setBetInvolved(bool between){ isBetween=between;}
67 DbRetVal open();
68 void* next();
69 void* prev();//used only for tree iter during deleteTuple
70 DbRetVal close();
73 class TableImpl:public Table
75 private:
78 LockManager *lMgr_;
79 Transaction **trans;
80 //This is pointer to the pointer stored in the
81 //Transaction manager.
82 //If the transaction commits/aborts this pointer changes
83 //and this will get that newly allocated transaction
85 char tblName_[IDENTIFIER_LENGTH];
86 int tblID_;
87 size_t length_; //length of the tuple
88 int numFlds_;
89 void* chunkPtr_;
90 void *curTuple_; //holds the current tuple ptr. moved during fetch() calls
92 Predicate *pred_;
93 ScanType scanType_;
94 //ChunkIterator *iter;
95 //BucketIter *bIter;
97 TupleIterator *iter;
99 bool undoFlag;
101 public:
102 FieldList fldList_;
103 int numIndexes_;
104 char** indexPtr_; // array of index ptrs to the catalog table for the indexes of this table.
105 IndexInfo **idxInfo;
106 int useIndex_;//offet in the above array indexPtr_ for scan
107 bool isPlanCreated;
109 Database *db_;
110 Database *sysDB_;
112 //Either one of the below is populated based on the no of fields and
113 //is used for tuple insertions
114 bool isIntUsedForNULL;
115 int iNullInfo;
116 char *cNullInfo;
117 int iNotNullInfo;
118 char *cNotNullInfo;
120 private:
122 //copy Values from binded buffer to tuple pointed by arg
123 DbRetVal copyValuesFromBindBuffer(void *tuple, bool isInsert=true);
124 DbRetVal copyValuesToBindBuffer(void *tuple);
125 void setNullBit(int fldpos);
126 void clearNullBit(int fldpos);
127 DbRetVal insertIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
128 DbRetVal updateIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
129 DbRetVal deleteIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
131 DbRetVal createPlan(bool &bet);
132 Chunk* getSystemTableChunk(CatalogTableID id)
134 return sysDB_->getSystemDatabaseChunk(id);
137 public:
138 TableImpl() { db_ = NULL; chunkPtr_ = NULL; iter = NULL;
139 idxInfo = NULL; indexPtr_ = NULL; scanType_ = unknownScan;
140 pred_ = NULL; useIndex_ = -1; numFlds_ = 0;
141 iNullInfo = 0; cNullInfo = NULL; isIntUsedForNULL = true;
142 iNotNullInfo = 0; cNotNullInfo = NULL; curTuple_ = NULL;
143 isPlanCreated = false; undoFlag = true;}
144 ~TableImpl();
146 void setDB(Database *db) { db_ = db; }
147 Database* getDB() { return db_;}
148 void setSystemDB(Database *db) { sysDB_ = db; }
149 void setLockManager(LockManager *lmgr) { lMgr_ = lmgr; }
150 void setTrans(Transaction **t) { trans = t; }
151 void setCurTuple(void *tuple){ curTuple_=tuple; }
152 DataType getFieldType(const char *name)
153 { return fldList_.getFieldType(name); }
154 int getFieldOffset(const char *name)
155 { return fldList_.getFieldOffset(name); }
156 size_t getFieldLength(const char *name)
157 { return fldList_.getFieldLength(name); }
159 DbRetVal getFieldInfo(const char *fieldName, FieldInfo *&info)
161 char tblName[IDENTIFIER_LENGTH];
162 char fldName[IDENTIFIER_LENGTH];
163 getTableNameAlone((char*)fieldName, tblName);
164 getFieldNameAlone((char*)fieldName, fldName);
165 if (0 == strcmp(tblName, "") || 0 ==strcmp(tblName, getName()))
166 return fldList_.getFieldInfo(fldName, info);
167 else
168 return ErrNotExists;
171 List getFieldNameList();
173 // search predicate
174 void setCondition(Condition *p)
175 { isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
177 //binding
178 DbRetVal bindFld(const char *name, void *val);
179 void *getBindFldAddr(const char *name);
180 int getFldPos(char *name);
181 DbRetVal markFldNull(const char *name);
182 DbRetVal markFldNull(int colpos);
183 bool isFldNull(const char *name);
184 bool isFldNull(int colpos);
186 void clearFldNull(const char *name);
187 void clearFldNull(int colpos);
188 void resetNullinfo();
189 DbRetVal insertTuple();
190 DbRetVal updateTuple();
192 DbRetVal deleteTuple();
193 int deleteWhere();
194 int truncate();
196 DbRetVal execute();
198 void* fetch();
199 void* fetch(DbRetVal &rv);
200 void* fetchNoBind();
201 void* fetchNoBind(DbRetVal &rv);
203 DbRetVal close();
204 DbRetVal closeScan();
207 long spaceUsed();
208 long numTuples();
209 int pagesUsed();
210 void printInfo();
211 void printPlan(int space);
213 DbRetVal lock(bool shared);
214 DbRetVal unlock();
216 DbRetVal setUndoLogging(bool flag) { undoFlag = flag; }
218 void printSQLIndexString();
219 bool isTableInvolved(char *tblName);
220 bool pushPredicate(Predicate *pred);
221 void setPredicate(Predicate *pred);
222 char* getName() { return tblName_; }
223 void setTableInfo(char *name, int tblid, size_t length,
224 int numFld, int numIdx, void *chunk);
225 friend class DatabaseManagerImpl;
229 #endif