Merge branch 'master' into 2.3beta-2.5beta
[csql/przemoc.git] / include / Parser.h
blob95ce9a9ee9d4198e5c9fc132767d6e527cd9d65b
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 enum StatementType
29 UnknownStatement,
30 SelectStatement,
31 InsertStatement,
32 UpdateStatement,
33 DeleteStatement,
34 CreateTableStatement,
35 DropTableStatement,
36 CreateIndexStatement,
37 DropIndexStatement
40 struct FieldValue
42 char fldName[IDENTIFIER_LENGTH];
43 char *parsedString;
44 void *value;
45 int paramNo; // 0 ->not a param. It stores the param position
46 DataType type;
47 int length;
48 bool isNullable;
49 bool isAllocVal;
53 struct ConditionValue
55 char *parsedString;
56 void *value;
57 int paramNo; // 0 ->not a param. It stores the param position
58 DataType type;
59 int length;
60 bool opLike;
61 bool isNullable;
62 char fName[IDENTIFIER_LENGTH];
65 struct FieldName
67 char fldName[IDENTIFIER_LENGTH];
68 AggType aType; //used only in case of select projection
69 FieldName()
71 strcpy(fldName,"");
72 aType = AGG_UNKNOWN;
75 struct TableName
77 char tblName[IDENTIFIER_LENGTH];
78 TableName()
80 strcpy(tblName,"");
84 struct UpdateFieldValue
86 char fldName[IDENTIFIER_LENGTH];
87 char *parsedString;
88 void *value;
89 Expression *expre;
90 DataType type;
91 int length;
92 bool isNullable;
93 int paramNo;
97 class ParsedData
99 private:
100 char tblName[IDENTIFIER_LENGTH];
101 char idxName[IDENTIFIER_LENGTH];
103 StatementType stmtType;
105 int paramCounter;
107 List tableNameList;
108 //holds pointer to field names. used in insert to store field name list
109 //and for projection list of select
110 //also used to store primary or unique key fields in create statement
111 List fieldNameList;
113 List groupFieldNameList;
115 //holds pointer to condition values.
116 List conditionValueList;
117 List secondaryIndexFieldList;
118 //holds pointer to field values. used in insert to store field values
119 //used in update to store the current value returned by fetch().This gets replaced
120 //by value in updFldValList and then update() is called.
121 List fieldValueList;
123 //used to store IN values of SELECT statement
124 //This should be a list of list. so that multiple IN shall be present
125 //in the select statement
126 List inValueList;
128 //update field value list. used to store the values to be updated
129 //value in the SET clause of UPDATE statement is stored here.
130 List updFldValList;
132 //stores the where clause condition for SELECT, UPDATE and DELETE
133 Condition predicate;
135 //stores field information in CREATE TABLE
136 FieldDef fldDef;
138 //stores list of fields for CREATE TABLE
139 FieldList creFldList;
141 //stores index information
142 bool isUnique;
143 bool isPrimary;
144 IndexType indexType;
145 int bucketSize;
146 bool isAutoIncrement;
147 public:
148 ParsedData() { paramCounter = 0; stmtType = UnknownStatement;
149 isUnique = false; isPrimary = false; indexType = hashIndex; bucketSize=0; isAutoIncrement=false;}
150 int getBucketSize(){return bucketSize; };
151 void setBucketSize(int bucket){ bucketSize = bucket; };
152 void setStmtType(StatementType type) { stmtType = type; }
153 void setTableName(char *name) { strcpy(tblName, name); }
154 void setIndexName(char *name) { strcpy(idxName, name); }
156 char* getTableName() { return tblName; }
157 char* getIndexName() { return idxName; }
158 DbRetVal setAutoIncreament(bool flag);
159 bool getAutoIncreament();
161 void insertValue(char *value);
162 void insertInValue(char *value);
163 // third parameter is to avoid conflict between '?' between like operand and parameterized value in sql statement.
164 // eg: select * from t1 where f1 = ? and f2 like '_ti%';
165 // _ is converted to ? before it is processed
166 void** insertCondValueAndGetPtr(char *fName, char *value, bool opLike=false);
167 void insertCondValue(char *fldName); //For Predecate t1.f1=t2.f1
168 void insertUpdateValue(char *fldName, char *value);
170 void insertField(char *fName, AggType aggType= AGG_UNKNOWN);
171 void insertGroupField(char *fName);
172 void clearFieldNameList();
173 void insertTableName(char *value);
176 Predicate* insertPredicate(char *fldName, ComparisionOp op, void** value);
177 Predicate* insertPredicate(char *fldName1, ComparisionOp op, char *fldName2);
178 Predicate* insertBetPredicate(char *fldName, ComparisionOp op1, void **value1, ComparisionOp op2, void **value2);
179 Predicate* insertPredicate(Predicate *p1, LogicalOp op, Predicate *p2 = NULL);
180 void setCondition(Predicate *pred)
182 //No body is deleting memory allocated during condition::setTerm for PredicateImpl
183 //have list in this pared data and delete it during reset
184 predicate.setPredicate(pred);
186 Condition* getCondition() { return &predicate; }
188 void insertFieldValue(FieldValue *newVal) { fieldValueList.append(newVal); }
190 List getFieldNameList() { return fieldNameList; }
191 List getGroupFieldNameList() { return groupFieldNameList; }
192 List getConditionValueList() { return conditionValueList; }
193 List getFieldValueList() { return fieldValueList; }
194 List getInValueList() { return inValueList; }
195 List getUpdateFieldValueList() { return updFldValList; }
196 List getTableNameList() { return tableNameList; }
197 List getSecondaryIndexFieldList() { return secondaryIndexFieldList; }
198 void setFldName(char *name);
199 void setFldType(DataType type);
200 DataType getFldType();
201 DbRetVal setFldLength(size_t length);
202 void setDefaultValue(char * value);
203 //void setFldDefaultValue -- will need two parametersers, check how u want to pass default value.
204 void setFldNotNull(bool notNull);
206 void setUnique(bool unique){ isUnique = unique; }
207 void setPrimary(bool primary) { isPrimary = primary; }
208 void setIndexType (IndexType type) { indexType = type; }
209 IndexType getIndexType(){ return indexType; }
210 bool getUnique() { return isUnique; }
211 bool getPrimary() { return isPrimary; }
212 Expression* insertExpression(char *fldName);
213 Expression* insertExpression(char *value, bool flag);
214 Expression* insertExpression(Expression* exp1, ArithOperator op, Expression* exp2);
215 void insertUpdateExpression(char *fName, Expression *exp);
217 void insertFldDef(); //check if fldDef needs to be a part of ParsedData
219 FieldList getCreFldList() { return creFldList; }
221 StatementType getStmtType() { return stmtType; }
222 char *getFldName();
223 void setAutoFldName(char *fldName);
224 void reset();
228 #endif
230 //TODO: Aruna
231 //variable and function names suck, change if u want to
232 //setFldDefaultValue
233 //finding out if fldDef needs to be part of parsedData, or can allocate memory and pass around
234 //primary key
235 //foreign key