NULL fixes through network and SetBinaryParam with int argument added
[csql.git] / src / network / NetworkPacket.cxx
blob1c93f4c22dac8561fba25bdfccebf7a4dbd10555
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 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <os.h>
21 #include <CSql.h>
22 #include <Network.h>
23 #include <DataType.h>
24 #include <SqlLogStatement.h>
25 #include <SqlNwStatement.h>
26 #include <SqlNetworkHandler.h>
28 DbRetVal PacketPrepare::marshall()
30 printDebug(DM_Network, "PacketPrepare::marshall called\n");
31 bufferSize = sizeof(int) * 4 + strlen(stmtString) + 1;
32 printDebug(DM_Network, "NOOFPARAMS %d buffer size %d\n", noParams, bufferSize);
33 printDebug(DM_Network, "stmt %s size %d\n", stmtString, strlen(stmtString));
34 printDebug(DM_Network, "noParams is %d\n", noParams);
35 if (noParams >0)
36 bufferSize = bufferSize + 2 * sizeof(int) * noParams;
37 buffer = (char*) malloc(bufferSize);
38 *(int*)buffer = stmtID;
39 char *bufIter = buffer + sizeof(int);
40 *(int*)bufIter = syncMode;
41 bufIter = bufIter + sizeof(int);
42 *(int*)bufIter = strlen(stmtString);
43 bufIter = bufIter + sizeof(int);
44 *(int*)bufIter = noParams;
45 bufIter = bufIter + sizeof(int);
46 if (noParams >0) {
47 memcpy(bufIter, type, sizeof(int) * noParams);
48 bufIter = bufIter + sizeof(int)* noParams;
49 memcpy(bufIter, length, sizeof(int) * noParams);
50 bufIter = bufIter + sizeof(int)* noParams;
52 strcpy(bufIter, stmtString);
53 printDebug(DM_Network, "PacketPrepare::marshall ended\n");
54 return OK;
56 DbRetVal PacketPrepare::unmarshall()
58 printDebug(DM_Network, "PacketPrepare::unmarshall called\n");
59 stmtID = *(int*)buffer;
60 printDebug(DM_Network, "start of the buffer is %x\n", buffer);
61 char *bufIter = buffer + sizeof (int);
62 syncMode = *(int*)bufIter;
63 bufIter = bufIter + sizeof(int);
64 stmtLength = *(int*)bufIter;
65 bufIter = bufIter + sizeof(int);
66 noParams = *(int*)bufIter;
67 bufIter = bufIter + sizeof(int);
68 if (noParams >0) {
69 type = (int*) bufIter;
70 bufIter = bufIter + sizeof(int) * noParams;
71 length = (int*) bufIter;
72 bufIter = bufIter + sizeof(int) * noParams;
74 stmtString = bufIter;
75 printDebug(DM_Network, "stmtString ptr is %x\n", stmtString);
76 stmtString[stmtLength+1] = '\0';
77 printDebug(DM_Network, "PacketPrepare::unmarshall ended\n");
78 return OK;
80 DbRetVal PacketFree::marshall()
82 bufferSize = sizeof(int);
83 buffer = (char*) malloc(bufferSize);
84 *(int*)buffer = stmtID;
85 return OK;
87 DbRetVal PacketFree::unmarshall()
89 stmtID = *(int*)buffer;
90 return OK;
92 void PacketExecute::setParams(List list)
94 paramList = list;
95 noParams = list.size();
96 paramValues = new char*[noParams];
97 BindSqlField* bindField = NULL;
98 for (int i = 0 ; i < noParams; i++)
100 bindField = (BindSqlField*) paramList.get(i+1);
101 paramValues[i] = (char*) bindField->value;
103 return;
106 SqlPacketExecute::SqlPacketExecute()
108 buffer=NULL; bufferSize =0; pktType = NW_PKT_EXECUTE;
109 paramValues = NULL; nullInfo = NULL;
111 SqlPacketExecute::~SqlPacketExecute()
113 if(noParams >= 10) delete [] paramValues;
114 nullInfo = NULL;
115 free(buffer);
116 bufferSize =0;
117 buffer = NULL;
119 void SqlPacketExecute::setParams(List list)
121 paramList = list;
122 noParams = list.size();
123 if (!noParams) return;
124 if (noParams <10)
125 paramValues = localBuf;
126 else
127 paramValues = new char*[noParams];
128 BindSqlField* bindField = NULL;
129 for (int i = 0 ; i < noParams; i++)
131 bindField = (BindSqlField*) paramList.get(i+1);
132 paramValues[i] = (char*) bindField->value;
134 return;
137 void SqlPacketResultSet::setProjList(List list)
139 projList = list;
140 noProjs = list.size();
141 projValues = new char*[noProjs];
142 BindSqlProjectField* prjFld = NULL;
143 for (int i = 0 ; i < noProjs; i++)
145 prjFld = (BindSqlProjectField*) projList.get(i+1);
146 projValues[i] = (char*) prjFld->value;
148 return;
151 void PacketExecute::setStatementList(List stmtlist)
153 stmtList = stmtlist;
154 return;
157 void SqlPacketExecute::setStatementList(List stmtlist)
159 stmtList = stmtlist;
160 return;
163 DbRetVal PacketExecute::marshall()
165 bufferSize = sizeof(int)+ sizeof(int);
166 BindSqlField* bindField = NULL;
167 for (int i = 0 ; i < noParams; i++)
169 bindField = (BindSqlField*) paramList.get(i+1);
170 bufferSize = bufferSize + AllDataType::size(bindField->type, bindField->length);
172 buffer = (char*) malloc(bufferSize);
173 *(int*)buffer = stmtID;
174 char* bufIter = (char*) buffer + sizeof(int);
175 *(int*)bufIter = noParams;
176 bufIter = (char*) bufIter + sizeof(int);
177 for (int i = 0 ; i < noParams; i++)
179 bindField = (BindSqlField*) paramList.get(i+1);
180 AllDataType::copyVal(bufIter, bindField->value, bindField->type,bindField->length);
181 bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
183 return OK;
185 DbRetVal PacketExecute::unmarshall()
187 stmtID = *(int*)buffer;
188 char *bufIter = buffer + sizeof(int);
189 noParams = *(int*)bufIter;
190 bufIter = bufIter +sizeof(int);
191 ListIterator stmtIter = stmtList.getIterator();
192 NetworkStmt *stmt;
193 while (stmtIter.hasElement())
195 stmt = (NetworkStmt*) stmtIter.nextElement();
196 //TODO::Also check teh srcNetworkID
197 if (stmt->stmtID == stmtID ) break;
199 if (noParams == 0) return OK;
200 paramValues = new char*[noParams];
201 ListIterator paramIter = stmt->paramList.getIterator();
202 BindSqlField *bindField = NULL;
203 for (int i=0; i <noParams; i++)
205 paramValues[i] = bufIter;
206 bindField = (BindSqlField*) stmt->paramList.get(i+1);
207 bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
209 return OK;
212 void PacketCommit::setExecPackets(int tid, List list)
214 txnID = tid;
215 noOfStmts = list.size();
216 stmtBufSize = new int[noOfStmts];
217 stmtBuffer = new char*[noOfStmts];
218 PacketExecute* pkt = NULL;
219 int totalSize =0;
220 for (int i = 0 ; i < noOfStmts; i++)
222 pkt = (PacketExecute*) list.get(i+1);
223 if (pkt == NULL) printError(ErrSysFatal, "pkt is null.should never happen\n");
224 stmtBufSize[i] = pkt->getBufferSize();
225 stmtBuffer[i] = pkt->getMarshalledBuffer();
226 // *(int*)(((char*)stmtBuffer[i]) + 4));
227 totalSize = totalSize + stmtBufSize[i];
229 totalSize = sizeof(int) + sizeof(int) + noOfStmts * sizeof(int) +
230 totalSize;
231 bufferSize = totalSize;
232 return;
234 DbRetVal PacketCommit::marshall()
236 buffer = (char*) malloc(bufferSize);
237 *(int*)buffer = txnID;
238 char* bufIter = (char*) buffer + sizeof(int);
239 *(int*)bufIter = noOfStmts;
240 bufIter = (char*) bufIter + sizeof(int);
241 memcpy(bufIter, stmtBufSize, noOfStmts*sizeof(int));
242 bufIter = (char*) bufIter + noOfStmts* sizeof(int);
243 for (int i=0; i < noOfStmts; i++)
245 memcpy(bufIter, stmtBuffer[i], stmtBufSize[i]);
246 bufIter = bufIter + stmtBufSize[i];
248 return OK;
250 DbRetVal PacketCommit::unmarshall()
252 txnID = *(int*)buffer;
253 char *bufIter = buffer + sizeof(int);
254 noOfStmts = *(int*)bufIter;
255 bufIter = bufIter + sizeof(int);
256 stmtBufSize = new int[noOfStmts];
257 memcpy(stmtBufSize, bufIter, noOfStmts*sizeof(int));
258 bufIter = bufIter + noOfStmts * sizeof(int);
259 stmtBuffer = new char*[noOfStmts];
260 for (int i = 0 ; i <noOfStmts; i++)
262 stmtBuffer[i] = bufIter;
263 bufIter = bufIter + stmtBufSize[i];
266 //call unmarshall before calling this
267 void PacketCommit::getExecPacketList(List stmtList, List &list)
269 PacketExecute* pkt = NULL;
270 for (int i = 0 ; i < noOfStmts; i++)
272 pkt = new PacketExecute();
273 pkt->setBuffer(stmtBuffer[i]);
274 pkt->setBufferSize(stmtBufSize[i]);
275 pkt->setStatementList(stmtList);
276 pkt->unmarshall();
277 list.append(pkt);
281 DbRetVal SqlPacketConnect::marshall()
283 char *ptr = buffer; // moves over buffer
284 strncpy(ptr, userName, IDENTIFIER_LENGTH);
285 ptr = buffer+IDENTIFIER_LENGTH-1;
286 *ptr++ = '\0';
287 strncpy(ptr, passWord, IDENTIFIER_LENGTH);
288 ptr = ptr + IDENTIFIER_LENGTH-1;
289 *ptr++ = '\0';
290 *ptr = sqlApiImplType;
291 return OK;
294 DbRetVal SqlPacketConnect::unmarshall()
296 char *ptr = buffer;
297 strncpy(userName, ptr, IDENTIFIER_LENGTH);
298 strncpy(passWord, ptr + IDENTIFIER_LENGTH, IDENTIFIER_LENGTH);
299 sqlApiImplType = *(ptr + 2 * IDENTIFIER_LENGTH);
300 return OK;
303 DbRetVal SqlPacketPrepare::marshall()
305 printDebug(DM_Network, "PacketPrepare::marshall called\n");
306 bufferSize = sizeof(int) + stmtLength;
307 printDebug(DM_Network, "Buffer size %d\n", bufferSize);
308 printDebug(DM_Network, "stmt %s size %d\n", stmtString, strlen(stmtString));
309 buffer = (char*) malloc(bufferSize);
310 *(int*)buffer = stmtLength;
311 char *bufIter = buffer + sizeof(int);
312 strcpy(bufIter, stmtString);
313 bufIter[strlen(stmtString)] = '\0';
314 printDebug(DM_Network, "PacketPrepare::marshall ended\n");
315 return OK;
318 DbRetVal SqlPacketPrepare::unmarshall()
320 printDebug(DM_Network, "PacketPrepare::unmarshall called\n");
321 printDebug(DM_Network, "start of the buffer is %x\n", buffer);
322 stmtLength = *(int*)buffer;
323 char *bufIter = buffer + sizeof(int);
324 stmtString = bufIter;
325 printDebug(DM_Network, "stmtString ptr is %x\n", stmtString);
326 stmtString[strlen(stmtString)] = '\0';
327 printDebug(DM_Network, "PacketPrepare::unmarshall ended\n");
328 return OK;
331 // the following marshall and unmarshall function is modified to accommodate
332 // the fix for MySQL Bug:1382
333 // SQLDescribeParam returns the same type information
334 // by sending type and length in exec packets.
335 DbRetVal SqlPacketExecute::marshall()
337 bufferSize = sizeof(int)+ sizeof(int);
338 BindSqlField* bindField = NULL;
339 for (int i = 0 ; i < noParams; i++)
341 bufferSize = bufferSize + 2 * sizeof(int);
342 bindField = (BindSqlField*) paramList.get(i+1);
343 if (bindField->type == typeBinary)
344 bufferSize = bufferSize + 2 * AllDataType::size(bindField->type, bindField->length);
345 else
346 bufferSize = bufferSize + AllDataType::size(bindField->type, bindField->length);
348 //allocate null info for setNull info for dml parameterized statements.
349 if (noParams) bufferSize += os::align(noParams);
350 buffer = (char*) malloc(bufferSize);
351 *(int*)buffer = stmtID;
352 char* bufIter = (char*) buffer + sizeof(int);
353 *(int*)bufIter = noParams;
354 bufIter = (char*) bufIter + sizeof(int);
355 for (int i = 0 ; i < noParams; i++)
357 bindField = (BindSqlField*) paramList.get(i+1);
358 if (bindField->type == typeBinary) {
359 *(int *)bufIter = typeBinary;
360 bufIter = (char*) bufIter + sizeof(int);
361 *(int *)bufIter = AllDataType::size(bindField->type, bindField->length);
362 bufIter = (char*) bufIter + sizeof(int);
363 AllDataType::copyVal(bufIter, bindField->value, bindField->type, 2 * bindField->length);
364 bufIter = bufIter + 2 * AllDataType::size(bindField->type, bindField->length);
365 } else {
366 *(int *)bufIter = bindField->type;
367 bufIter = (char*) bufIter + sizeof(int);
368 *(int *)bufIter = AllDataType::size(bindField->type, bindField->length);
369 bufIter = (char*) bufIter + sizeof(int);
370 AllDataType::copyVal(bufIter, bindField->value, bindField->type, bindField->length);
371 bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
374 if(noParams) memcpy(bufIter, nullInfo, os::align(noParams));
375 return OK;
378 DbRetVal SqlPacketExecute::unmarshall()
380 stmtID = *(int*)buffer;
381 char *bufIter = buffer + sizeof(int);
382 noParams = *(int*)bufIter;
383 bufIter = bufIter +sizeof(int);
384 ListIterator stmtIter = stmtList.getIterator();
385 NetworkStmt *stmt;
386 while (stmtIter.hasElement())
388 stmt = (NetworkStmt*) stmtIter.nextElement();
389 //TODO::Also check teh srcNetworkID
390 if (stmt->stmtID == stmtID ) break;
392 if (noParams == 0) return OK;
393 paramValues = new char*[noParams];
394 ListIterator paramIter = stmt->paramList.getIterator();
395 BindSqlField *bindField = NULL;
396 for (int i=0; i <noParams; i++)
398 bindField = (BindSqlField*) stmt->paramList.get(i+1);
399 bindField->type = (DataType) *(int *)bufIter;
400 bufIter = (char*) bufIter + sizeof(int);
401 bindField->length = *(int *)bufIter;
402 bufIter = (char*) bufIter + sizeof(int);
403 paramValues[i] = bufIter;
404 bindField->value = paramValues[i];
405 if (bindField->type == typeBinary)
406 bufIter = bufIter + 2 * AllDataType::size(bindField->type, bindField->length);
407 else
408 bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
410 nullInfo = (char*) malloc(os::align(noParams));
411 memcpy(nullInfo, bufIter, os::align(noParams));
412 return OK;
415 DbRetVal SqlPacketParamMetadata::marshall()
417 printDebug(DM_Network, "SqlPacketParamMetadata::marshall called\n");
418 bufferSize = sizeof(int) * 2;
419 printDebug(DM_Network, "NOOFPARAMS %d buffer size %d\n", noParams, bufferSize);
420 printDebug(DM_Network, "noParams is %d\n", noParams);
421 if (noParams >0)
422 bufferSize = bufferSize + sizeof(FieldInfo) * noParams;
423 buffer = (char*) malloc(bufferSize);
424 *(int*)buffer = stmtID;
425 char *bufIter = buffer + sizeof(int);
426 *(int*)bufIter = noParams;
427 bufIter = bufIter + sizeof(int);
428 ListIterator stmtIter = SqlNetworkHandler::stmtList.getIterator();
429 NetworkStmt *stmt;
430 while (stmtIter.hasElement())
432 stmt = (NetworkStmt*) stmtIter.nextElement();
433 if (stmt->stmtID == stmtID ) break;
435 FieldInfo *fldInfo = new FieldInfo();
436 for (int i=0; i <noParams; i++) {
437 BindSqlField *bindField = (BindSqlField*) stmt->paramList.get(i+1);
438 strcpy(fldInfo->fldName, bindField->fName);
439 fldInfo->type = bindField->type;
440 fldInfo->length = bindField->length;
441 fldInfo->offset = bindField->offset;
442 fldInfo->isNull = bindField->isNull;
443 fldInfo->isPrimary = bindField->isPrimary;
444 fldInfo->isDefault = bindField->isDefault;
445 fldInfo->isUnique = bindField->isUnique;
446 strcpy(fldInfo->defaultValueBuf, bindField->defaultValueBuf);
447 *(FieldInfo *) bufIter = *fldInfo;
448 bufIter += sizeof(FieldInfo);
450 delete fldInfo;
451 printDebug(DM_Network, "SqlPacketParamMetadata::marshall ended\n");
452 return OK;
455 DbRetVal SqlPacketParamMetadata::unmarshall()
457 printDebug(DM_Network, "SqlPacketParamMetadata::unmarshall called\n");
458 stmtID = *(int*)buffer;
459 printDebug(DM_Network, "start of the buffer is %x\n", buffer);
460 char *bufIter = buffer + sizeof (int);
461 noParams = *(int*)bufIter;
462 bufIter = bufIter + sizeof(int);
463 if(noParams > 0) data = bufIter;
464 printDebug(DM_Network, "SqlPacketParamMetadata::unmarshall ended\n");
465 return OK;
468 DbRetVal SqlPacketProjMetadata::marshall()
470 printDebug(DM_Network, "SqlPacketParamMetadata::marshall called\n");
471 bufferSize = sizeof(int) * 2;
472 printDebug(DM_Network, "NOOFPROJS %d buffer size %d\n", noProjs, bufferSize);
473 printDebug(DM_Network, "noParams is %d\n", noProjs);
474 if (noProjs >0)
475 bufferSize = bufferSize + sizeof(FieldInfo) * noProjs;
476 buffer = (char*) malloc(bufferSize);
477 *(int*)buffer = stmtID;
478 char *bufIter = buffer + sizeof(int);
479 *(int*)bufIter = noProjs;
480 bufIter = bufIter + sizeof(int);
481 ListIterator stmtIter = SqlNetworkHandler::stmtList.getIterator();
482 NetworkStmt *stmt;
483 while (stmtIter.hasElement())
485 stmt = (NetworkStmt*) stmtIter.nextElement();
486 if (stmt->stmtID == stmtID ) break;
488 FieldInfo *fldInfo = new FieldInfo();
489 for (int i=0; i < noProjs; i++) {
490 BindSqlProjectField *bindField = (BindSqlProjectField*) stmt->projList.get(i+1);
491 strcpy(fldInfo->fldName, bindField->fName);
492 fldInfo->type = bindField->type;
493 fldInfo->length = bindField->length;
494 fldInfo->offset = bindField->offset;
495 fldInfo->isNull = bindField->isNull;
496 fldInfo->isPrimary = bindField->isPrimary;
497 fldInfo->isDefault = bindField->isDefault;
498 fldInfo->isUnique = bindField->isUnique;
499 strcpy(fldInfo->defaultValueBuf, bindField->defaultValueBuf);
500 *(FieldInfo *) bufIter = *fldInfo;
501 bufIter += sizeof(FieldInfo);
503 delete fldInfo;
504 printDebug(DM_Network, "SqlPacketParamMetadata::marshall ended\n");
505 return OK;
508 DbRetVal SqlPacketProjMetadata::unmarshall()
510 printDebug(DM_Network, "SqlPacketProjMetadata::unmarshall called\n");
511 stmtID = *(int*)buffer;
512 printDebug(DM_Network, "start of the buffer is %x\n", buffer);
513 char *bufIter = buffer + sizeof (int);
514 noProjs = *(int*)bufIter;
515 bufIter = bufIter + sizeof(int);
516 if(noProjs > 0) data = bufIter;
517 printDebug(DM_Network, "SqlPacketProjMetadata::unmarshall ended\n");
518 return OK;
521 DbRetVal SqlPacketFetch::marshall()
523 bufferSize = sizeof(int);
524 buffer = (char*) malloc(bufferSize);
525 *(int*)buffer = stmtID;
526 return OK;
529 DbRetVal SqlPacketFetch::unmarshall()
531 stmtID = *(int *)buffer;
534 DbRetVal SqlPacketFree::marshall()
536 bufferSize = sizeof(int);
537 buffer = (char*) malloc(bufferSize);
538 *(int*)buffer = stmtID;
539 return OK;
542 DbRetVal SqlPacketFree::unmarshall()
544 stmtID = *(int *)buffer;
547 DbRetVal SqlPacketResultSet::marshall()
549 bufferSize = sizeof(int)+ sizeof(int);
550 BindSqlProjectField* prjFld = NULL;
551 for (int i = 0 ; i < noProjs; i++)
553 prjFld = (BindSqlProjectField*) projList.get(i+1);
554 bufferSize = bufferSize + AllDataType::size(prjFld->type, prjFld->length);
556 //allocate buffer for nullInfo
557 bufferSize = bufferSize + os::align(noProjs);
558 buffer = (char*) malloc(bufferSize);
559 *(int*)buffer = hasData;
560 char* bufIter = (char*) buffer + sizeof(int);
561 *(int*)bufIter = noProjs;
562 bufIter = (char*) bufIter + sizeof(int);
563 for (int i = 0 ; i < noProjs; i++)
565 prjFld = (BindSqlProjectField*) projList.get(i+1);
566 AllDataType::copyVal(bufIter, prjFld->value, prjFld->type, prjFld->length);
567 bufIter = bufIter + AllDataType::size(prjFld->type, prjFld->length);
569 memcpy(bufIter, nullInfo, os::align(noProjs));
570 return OK;
573 DbRetVal SqlPacketResultSet::unmarshall()
575 hasData = *(int*)buffer;
576 char *bufIter = buffer + sizeof(int);
577 noProjs = *(int*)bufIter;
578 bufIter = bufIter + sizeof(int);
579 BindSqlProjectField *prjFld = NULL;
580 for (int i=0; i <noProjs; i++)
582 prjFld = (BindSqlProjectField*) projList.get(i+1);
583 AllDataType::copyVal(prjFld->value, bufIter, prjFld->type, prjFld->length);
584 bufIter = bufIter + AllDataType::size(prjFld->type, prjFld->length);
586 memcpy(nullInfo, bufIter, os::align(noProjs));
587 return OK;
590 DbRetVal SqlPacketShowTables::marshall()
592 bufferSize = numOfTables * IDENTIFIER_LENGTH;
593 buffer = (char*) malloc(bufferSize);
594 char *bufIter = buffer;
595 Identifier *elem = NULL;
596 ListIterator tblIter = SqlNetworkHandler::tableNameList.getIterator();
597 while (tblIter.hasElement()) {
598 elem = (Identifier*) tblIter.nextElement();
599 strncpy(bufIter, elem->name, IDENTIFIER_LENGTH);
600 bufIter += IDENTIFIER_LENGTH;
602 return OK;
605 DbRetVal SqlPacketShowTables::unmarshall()
607 data = buffer;
608 return OK;