stmt->close calls table->closeScan not close as earlier.
[csql.git] / include / TableImpl.h
blob1cd33de8f387da7cfa9205583ef9f9aa07791c7b
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 bool isPointLook;
56 TupleIterator(){}
57 public:
58 TupleIterator(Predicate *p, ScanType t, IndexInfo *i, void *cptr, int pslot,bool between , bool isPtLook)
59 { bIter = NULL; pred_ = p ; scanType_ = t; info = i; chunkPtr_ = cptr; procSlot =pslot; isBetween=between; isPointLook=isPtLook; }
61 ~TupleIterator()
63 if (bIter) delete bIter;
64 bIter = NULL;
66 bool isBetInvolved(){ return isBetween;}
67 void setBetInvolved(bool between){ isBetween=between;}
68 bool isPointLookInvolved(){return isPointLook;}
69 DbRetVal open();
70 void* next();
71 void* prev();//used only for tree iter during deleteTuple
72 void reset();
73 DbRetVal close();
76 class TableImpl:public Table
78 private:
81 LockManager *lMgr_;
82 Transaction **trans;
83 //This is pointer to the pointer stored in the
84 //Transaction manager.
85 //If the transaction commits/aborts this pointer changes
86 //and this will get that newly allocated transaction
88 char tblName_[IDENTIFIER_LENGTH];
89 int tblID_;
90 size_t length_; //length of the tuple
91 int numFlds_;
92 void* chunkPtr_;
93 void *curTuple_; //holds the current tuple ptr. moved during fetch() calls
95 Predicate *pred_;
96 ScanType scanType_;
97 //ChunkIterator *iter;
98 //BucketIter *bIter;
100 TupleIterator *iter;
102 bool undoFlag;
104 public:
105 FieldList fldList_;
106 int numIndexes_;
107 char** indexPtr_; // array of index ptrs to the catalog table for the indexes of this table.
108 IndexInfo **idxInfo;
109 int useIndex_;//offet in the above array indexPtr_ for scan
110 bool isPlanCreated;
111 bool isPointLook;
112 bool isBetween;
113 Database *db_;
114 Database *sysDB_;
116 //Either one of the below is populated based on the no of fields and
117 //is used for tuple insertions
118 bool isIntUsedForNULL;
119 int iNullInfo;
120 char *cNullInfo;
121 int iNotNullInfo;
122 char *cNotNullInfo;
124 private:
126 //copy Values from binded buffer to tuple pointed by arg
127 DbRetVal copyValuesFromBindBuffer(void *tuple, bool isInsert=true);
128 DbRetVal copyValuesToBindBuffer(void *tuple);
129 void setNullBit(int fldpos);
130 void clearNullBit(int fldpos);
131 DbRetVal insertIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
132 DbRetVal updateIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
133 DbRetVal deleteIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
135 DbRetVal createPlan();
136 Chunk* getSystemTableChunk(CatalogTableID id)
138 return sysDB_->getSystemDatabaseChunk(id);
141 public:
142 TableImpl() { db_ = NULL; chunkPtr_ = NULL; iter = NULL;
143 idxInfo = NULL; indexPtr_ = NULL; scanType_ = unknownScan;
144 pred_ = NULL; useIndex_ = -1; numFlds_ = 0;
145 iNullInfo = 0; cNullInfo = NULL; isIntUsedForNULL = true;
146 iNotNullInfo = 0; cNotNullInfo = NULL; curTuple_ = NULL;
147 isPlanCreated = false; undoFlag = true;}
148 ~TableImpl();
150 void setDB(Database *db) { db_ = db; }
151 Database* getDB() { return db_;}
152 void setSystemDB(Database *db) { sysDB_ = db; }
153 void setLockManager(LockManager *lmgr) { lMgr_ = lmgr; }
154 void setTrans(Transaction **t) { trans = t; }
155 void setCurTuple(void *tuple){ curTuple_=tuple; }
156 DataType getFieldType(const char *name)
157 { return fldList_.getFieldType(name); }
158 int getFieldOffset(const char *name)
159 { return fldList_.getFieldOffset(name); }
160 size_t getFieldLength(const char *name)
161 { return fldList_.getFieldLength(name); }
163 DbRetVal getFieldInfo(const char *fieldName, FieldInfo *&info)
165 char tblName[IDENTIFIER_LENGTH];
166 char fldName[IDENTIFIER_LENGTH];
167 getTableNameAlone((char*)fieldName, tblName);
168 getFieldNameAlone((char*)fieldName, fldName);
169 if (0 == strcmp(tblName, "") || 0 ==strcmp(tblName, getName()))
170 return fldList_.getFieldInfo(fldName, info);
171 else
172 return ErrNotExists;
175 List getFieldNameList();
177 // search predicate
178 void setCondition(Condition *p)
179 { isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
181 //binding
182 DbRetVal bindFld(const char *name, void *val);
183 void *getBindFldAddr(const char *name);
184 int getFldPos(char *name);
185 DbRetVal markFldNull(const char *name);
186 DbRetVal markFldNull(int colpos);
187 bool isFldNull(const char *name);
188 bool isFldNull(int colpos);
190 void clearFldNull(const char *name);
191 void clearFldNull(int colpos);
192 void resetNullinfo();
193 DbRetVal insertTuple();
194 DbRetVal updateTuple();
196 DbRetVal deleteTuple();
197 int deleteWhere();
198 int truncate();
200 DbRetVal execute();
202 void* fetch();
203 void* fetch(DbRetVal &rv);
204 void* fetchNoBind();
205 void* fetchNoBind(DbRetVal &rv);
207 DbRetVal close();
208 DbRetVal closeScan();
211 long spaceUsed();
212 long numTuples();
213 int pagesUsed();
214 void printInfo();
215 void printPlan(int space);
217 DbRetVal lock(bool shared);
218 DbRetVal unlock();
220 DbRetVal setUndoLogging(bool flag) { undoFlag = flag; }
222 void printSQLIndexString();
223 bool isTableInvolved(char *tblName);
224 bool pushPredicate(Predicate *pred);
225 void setPredicate(Predicate *pred);
226 char* getName() { return tblName_; }
227 void setTableInfo(char *name, int tblid, size_t length,
228 int numFld, int numIdx, void *chunk);
229 friend class DatabaseManagerImpl;
233 #endif