two IN clause in sql statement does not clear the value clause before
[csql.git] / src / sql / ParsedData.cxx
blobee589c523bc7840a03115760eb7aa9d2c2638d89
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);
53 void** ParsedData::insertCondValueAndGetPtr(char *fldName, char *val, bool opLike, AggType aType, bool isInHaving,bool function)
55 ConditionValue *newVal = new ConditionValue();
56 if (val == NULL)
57 newVal->parsedString = NULL;
58 else
59 newVal->parsedString = strdup(val);
60 newVal->value = NULL;
61 newVal->paramNo = 0;
62 newVal->type = typeUnknown;
63 newVal->aType = aType;
64 newVal->length = 0;
65 strcpy(newVal->fName, fldName);
66 newVal->opLike = opLike;
67 newVal->isFunctionInvolve = function;
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(Expression *exp, ComparisionOp op,void **val)
195 PredicateImpl *pImpl = new PredicateImpl();
196 exp->convertStrToVal(AllDataType::getCsqlTypeFromFunctionType(exp->getFunctionType()));
197 pImpl->setTerm(exp, op, val);
198 predList.append(pImpl);
199 return (Predicate*) pImpl;
201 Predicate* ParsedData::insertPredicate(Expression *exp, ComparisionOp op,char *fName2)
203 PredicateImpl *pImpl = new PredicateImpl();
204 exp->convertStrToVal(AllDataType::getCsqlTypeFromFunctionType(exp->getFunctionType()));
205 pImpl->setTerm(exp, op, fName2);
206 predList.append(pImpl);
207 return (Predicate*) pImpl;
210 Predicate* ParsedData::insertPredicate(Expression *exp1, ComparisionOp op,Expression *exp2)
212 PredicateImpl *pImpl = new PredicateImpl();
213 exp1->convertStrToVal(AllDataType::getCsqlTypeFromFunctionType(exp1->getFunctionType()));
214 exp2->convertStrToVal(AllDataType::getCsqlTypeFromFunctionType(exp2->getFunctionType()));
215 pImpl->setTerm(exp1, op, exp2);
216 predList.append(pImpl);
217 return (Predicate*) pImpl;
220 Predicate* ParsedData::insertPredicate(char *fName, ComparisionOp op, void **val, AggType aggType)
222 PredicateImpl *pImpl = new PredicateImpl();
223 pImpl->setTerm(fName, op, val, aggType);
224 predList.append(pImpl);
225 return (Predicate*) pImpl;
227 Predicate* ParsedData::insertBetPredicate(char *fName, ComparisionOp op1,
228 void **val1, ComparisionOp op2, void **val2)
230 PredicateImpl *pImpl = new PredicateImpl();
231 pImpl->setTerm(fName, op1, val1, op2, val2);
232 predList.append(pImpl);
233 return (Predicate*) pImpl;
235 Predicate* ParsedData::insertNullPredicate(char *fName, ComparisionOp op,bool nullFlag)
237 PredicateImpl *pImpl = new PredicateImpl();
238 pImpl->setTerm(fName, op, nullFlag);
239 predList.append(pImpl);
240 return (Predicate*) pImpl;
243 Predicate* ParsedData::insertPredicate(char *fName1, ComparisionOp op, char *fName2)
245 PredicateImpl *pImpl = new PredicateImpl();
246 pImpl->setTerm(fName1, op, fName2);
247 predList.append(pImpl);
248 return (Predicate*) pImpl;
251 Predicate* ParsedData::insertPredicate(Predicate *p1, LogicalOp op, Predicate *p2)
253 PredicateImpl *pImpl = new PredicateImpl();
254 pImpl->setTerm(p1, op, p2);
255 predList.append(pImpl);
256 return (Predicate*) pImpl;
258 void ParsedData::init()
260 tableNameList.init();
261 fieldNameList.init();
262 fieldValueList.init();
263 inValueList.init();
264 setCondition(NULL);
265 setHavingCondition(NULL);
266 predList.init();
267 conditionValueList.init();
268 updFldValList.init();
269 groupFieldNameList.init();
270 havingFieldNameList.init();
271 orderFieldNameList.init();
272 isDistinct = false;
273 joinTypeList.init();
274 isUnique = false;
275 isPrimary = false;
276 isAutoIncrement =false;
277 isForeign=false;
278 indexType = hashIndex;
279 hCondFld=false; vCondFld=false;
280 shouldCreateTbl=false;
281 limit =0;
282 offset =0;
283 paramCounter = 0; stmtType = UnknownStatement;
284 isExplain=false;
285 plan = Normal;
287 void ParsedData::resetInValueList()
289 FieldValue *value;
290 ListIterator inIter = inValueList.getIterator();
291 while (inIter.hasElement()) {
292 value = (FieldValue *) inIter.nextElement();
293 if (value->parsedString) free(value->parsedString);
294 delete value;
296 inValueList.reset();
297 return;
300 //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
301 void ParsedData::reset()
303 ListIterator fNameIter = fieldNameList.getIterator();
304 fNameIter.reset();
305 while (fNameIter.hasElement())
306 delete ((FieldName *) fNameIter.nextElement());
307 fieldNameList.reset();
308 fNameIter = secondaryIndexFieldList.getIterator();
309 fNameIter.reset();
310 while (fNameIter.hasElement())
311 delete ((FieldName *) fNameIter.nextElement());
312 secondaryIndexFieldList.reset();
314 fNameIter =pkFieldNameList.getIterator();
315 fNameIter.reset();
316 while (fNameIter.hasElement())
317 delete ((FieldName *) fNameIter.nextElement());
318 pkFieldNameList.reset();
319 fNameIter = fkFieldNameList.getIterator();
320 fNameIter.reset();
321 while (fNameIter.hasElement())
322 delete ((FieldName *) fNameIter.nextElement());
323 fkFieldNameList.reset();
324 ForeignKeyInfo *info=NULL;
325 fNameIter = foreignKeyList.getIterator();
326 while (fNameIter.hasElement())
328 info=(ForeignKeyInfo *) fNameIter.nextElement();
329 info->fkFldList.removeAll();
330 info->pkFldList.removeAll();
331 delete info;
333 foreignKeyList.reset();
334 ListIterator iter = fieldValueList.getIterator();
335 FieldValue *value;
336 while (iter.hasElement())
338 value = (FieldValue*)iter.nextElement();
339 if (value->parsedString) free(value->parsedString);
340 if (value->isAllocVal) free(value->value);
341 delete value;
343 fieldValueList.reset();
345 resetInValueList();
347 predicate.reset();
348 havingPredicate.reset();
349 ListIterator pIter = predList.getIterator();
350 while (pIter.hasElement()) {
351 PredicateImpl *pImpl= (PredicateImpl *) pIter.nextElement();
352 delete pImpl;
354 predList.reset();
356 iter = conditionValueList.getIterator();
357 ConditionValue *condVal;
358 while (iter.hasElement())
360 condVal = (ConditionValue*)iter.nextElement();
361 free(condVal->parsedString);
362 free(condVal->value);
363 delete condVal;
365 conditionValueList.reset();
367 iter = updFldValList.getIterator();
368 UpdateFieldValue *updFldVal;
369 while (iter.hasElement())
371 updFldVal = (UpdateFieldValue*)iter.nextElement();
372 if(updFldVal->parsedString!=NULL)
373 free(updFldVal->parsedString);
374 if(updFldVal->expre!=NULL){
375 updFldVal->expre->freeVal();
376 delete updFldVal->expre;
378 free(updFldVal->value);
379 delete updFldVal;
381 updFldValList.reset();
382 iter = groupFieldNameList.getIterator();
383 while(iter.hasElement())
385 delete iter.nextElement();
387 groupFieldNameList.reset();
388 iter = orderFieldNameList.getIterator();
389 while(iter.hasElement())
391 delete iter.nextElement();
394 iter = havingFieldNameList.getIterator();
395 while(iter.hasElement())
397 delete iter.nextElement();
399 havingFieldNameList.reset();
401 orderFieldNameList.reset();
402 isDistinct = false;
403 isExplain = false;
405 iter = tableNameList.getIterator();
406 TableName *tname;
407 while (iter.hasElement())
409 tname = (TableName*)iter.nextElement();
410 delete tname;
412 tableNameList.reset();
414 iter = joinTypeList.getIterator();
415 JoinTypeNode *jNode;
416 while (iter.hasElement())
418 jNode = (JoinTypeNode*)iter.nextElement();
419 delete jNode;
421 joinTypeList.reset();
423 if(userNode) { delete userNode; userNode =NULL;}
425 creFldList.removeAll();
426 isUnique = false;
427 isPrimary = false;
428 isAutoIncrement =false;
429 isForeign=false;
430 indexType = hashIndex;
431 hCondFld=false; vCondFld=false;pkFld=false;forceOption=false; direct=false; uncache=false; noschema=false; dsn=false;
432 bucketSize = 0;
433 shouldCreateTbl=false;
434 limit =0;
435 offset =0;
436 ftype = UNKNOWN_FUNCTION;
438 void ParsedData::clearFieldNameList()
440 ListIterator it = fieldNameList.getIterator();
441 while (it.hasElement()) delete ((FieldName *) it.nextElement());
442 fieldNameList.reset();
445 void ParsedData::setFldName(char *name)
447 strcpy(fldDef.fldName_, name);
448 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
450 char *ParsedData::getFldName()
452 return fldDef.fldName_;
454 void ParsedData::setAutoFldName(char *fldName)
456 FieldInfo *newVal = new FieldInfo();
457 strcpy(newVal->fldName , fldName);
458 newVal->isAutoIncrement = true;
459 secondaryIndexFieldList.append(newVal);
461 void ParsedData::setFldType(DataType type)
463 fldDef.type_ = type;
465 DataType ParsedData::getFldType()
467 return fldDef.type_;
469 DbRetVal ParsedData::setAutoIncreament(bool flag)
471 if(isAutoIncrement){return ErrAlready;}
472 fldDef.isAutoIncrement_=flag;
473 isAutoIncrement=true;
474 return OK;
476 bool ParsedData::getAutoIncreament()
478 return fldDef.isAutoIncrement_;
481 DbRetVal ParsedData::setFldLength(size_t length)
483 if(fldDef.type_ == typeBinary && (length-1) && (length-1) <= 256) {
484 fldDef.length_ = length - 1; return OK;
486 else if (fldDef.type_ == typeBinary && (length-1) > 256) {
487 return ErrBadRange;
489 else { fldDef.length_ = length; return OK; }
492 void ParsedData::setFldNotNull(bool notNull)
494 fldDef.isNull_ = notNull;
496 DbRetVal ParsedData::setDefaultValue(char *value)
498 DbRetVal rv=OK;
499 fldDef.isDefault_ = true;
500 if (value == NULL) {
501 fldDef.defaultValueBuf_[0]='\0';
502 return OK;;
504 /* Function to check Default Value */
505 rv=validateDefaultValue(value);
506 if(rv !=OK) return ErrDefaultValue;
508 if (strlen(value) > DEFAULT_VALUE_BUF_LENGTH -1)
510 strncpy(fldDef.defaultValueBuf_, value, DEFAULT_VALUE_BUF_LENGTH -1);
511 fldDef.defaultValueBuf_[DEFAULT_VALUE_BUF_LENGTH] ='\0';
512 } else strcpy(fldDef.defaultValueBuf_, value);
513 return rv;
515 /* Validate Default value */
516 DbRetVal ParsedData::validateDefaultValue(char *value)
518 DbRetVal rv=OK;
519 Date date;
520 Time time;
521 TimeStamp timeStamp;
522 int ret=0;
523 /* Default value checking for INT, SHORT, LONG, LONGLONG data type */
524 if(fldDef.type_ == typeInt || fldDef.type_ == typeShort || fldDef.type_ == typeByteInt || fldDef.type_ == typeLongLong || fldDef.type_ == typeLong){
525 for(int i=0; i<strlen(value); i++){
526 if(!(value[i] >= '0' && value[i] <= '9' || value[0]=='-')){
527 return ErrDefaultValue;
531 /* Default value check for FLOAT and DOUBLE data type*/
532 else if (fldDef.type_ == typeFloat || fldDef.type_ == typeDouble){
533 for(int i=0;i<strlen(value);i++){
534 if(!(value[i] >= '0' && value[i] <='9' || value[0]=='-' || value[i] == '.')){
535 return ErrDefaultValue;
539 /* Default value checking for DATE */
540 else if(fldDef.type_ == typeDate ){
541 ret=date.parseFrom(value);
542 if(ret==-1){ return ErrDefaultValue;}
544 /*Default value checking for TIME */
545 else if(fldDef.type_ == typeTime ){
546 ret=time.parseFrom(value);
547 if(ret==-1){ return ErrDefaultValue;}
549 /* Default value checking for TIMESTAMP */
550 else if(fldDef.type_ == typeTimeStamp ){
551 ret=timeStamp.parseFrom(value);
552 if(ret==-1){ return ErrDefaultValue;}
554 return rv;
560 void ParsedData::insertFldDef()
562 DbRetVal rv = creFldList.append(fldDef);
563 fldDef.init();
565 Expression* ParsedData::insertExpression(Expression* exp1, FunctionType type,Expression* exp2)
567 Expression *exp =new Expression();
568 exp->setExpr(exp1, type, exp2);
569 return exp;
571 Expression* ParsedData::insertExpression(char *fldName)
573 Expression *exp =new Expression();
574 exp->setExpr(fldName);
575 return exp;
578 Expression* ParsedData::insertExpression(char *value,bool flag)
580 Expression *exp =new Expression();
581 exp->setExpr(strdup(value),flag);
582 return exp;
585 Expression* ParsedData::insertExpression(Expression* exp1, ArithOperator op ,Expression* exp2)
587 Expression *exp =new Expression();
588 exp->setExpr(exp1, op, exp2);
589 return exp;
591 void ParsedData::insertUpdateExpression(char *fName, Expression *exp)
593 UpdateFieldValue *newVal = new UpdateFieldValue();
594 strcpy(newVal->fldName, fName);
595 newVal->parsedString = NULL;
596 newVal->value = NULL;
597 newVal->expre=exp;
598 newVal->paramNo = 0;
599 updFldValList.append(newVal);
601 void ParsedData::createUserNode(char *name, char *password)
603 userNode = new UserNode();
604 strcpy(userNode->userName, name);
605 strcpy(userNode->passName, password);
606 userNode->type = CREATEUSER;
608 void ParsedData::dropUserNode(char *name)
610 userNode = new UserNode();
611 strcpy(userNode->userName, name);
612 userNode->type = DROPUSER;
614 void ParsedData::alterUserNode(char *name, char *password)
616 userNode = new UserNode();
617 strcpy(userNode->userName, name);
618 strcpy(userNode->passName, password);
619 userNode->type = ALTERUSER;