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 ***************************************************************************/
16 #include "Statement.h"
19 InsStatement::InsStatement()
29 InsStatement::~InsStatement()
39 DbRetVal
InsStatement::getParamFldInfo(int paramPos
, FieldInfo
*&info
)
41 if (paramPos
>totalParams
) return ErrBadCall
;
42 FieldValue
*value
= (FieldValue
*)params
[paramPos
-1];
45 printError(ErrSysFatal
, "param ptr is null: should never happen\n");
48 info
->type
= value
->type
;
49 info
->length
= value
->length
;
53 DbRetVal
InsStatement::execute(int &rowsAffected
)
56 //copy param values to binded buffer
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");
67 AllDataType::copyVal(value
->value
, paramValues
[i
], value
->type
, value
->length
);
69 rv
= table
->insertTuple();
70 if (rv
==OK
) rowsAffected
= 1;
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
;
82 DbRetVal
InsStatement::setShortParam(int paramNo
, short value
)
84 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
85 FieldValue
*fValue
= (FieldValue
*) params
[paramNo
-1];
88 printError(ErrSysFatal
, "field value is null. Should never happen");
91 *(short*)fValue
->value
= value
;
95 DbRetVal
InsStatement::setIntParam(int paramNo
, int value
)
97 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
98 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
101 printError(ErrSysFatal
, "condition value is null. Should never happen");
105 *(int*)cValue
->value
= value
;
108 DbRetVal
InsStatement::setLongParam(int paramNo
, long value
)
110 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
111 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
114 printError(ErrSysFatal
, "condition value is null. Should never happen");
117 *(long*)cValue
->value
= value
;
121 DbRetVal
InsStatement::setLongLongParam(int paramNo
, long long value
)
123 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
124 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
127 printError(ErrSysFatal
, "condition value is null. Should never happen");
130 *(long long*)cValue
->value
= value
;
133 DbRetVal
InsStatement::setByteIntParam(int paramNo
, ByteInt value
)
135 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
136 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
139 printError(ErrSysFatal
, "condition value is null. Should never happen");
142 *(ByteInt
*)cValue
->value
= value
;
145 DbRetVal
InsStatement::setFloatParam(int paramNo
, float value
)
147 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
148 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
151 printError(ErrSysFatal
, "condition value is null. Should never happen");
154 *(float*)cValue
->value
= value
;
157 DbRetVal
InsStatement::setDoubleParam(int paramNo
, double value
)
159 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
160 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
163 printError(ErrSysFatal
, "condition value is null. Should never happen");
166 *(double*)cValue
->value
= value
;
169 DbRetVal
InsStatement::setStringParam(int paramNo
, char *value
)
171 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
172 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
175 printError(ErrSysFatal
, "condition value is null. Should never happen");
178 //TODO::check if the strlen(value) is greater than field length
180 strcpy((char*)cValue
->value
, value
);
183 DbRetVal
InsStatement::setDateParam(int paramNo
, Date value
)
185 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
186 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
189 printError(ErrSysFatal
, "condition value is null. Should never happen");
192 *(Date
*)cValue
->value
= value
;
195 DbRetVal
InsStatement::setTimeParam(int paramNo
, Time value
)
197 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
198 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
201 printError(ErrSysFatal
, "condition value is null. Should never happen");
204 *(Time
*)cValue
->value
= value
;
207 DbRetVal
InsStatement::setTimeStampParam(int paramNo
, TimeStamp value
)
209 if (paramNo
<=0 || paramNo
> totalParams
) return ErrBadArg
;
210 FieldValue
*cValue
= (FieldValue
*) params
[paramNo
-1];
213 printError(ErrSysFatal
, "condition value is null. Should never happen");
216 *(TimeStamp
*)cValue
->value
= value
;
220 DbRetVal
InsStatement::resolve()
222 if (dbMgr
== NULL
) return ErrNoConnection
;
223 //check whether the table exists
224 table
= dbMgr
->openTable(parsedData
->getTableName());
227 printError(ErrNotExists
, "Unable to open the table:Table not exists");
232 //check whether filed list is specified
233 if( 0 == parsedData
->getFieldNameList().size() )
234 fieldNameList
= table
->getFieldNameList();
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
);
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();
256 while (iter
.hasElement())
258 name
= (FieldName
*)iter
.nextElement();
259 value
= (FieldValue
*) valIter
.nextElement();
260 if (NULL
== name
|| NULL
== value
)
262 dbMgr
->closeTable(table
);
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
);
274 printError(ErrSyntaxError
, "Field %s does not exist in table",
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
++;
288 AllDataType::strToValue(value
->value
, value
->parsedString
, fInfo
->type
, value
->length
);
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*));
298 while(valIter
.hasElement())
300 value
= (FieldValue
*) valIter
.nextElement();
303 dbMgr
->closeTable(table
);
305 free(params
); params
= NULL
;
306 free(paramValues
); paramValues
= NULL
;
307 printError(ErrSysFatal
, "Should never happen. value NULL after iteration");
310 if (value
->paramNo
== 0) continue;
311 params
[value
->paramNo
-1 ] = value
;