Fixing leaks and JDBCTest performance improvement
[csql.git] / src / sql / ParsedData.cxx
blob200b8e9a58290a2feab2c67477a3224775b3e1da
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 "Parser.h"
17 #include <CSql.h>
18 #include<PredicateImpl.h>
21 void ParsedData::insertValue(char *val)
23 FieldValue *newVal = new FieldValue();
24 if (val == NULL)
25 newVal->parsedString = NULL;
26 else
27 newVal->parsedString = strdup(val);
28 strcpy(newVal->fldName," ");
29 newVal->value = NULL;
30 newVal->paramNo = 0;
31 newVal->type = typeUnknown;
32 newVal->length = 0;
33 newVal->isAllocVal=false;
34 fieldValueList.append(newVal);
37 void ParsedData::insertInValue(char *val)
39 FieldValue *newVal = new FieldValue();
40 if (val == NULL)
41 newVal->parsedString = NULL;
42 else
43 newVal->parsedString = strdup(val);
44 strcpy(newVal->fldName," ");
45 newVal->value = NULL;
46 newVal->paramNo = 0;
47 newVal->type = typeUnknown;
48 newVal->length = 0;
49 newVal->isAllocVal=false;
50 inValueList.append(newVal);
53 void** ParsedData::insertCondValueAndGetPtr(char *fldName, char *val, bool opLike)
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->length = 0;
64 strcpy(newVal->fName, fldName);
65 newVal->opLike = opLike;
66 conditionValueList.append(newVal);
67 return &(newVal->value);
71 void ParsedData::insertField(char *fName, AggType type)
73 FieldName *newVal = new FieldName();
74 strcpy(newVal->fldName , fName);
75 newVal->aType = type;
76 fieldNameList.append(newVal);
78 void ParsedData::insertTableName(char *tName)
80 TableName *newVal = new TableName();
81 strcpy(newVal->tblName , tName);
82 tableNameList.append(newVal);
84 void ParsedData::insertGroupField(char *fName)
86 FieldName *newVal = new FieldName();
87 strcpy(newVal->fldName , fName);
88 groupFieldNameList.append(newVal);
91 void ParsedData::insertUpdateValue(char *fName, char *val)
93 UpdateFieldValue *newVal = new UpdateFieldValue();
94 strcpy(newVal->fldName, fName);
95 if (val == NULL)
96 newVal->parsedString = NULL;
97 else
98 newVal->parsedString = strdup(val);
99 newVal->value = NULL;
100 newVal->expre = NULL;
101 newVal->paramNo = 0;
102 updFldValList.append(newVal);
105 Predicate* ParsedData::insertPredicate(char *fName, ComparisionOp op, void **val)
107 PredicateImpl *pImpl = new PredicateImpl();
108 pImpl->setTerm(fName, op, val);
109 return (Predicate*) pImpl;
111 Predicate* ParsedData::insertPredicate(char *fName1, ComparisionOp op, char *fName2)
113 PredicateImpl *pImpl = new PredicateImpl();
114 pImpl->setTerm(fName1, op, fName2);
115 return (Predicate*) pImpl;
118 Predicate* ParsedData::insertPredicate(Predicate *p1, LogicalOp op, Predicate *p2)
120 PredicateImpl *pImpl = new PredicateImpl();
121 pImpl->setTerm(p1, op, p2);
122 return (Predicate*) pImpl;
125 void ParsedData::reset()
127 ListIterator fNameIter = fieldNameList.getIterator();
128 fNameIter.reset();
129 while (fNameIter.hasElement())
130 delete ((FieldName *) fNameIter.nextElement());
131 fieldNameList.reset();
132 ListIterator iter = fieldValueList.getIterator();
133 FieldValue *value;
134 while (iter.hasElement())
136 value = (FieldValue*)iter.nextElement();
137 free(value->parsedString);
138 if (value->isAllocVal) free(value->value);
139 delete value;
141 fieldValueList.reset();
142 inValueList.reset();
143 predicate.reset();
145 iter = conditionValueList.getIterator();
146 ConditionValue *condVal;
147 while (iter.hasElement())
149 condVal = (ConditionValue*)iter.nextElement();
150 free(condVal->parsedString);
151 free(condVal->value);
152 delete condVal;
154 conditionValueList.reset();
156 iter = updFldValList.getIterator();
157 UpdateFieldValue *updFldVal;
158 while (iter.hasElement())
160 updFldVal = (UpdateFieldValue*)iter.nextElement();
161 if(updFldVal->parsedString!=NULL)
162 free(updFldVal->parsedString);
163 if(updFldVal->expre!=NULL)
164 updFldVal->expre->freeVal();
165 free(updFldVal->value);
166 delete updFldVal;
168 updFldValList.reset();
169 groupFieldNameList.reset();
171 iter = tableNameList.getIterator();
172 TableName *tname;
173 while (iter.hasElement())
175 tname = (TableName*)iter.nextElement();
176 delete tname;
178 tableNameList.reset();
180 creFldList.removeAll();
181 isUnique = false;
182 isPrimary = false;
183 indexType = hashIndex;
185 void ParsedData::clearFieldNameList()
187 fieldNameList.reset();
190 void ParsedData::setFldName(char *name)
192 strcpy(fldDef.fldName_, name);
193 fldDef.fldName_[IDENTIFIER_LENGTH] = '\0';
196 void ParsedData::setFldType(DataType type)
198 fldDef.type_ = type;
201 DbRetVal ParsedData::setFldLength(size_t length)
203 if(fldDef.type_ == typeBinary && (length-1) && (length-1) <= 256) {
204 fldDef.length_ = length - 1; return OK;
206 else if (fldDef.type_ == typeBinary && (length-1) > 256) {
207 return ErrBadRange;
209 else { fldDef.length_ = length; return OK; }
212 void ParsedData::setFldNotNull(bool notNull)
214 fldDef.isNull_ = notNull;
216 void ParsedData::setDefaultValue(char *value)
218 fldDef.isDefault_ = true;
219 if (strlen(value) > DEFAULT_VALUE_BUF_LENGTH -1)
221 strncpy(fldDef.defaultValueBuf_, value, DEFAULT_VALUE_BUF_LENGTH -1);
222 fldDef.defaultValueBuf_[DEFAULT_VALUE_BUF_LENGTH] ='\0';
223 } else
224 strcpy(fldDef.defaultValueBuf_, value);
225 return;
229 void ParsedData::insertFldDef()
231 DbRetVal rv = creFldList.append(fldDef);
232 fldDef.init();
235 Expression* ParsedData::insertExpression(char *fldName)
237 Expression *exp =new Expression();
238 exp->setExpr(fldName);
239 return exp;
241 Expression* ParsedData::insertExpression(char *value,bool flag)
243 Expression *exp =new Expression();
244 exp->setExpr(strdup(value),flag);
245 return exp;
247 Expression* ParsedData::insertExpression(Expression* exp1, ArithOperator op ,Expression* exp2)
249 Expression *exp =new Expression();
250 exp->setExpr(exp1, op, exp2);
251 return exp;
253 void ParsedData::insertUpdateExpression(char *fName, Expression *exp)
255 UpdateFieldValue *newVal = new UpdateFieldValue();
256 strcpy(newVal->fldName, fName);
257 newVal->parsedString = NULL;
258 newVal->value = NULL;
259 newVal->expre=exp;
260 newVal->paramNo = 0;
261 updFldValList.append(newVal);