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
);
54 void** ParsedData::insertCondValueAndGetPtr(char *fldName
, char *val
, bool opLike
, AggType aType
, bool isInHaving
)
56 ConditionValue
*newVal
= new ConditionValue();
58 newVal
->parsedString
= NULL
;
60 newVal
->parsedString
= strdup(val
);
63 newVal
->type
= typeUnknown
;
64 newVal
->aType
= aType
;
66 strcpy(newVal
->fName
, fldName
);
67 newVal
->opLike
= opLike
;
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(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();
238 setHavingCondition(NULL
);
240 conditionValueList
.init();
241 updFldValList
.init();
242 groupFieldNameList
.init();
243 havingFieldNameList
.init();
244 orderFieldNameList
.init();
249 isAutoIncrement
=false;
251 indexType
= hashIndex
;
252 hCondFld
=false; vCondFld
=false;
253 shouldCreateTbl
=false;
256 paramCounter
= 0; stmtType
= UnknownStatement
;
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();
266 while (fNameIter
.hasElement())
267 delete ((FieldName
*) fNameIter
.nextElement());
268 fieldNameList
.reset();
269 fNameIter
= secondaryIndexFieldList
.getIterator();
271 while (fNameIter
.hasElement())
272 delete ((FieldName
*) fNameIter
.nextElement());
273 secondaryIndexFieldList
.reset();
275 fNameIter
=pkFieldNameList
.getIterator();
277 while (fNameIter
.hasElement())
278 delete ((FieldName
*) fNameIter
.nextElement());
279 pkFieldNameList
.reset();
280 fNameIter
= fkFieldNameList
.getIterator();
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();
294 foreignKeyList
.reset();
295 ListIterator iter
= fieldValueList
.getIterator();
297 while (iter
.hasElement())
299 value
= (FieldValue
*)iter
.nextElement();
300 if (value
->parsedString
) free(value
->parsedString
);
301 if (value
->isAllocVal
) free(value
->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
);
314 havingPredicate
.reset();
315 ListIterator pIter
= predList
.getIterator();
316 while (pIter
.hasElement()) {
317 PredicateImpl
*pImpl
= (PredicateImpl
*) pIter
.nextElement();
322 iter
= conditionValueList
.getIterator();
323 ConditionValue
*condVal
;
324 while (iter
.hasElement())
326 condVal
= (ConditionValue
*)iter
.nextElement();
327 free(condVal
->parsedString
);
328 free(condVal
->value
);
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
);
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();
370 iter
= tableNameList
.getIterator();
372 while (iter
.hasElement())
374 tname
= (TableName
*)iter
.nextElement();
377 tableNameList
.reset();
379 iter
= joinTypeList
.getIterator();
381 while (iter
.hasElement())
383 jNode
= (JoinTypeNode
*)iter
.nextElement();
386 joinTypeList
.reset();
388 if(userNode
) { delete userNode
; userNode
=NULL
;}
390 creFldList
.removeAll();
393 isAutoIncrement
=false;
395 indexType
= hashIndex
;
396 hCondFld
=false; vCondFld
=false;pkFld
=false;forceOption
=false; direct
=false; uncache
=false; noschema
=false; dsn
=false;
398 shouldCreateTbl
=false;
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
)
429 DataType
ParsedData::getFldType()
433 DbRetVal
ParsedData::setAutoIncreament(bool flag
)
435 if(isAutoIncrement
){return ErrAlready
;}
436 fldDef
.isAutoIncrement_
=flag
;
437 isAutoIncrement
=true;
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) {
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;
464 fldDef
.defaultValueBuf_
[0]='\0';
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
);
476 void ParsedData::insertFldDef()
478 DbRetVal rv
= creFldList
.append(fldDef
);
482 Expression
* ParsedData::insertExpression(char *fldName
)
484 Expression
*exp
=new Expression();
485 exp
->setExpr(fldName
);
488 Expression
* ParsedData::insertExpression(char *value
,bool flag
)
490 Expression
*exp
=new Expression();
491 exp
->setExpr(strdup(value
),flag
);
494 Expression
* ParsedData::insertExpression(Expression
* exp1
, ArithOperator op
,Expression
* exp2
)
496 Expression
*exp
=new Expression();
497 exp
->setExpr(exp1
, op
, exp2
);
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
;
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
;