allocator fixes
[csql.git] / include / Statement.h
blob57b451aaeff2d49c579661e3ff1e097dbb7bed06
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.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 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifndef STATEMENT_H
21 #define STATEMENT_H
22 #include "Parser.h"
23 #include "CacheTableLoader.h"
24 class Statement
26 protected:
27 ParsedData *parsedData;
28 DatabaseManager *dbMgr;
30 public:
31 void setParsedData(ParsedData *pData) { parsedData = pData; }
32 void setDbMgr(DatabaseManager *dbmgr) { dbMgr = dbmgr; }
35 virtual int noOfParamFields() { return 0; }
36 virtual DbRetVal execute(int &rowsAffected)=0;
37 virtual DbRetVal setParam(int paramNo, void *value)=0;
39 virtual DbRetVal setShortParam(int paramNo, short value)=0;
40 virtual DbRetVal setIntParam(int paramNo, int value)=0;
41 virtual DbRetVal setLongParam(int paramNo, long value)=0;
42 virtual DbRetVal setLongLongParam(int paramNo, long long value)=0;
43 virtual DbRetVal setByteIntParam(int paramNo, ByteInt value)=0;
44 virtual DbRetVal setFloatParam(int paramNo, float value)=0;
45 virtual DbRetVal setDoubleParam(int paramNo, double value)=0;
46 virtual DbRetVal setStringParam(int paramNo, char *value)=0;
47 virtual DbRetVal setDateParam(int paramNo, Date value)=0;
48 virtual DbRetVal setTimeParam(int paramNo, Time value)=0;
49 virtual DbRetVal setTimeStampParam(int paramNo, TimeStamp value)=0;
50 virtual DbRetVal setBinaryParam(int paramNo, void *value, int length)=0;
51 virtual DbRetVal resolve()=0;
52 virtual bool isFldNull(int pos)=0;
53 virtual bool isFldNull(char *name)=0;
54 virtual int getFldPos(char *name)=0;
55 virtual DbRetVal setNull(int pos)=0;
56 virtual ResultSetPlan getResultSetPlan()=0;
57 virtual long long getLastInsertedVal(DbRetVal &rv)=0;
58 virtual ~Statement(){}
61 class DmlStatement : public Statement
63 protected:
64 Table *table;
66 void **params;
67 //used to store FieldValue* for insert or ConditionValue* for other stmts
69 char **paramValues;
70 int totalParams;
72 public:
73 int noOfParamFields() { return totalParams; }
74 virtual DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info) =0;
76 virtual DbRetVal execute(int &rowsAffected)=0;
77 virtual DbRetVal setParam(int paramNo, void *value)=0;
79 virtual DbRetVal setShortParam(int paramNo, short value)=0;
80 virtual DbRetVal setIntParam(int paramNo, int value)=0;
81 virtual DbRetVal setLongParam(int paramNo, long value)=0;
82 virtual DbRetVal setLongLongParam(int paramNo, long long value)=0;
83 virtual DbRetVal setByteIntParam(int paramNo, ByteInt value)=0;
84 virtual DbRetVal setFloatParam(int paramNo, float value)=0;
85 virtual DbRetVal setDoubleParam(int paramNo, double value)=0;
86 virtual DbRetVal setStringParam(int paramNo, char *value)=0;
87 virtual DbRetVal setDateParam(int paramNo, Date value)=0;
88 virtual DbRetVal setTimeParam(int paramNo, Time value)=0;
89 virtual DbRetVal setTimeStampParam(int paramNo, TimeStamp value)=0;
90 virtual DbRetVal setBinaryParam(int paramNo, void *value, int length)=0;
91 virtual bool isFldNull(int pos)=0;
92 virtual bool isFldNull(char *fldName)=0;
93 virtual DbRetVal setNull(int pos)=0;
94 virtual DbRetVal resolve()=0;
95 virtual void* getParamValuePtr( int pos )=0;
96 virtual int getFldPos(char *name)=0;
97 virtual ~DmlStatement(){}
98 virtual ResultSetPlan getResultSetPlan()=0;
99 virtual long long getLastInsertedVal(DbRetVal &rv)=0;
100 void setLoading(bool flag) { table->setLoading(flag); }
103 class InsStatement : public DmlStatement
105 public:
106 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
108 DbRetVal execute(int &rowsAffected);
109 DbRetVal setParam(int paramNo, void *value);
111 DbRetVal setShortParam(int paramNo, short value);
112 DbRetVal setIntParam(int paramNo, int value);
113 DbRetVal setLongParam(int paramNo, long value);
114 DbRetVal setLongLongParam(int paramNo, long long value);
115 DbRetVal setByteIntParam(int paramNo, ByteInt value);
116 DbRetVal setFloatParam(int paramNo, float value);
117 DbRetVal setDoubleParam(int paramNo, double value);
118 DbRetVal setStringParam(int paramNo, char *value);
119 DbRetVal setDateParam(int paramNo, Date value);
120 DbRetVal setTimeParam(int paramNo, Time value);
121 DbRetVal setTimeStampParam(int paramNo, TimeStamp value);
122 DbRetVal setBinaryParam(int paramNo, void *value, int length);
123 bool isFldNull(int pos){return table->isFldNull(pos);}
124 bool isFldNull(char *fldName){return table->isFldNull(fldName);}
125 void* getParamValuePtr( int );
126 int getFldPos(char *name);
127 DbRetVal setNull(int pos);
128 long long getLastInsertedVal(DbRetVal &rv);
129 DbRetVal resolve();
130 ResultSetPlan getResultSetPlan(){return Normal;}
131 InsStatement();
132 ~InsStatement();
135 class SelStatement : public DmlStatement
137 private:
138 DbRetVal resolveStar();
139 DbRetVal setBindFieldAndValues();
140 DbRetVal resolveForCondition();
141 DbRetVal resolveDistinct();
144 public:
145 FieldValue **bindFields;
146 char **bindFieldValues;
147 int totalFields;
149 bool isPointReturned;
150 bool handleAggWithTbl;
151 bool isRecLimitReached;
152 bool isOffsetReached;
153 int numRecords;
154 bool isExplain;
155 bool isJoin;
157 DbRetVal execute(int &rowsAffected);
158 DbRetVal setParam(int paramNo, void *value);
160 DbRetVal setShortParam(int paramNo, short value);
161 DbRetVal setIntParam(int paramNo, int value);
162 DbRetVal setLongParam(int paramNo, long value);
163 DbRetVal setLongLongParam(int paramNo, long long value);
164 DbRetVal setByteIntParam(int paramNo, ByteInt value);
165 DbRetVal setFloatParam(int paramNo, float value);
166 DbRetVal setDoubleParam(int paramNo, double value);
167 DbRetVal setStringParam(int paramNo, char *value);
168 DbRetVal setDateParam(int paramNo, Date value);
169 DbRetVal setTimeParam(int paramNo, Time value);
170 DbRetVal setTimeStampParam(int paramNo, TimeStamp value);
171 DbRetVal setBinaryParam(int paramNo, void *value, int length);
173 DbRetVal replaceStarWithFirstFldName(FieldName *name);
174 DbRetVal resolveGroupFld(AggTableImpl *impl);
175 DbRetVal resolveOrderByFld();
176 bool isInProjectionList(char *name, AggType aType);
177 bool isFldNull(int pos){return table->isFldNull(pos);}
178 bool isFldNull(char *fldName){ return table->isFldNull(fldName); }
179 int getFldPos(char *name);
180 DbRetVal setNull(int pos){ return ErrBadCall;}
181 DbRetVal close();
182 DbRetVal resolve();
183 long long getLastInsertedVal(DbRetVal &rv){rv = ErrBadCall;return 0;}
185 void* handleSingleTableAggWithoutGroup();
186 bool isGroupFld(char *fName);
187 bool isJoinStmt() { return isJoin; }
189 SelStatement();
190 ~SelStatement();
192 bool isTableAlreadyPresent(char* tblName, char* aliasName);
193 DbRetVal openTables();
194 // DbRetVal resolveGroupFld(AggTableImpl *impl);
196 DbRetVal setBindField(int pos, void* value);
198 int noOfProjFields();
199 DbRetVal getProjFldInfo (int projpos, FieldInfo *&fInfo);
200 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
202 void *getParamValuePtr( int );
203 DataType getFieldType( int );
204 int getFieldLength( int );
205 char* getFieldName( int );
207 void *fetch();
208 void *fetch(DbRetVal &rv);
209 void *fetchAndPrint(bool SQL);
210 void *next();
211 void *getFieldValuePtr( int );
212 void *getFieldValuePtr( char *name );
213 void getProjFieldType(int *data);
214 ResultSetPlan getResultSetPlan(){ return Normal; }
217 class UpdStatement : public DmlStatement
219 public:
220 DbRetVal execute(int &rowsAffected);
221 DbRetVal setParam(int paramNo, void *value);
223 DbRetVal setShortParam(int paramNo, short value);
224 DbRetVal setIntParam(int paramNo, int value);
225 DbRetVal setLongParam(int paramNo, long value);
226 DbRetVal setLongLongParam(int paramNo, long long value);
227 DbRetVal setByteIntParam(int paramNo, ByteInt value);
228 DbRetVal setFloatParam(int paramNo, float value);
229 DbRetVal setDoubleParam(int paramNo, double value);
230 DbRetVal setStringParam(int paramNo, char *value);
231 DbRetVal setDateParam(int paramNo, Date value);
232 DbRetVal setTimeParam(int paramNo, Time value);
233 DbRetVal setTimeStampParam(int paramNo, TimeStamp value);
234 DbRetVal setBinaryParam(int paramNo, void *value, int length);
235 void* getParamValuePtr(int);
236 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
237 bool isFldNull(int pos){return table->isFldNull(pos);}
238 bool isFldNull(char *fldName){return table->isFldNull(fldName);}
239 int getFldPos(char *name);
240 DbRetVal setNull(int pos);
241 DbRetVal resolve();
242 long long getLastInsertedVal(DbRetVal &rv){ rv=ErrBadCall; return 0;}
243 UpdStatement();
244 ~UpdStatement();
245 ResultSetPlan getResultSetPlan() {return Normal;}
246 int totalAssignParams;
247 DbRetVal resolveForAssignment();
248 ResultSetPlan plan;
251 class DelStatement : public DmlStatement
253 public:
254 DbRetVal execute(int &rowsAffected);
255 DbRetVal setParam(int paramNo, void *value);
257 DbRetVal setShortParam(int paramNo, short value);
258 DbRetVal setIntParam(int paramNo, int value);
259 DbRetVal setLongParam(int paramNo, long value);
260 DbRetVal setLongLongParam(int paramNo, long long value);
261 DbRetVal setByteIntParam(int paramNo, ByteInt value);
262 DbRetVal setFloatParam(int paramNo, float value);
263 DbRetVal setDoubleParam(int paramNo, double value);
264 DbRetVal setStringParam(int paramNo, char *value);
265 DbRetVal setDateParam(int paramNo, Date value);
266 DbRetVal setTimeParam(int paramNo, Time value);
267 DbRetVal setTimeStampParam(int paramNo, TimeStamp value);
268 DbRetVal setBinaryParam(int paramNo, void *value, int length);
269 bool isFldNull(int pos){return table->isFldNull(pos);}
270 bool isFldNull(char *fldName){return table->isFldNull(fldName);}
271 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
272 void* getParamValuePtr(int);
273 DbRetVal setNull(int pos){ return ErrBadCall;}
274 DbRetVal resolve();
275 long long getLastInsertedVal(DbRetVal &rv){ rv = ErrBadCall;return 0;}
276 DelStatement();
277 ~DelStatement();
278 int getFldPos(char *name);
279 DbRetVal resolveForCondition(); //TODO::put this is Statement class, duplicated from SelStatement.
280 ResultSetPlan getResultSetPlan(){return Normal;}
283 class MetadataStatement : public DmlStatement
285 public:
286 FieldValue **bindFields;
287 char **bindFieldValues;
288 int totalFields;
289 ListIterator dataListIter;
291 int numRecords;
292 bool isPlanFreed;
293 bool isSecondFetch;
294 ResultSetPlan plan;
295 DbRetVal execute(int &rowsAffected);
296 DbRetVal setParam(int paramNo, void *value){return OK;};
298 DbRetVal setShortParam(int paramNo, short value){ return OK;};
299 DbRetVal setIntParam(int paramNo, int value){ return OK;};
300 DbRetVal setLongParam(int paramNo, long value){ return OK;};
301 DbRetVal setLongLongParam(int paramNo, long long value){ return OK;};
302 DbRetVal setByteIntParam(int paramNo, ByteInt value){ return OK;};
303 DbRetVal setFloatParam(int paramNo, float value){ return OK;};
304 DbRetVal setDoubleParam(int paramNo, double value){ return OK;};
305 DbRetVal setStringParam(int paramNo, char *value){ return OK;};
306 DbRetVal setDateParam(int paramNo, Date value){ return OK;};
307 DbRetVal setTimeParam(int paramNo, Time value){ return OK;};
308 DbRetVal setTimeStampParam(int paramNo, TimeStamp value){ return OK;};
309 DbRetVal setBinaryParam(int paramNo, void *value, int length){ return OK;};
311 DbRetVal replaceStarWithFirstFldName(FieldName *name){ return OK;};
312 DbRetVal resolveGroupFld(AggTableImpl *impl){ return OK;};
313 DbRetVal resolveOrderByFld(){ return OK;};
314 bool isFldNull(int pos){return table->isFldNull(pos);}
315 bool isFldNull(char *fldName){
316 if(plan != Normal) return false;
317 return table->isFldNull(fldName); }
318 int getFldPos(char *name);
319 DbRetVal setNull(int pos){ return ErrBadCall;}
320 DbRetVal close();
321 DbRetVal resolve();
323 MetadataStatement();
324 ~MetadataStatement();
325 long long getLastInsertedVal(DbRetVal &rv){rv=ErrBadCall; return 0;}
326 void *getParamValuePtr( int ){ return NULL;}
327 DbRetVal openTables();
328 DbRetVal setBindField(int pos, void* value);
329 int noOfProjFields();
330 DbRetVal getProjFldInfo (int projpos, FieldInfo *&fInfo);
331 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
332 DataType getFieldType( int );
333 int getFieldLength( int );
334 char* getFieldName( int );
336 void *fetch();
337 void *fetch(DbRetVal &rv);
338 void *fetchAndPrint(bool SQL);
339 void *next();
340 void *getFieldValuePtr( int );
341 void *getFieldValuePtr( char *name );
342 void getProjFieldType(int *data);
343 void *fetchMetaData();
344 void freeMetadata();
345 ResultSetPlan getResultSetPlan(){return plan;}
346 ListIterator getExportKeyIterator();
347 ListIterator getImportKeyIterator();
348 ListIterator getDataTypeInfoIterator();
350 class CopyTblStatement : public DmlStatement
352 public:
353 DbRetVal execute(int &rowsAffected);
354 DbRetVal resolve();
355 DbRetVal setParam(int paramNo, void *value);
356 DbRetVal setShortParam(int paramNo, short value);
357 DbRetVal setIntParam(int paramNo, int value);
358 DbRetVal setLongParam(int paramNo, long value);
359 DbRetVal setLongLongParam(int paramNo, long long value);
360 DbRetVal setByteIntParam(int paramNo, ByteInt value);
361 DbRetVal setFloatParam(int paramNo, float value);
362 DbRetVal setDoubleParam(int paramNo, double value);
363 DbRetVal setStringParam(int paramNo, char *value);
364 DbRetVal setDateParam(int paramNo, Date value);
365 DbRetVal setTimeParam(int paramNo, Time value);
366 DbRetVal setTimeStampParam(int paramNo, TimeStamp value);
367 DbRetVal setBinaryParam(int paramNo, void *value, int length);
368 bool isFldNull(int pos){return table->isFldNull(pos);}
369 bool isFldNull(char *fldName){return table->isFldNull(fldName);}
370 DbRetVal getParamFldInfo(int paramPos, FieldInfo *&info);
371 void* getParamValuePtr(int);
372 DbRetVal setNull(int pos){ return ErrBadCall;}
373 int getFldPos(char *name);
374 ResultSetPlan getResultSetPlan(){return Normal;}
375 DbRetVal resolveForInsert();
376 DbRetVal resolveForCreate();
377 long long getLastInsertedVal(DbRetVal &rv){ rv = ErrBadCall; return 0;}
378 CopyTblStatement();
379 ~CopyTblStatement();
380 private:
381 void **bindFieldValues;
382 int totalFields;
383 Statement *innerSelStmt;
384 char tblName[IDENTIFIER_LENGTH];
389 class DdlStatement : public Statement
391 public:
392 DbRetVal execute(int &rowsAffected)=0;
393 //TODO: i think rowsAffected is not part of ddl - gopal said its design decision
394 DbRetVal resolve()=0;
396 DbRetVal setParam(int paramNo, void *value) { return ErrBadCall; }
397 DbRetVal setShortParam(int paramNo, short value) { return ErrBadCall; }
398 DbRetVal setIntParam(int paramNo, int value) { return ErrBadCall; }
399 DbRetVal setLongParam(int paramNo, long value) { return ErrBadCall; }
400 DbRetVal setLongLongParam(int paramNo, long long value) { return ErrBadCall; }
401 DbRetVal setByteIntParam(int paramNo, ByteInt value) { return ErrBadCall; }
402 DbRetVal setFloatParam(int paramNo, float value) { return ErrBadCall; }
403 DbRetVal setDoubleParam(int paramNo, double value) { return ErrBadCall; }
404 DbRetVal setStringParam(int paramNo, char *value) { return ErrBadCall; }
405 DbRetVal setDateParam(int paramNo, Date value) { return ErrBadCall; }
406 DbRetVal setTimeParam(int paramNo, Time value) { return ErrBadCall; }
407 DbRetVal setTimeStampParam(int paramNo, TimeStamp value) { return ErrBadCall;}
408 DbRetVal setBinaryParam(int paramNo, void *value, int length) { return ErrBadCall;}
409 bool isFldNull(int pos){ return false;}
410 bool isFldNull(char *fldName ){ return false;}
411 int getFldPos(char *name){ return -1;}
412 long long getLastInsertedVal(DbRetVal &rv){ rv = ErrBadCall; return 0;}
413 DbRetVal setNull(int pos){ return ErrBadCall;}
414 ResultSetPlan getResultSetPlan(){ ResultSetPlan dummy; return dummy;}
417 class CreateTblStatement : public DdlStatement
419 public:
420 DbRetVal execute(int &rowsAffected);
421 DbRetVal resolve();
422 DbRetVal checkForDot(char *name);
423 CreateTblStatement();
424 ~CreateTblStatement();
426 private:
427 char tblName[IDENTIFIER_LENGTH];
428 TableDef tblDef;
431 class CreateIdxStatement : public DdlStatement
433 public:
434 DbRetVal execute(int &rowsAffected);
435 DbRetVal resolve(){return OK; }
437 CreateIdxStatement();
438 ~CreateIdxStatement();
442 class DropTblStatement : public DdlStatement
444 public:
445 DbRetVal execute(int &rowsAffected);
446 DbRetVal resolve(){return OK; }
448 DropTblStatement(){}
449 ~DropTblStatement(){}
451 class TruncateTblStatement : public DdlStatement
453 Table *table;
454 public:
455 DbRetVal execute(int &rowsAffected);
456 DbRetVal resolve();
458 TruncateTblStatement(){}
459 ~TruncateTblStatement(){}
463 class CacheTblStatement : public DdlStatement
465 public:
466 DbRetVal execute(int &rowsAffected);
467 DbRetVal resolve();
468 CacheTableLoader cacheLoader;
469 CacheTblStatement(){}
473 class UserTblStatement : public DdlStatement
475 public:
476 char userName[IDENTIFIER_LENGTH];
477 UserManager *usrMgr;
478 UserNodeType uType;
479 DbRetVal execute(int &rowsAffected);
480 DbRetVal resolve();
481 void setUserManager(UserManager *mgr,char *user){ usrMgr = mgr; strcpy(userName,user);}
482 UserTblStatement(){ usrMgr = NULL;}
483 //~UserTblStatement(){ delete usrMgr;}
486 class AlterTblStatement : public DdlStatement
488 public:
489 DbRetVal execute(int &rowsAffected);
490 DbRetVal resolve();
491 DbRetVal resolveForModifyColumn();
492 DbRetVal resolveForAddDropColumn();
493 DbRetVal executeForAddDropColumn(int &rowsAffected);
494 DbRetVal executeForModifyColumn(int &rowsAffected){}
495 DbRetVal createIndex(const char *oldName,const char *newName);
496 AlterTblStatement();
497 ~AlterTblStatement();
498 AlterTableType altType;
499 void **bindFieldValues;
500 int totalFields;
501 int noOfOldTabFld;
502 Table *table;
503 TableDef tblDef;
507 class DropIdxStatement : public DdlStatement
509 public:
510 DbRetVal execute(int &rowsAffected);
511 DbRetVal resolve() {return OK; }
512 DropIdxStatement(){}
513 ~DropIdxStatement(){}
515 class CompactTblStatement : public DdlStatement
517 Table *table;
518 public:
519 DbRetVal execute(int &rowsAffected);
520 DbRetVal resolve();
521 CompactTblStatement(){ table=NULL;}
522 ~CompactTblStatement(){}
525 class StatementFactory
527 public:
528 static Statement* getStatement(ParsedData *pData );
530 #endif