performance fixes for join table based on selectivity
[csql.git] / include / Table.h
blob87538efec2ce30aadc853a64ec607e7eec895bc4
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_H
17 #define TABLE_H
18 #include<ErrorType.h>
19 #include<Util.h>
20 class Predicate;
21 class Condition;
22 #ifndef SCANTYPE
23 enum ScanType
25 fullTableScan = 0,
26 hashIndexScan,
27 treeIndexScan,
28 unknownScan
30 #define SCANTYPE
31 #endif
32 /**
33 * @class Table
35 * @brief Handle to the database table.
36 * Table is a set of data values organized using a model of horizontal rows and columns.<br>
37 * Columns are identified by name and rows by values. It shall be visualized as a linked <br/>
38 * list of related data<br/>
39 * Functionality: <br/>
40 * 1.insert <br/>
41 * 2.update <br/>
42 * 3.delete <br/>
43 * 4.select with predicate or condition<br/>
44 * <br/>
48 class FieldInfo;
49 class Table
51 public:
52 /** sets condition for the select, update and delete operations
53 * @param c condition
55 virtual void setCondition(Condition *c)=0;
57 /** binds application buffer to the specified field of the table.
58 * Before doing any DML operations required fields are binded first.
59 * Below are the candidates for this: <br/>
60 * 1.All the fields in the projection list of select statement. <br/>
61 * 2.Field list in the insert statement.<br/>
62 * 3.Fields in the assignment list of update statement.<br/>
63 * <br/>
64 * @param name field name in the table
65 * @param val address of the application buffer. Memory should be allocated by
66 * the application before binding the buffer.
69 virtual DbRetVal bindFld(const char *name, void *val)=0;
70 /** marks the specified field to insert null when insert method is called.
71 * @param name field name in the table
73 virtual void *getBindFldAddr(const char *name)=0;
74 virtual DbRetVal markFldNull(const char *name)=0;
75 /** marks the specified field to insert null when insert method is called.
76 * @param colpos field position
78 virtual DbRetVal markFldNull(int colpos)=0;
80 /** clears the null mark which is set before.
81 * @param name field name
83 virtual void clearFldNull(const char *name)=0;
85 /** clears the null mark which is set before.
86 * @param colpos field position
88 virtual void clearFldNull(int colpos)=0;
90 /** checks whether the field value is null.
91 * @param name field name
93 virtual bool isFldNull(const char *name)=0;
95 /** checks whether the field value is null for the specified field position.
96 * @param colpos field position
98 virtual bool isFldNull(int colpos)=0;
100 virtual void clearFldNull(const char *name)=0;
101 virtual void clearFldNull(int colpos) =0;
104 //DML operations
105 /** executes the select statement. Based on the predicate(condition), respective index is
106 * chosen for the select. Application should call execute before they start fetching
107 * the values from the table.This starts scan on the table.
108 * @returns DbRetVal
110 virtual DbRetVal execute()=0;
112 /** insert the tuple into the table. Prior to this, the binded buffer should be updated
113 * Values present in the binded buffers of all the fields are taken and stored in
114 * the table.<br/>
115 * @returns DbRetVal
117 virtual DbRetVal insertTuple()=0;
119 /**update values in the current tuple of the table. It works only on top of select
120 * using fetch go to the tuple to be updated, then update the binded buffer value and
121 * then call this method to updates the field values in the tuple.
122 * @returns DbRetVal
124 virtual DbRetVal updateTuple()=0;
126 /**deletes the current tuple of the table. It works only on top of select.
127 * using fetch first go to the tuple to be deleted, then call this method to delete.<br/>
128 * To delete all the tuples, do not set any condition and call fetch followed by delete
129 * in a loop till fetch exhausts.
130 * @returns DbRetVal
133 virtual DbRetVal deleteTuple()=0;
136 /**deletes all the records which satisfies the condition set.
137 * @returns int totalTuples deleted
140 virtual int deleteWhere()=0;
144 /**deletes the all the records in the table. No transaction required to call this.
145 * It takes table level lock.
146 * @returns DbRetVal
149 virtual int truncate()=0;
151 //scan
153 /**fetches the next tuple in the table which satisfies the condition specified.
154 * execute should be called before calling this method. Application buffer should be
155 * binded to get the tuple values.
156 * @returns void* NULL if there is no tuple.
158 virtual void* fetch()=0;
159 virtual void* fetch(DbRetVal &rv)=0;
161 /**fetches the next tuple in the table which satisfies the condition specified.
162 * execute should be called before calling this method. Application buffer need not be
163 * binded to call this method.
164 * @returns void* NULL if there is no tuple.
166 virtual void* fetchNoBind()=0;
167 virtual void* fetchNoBind(DbRetVal &rv)=0;
170 /**closes the scan. Needs to be called before calling execute again on the same table handle. It releases the resources
171 acquired during the scan.
172 * @returns DbRetVal
175 virtual DbRetVal close()=0;
176 virtual DbRetVal closeScan()=0;
178 /**Retrieves the total space used for this table in bytes
179 * @returns DbRetVal
181 virtual long spaceUsed()=0;
183 /**Retrieves the total number of tuples present in this table
184 * @returns DbRetVal
186 virtual long numTuples()=0;
188 /**takes lock on the table
189 * if bool shared flag is set, it will take shared lock, or else exclusive lock
190 * @returns DbRetVal
192 virtual DbRetVal lock(bool shared)=0;
194 /**releases the lock acquired on the table
195 * @returns DbRetVal
197 virtual DbRetVal unlock()=0;
199 /**sets the undo log flag. If this flag is unset then undo logs will not be generated
200 * @returns DbRetVal
202 virtual DbRetVal setUndoLogging(bool flag)=0;
204 virtual DbRetVal getFieldInfo(const char *fieldName, FieldInfo *&info)=0;
205 virtual List getFieldNameList()=0;
206 virtual char* getName()=0;
207 virtual void printSQLIndexString()=0;
209 //optimizer
210 virtual DbRetVal optimize()=0;
211 virtual ScanType getScanType()=0;
212 virtual bool isTableInvolved(char *tblName)=0;
213 virtual bool pushPredicate(Predicate *pred)=0;
214 virtual void setPredicate(Predicate *pred)=0;
215 virtual void printPlan(int space)=0;
217 virtual void resetNullinfo()=0;
218 virtual int getFldPos(char *name)=0;
219 virtual ~Table() { }
222 //non virtual functions
223 static void getFieldNameAlone(char *fname, char *name);
224 static void getTableNameAlone(char *fname, char *name);
227 #endif