1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
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. *
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. *
15 ***************************************************************************/
19 #include<PredicateImpl.h>
22 void ParsedData::insertValue(char *val
)
24 FieldValue
*newVal
= new FieldValue();
26 newVal
->parsedString
= NULL
;
28 newVal
->parsedString
= strdup(val
);
29 strcpy(newVal
->fldName
," ");
32 newVal
->type
= typeUnknown
;
34 newVal
->isAllocVal
=false;
35 fieldValueList
.append(newVal
);
38 void ParsedData::insertInValue(char *val
)
40 FieldValue
*newVal
= new FieldValue();
42 newVal
->parsedString
= NULL
;
44 newVal
->parsedString
= strdup(val
);
45 strcpy(newVal
->fldName
," ");
48 newVal
->type
= typeUnknown
;
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();
57 newVal
->parsedString
= NULL
;
59 newVal
->parsedString
= strdup(val
);
62 newVal
->type
= typeUnknown
;
63 newVal
->aType
= aType
;
65 strcpy(newVal
->fName
, fldName
);
66 newVal
->opLike
= opLike
;
67 newVal
->isFunctionInvolve
= function
;
68 conditionValueList
.append(newVal
);
70 FieldName
*fName
= new FieldName();
71 strcpy(fName
->fldName
, fldName
);
73 havingFieldNameList
.append(fName
);
75 return &(newVal
->value
);
79 void ParsedData::insertCondValue(char *fldName
)
81 ConditionValue
*newVal
= new ConditionValue();
82 newVal
->parsedString
= NULL
;
84 newVal
->paramNo
= 1;//To solve parsedString Null problem
85 newVal
->type
= typeUnknown
;
86 newVal
->aType
= AGG_UNKNOWN
;
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
);
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();
126 while (fNameIter
.hasElement())
128 name
= (FieldName
*)fNameIter
.nextElement();
129 fkInfo
->fkFldList
.append(name
->fldName
);
132 fkFieldNameList
.reset();
133 fNameIter
= getPkFieldNameList().getIterator();
135 while (fNameIter
.hasElement())
137 name
= (FieldName
*)fNameIter
.nextElement();
138 fkInfo
->pkFldList
.append(name
->fldName
);
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
);
184 newVal
->parsedString
= NULL
;
186 newVal
->parsedString
= strdup(val
);
187 newVal
->value
= NULL
;
188 newVal
->expre
= NULL
;
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();
265 setHavingCondition(NULL
);
267 conditionValueList
.init();
268 updFldValList
.init();
269 groupFieldNameList
.init();
270 havingFieldNameList
.init();
271 orderFieldNameList
.init();
276 isAutoIncrement
=false;
278 indexType
= hashIndex
;
279 hCondFld
=false; vCondFld
=false;
280 shouldCreateTbl
=false;
283 paramCounter
= 0; stmtType
= UnknownStatement
;
288 //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
289 void ParsedData::reset()
291 ListIterator fNameIter
= fieldNameList
.getIterator();
293 while (fNameIter
.hasElement())
294 delete ((FieldName
*) fNameIter
.nextElement());
295 fieldNameList
.reset();
296 fNameIter
= secondaryIndexFieldList
.getIterator();
298 while (fNameIter
.hasElement())
299 delete ((FieldName
*) fNameIter
.nextElement());
300 secondaryIndexFieldList
.reset();
302 fNameIter
=pkFieldNameList
.getIterator();
304 while (fNameIter
.hasElement())
305 delete ((FieldName
*) fNameIter
.nextElement());
306 pkFieldNameList
.reset();
307 fNameIter
= fkFieldNameList
.getIterator();
309 while (fNameIter
.hasElement())
310 delete ((FieldName
*) fNameIter
.nextElement());
311 fkFieldNameList
.reset();
312 ForeignKeyInfo
*info
=NULL
;
313 fNameIter
= foreignKeyList
.getIterator();
314 while (fNameIter
.hasElement())
316 info
=(ForeignKeyInfo
*) fNameIter
.nextElement();
317 info
->fkFldList
.removeAll();
318 info
->pkFldList
.removeAll();
321 foreignKeyList
.reset();
322 ListIterator iter
= fieldValueList
.getIterator();
324 while (iter
.hasElement())
326 value
= (FieldValue
*)iter
.nextElement();
327 if (value
->parsedString
) free(value
->parsedString
);
328 if (value
->isAllocVal
) free(value
->value
);
331 fieldValueList
.reset();
332 ListIterator inIter
= inValueList
.getIterator();
333 while (inIter
.hasElement()) {
334 value
= (FieldValue
*) inIter
.nextElement();
335 if (value
->parsedString
) free(value
->parsedString
);
341 havingPredicate
.reset();
342 ListIterator pIter
= predList
.getIterator();
343 while (pIter
.hasElement()) {
344 PredicateImpl
*pImpl
= (PredicateImpl
*) pIter
.nextElement();
349 iter
= conditionValueList
.getIterator();
350 ConditionValue
*condVal
;
351 while (iter
.hasElement())
353 condVal
= (ConditionValue
*)iter
.nextElement();
354 free(condVal
->parsedString
);
355 free(condVal
->value
);
358 conditionValueList
.reset();
360 iter
= updFldValList
.getIterator();
361 UpdateFieldValue
*updFldVal
;
362 while (iter
.hasElement())
364 updFldVal
= (UpdateFieldValue
*)iter
.nextElement();
365 if(updFldVal
->parsedString
!=NULL
)
366 free(updFldVal
->parsedString
);
367 if(updFldVal
->expre
!=NULL
){
368 updFldVal
->expre
->freeVal();
369 delete updFldVal
->expre
;
371 free(updFldVal
->value
);
374 updFldValList
.reset();
375 iter
= groupFieldNameList
.getIterator();
376 while(iter
.hasElement())
378 delete iter
.nextElement();
380 groupFieldNameList
.reset();
381 iter
= orderFieldNameList
.getIterator();
382 while(iter
.hasElement())
384 delete iter
.nextElement();
387 iter
= havingFieldNameList
.getIterator();
388 while(iter
.hasElement())
390 delete iter
.nextElement();
392 havingFieldNameList
.reset();
394 orderFieldNameList
.reset();
398 iter
= tableNameList
.getIterator();
400 while (iter
.hasElement())
402 tname
= (TableName
*)iter
.nextElement();
405 tableNameList
.reset();
407 iter
= joinTypeList
.getIterator();
409 while (iter
.hasElement())
411 jNode
= (JoinTypeNode
*)iter
.nextElement();
414 joinTypeList
.reset();
416 if(userNode
) { delete userNode
; userNode
=NULL
;}
418 creFldList
.removeAll();
421 isAutoIncrement
=false;
423 indexType
= hashIndex
;
424 hCondFld
=false; vCondFld
=false;pkFld
=false;forceOption
=false; direct
=false; uncache
=false; noschema
=false; dsn
=false;
426 shouldCreateTbl
=false;
429 ftype
= UNKNOWN_FUNCTION
;
431 void ParsedData::clearFieldNameList()
433 ListIterator it
= fieldNameList
.getIterator();
434 while (it
.hasElement()) delete ((FieldName
*) it
.nextElement());
435 fieldNameList
.reset();
438 void ParsedData::setFldName(char *name
)
440 strcpy(fldDef
.fldName_
, name
);
441 fldDef
.fldName_
[IDENTIFIER_LENGTH
] = '\0';
443 char *ParsedData::getFldName()
445 return fldDef
.fldName_
;
447 void ParsedData::setAutoFldName(char *fldName
)
449 FieldInfo
*newVal
= new FieldInfo();
450 strcpy(newVal
->fldName
, fldName
);
451 newVal
->isAutoIncrement
= true;
452 secondaryIndexFieldList
.append(newVal
);
454 void ParsedData::setFldType(DataType type
)
458 DataType
ParsedData::getFldType()
462 DbRetVal
ParsedData::setAutoIncreament(bool flag
)
464 if(isAutoIncrement
){return ErrAlready
;}
465 fldDef
.isAutoIncrement_
=flag
;
466 isAutoIncrement
=true;
469 bool ParsedData::getAutoIncreament()
471 return fldDef
.isAutoIncrement_
;
474 DbRetVal
ParsedData::setFldLength(size_t length
)
476 if(fldDef
.type_
== typeBinary
&& (length
-1) && (length
-1) <= 256) {
477 fldDef
.length_
= length
- 1; return OK
;
479 else if (fldDef
.type_
== typeBinary
&& (length
-1) > 256) {
482 else { fldDef
.length_
= length
; return OK
; }
485 void ParsedData::setFldNotNull(bool notNull
)
487 fldDef
.isNull_
= notNull
;
489 DbRetVal
ParsedData::setDefaultValue(char *value
)
492 fldDef
.isDefault_
= true;
494 fldDef
.defaultValueBuf_
[0]='\0';
497 /* Function to check Default Value */
498 rv
=validateDefaultValue(value
);
499 if(rv
!=OK
) return ErrDefaultValue
;
501 if (strlen(value
) > DEFAULT_VALUE_BUF_LENGTH
-1)
503 strncpy(fldDef
.defaultValueBuf_
, value
, DEFAULT_VALUE_BUF_LENGTH
-1);
504 fldDef
.defaultValueBuf_
[DEFAULT_VALUE_BUF_LENGTH
] ='\0';
505 } else strcpy(fldDef
.defaultValueBuf_
, value
);
508 /* Validate Default value */
509 DbRetVal
ParsedData::validateDefaultValue(char *value
)
516 /* Default value checking for INT, SHORT, LONG, LONGLONG data type */
517 if(fldDef
.type_
== typeInt
|| fldDef
.type_
== typeShort
|| fldDef
.type_
== typeByteInt
|| fldDef
.type_
== typeLongLong
|| fldDef
.type_
== typeLong
){
518 for(int i
=0; i
<strlen(value
); i
++){
519 if(!(value
[i
] >= '0' && value
[i
] <= '9' || value
[0]=='-')){
520 return ErrDefaultValue
;
524 /* Default value check for FLOAT and DOUBLE data type*/
525 else if (fldDef
.type_
== typeFloat
|| fldDef
.type_
== typeDouble
){
526 for(int i
=0;i
<strlen(value
);i
++){
527 if(!(value
[i
] >= '0' && value
[i
] <='9' || value
[0]=='-' || value
[i
] == '.')){
528 return ErrDefaultValue
;
532 /* Default value checking for DATE */
533 else if(fldDef
.type_
== typeDate
){
534 ret
=date
.parseFrom(value
);
535 if(ret
==-1){ return ErrDefaultValue
;}
537 /*Default value checking for TIME */
538 else if(fldDef
.type_
== typeTime
){
539 ret
=time
.parseFrom(value
);
540 if(ret
==-1){ return ErrDefaultValue
;}
542 /* Default value checking for TIMESTAMP */
543 else if(fldDef
.type_
== typeTimeStamp
){
544 ret
=timeStamp
.parseFrom(value
);
545 if(ret
==-1){ return ErrDefaultValue
;}
553 void ParsedData::insertFldDef()
555 DbRetVal rv
= creFldList
.append(fldDef
);
558 Expression
* ParsedData::insertExpression(Expression
* exp1
, FunctionType type
,Expression
* exp2
)
560 Expression
*exp
=new Expression();
561 exp
->setExpr(exp1
, type
, exp2
);
564 Expression
* ParsedData::insertExpression(char *fldName
)
566 Expression
*exp
=new Expression();
567 exp
->setExpr(fldName
);
571 Expression
* ParsedData::insertExpression(char *value
,bool flag
)
573 Expression
*exp
=new Expression();
574 exp
->setExpr(strdup(value
),flag
);
578 Expression
* ParsedData::insertExpression(Expression
* exp1
, ArithOperator op
,Expression
* exp2
)
580 Expression
*exp
=new Expression();
581 exp
->setExpr(exp1
, op
, exp2
);
584 void ParsedData::insertUpdateExpression(char *fName
, Expression
*exp
)
586 UpdateFieldValue
*newVal
= new UpdateFieldValue();
587 strcpy(newVal
->fldName
, fName
);
588 newVal
->parsedString
= NULL
;
589 newVal
->value
= NULL
;
592 updFldValList
.append(newVal
);
594 void ParsedData::createUserNode(char *name
, char *password
)
596 userNode
= new UserNode();
597 strcpy(userNode
->userName
, name
);
598 strcpy(userNode
->passName
, password
);
599 userNode
->type
= CREATEUSER
;
601 void ParsedData::dropUserNode(char *name
)
603 userNode
= new UserNode();
604 strcpy(userNode
->userName
, name
);
605 userNode
->type
= DROPUSER
;
607 void ParsedData::alterUserNode(char *name
, char *password
)
609 userNode
= new UserNode();
610 strcpy(userNode
->userName
, name
);
611 strcpy(userNode
->passName
, password
);
612 userNode
->type
= ALTERUSER
;