allocator fixes
[csql.git] / include / Parser.h
blob31c19a3da9f1e1e707c2028655dd015cecfebcc3
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 PARSER_H
21 #define PARSER_H
22 #include <CSql.h>
23 #include <AggTableImpl.h>
24 #include <JoinTableImpl.h>
25 #include <os.h>
26 #include <Util.h>
27 #include <Function.h>
28 #ifndef STMT_TYPE
29 #define STMT_TYPE
30 enum StatementType
32 UnknownStatement,
33 SelectStatement,
34 InsertStatement,
35 UpdateStatement,
36 DeleteStatement,
37 CreateTableStatement,
38 DropTableStatement,
39 CreateIndexStatement,
40 DropIndexStatement,
41 CacheTableStatement,
42 CompactTableStatement,
43 CopyTableStatement,
44 MetaStatement,
45 UserStatement,
46 MgmtStatement,
47 AlterStatement,
48 TruncateStatement
50 #endif
52 struct FieldValue
54 char fldName[IDENTIFIER_LENGTH];
55 char defValBuf[DEFAULT_VALUE_BUF_LENGTH];
56 char *parsedString;
57 void *value;
58 int paramNo; // 0 ->not a param. It stores the param position
59 DataType type;
60 AggType aType;
61 int length;
62 size_t offset;
63 bool isNullable;
64 bool isAllocVal;
65 bool isInResSet;
66 bool isPrimary;
67 bool isUnique;
68 bool isAutoIncrement;
69 bool isDefault;
73 struct ConditionValue
75 char *parsedString;
76 void *value;
77 int paramNo; // 0 ->not a param. It stores the param position
78 DataType type;
79 AggType aType;
80 int length;
81 bool opLike;
82 bool isNullable;
83 char fName[IDENTIFIER_LENGTH];
84 bool isFunctionInvolve;
87 struct FieldName
89 char fldName[IDENTIFIER_LENGTH];
90 AggType aType; //used only in case of select projection
91 char aliasFldName[IDENTIFIER_LENGTH];
92 FieldName()
94 strcpy(fldName,"");
95 strcpy(aliasFldName,"");
96 aType = AGG_UNKNOWN;
99 struct TableName
101 char tblName[IDENTIFIER_LENGTH];
102 char aliasName[IDENTIFIER_LENGTH];
103 TableName()
105 strcpy(tblName,"");
106 strcpy(aliasName,"");
109 #ifndef JOINTYPE
110 #define JOINTYPE
111 enum JoinType
113 INNER_JOIN = 1,
114 RIGHT_JOIN,
115 LEFT_JOIN,
116 FULL_JOIN,
117 UNKNOWN_JOIN
119 #endif
121 struct JoinTypeNode
123 JoinType jType;
124 JoinTypeNode(){ jType = INNER_JOIN; }
127 struct UpdateFieldValue
129 char fldName[IDENTIFIER_LENGTH];
130 char *parsedString;
131 void *value;
132 Expression *expre;
133 DataType type;
134 int length;
135 bool isNullable;
136 int paramNo;
139 enum UserNodeType
141 CREATEUSER=0,
142 DROPUSER,
143 ALTERUSER
145 enum AlterTableType
147 ALTERADD=0,
148 ALTERDROP,
149 ALTERMODIFY,
150 ALTERFIELDRENAME,
151 ALTERTABLERENAME
155 class UserNode
157 public:
158 char userName[IDENTIFIER_LENGTH];
159 char passName[IDENTIFIER_LENGTH];
160 UserNodeType type;
163 class ParsedData
165 private:
166 char tblName[IDENTIFIER_LENGTH];
167 char idxName[IDENTIFIER_LENGTH]; //Also used for rename table
168 char pkTblName[IDENTIFIER_LENGTH];//This is also used as DSN name when cachestatement executed and copy table statemnet
169 StatementType stmtType;
170 bool isDistinct;
171 bool isExplain;
173 int paramCounter;
175 List tableNameList;
176 List joinTypeList;
177 //holds pointer to field names. used in insert to store field name list
178 //and for projection list of select
179 //also used to store primary or unique key fields in create statement
180 List fieldNameList;
182 List groupFieldNameList;
184 List havingFieldNameList;
186 List orderFieldNameList;
188 //holds pointer to condition values.
189 List conditionValueList;
191 List secondaryIndexFieldList;
192 //holds pointer to field values. used in insert to store field values
193 //used in update to store the current value returned by fetch().This gets replaced
194 //by value in updFldValList and then update() is called.
195 List fieldValueList;
197 //used to store IN values of SELECT statement
198 //This should be a list of list. so that multiple IN shall be present
199 //in the select statement
200 List inValueList;
202 //update field value list. used to store the values to be updated
203 //value in the SET clause of UPDATE statement is stored here.
204 List updFldValList;
206 //stores the where clause condition for SELECT, UPDATE and DELETE
207 Condition predicate;
208 Condition havingPredicate;
209 List predList;
211 //stores field information in CREATE TABLE
212 FieldDef fldDef;
213 //User Management
214 UserNode *userNode;
215 //stores list of fields for CREATE TABLE
216 FieldList creFldList;
217 //Foreign Key storage
219 List fkFieldNameList;
220 List pkFieldNameList;
221 List foreignKeyList;
222 bool isForeign;
223 bool shouldCreateTbl;
224 //ALTER Statement
225 AlterTableType aTblType;
226 //stores index information
227 bool isUnique;
228 bool isPrimary;
229 bool isAutoIncrement;
230 IndexType indexType;
231 ResultSetPlan plan;
232 int bucketSize;
233 //Cache table
234 char hcondition[IDENTIFIER_LENGTH];
235 char vcondition[IDENTIFIER_LENGTH];
236 bool hCondFld;
237 bool vCondFld;
238 bool pkFld;
239 bool forceOption;
240 bool direct;
241 bool uncache;
242 bool noschema;
243 bool dsn;
244 int limit;
245 int offset;
246 bool isWorthyToCache;
247 FunctionType ftype;
248 public:
249 ParsedData() { limit = 0; offset= 0; paramCounter = 0; stmtType = UnknownStatement; isDistinct = false; isExplain=false;
250 isUnique = false; isPrimary = false; isAutoIncrement=false ;indexType = hashIndex; plan = Normal; bucketSize=0; isForeign=false; hCondFld=false; vCondFld=false;pkFld=false;forceOption=false; direct=false; uncache=false; noschema=false; dsn=false;
251 shouldCreateTbl=false; userNode = NULL; isWorthyToCache=false; ftype = UNKNOWN_FUNCTION;
253 void setFunctionType(FunctionType type) { ftype = type; }
254 FunctionType getFunctionType(){ return ftype;}
255 void setAlterType(AlterTableType type){ aTblType = type;}
256 AlterTableType getAlterType(){ return aTblType; }
258 void createUserNode(char *name, char *password);
259 char *getUserName() { return userNode->userName; }
260 char *getPassWord() { return userNode->passName; }
261 UserNodeType getUserType() { return userNode->type; }
262 void dropUserNode(char *name);
263 void alterUserNode(char *name, char *password);
265 void setCreateTbl(){ shouldCreateTbl=true; }
266 bool getCreateTbl(){ return shouldCreateTbl; }
268 void setDSN(bool flag){ dsn = flag;}
269 bool getDSN(){ return dsn; }
270 void setNoSchema(bool flag){ noschema = flag; }
271 bool getNoSchema(){return noschema;}
272 char *getHCondition(){return hcondition;}
273 char *getVCondition(){return vcondition;}
274 void setHCondition(char *fld){ strcpy(hcondition,fld);}
275 void setVCondition(char *fld){ strcpy(vcondition,fld);}
276 void setHCondFld(bool flag){hCondFld = flag;}
277 bool getHCondFld(){return hCondFld;}
278 void setVCondFld(bool flag){vCondFld = flag;}
279 bool getVCondFld(){return vCondFld;}
280 void setPkFld(bool flag){pkFld = flag;}
281 bool getPkFld(){return pkFld;}
282 void setDirect(bool flag){direct=flag;}
283 bool getDirect(){return direct;}
284 void setUnCache(bool flag){uncache=flag;}
285 bool getUnCache(){return uncache;}
286 void setLimit(int l, int o) { limit =l; offset=o; }
287 int getLimit(){ return limit; }
288 int getOffset(){ return offset; }
291 int getBucketSize(){return bucketSize; };
292 void setBucketSize(int bucket){ bucketSize = bucket; };
293 void setStmtType(StatementType type) { stmtType = type; }
294 void setTableName(char *name) { strcpy(tblName, name); }
295 void setIndexName(char *name) { strcpy(idxName, name); }
296 void setPKTableName(char *name){strcpy(pkTblName, name); }
297 void setResultSetPlan(ResultSetPlan pl){plan = pl;}
298 ResultSetPlan getResultSetPlan(){return plan;}
299 char* getTableName() { return tblName; }
300 char* getIndexName() { return idxName; }
301 char* getPKTableName(){ return pkTblName;}
302 DbRetVal setAutoIncreament(bool flag);
303 bool getAutoIncreament();
304 void insertValue(char *value);
305 void insertInValue(char *value);
306 // third parameter is to avoid conflict between '?' between like operand and parameterized value in sql statement.
307 // eg: select * from t1 where f1 = ? and f2 like '_ti%';
308 // _ is converted to ? before it is processed
309 void** insertCondValueAndGetPtr(char *fName, char *value, bool opLike=false, AggType atp=AGG_UNKNOWN, bool isInHaving=false,bool function=false);
310 void insertCondValue(char *fldName); //For Predecate t1.f1=t2.f1
311 void insertUpdateValue(char *fldName, char *value);
313 void insertField(char *fName, AggType aggType= AGG_UNKNOWN);
314 void insertFieldAlias(char *name);
315 void insertGroupField(char *fName);
316 void insertOrderByField(char *fName, bool isDesc= false);
317 void clearFieldNameList();
318 void insertTableName(char *value);
319 void insertJoinType(JoinType jType);
320 void insertTableName(char *value, char *alias);
321 void insertFKField(char *fkName);
322 void insertPKField(char *pkName);
323 void insertForeignKeyList();
324 Predicate* insertPredicate(char *fldName, ComparisionOp op, void** value, AggType aggType = AGG_UNKNOWN);
325 Predicate* insertPredicate(char *fldName, ComparisionOp op, char *fldName1);
326 Predicate* insertBetPredicate(char *fldName, ComparisionOp op1, void **value1, ComparisionOp op2, void **value2);
327 Predicate* insertPredicate(Predicate *p1, LogicalOp op, Predicate *p2 = NULL);
328 Predicate* insertPredicate(Expression *exp, ComparisionOp op,void **val);
329 Predicate* insertPredicate(Expression *exp, ComparisionOp op,char *fName2);
330 Predicate* insertPredicate(Expression *exp1, ComparisionOp op,Expression *exp2);
331 Predicate* insertNullPredicate(char *fName, ComparisionOp op,bool nullFlag);
332 void setCondition(Predicate *pred)
334 predicate.setPredicate(pred);
336 void setHavingCondition(Predicate *pred)
338 havingPredicate.setPredicate(pred);
340 Condition* getCondition() { return &predicate; }
341 Condition* getHavingCondition() { return &havingPredicate; }
343 void insertFieldValue(FieldValue *newVal) { fieldValueList.append(newVal); }
345 List getFieldNameList() { return fieldNameList; }
346 List getGroupFieldNameList() { return groupFieldNameList; }
347 List getHavingFieldNameList() { return havingFieldNameList; }
348 List getOrderFieldNameList() { return orderFieldNameList; }
349 List getConditionValueList() { return conditionValueList; }
350 List getFieldValueList() { return fieldValueList; }
351 List getInValueList() { return inValueList; }
352 List getUpdateFieldValueList() { return updFldValList; }
353 List getTableNameList() { return tableNameList; }
354 List getJoinTypeList() { return joinTypeList; }
355 List getSecondaryIndexFieldList() { return secondaryIndexFieldList; }
356 List getForeignKeyList(){ return foreignKeyList;}
357 List getPkFieldNameList(){return pkFieldNameList;}
358 List getFkFieldNameList(){return fkFieldNameList;}
360 void setDistinct() { isDistinct = true;}
361 bool getDistinct() { return isDistinct; }
362 void setExplain() { isExplain=true; }
363 bool getExplain() { return isExplain; }
364 bool getCacheWorthy() { return isWorthyToCache; }
365 void setCacheWorthy(bool flag) { isWorthyToCache = flag;}
368 void setFldName(char *name);
369 void setFldType(DataType type);
370 DataType getFldType();
371 DbRetVal setFldLength(size_t length);
372 DbRetVal setDefaultValue(char * value);
373 DbRetVal validateDefaultValue(char* value);
374 //void setFldDefaultValue -- will need two parametersers, check how u want to pass default value.
375 void setFldNotNull(bool notNull);
376 void setForeign(bool foreign){ isForeign = foreign; }
377 void setUnique(bool unique){ isUnique = unique; }
378 void setPrimary(bool primary) { isPrimary = primary; }
379 void setIndexType (IndexType type) { indexType = type; }
380 IndexType getIndexType(){ return indexType; }
381 bool getUnique() { return isUnique; }
382 bool getPrimary() { return isPrimary; }
383 Expression* insertExpression(char *fldName);
384 Expression* insertExpression(char *value, bool flag);
385 Expression* insertExpression(Expression* exp1, ArithOperator op ,Expression* exp2);
386 Expression* insertExpression(Expression* exp1, FunctionType type,Expression* exp2);
387 void insertUpdateExpression(char *fName, Expression *exp);
389 void insertFldDef(); //check if fldDef needs to be a part of ParsedData
391 FieldList getCreFldList() { return creFldList; }
393 StatementType getStmtType() { return stmtType; }
394 char *getFldName();
395 void setAutoFldName(char *fldName);
396 void reset();
397 void init();
401 #endif
403 //TODO: Aruna
404 //variable and function names suck, change if u want to
405 //setFldDefaultValue
406 //finding out if fldDef needs to be part of parsedData, or can allocate memory and pass around
407 //primary key
408 //foreign key