reverting back the changes. causes core dump
[csql.git] / include / Parser.h
blob31c277b37ed3c523b70d1d390cb72adcfee29017
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,
152 ALTERINDEXRENAME
156 class UserNode
158 public:
159 char userName[IDENTIFIER_LENGTH];
160 char passName[IDENTIFIER_LENGTH];
161 UserNodeType type;
164 class DllExport ParsedData
166 private:
167 char tblName[IDENTIFIER_LENGTH];
168 char idxName[IDENTIFIER_LENGTH]; //Also used for rename table
169 char pkTblName[IDENTIFIER_LENGTH];//This is also used as DSN name when cachestatement executed and copy table statemnet
170 StatementType stmtType;
171 bool isDistinct;
172 bool isExplain;
174 int paramCounter;
176 List tableNameList;
177 List joinTypeList;
178 //holds pointer to field names. used in insert to store field name list
179 //and for projection list of select
180 //also used to store primary or unique key fields in create statement
181 List fieldNameList;
183 List groupFieldNameList;
185 List havingFieldNameList;
187 List orderFieldNameList;
189 //holds pointer to condition values.
190 List conditionValueList;
192 List secondaryIndexFieldList;
193 //holds pointer to field values. used in insert to store field values
194 //used in update to store the current value returned by fetch().This gets replaced
195 //by value in updFldValList and then update() is called.
196 List fieldValueList;
198 //used to store IN values of SELECT statement
199 //This should be a list of list. so that multiple IN shall be present
200 //in the select statement
201 List inValueList;
203 //update field value list. used to store the values to be updated
204 //value in the SET clause of UPDATE statement is stored here.
205 List updFldValList;
207 //stores the where clause condition for SELECT, UPDATE and DELETE
208 Condition predicate;
209 Condition havingPredicate;
210 List predList;
212 //stores field information in CREATE TABLE
213 FieldDef fldDef;
214 //User Management
215 UserNode *userNode;
216 //stores list of fields for CREATE TABLE
217 FieldList creFldList;
218 //Foreign Key storage
220 List fkFieldNameList;
221 List pkFieldNameList;
222 List foreignKeyList;
223 bool isForeign;
224 bool shouldCreateTbl;
225 //ALTER Statement
226 AlterTableType aTblType;
227 //stores index information
228 bool isUnique;
229 bool isPrimary;
230 bool isAutoIncrement;
231 IndexType indexType;
232 ResultSetPlan plan;
233 int bucketSize;
234 //Cache table
235 char hcondition[IDENTIFIER_LENGTH];
236 char vcondition[IDENTIFIER_LENGTH];
237 bool hCondFld;
238 bool vCondFld;
239 bool pkFld;
240 bool forceOption;
241 bool direct;
242 bool uncache;
243 bool noschema;
244 bool dsn;
245 int limit;
246 int offset;
247 bool isWorthyToCache;
248 FunctionType ftype;
249 public:
250 ParsedData() { limit = 0; offset= 0; paramCounter = 0; stmtType = UnknownStatement; isDistinct = false; isExplain=false;
251 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;
252 shouldCreateTbl=false; userNode = NULL; isWorthyToCache=false; ftype = UNKNOWN_FUNCTION;
254 void setFunctionType(FunctionType type) { ftype = type; }
255 FunctionType getFunctionType(){ return ftype;}
256 void setAlterType(AlterTableType type){ aTblType = type;}
257 AlterTableType getAlterType(){ return aTblType; }
259 void createUserNode(char *name, char *password);
260 char *getUserName() { return userNode->userName; }
261 char *getPassWord() { return userNode->passName; }
262 UserNodeType getUserType() { return userNode->type; }
263 void dropUserNode(char *name);
264 void alterUserNode(char *name, char *password);
266 void setCreateTbl(){ shouldCreateTbl=true; }
267 bool getCreateTbl(){ return shouldCreateTbl; }
269 void setDSN(bool flag){ dsn = flag;}
270 bool getDSN(){ return dsn; }
271 void setNoSchema(bool flag){ noschema = flag; }
272 bool getNoSchema(){return noschema;}
273 char *getHCondition(){return hcondition;}
274 char *getVCondition(){return vcondition;}
275 void setHCondition(char *fld){ strcpy(hcondition,fld);}
276 void setVCondition(char *fld){ strcpy(vcondition,fld);}
277 void setHCondFld(bool flag){hCondFld = flag;}
278 bool getHCondFld(){return hCondFld;}
279 void setVCondFld(bool flag){vCondFld = flag;}
280 bool getVCondFld(){return vCondFld;}
281 void setPkFld(bool flag){pkFld = flag;}
282 bool getPkFld(){return pkFld;}
283 void setDirect(bool flag){direct=flag;}
284 bool getDirect(){return direct;}
285 void setUnCache(bool flag){uncache=flag;}
286 bool getUnCache(){return uncache;}
287 void setLimit(int l, int o) { limit =l; offset=o; }
288 int getLimit(){ return limit; }
289 int getOffset(){ return offset; }
292 int getBucketSize(){return bucketSize; };
293 void setBucketSize(int bucket){ bucketSize = bucket; };
294 void setStmtType(StatementType type) { stmtType = type; }
295 void setTableName(char *name) { strcpy(tblName, name); }
296 void setIndexName(char *name) { strcpy(idxName, name); }
297 void setPKTableName(char *name){strcpy(pkTblName, name); }
298 void setResultSetPlan(ResultSetPlan pl){plan = pl;}
299 ResultSetPlan getResultSetPlan(){return plan;}
300 char* getTableName() { return tblName; }
301 char* getIndexName() { return idxName; }
302 char* getPKTableName(){ return pkTblName;}
303 DbRetVal setAutoIncreament(bool flag);
304 bool getAutoIncreament();
305 void insertValue(char *value);
306 void insertInValue(char *value);
307 // third parameter is to avoid conflict between '?' between like operand and parameterized value in sql statement.
308 // eg: select * from t1 where f1 = ? and f2 like '_ti%';
309 // _ is converted to ? before it is processed
310 void** insertCondValueAndGetPtr(char *fName, char *value, bool opLike=false, AggType atp=AGG_UNKNOWN, bool isInHaving=false,bool function=false);
311 void insertCondValue(char *fldName); //For Predecate t1.f1=t2.f1
312 void insertUpdateValue(char *fldName, char *value);
314 void insertField(char *fName, AggType aggType= AGG_UNKNOWN);
315 void insertFieldAlias(char *name);
316 void insertGroupField(char *fName);
317 void insertOrderByField(char *fName, bool isDesc= false);
318 void clearFieldNameList();
319 void insertTableName(char *value);
320 void insertJoinType(JoinType jType);
321 void insertTableName(char *value, char *alias);
322 void insertFKField(char *fkName);
323 void insertPKField(char *pkName);
324 void insertForeignKeyList();
325 Predicate* insertPredicate(char *fldName, ComparisionOp op, void** value, AggType aggType = AGG_UNKNOWN);
326 Predicate* insertPredicate(char *fldName, ComparisionOp op, char *fldName1);
327 Predicate* insertBetPredicate(char *fldName, ComparisionOp op1, void **value1, ComparisionOp op2, void **value2);
328 Predicate* insertPredicate(Predicate *p1, LogicalOp op, Predicate *p2 = NULL);
329 Predicate* insertPredicate(Expression *exp, ComparisionOp op,void **val);
330 Predicate* insertPredicate(Expression *exp, ComparisionOp op,char *fName2);
331 Predicate* insertPredicate(Expression *exp1, ComparisionOp op,Expression *exp2);
332 Predicate* insertNullPredicate(char *fName, ComparisionOp op,bool nullFlag);
333 void setCondition(Predicate *pred)
335 predicate.setPredicate(pred);
337 void setHavingCondition(Predicate *pred)
339 havingPredicate.setPredicate(pred);
341 Condition* getCondition() { return &predicate; }
342 Condition* getHavingCondition() { return &havingPredicate; }
344 void insertFieldValue(FieldValue *newVal) { fieldValueList.append(newVal); }
346 List getFieldNameList() { return fieldNameList; }
347 List getGroupFieldNameList() { return groupFieldNameList; }
348 List getHavingFieldNameList() { return havingFieldNameList; }
349 List getOrderFieldNameList() { return orderFieldNameList; }
350 List getConditionValueList() { return conditionValueList; }
351 List getFieldValueList() { return fieldValueList; }
352 List getInValueList() { return inValueList; }
353 List getUpdateFieldValueList() { return updFldValList; }
354 List getTableNameList() { return tableNameList; }
355 List getJoinTypeList() { return joinTypeList; }
356 List getSecondaryIndexFieldList() { return secondaryIndexFieldList; }
357 List getForeignKeyList(){ return foreignKeyList;}
358 List getPkFieldNameList(){return pkFieldNameList;}
359 List getFkFieldNameList(){return fkFieldNameList;}
361 void setDistinct() { isDistinct = true;}
362 bool getDistinct() { return isDistinct; }
363 void setExplain() { isExplain=true; }
364 bool getExplain() { return isExplain; }
365 bool getCacheWorthy() { return isWorthyToCache; }
366 void setCacheWorthy(bool flag) { isWorthyToCache = flag;}
369 void setFldName(char *name);
370 void setFldType(DataType type);
371 DataType getFldType();
372 DbRetVal setFldLength(size_t length);
373 DbRetVal setDefaultValue(char * value);
374 DbRetVal validateDefaultValue(char* value);
375 //void setFldDefaultValue -- will need two parametersers, check how u want to pass default value.
376 void setFldNotNull(bool notNull);
377 void setForeign(bool foreign){ isForeign = foreign; }
378 void setUnique(bool unique){ isUnique = unique; }
379 void setPrimary(bool primary) { isPrimary = primary; }
380 void setIndexType (IndexType type) { indexType = type; }
381 IndexType getIndexType(){ return indexType; }
382 bool getUnique() { return isUnique; }
383 bool getPrimary() { return isPrimary; }
384 Expression* insertExpression(char *fldName);
385 Expression* insertExpression(char *value, bool flag);
386 Expression* insertExpression(Expression* exp1, ArithOperator op ,Expression* exp2);
387 Expression* insertExpression(Expression* exp1, FunctionType type,Expression* exp2);
388 void insertUpdateExpression(char *fName, Expression *exp);
390 void insertFldDef(); //check if fldDef needs to be a part of ParsedData
392 FieldList getCreFldList() { return creFldList; }
394 StatementType getStmtType() { return stmtType; }
395 char *getFldName();
396 void setAutoFldName(char *fldName);
397 void reset();
398 void init();
402 #endif
404 //TODO: Aruna
405 //variable and function names suck, change if u want to
406 //setFldDefaultValue
407 //finding out if fldDef needs to be part of parsedData, or can allocate memory and pass around
408 //primary key
409 //foreign key