Fixing bugs in aggregate and join
[csql.git] / include / TableImpl.h
blob96e8cb133f22dbe33098007bdc21b1846edcc4ec
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;
54 TupleIterator(){}
55 public:
57 TupleIterator(Predicate *p, ScanType t, IndexInfo *i, void *cptr, int pslot)
58 { bIter = NULL; pred_ = p ; scanType_ = t; info = i; chunkPtr_ = cptr; procSlot =pslot;}
60 ~TupleIterator()
62 if (bIter) delete bIter;
63 bIter = NULL;
66 DbRetVal open();
67 void* next();
68 void* prev();//used only for tree iter during deleteTuple
69 DbRetVal close();
72 class TableImpl:public Table
74 private:
77 LockManager *lMgr_;
78 Transaction **trans;
79 //This is pointer to the pointer stored in the
80 //Transaction manager.
81 //If the transaction commits/aborts this pointer changes
82 //and this will get that newly allocated transaction
84 char tblName_[IDENTIFIER_LENGTH];
85 int tblID_;
86 size_t length_; //length of the tuple
87 int numFlds_;
88 void* chunkPtr_;
89 void *curTuple_; //holds the current tuple ptr. moved during fetch() calls
91 Predicate *pred_;
92 ScanType scanType_;
93 //ChunkIterator *iter;
94 //BucketIter *bIter;
96 TupleIterator *iter;
98 bool undoFlag;
100 public:
101 FieldList fldList_;
102 int numIndexes_;
103 char** indexPtr_; // array of index ptrs to the catalog table for the indexes of this table.
104 IndexInfo **idxInfo;
105 int useIndex_;//offet in the above array indexPtr_ for scan
106 bool isPlanCreated;
108 Database *db_;
109 Database *sysDB_;
111 //Either one of the below is populated based on the no of fields and
112 //is used for tuple insertions
113 bool isIntUsedForNULL;
114 int iNullInfo;
115 char *cNullInfo;
116 int iNotNullInfo;
117 char *cNotNullInfo;
119 private:
121 //copy Values from binded buffer to tuple pointed by arg
122 DbRetVal copyValuesFromBindBuffer(void *tuple, bool isInsert=true);
123 DbRetVal copyValuesToBindBuffer(void *tuple);
124 void setNullBit(int fldpos);
125 void clearNullBit(int fldpos);
126 DbRetVal insertIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
127 DbRetVal updateIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
128 DbRetVal deleteIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
130 DbRetVal createPlan();
131 Chunk* getSystemTableChunk(CatalogTableID id)
133 return sysDB_->getSystemDatabaseChunk(id);
136 public:
137 TableImpl() { db_ = NULL; chunkPtr_ = NULL; iter = NULL;
138 idxInfo = NULL; indexPtr_ = NULL; scanType_ = unknownScan;
139 pred_ = NULL; useIndex_ = -1; numFlds_ = 0;
140 iNullInfo = 0; cNullInfo = NULL; isIntUsedForNULL = true;
141 iNotNullInfo = 0; cNotNullInfo = NULL; curTuple_ = NULL;
142 isPlanCreated = false; undoFlag = true;}
143 ~TableImpl();
145 void setDB(Database *db) { db_ = db; }
146 Database* getDB() { return db_;}
147 void setSystemDB(Database *db) { sysDB_ = db; }
148 void setLockManager(LockManager *lmgr) { lMgr_ = lmgr; }
149 void setTrans(Transaction **t) { trans = t; }
151 DataType getFieldType(const char *name)
152 { return fldList_.getFieldType(name); }
153 int getFieldOffset(const char *name)
154 { return fldList_.getFieldOffset(name); }
155 size_t getFieldLength(const char *name)
156 { return fldList_.getFieldLength(name); }
158 DbRetVal getFieldInfo(const char *fieldName, FieldInfo *&info)
160 char tblName[IDENTIFIER_LENGTH];
161 char fldName[IDENTIFIER_LENGTH];
162 getTableNameAlone((char*)fieldName, tblName);
163 getFieldNameAlone((char*)fieldName, fldName);
164 if (0 == strcmp(tblName, "") || 0 ==strcmp(tblName, getName()))
165 return fldList_.getFieldInfo(fldName, info);
166 else
167 return ErrNotExists;
170 List getFieldNameList();
172 // search predicate
173 void setCondition(Condition *p)
174 { isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
176 //binding
177 DbRetVal bindFld(const char *name, void *val);
178 void *getBindFldAddr(const char *name);
179 int getFldPos(char *name);
180 void markFldNull(const char *name);
181 void markFldNull(int colpos);
182 bool isFldNull(const char *name);
183 bool isFldNull(int colpos);
185 void clearFldNull(const char *name);
186 void clearFldNull(int colpos);
187 void resetNullinfo();
188 DbRetVal insertTuple();
189 DbRetVal updateTuple();
191 DbRetVal deleteTuple();
192 int deleteWhere();
193 int truncate();
195 DbRetVal execute();
197 void* fetch();
198 void* fetch(DbRetVal &rv);
199 void* fetchNoBind();
200 void* fetchNoBind(DbRetVal &rv);
202 DbRetVal close();
203 DbRetVal closeScan();
206 long spaceUsed();
207 long numTuples();
208 int pagesUsed();
209 void printInfo();
210 void printPlan(int space);
212 DbRetVal lock(bool shared);
213 DbRetVal unlock();
215 DbRetVal setUndoLogging(bool flag) { undoFlag = flag; }
217 void printSQLIndexString();
218 bool isTableInvolved(char *tblName);
219 bool pushPredicate(Predicate *pred);
220 void setPredicate(Predicate *pred);
221 char* getName() { return tblName_; }
222 void setTableInfo(char *name, int tblid, size_t length,
223 int numFld, int numIdx, void *chunk);
224 friend class DatabaseManagerImpl;
228 #endif