code reorganization phase - I
[csql.git] / src / sql / ParsedData.cxx
blob34356abca5ae64094daf204f6a374f0994398a71
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 ***************************************************************************/
16 #include<os.h>
17 #include <Parser.h>
18 #include <CSql.h>
19 #include<PredicateImpl.h>
22 void ParsedData::insertValue(char *val)
24 FieldValue *newVal = new FieldValue();
25 if (val == NULL)
26 newVal->parsedString = NULL;
27 else
28 newVal->parsedString = strdup(val);
29 strcpy(newVal->fldName," ");
30 newVal->value = NULL;
31 newVal->paramNo = 0;
32 newVal->type = typeUnknown;
33 newVal->length = 0;
34 newVal->isAllocVal=false;
35 fieldValueList.append(newVal);
38 void ParsedData::insertInValue(char *val)
40 FieldValue *newVal = new FieldValue();
41 if (val == NULL)
42 newVal->parsedString = NULL;
43 else
44 newVal->parsedString = strdup(val);
45 strcpy(newVal->fldName," ");
46 newVal->value = NULL;
47 newVal->paramNo = 0;
48 newVal->type = typeUnknown;
49 newVal->length = 0;
50 newVal->isAllocVal=false;
51 inValueList.append(newVal);
54 void** ParsedData::insertCondValueAndGetPtr(char *fldName, char *val, bool opLike, AggType aType, bool isInHaving)
56 ConditionValue *newVal = new ConditionValue();
57 if (val == NULL)
58 newVal->parsedString = NULL;
59 else
60 newVal->parsedString = strdup(val);
61 newVal->value = NULL;
62 newVal->paramNo = 0;
63 newVal->type = typeUnknown;
64 newVal->aType = aType;
65 newVal->length = 0;
66 strcpy(newVal->fName, fldName);
67 newVal->opLike = opLike;
68 conditionValueList.append(newVal);
69 if (isInHaving) {
70 FieldName *fName = new FieldName();
71 strcpy(fName->fldName, fldName);
72 fName->aType = aType;
73 havingFieldNameList.append(fName);
75 return &(newVal->value);
79 void ParsedData::insertCondValue(char *fldName)
81 ConditionValue *newVal = new ConditionValue();
82 newVal->parsedString = NULL;
83 newVal->value = NULL;
84 newVal->paramNo = 1;//To solve parsedString Null problem
85 newVal->type = typeUnknown;
86 newVal->aType = AGG_UNKNOWN;
87 newVal->length = 0;
88 strcpy(newVal->fName, fldName);
89 newVal->opLike = false;
90 conditionValueList.append(newVal);
92 void ParsedData::insertField(char *fName, AggType type)
94 FieldName *newVal = new FieldName();
95 strcpy(newVal->fldName , fName);
96 newVal->aType = type;
97 fieldNameList.append(newVal);
99 void ParsedData::insertFieldAlias(char *name)
101 FieldName *newVal=NULL;
102 newVal = (FieldName *)fieldNameList.get(fieldNameList.size());
103 strcpy(newVal->aliasFldName, name);
106 void ParsedData::insertFKField(char *fkName)
108 FieldName *newVal = new FieldName();
109 strcpy(newVal->fldName , fkName);
110 fkFieldNameList.append(newVal);
112 void ParsedData::insertPKField(char *pkName)
114 FieldName *newVal = new FieldName();
115 strcpy(newVal->fldName , pkName);
116 pkFieldNameList.append(newVal);
118 void ParsedData::insertForeignKeyList()
120 FieldName *name=NULL;
121 ForeignKeyInfo *fkInfo = new ForeignKeyInfo();
122 // strcpy(fkInfo->fkTableName,tblName);
123 strcpy(fkInfo->pkTableName,pkTblName);
124 ListIterator fNameIter = getFkFieldNameList().getIterator();
125 fNameIter.reset();
126 while (fNameIter.hasElement())
128 name = (FieldName *)fNameIter.nextElement();
129 fkInfo->fkFldList.append(name->fldName);
130 delete name;
132 fkFieldNameList.reset();
133 fNameIter = getPkFieldNameList().getIterator();
134 fNameIter.reset();
135 while (fNameIter.hasElement())
137 name = (FieldName *)fNameIter.nextElement();
138 fkInfo->pkFldList.append(name->fldName);
139 delete name;
141 pkFieldNameList.reset();
142 foreignKeyList.append(fkInfo);
144 void ParsedData::insertTableName(char *tName)
146 TableName *newVal = new TableName();
147 strcpy(newVal->tblName , tName);
148 tableNameList.append(newVal);
150 void ParsedData::insertJoinType(JoinType joinType)
152 JoinTypeNode *newVal = new JoinTypeNode();
153 newVal->jType = joinType;
154 joinTypeList.append(newVal);
158 void ParsedData::insertTableName(char *tName, char *aName)
160 TableName *newVal = new TableName();
161 strcpy(newVal->tblName , tName);
162 strcpy(newVal->aliasName , aName);
163 tableNameList.append(newVal);
165 void ParsedData::insertGroupField(char *fName)
167 FieldName *newVal = new FieldName();
168 strcpy(newVal->fldName , fName);
169 groupFieldNameList.append(newVal);
171 void ParsedData::insertOrderByField(char *fName, bool isDesc)
173 FieldName *newVal = new FieldName();
174 strcpy(newVal->fldName , fName);
175 if (isDesc) newVal->aType = AGG_MIN; //using MIN to denote descending order
176 orderFieldNameList.append(newVal);
179 void ParsedData::insertUpdateValue(char *fName, char *val)
181 UpdateFieldValue *newVal = new UpdateFieldValue();
182 strcpy(newVal->fldName, fName);
183 if (val == NULL)
184 newVal->parsedString = NULL;
185 else
186 newVal->parsedString = strdup(val);
187 newVal->value = NULL;
188 newVal->expre = NULL;
189 newVal->paramNo = 0;
190 updFldValList.append(newVal);
193 Predicate* ParsedData::insertPredicate(char *fName, ComparisionOp op, void **val, AggType aggType)
195 PredicateImpl *pImpl = new PredicateImpl();
196 pImpl->setTerm(fName, op, val, aggType);
197 predList.append(pImpl);
198 return (Predicate*) pImpl;
200 Predicate* ParsedData::insertBetPredicate(char *fName, ComparisionOp op1,
201 void **val1, ComparisionOp op2, void **val2)
203 PredicateImpl *pImpl = new PredicateImpl();
204 pImpl->setTerm(fName, op1, val1, op2, val2);
205 predList.append(pImpl);
206 return (Predicate*) pImpl;
208 Predicate* ParsedData::insertNullPredicate(char *fName, ComparisionOp op,bool nullFlag)
210 PredicateImpl *pImpl = new PredicateImpl();
211 pImpl->setTerm(fName, op, nullFlag);
212 predList.append(pImpl);
213 return (Predicate*) pImpl;
216 Predicate* ParsedData::insertPredicate(char *fName1, ComparisionOp op, char *fName2)
218 PredicateImpl *pImpl = new PredicateImpl();
219 pImpl->setTerm(fName1, op, fName2);
220 predList.append(pImpl);
221 return (Predicate*) pImpl;
224 Predicate* ParsedData::insertPredicate(Predicate *p1, LogicalOp op, Predicate *p2)
226 PredicateImpl *pImpl = new PredicateImpl();
227 pImpl->setTerm(p1, op, p2);
228 predList.append(pImpl);
229 return (Predicate*) pImpl;
231 void ParsedData::init()
233 tableNameList.init();
234 fieldNameList.init();
235 fieldValueList.init();
236 inValueList.init();
237 setCondition(NULL);
238 setHavingCondition(NULL);
239 predList.init();
240 conditionValueList.init();
241 updFldValList.init();
242 groupFieldNameList.init();
243 havingFieldNameList.init();
244 orderFieldNameList.init();
245 isDistinct = false;
246 joinTypeList.init();
247 isUnique = false;
248 isPrimary = false;
249 isAutoIncrement =false;
250 isForeign=false;
251 indexType = hashIndex;
252 hCondFld=false; vCondFld=false;
253 shouldCreateTbl=false;
254 limit =0;
255 offset =0;
256 paramCounter = 0; stmtType = UnknownStatement;
257 isExplain=false;
258 plan = Normal;
261 //NOTE::when you add members to reset() check whether it needs to be added in init() as it is used for statement caching when it contains parameters
262 void ParsedData::reset()
264 ListIterator fNameIter = fieldNameList.getIterator();
265 fNameIter.reset();
266 while (fNameIter.hasElement())
267 delete ((FieldName *) fNameIter.nextElement());
268 fieldNameList.reset();
269 fNameIter = secondaryIndexFieldList.getIterator();
270 fNameIter.reset();
271 while (fNameIter.hasElement())
272 delete ((FieldName *) fNameIter.nextElement());
273 secondaryIndexFieldList.reset();
275 fNameIter =pkFieldNameList.getIterator();
276 fNameIter.reset();
277 while (fNameIter.hasElement())
278 delete ((FieldName *) fNameIter.nextElement());
279 pkFieldNameList.reset();
280 fNameIter = fkFieldNameList.getIterator();
281 fNameIter.reset();
282 while (fNameIter.hasElement())
283 delete ((FieldName *) fNameIter.nextElement());
284 fkFieldNameList.reset();
285 ForeignKeyInfo *info=NULL;
286 fNameIter = foreignKeyList.getIterator();
287 while (fNameIter.hasElement())
289 info=(ForeignKeyInfo *) fNameIter.nextElement();
290 info->fkFldList.removeAll();
291 info->pkFldList.removeAll();
292 delete info;
294 foreignKeyList.reset();
295 ListIterator iter = fieldValueList.getIterator();
296 FieldValue *value;
297 while (iter.hasElement())
299 value = (FieldValue*)iter.nextElement();
300 if (value->parsedString) free(value->parsedString);
301 if (value->isAllocVal) free(value->value);
302 delete value;
304 fieldValueList.reset();
305 ListIterator inIter = inValueList.getIterator();
306 while (inIter.hasElement()) {
307 value = (FieldValue *) inIter.nextElement();
308 if (value->parsedString) free(value->parsedString);
309 delete value;
311 inValueList.reset();
313 predicate.reset();
314 havingPredicate.reset();
315 ListIterator pIter = predList.getIterator();
316 while (pIter.hasElement()) {
317 PredicateImpl *pImpl= (PredicateImpl *) pIter.nextElement();
318 delete pImpl;
320 predList.reset();
322 iter = conditionValueList.getIterator();
323 ConditionValue *condVal;
324 while (iter.hasElement())
326 condVal = (ConditionValue*)iter.nextElement();
327 free(condVal->parsedString);
328 free(condVal->value);
329 delete condVal;
331 conditionValueList.reset();
333 iter = updFldValList.getIterator();
334 UpdateFieldValue *updFldVal;
335 while (iter.hasElement())
337 updFldVal = (UpdateFieldValue*)iter.nextElement();
338 if(updFldVal->parsedString!=NULL)
339 free(updFldVal->parsedString);
340 if(updFldVal->expre!=NULL){
341 updFldVal->expre->freeVal();
342 delete updFldVal->expre;
344 free(updFldVal->value);
345 delete updFldVal;
347 updFldValList.reset();
348 iter = groupFieldNameList.getIterator();
349 while(iter.hasElement())
351 delete iter.nextElement();
353 groupFieldNameList.reset();
354 iter = orderFieldNameList.getIterator();
355 while(iter.hasElement())
357 delete iter.nextElement();
360 iter = havingFieldNameList.getIterator();
361 while(iter.hasElement())
363 delete iter.nextElement();
365 havingFieldNameList.reset();
367 orderFieldNameList.reset();
368 isDistinct = false;
370 iter = tableNameList.getIterator();
371 TableName *tname;
372 while (iter.hasElement())
374 tname = (TableName*)iter.nextElement();
375 delete tname;
377 tableNameList.reset();
379 iter = joinTypeList.getIterator();
380 JoinTypeNode *jNode;
381 while (iter.hasElement())
383 jNode = (JoinTypeNode*)iter.nextElement();
384 delete jNode;
386 joinTypeList.reset();
388 if(userNode) { delete userNode; userNode =NULL;}
390 creFldList.removeAll();
391 isUnique = false;
392 isPrimary = false;
393 isAutoIncrement =false;
394 isForeign=false;
395 indexType = hashIndex;
396 hCondFld=false; vCondFld=false;pkFld=false;forceOption=false; direct=false; uncache=false; noschema=false; dsn=false;
397 bucketSize = 0;
398 shouldCreateTbl=false;
399 limit =0;
400 offset =0;
402 void ParsedData::clearFieldNameList()
404 ListIterator it = fieldNameList.getIterator();
405 while (it.hasElement()) delete ((FieldName *) it.nextElement());
406 fieldNameList.reset();
409 void ParsedData::setFldName(char *name)
411 strcpy(fldDef.fldName_, name);
412 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
414 char *ParsedData::getFldName()
416 return fldDef.fldName_;
418 void ParsedData::setAutoFldName(char *fldName)
420 FieldInfo *newVal = new FieldInfo();
421 strcpy(newVal->fldName , fldName);
422 newVal->isAutoIncrement = true;
423 secondaryIndexFieldList.append(newVal);
425 void ParsedData::setFldType(DataType type)
427 fldDef.type_ = type;
429 DataType ParsedData::getFldType()
431 return fldDef.type_;
433 DbRetVal ParsedData::setAutoIncreament(bool flag)
435 if(isAutoIncrement){return ErrAlready;}
436 fldDef.isAutoIncrement_=flag;
437 isAutoIncrement=true;
438 return OK;
440 bool ParsedData::getAutoIncreament()
442 return fldDef.isAutoIncrement_;
445 DbRetVal ParsedData::setFldLength(size_t length)
447 if(fldDef.type_ == typeBinary && (length-1) && (length-1) <= 256) {
448 fldDef.length_ = length - 1; return OK;
450 else if (fldDef.type_ == typeBinary && (length-1) > 256) {
451 return ErrBadRange;
453 else { fldDef.length_ = length; return OK; }
456 void ParsedData::setFldNotNull(bool notNull)
458 fldDef.isNull_ = notNull;
460 void ParsedData::setDefaultValue(char *value)
462 fldDef.isDefault_ = true;
463 if (value == NULL) {
464 fldDef.defaultValueBuf_[0]='\0';
465 return;
467 if (strlen(value) > DEFAULT_VALUE_BUF_LENGTH -1)
469 strncpy(fldDef.defaultValueBuf_, value, DEFAULT_VALUE_BUF_LENGTH -1);
470 fldDef.defaultValueBuf_[DEFAULT_VALUE_BUF_LENGTH] ='\0';
471 } else strcpy(fldDef.defaultValueBuf_, value);
472 return;
476 void ParsedData::insertFldDef()
478 DbRetVal rv = creFldList.append(fldDef);
479 fldDef.init();
482 Expression* ParsedData::insertExpression(char *fldName)
484 Expression *exp =new Expression();
485 exp->setExpr(fldName);
486 return exp;
488 Expression* ParsedData::insertExpression(char *value,bool flag)
490 Expression *exp =new Expression();
491 exp->setExpr(strdup(value),flag);
492 return exp;
494 Expression* ParsedData::insertExpression(Expression* exp1, ArithOperator op ,Expression* exp2)
496 Expression *exp =new Expression();
497 exp->setExpr(exp1, op, exp2);
498 return exp;
500 void ParsedData::insertUpdateExpression(char *fName, Expression *exp)
502 UpdateFieldValue *newVal = new UpdateFieldValue();
503 strcpy(newVal->fldName, fName);
504 newVal->parsedString = NULL;
505 newVal->value = NULL;
506 newVal->expre=exp;
507 newVal->paramNo = 0;
508 updFldValList.append(newVal);
510 void ParsedData::createUserNode(char *name, char *password)
512 userNode = new UserNode();
513 strcpy(userNode->userName, name);
514 strcpy(userNode->passName, password);
515 userNode->type = CREATEUSER;
517 void ParsedData::dropUserNode(char *name)
519 userNode = new UserNode();
520 strcpy(userNode->userName, name);
521 userNode->type = DROPUSER;
523 void ParsedData::alterUserNode(char *name, char *password)
525 userNode = new UserNode();
526 strcpy(userNode->userName, name);
527 strcpy(userNode->passName, password);
528 userNode->type = ALTERUSER;