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>
36 static char ScanTypeNames
[][10] =
38 "TableScan", "HashScan", "TreeScan", "Invalid"
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
;}
62 if (bIter
) delete bIter
;
65 bool isBetInvolved(){ return isBetween
;}
66 void setBetInvolved(bool between
){ isBetween
=between
;}
69 void* prev();//used only for tree iter during deleteTuple
73 class TableImpl
:public Table
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
];
87 size_t length_
; //length of the tuple
90 void *curTuple_
; //holds the current tuple ptr. moved during fetch() calls
94 //ChunkIterator *iter;
104 char** indexPtr_
; // array of index ptrs to the catalog table for the indexes of this table.
106 int useIndex_
;//offet in the above array indexPtr_ for scan
112 //Either one of the below is populated based on the no of fields and
113 //is used for tuple insertions
114 bool isIntUsedForNULL
;
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
);
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;}
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
);
171 List
getFieldNameList();
174 void setCondition(Condition
*p
)
175 { isPlanCreated
= false; if (p
) pred_
= p
->getPredicate(); else pred_
= NULL
;}
178 DbRetVal
bindFld(const char *name
, void *val
);
179 void *getBindFldAddr(const char *name
);
180 int getFldPos(char *name
);
181 void markFldNull(const char *name
);
182 void 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();
199 void* fetch(DbRetVal
&rv
);
201 void* fetchNoBind(DbRetVal
&rv
);
204 DbRetVal
closeScan();
211 void printPlan(int space
);
213 DbRetVal
lock(bool shared
);
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
;