1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
20 #include<Transaction.h>
23 #include<CatalogTables.h>
26 #include<DatabaseManagerImpl.h>
28 #include<AggTableImpl.h>//for AggType
40 static char ScanTypeNames
[][10] =
42 "TableScan", "HashScan", "TreeScan", "Invalid"
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();
71 pred_
= p
; scanType_
= t
; info
= i
; chunkPtr_
= cptr
;
73 procSlot
=pslot
; isBetween
=between
; isPointLook
=isPtLook
; shouldNullCheck
=nullflag
;
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
;}
90 void* prev();//used only for tree iter during deleteTuple
94 class DllExport TableImpl
:public Table
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
];
106 size_t length_
; //length of the tuple
110 void *curTuple_
; //holds the current tuple ptr. moved during fetch() calls
115 //ChunkIterator *iter;
121 char aliasName
[IDENTIFIER_LENGTH
];
126 void **bindListArray_
;
130 char** indexPtr_
; // array of index ptrs to the catalog table for the indexes of this table.
132 int useIndex_
;//offet in the above array indexPtr_ for scan
139 long long tempAutoVal
;
140 //Either one of the below is populated based on the no of fields and
141 //is used for tuple insertions
142 bool isIntUsedForNULL
;
152 bool shouldNullSearch
;
155 //copy Values from binded buffer to tuple pointed by arg
156 DbRetVal
copyValuesFromBindBuffer(void *tuple
, bool isInsert
=true);
157 DbRetVal
copyValuesToBindBuffer(void *tuple
);
158 void setNullBit(int fldpos
);
159 void clearNullBit(int fldpos
);
160 DbRetVal
insertIndexNode(Transaction
*trans
, void *indexPtr
, IndexInfo
*info
, void *tuple
);
161 DbRetVal
updateIndexNode(Transaction
*trans
, void *indexPtr
, IndexInfo
*info
, void *tuple
);
162 DbRetVal
deleteIndexNode(Transaction
*trans
, void *indexPtr
, IndexInfo
*info
, void *tuple
);
163 bool isFKTable(){return isFkTbl
;};
164 DbRetVal
createPlan();
165 Chunk
* getSystemTableChunk(CatalogTableID id
)
167 return sysDB_
->getSystemDatabaseChunk(id
);
169 DbRetVal
trySharedLock(void *curTuple
, Transaction
**trans
);
170 DbRetVal
tryExclusiveLock(void *curTuple
, Transaction
**trans
);
171 DbRetVal
getCheckpointMutex();
175 TableImpl() { db_
= NULL
; chunkPtr_
= NULL
; vcChunkPtr_
=NULL
; iter
= NULL
;
176 idxInfo
= NULL
; indexPtr_
= NULL
; scanType_
= unknownScan
;
177 pred_
= NULL
; useIndex_
= -1; numFlds_
= 0; bindListArray_
= NULL
;
178 iNullInfo
= 0; cNullInfo
= NULL
; isIntUsedForNULL
= true;
179 iNotNullInfo
= 0; cNotNullInfo
= NULL
; curTuple_
= NULL
;
180 isPlanCreated
= false; loadFlag
= false;isFkTbl
=false;isPkTbl
=false;numFkRelation_
=0; shouldNullSearch
= false;}
183 void setDB(Database
*db
) { db_
= db
; }
184 Database
* getDB() { return db_
;}
185 void setSystemDB(Database
*db
) { sysDB_
= db
; }
186 void setLockManager(LockManager
*lmgr
) { lMgr_
= lmgr
; }
187 void setTrans(Transaction
**t
) { trans
= t
; }
188 inline void setCurTuple(void *tuple
){ curTuple_
=tuple
; }
189 DataType
getFieldType(const char *name
)
190 { return fldList_
.getFieldType(name
); }
191 int getFieldOffset(const char *name
)
192 { return fldList_
.getFieldOffset(name
); }
193 size_t getFieldLength(const char *name
)
194 { return fldList_
.getFieldLength(name
); }
195 int getNumFields() { return numFlds_
; }
196 void fillFieldInfo(int pos
, void* val
)
197 { return fldList_
.fillFieldInfo(pos
,val
); }
199 long long getLastInsertedVal(DbRetVal
&rv
);
200 DbRetVal
getFieldInfo(const char *fieldName
, FieldInfo
*&info
)
202 char tblName
[IDENTIFIER_LENGTH
];
203 char fldName
[IDENTIFIER_LENGTH
];
204 getTableNameAlone((char*)fieldName
, tblName
);
205 getFieldNameAlone((char*)fieldName
, fldName
);
206 if (0 == strcmp(tblName
, "") || 0 ==strcmp(tblName
, getName()) ||
207 0 == strcmp(tblName
, getAliasName()))
208 return fldList_
.getFieldInfo(fldName
, info
);
213 List
getFieldNameList();
216 void setCondition(Condition
*p
);
217 //{ isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
220 DbRetVal
bindFld(const char *name
, void *val
, bool isNullExpl
=false);
221 void *getBindFldAddr(const char *name
);
222 int getFldPos(char *name
);
223 DbRetVal
markFldNull(const char *name
);
224 DbRetVal
markFldNull(int colpos
);
225 bool isFldNull(const char *name
);
226 bool isFldNull(int colpos
);
228 void clearFldNull(const char *name
);
229 void clearFldNull(int colpos
);
230 void resetNullinfo();
231 DbRetVal
insertTuple();
232 DbRetVal
updateTuple();
234 DbRetVal
deleteTuple();
239 bool isPkTableHasRecord(char *name
,TableImpl
*fkTbl
,bool isInsert
);
240 bool isFkTableHasRecord(char *name
,TableImpl
*fkTbl
);
242 void* fetch(DbRetVal
&rv
);
244 void* fetchNoBind(DbRetVal
&rv
);
245 DbRetVal
fetchAgg(const char *fldName
, AggType aType
, void *buf
, bool &noRec
);
248 DbRetVal
closeScan();
255 void printPlan(int space
);
257 DbRetVal
lock(bool shared
);
259 DbRetVal
takeTableMutex();
260 DbRetVal
releaseTableMutex();
262 DbRetVal
setUndoLogging(bool flag
) { loadFlag
= flag
; return OK
;}
264 void printSQLIndexString(FILE *fp
, int fd
);
265 void printSQLForeignString();
267 bool isTableInvolved(char *tblName
);
268 bool pushPredicate(Predicate
*pred
);
269 void setPredicate(Predicate
*pred
);
270 ScanType
getScanType() { return scanType_
; }
271 bool hasIndex(char *fldName
);
272 IndexType
getIndexType(char *fldName
, int* pos
);
273 void addPredicate(char *fName
, ComparisionOp op
, void *buf
);
275 DbRetVal
compactIndexNode( void *indexPtr
);
276 char* getName() { return tblName_
; }
277 char* getAliasName() { return aliasName
; }
278 void setAliasName(char *name
);
279 void setTableInfo(char *name
, int tblid
, size_t length
,
280 int numFld
, int numIdx
, void *chunk
, void *vcchunk
);
281 void setLoading(bool flag
) { loadFlag
= flag
; }
282 friend class DatabaseManagerImpl
;