typo in line 495. Fix for Bug: 1943951
[csql.git] / src / sql / InsStatement.cxx
blobb87aaf52bab231a4268c7f22cdc5460621281f81
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 "Statement.h"
17 #include <Info.h>
19 InsStatement::InsStatement()
21 parsedData = NULL;
22 dbMgr = NULL;
23 table = NULL;
24 params = NULL;
25 paramValues = NULL;
26 totalParams = 0;
29 InsStatement::~InsStatement()
31 if (totalParams) {
32 free(params);
33 params = NULL;
34 free(paramValues);
35 paramValues = NULL;
39 DbRetVal InsStatement::getParamFldInfo(int paramPos, FieldInfo *&info)
41 if (paramPos >totalParams ) return ErrBadCall;
42 FieldValue *value = (FieldValue*)params[paramPos-1];
43 if (value == NULL)
45 printError(ErrSysFatal, "param ptr is null: should never happen\n");
46 return ErrBadArg;
48 info->type = value->type;
49 info->length = value->length;
50 return OK;
53 DbRetVal InsStatement::execute(int &rowsAffected)
55 DbRetVal rv = OK;
56 //copy param values to binded buffer
57 FieldValue *value;
58 for (int i = 0; i < totalParams; i ++)
60 value = (FieldValue*) params[i];
61 if (paramValues[i] == NULL)
63 //printError(ErrBadCall, "param values not set");
64 continue;
65 //return ErrBadCall;
67 AllDataType::copyVal(value->value, paramValues[i], value->type, value->length);
69 rv = table->insertTuple();
70 if (rv ==OK) rowsAffected = 1;
71 return rv;
74 DbRetVal InsStatement::setParam(int paramNo, void *value)
76 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
77 if (NULL == value) return ErrBadArg;
78 paramValues[paramNo -1] = (char*) value;
79 return OK;
82 DbRetVal InsStatement::setShortParam(int paramNo, short value)
84 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
85 FieldValue *fValue = (FieldValue*) params [paramNo-1];
86 if (NULL == fValue)
88 printError(ErrSysFatal, "field value is null. Should never happen");
89 return ErrSysFatal;
91 *(short*)fValue->value = value;
92 return OK;
95 DbRetVal InsStatement::setIntParam(int paramNo, int value)
97 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
98 FieldValue *cValue = (FieldValue*) params [paramNo-1];
99 if (NULL == cValue)
101 printError(ErrSysFatal, "condition value is null. Should never happen");
102 return ErrSysFatal;
105 *(int*)cValue->value = value;
106 return OK;
108 DbRetVal InsStatement::setLongParam(int paramNo, long value)
110 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
111 FieldValue *cValue = (FieldValue*) params [paramNo-1];
112 if (NULL == cValue)
114 printError(ErrSysFatal, "condition value is null. Should never happen");
115 return ErrSysFatal;
117 *(long*)cValue->value = value;
118 return OK;
121 DbRetVal InsStatement::setLongLongParam(int paramNo, long long value)
123 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
124 FieldValue *cValue = (FieldValue*) params [paramNo-1];
125 if (NULL == cValue)
127 printError(ErrSysFatal, "condition value is null. Should never happen");
128 return ErrSysFatal;
130 *(long long*)cValue->value = value;
131 return OK;
133 DbRetVal InsStatement::setByteIntParam(int paramNo, ByteInt value)
135 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
136 FieldValue *cValue = (FieldValue*) params [paramNo-1];
137 if (NULL == cValue)
139 printError(ErrSysFatal, "condition value is null. Should never happen");
140 return ErrSysFatal;
142 *(ByteInt*)cValue->value = value;
143 return OK;
145 DbRetVal InsStatement::setFloatParam(int paramNo, float value)
147 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
148 FieldValue *cValue = (FieldValue*) params [paramNo-1];
149 if (NULL == cValue)
151 printError(ErrSysFatal, "condition value is null. Should never happen");
152 return ErrSysFatal;
154 *(float*)cValue->value = value;
155 return OK;
157 DbRetVal InsStatement::setDoubleParam(int paramNo, double value)
159 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
160 FieldValue *cValue = (FieldValue*) params [paramNo-1];
161 if (NULL == cValue)
163 printError(ErrSysFatal, "condition value is null. Should never happen");
164 return ErrSysFatal;
166 *(double*)cValue->value = value;
167 return OK;
169 DbRetVal InsStatement::setStringParam(int paramNo, char *value)
171 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
172 FieldValue *cValue = (FieldValue*) params [paramNo-1];
173 if (NULL == cValue)
175 printError(ErrSysFatal, "condition value is null. Should never happen");
176 return ErrSysFatal;
178 //TODO::check if the strlen(value) is greater than field length
179 //do in all stmts
180 strcpy((char*)cValue->value, value);
181 return OK;
183 DbRetVal InsStatement::setDateParam(int paramNo, Date value)
185 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
186 FieldValue *cValue = (FieldValue*) params [paramNo-1];
187 if (NULL == cValue)
189 printError(ErrSysFatal, "condition value is null. Should never happen");
190 return ErrSysFatal;
192 *(Date*)cValue->value = value;
193 return OK;
195 DbRetVal InsStatement::setTimeParam(int paramNo, Time value)
197 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
198 FieldValue *cValue = (FieldValue*) params [paramNo-1];
199 if (NULL == cValue)
201 printError(ErrSysFatal, "condition value is null. Should never happen");
202 return ErrSysFatal;
204 *(Time*)cValue->value = value;
205 return OK;
207 DbRetVal InsStatement::setTimeStampParam(int paramNo, TimeStamp value)
209 if (paramNo <=0 || paramNo > totalParams) return ErrBadArg;
210 FieldValue *cValue = (FieldValue*) params [paramNo-1];
211 if (NULL == cValue)
213 printError(ErrSysFatal, "condition value is null. Should never happen");
214 return ErrSysFatal;
216 *(TimeStamp*)cValue->value = value;
217 return OK;
220 DbRetVal InsStatement::resolve()
222 if (dbMgr == NULL) return ErrNoConnection;
223 //check whether the table exists
224 table = dbMgr->openTable(parsedData->getTableName());
225 if (table == NULL)
227 printError(ErrNotExists, "Unable to open the table:Table not exists");
228 return ErrNotExists;
231 List fieldNameList;
232 //check whether filed list is specified
233 if( 0 == parsedData->getFieldNameList().size() )
234 fieldNameList = table->getFieldNameList();
235 else
236 fieldNameList = parsedData->getFieldNameList();
238 //check whether the total number of field name in the list is same as the total
239 //number of values provided in the values list.
240 if ( fieldNameList.size() !=
241 parsedData->getFieldValueList().size())
243 dbMgr->closeTable(table);
244 table = NULL;
245 printError(ErrSyntaxError, "Field Name list and field values do not match");
246 return ErrSyntaxError;
249 //get the fieldname list and validate field names
250 ListIterator iter = fieldNameList.getIterator();
251 ListIterator valIter = parsedData->getFieldValueList().getIterator();
252 FieldName *name; FieldValue *value;
253 FieldInfo *fInfo = new FieldInfo();
254 int paramPos =1;
255 DbRetVal rv = OK;
256 while (iter.hasElement())
258 name = (FieldName*)iter.nextElement();
259 value = (FieldValue*) valIter.nextElement();
260 if (NULL == name || NULL == value)
262 dbMgr->closeTable(table);
263 table = NULL;
264 delete fInfo;
265 printError(ErrSyntaxError, "Field Name list and field values do not match");
266 return ErrSyntaxError;
268 rv = table->getFieldInfo(name->fldName, fInfo);
269 if (ErrNotFound == rv)
271 dbMgr->closeTable(table);
272 table = NULL;
273 delete fInfo;
274 printError(ErrSyntaxError, "Field %s does not exist in table",
275 name->fldName);
276 return ErrSyntaxError;
278 value->type = fInfo->type;
279 value->length = fInfo->length;
280 value->value = AllDataType::alloc(fInfo->type, fInfo->length);
281 if (value->parsedString == NULL) continue;
282 table->bindFld(name->fldName, value->value);
283 if (value->parsedString[0] == '?')
285 value->paramNo = paramPos++;
287 if (!value->paramNo)
288 AllDataType::strToValue(value->value, value->parsedString, fInfo->type, value->length);
290 delete fInfo;
291 totalParams = paramPos -1;
292 if (0 == totalParams) return OK;
293 params = (void**) malloc ( totalParams * sizeof(FieldValue*));
294 paramValues = (char**) malloc( totalParams * sizeof(char*));
295 memset(params, 0, totalParams * sizeof(FieldValue*));
296 memset(paramValues, 0, totalParams * sizeof(char*));
297 valIter.reset();
298 while(valIter.hasElement())
300 value = (FieldValue*) valIter.nextElement();
301 if (value == NULL)
303 dbMgr->closeTable(table);
304 table = NULL;
305 free(params); params = NULL;
306 free(paramValues); paramValues = NULL;
307 printError(ErrSysFatal, "Should never happen. value NULL after iteration");
308 return ErrSysFatal;
310 if (value->paramNo == 0) continue;
311 params[value->paramNo -1 ] = value;
313 return OK;