show table through network supported
[csql.git] / src / network / NetworkPacket.cxx
blobfaf01b5458df949c29a73a7a679f668861666994
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 <CSql.h>
21 #include <Network.h>
22 #include <DataType.h>
23 #include <SqlLogStatement.h>
24 #include <SqlNwStatement.h>
25 #include <SqlNetworkHandler.h>
27 DbRetVal PacketPrepare::marshall()
29 printDebug(DM_Network, "PacketPrepare::marshall called\n");
30 bufferSize = sizeof(int) * 4 + strlen(stmtString) + 1;
31 printDebug(DM_Network, "NOOFPARAMS %d buffer size %d\n", noParams, bufferSize);
32 printDebug(DM_Network, "stmt %s size %d\n", stmtString, strlen(stmtString));
33 printDebug(DM_Network, "noParams is %d\n", noParams);
34 if (noParams >0)
35 bufferSize = bufferSize + 2 * sizeof(int) * noParams;
36 buffer = (char*) malloc(bufferSize);
37 *(int*)buffer = stmtID;
38 char *bufIter = buffer + sizeof(int);
39 *(int*)bufIter = syncMode;
40 bufIter = bufIter + sizeof(int);
41 *(int*)bufIter = strlen(stmtString);
42 bufIter = bufIter + sizeof(int);
43 *(int*)bufIter = noParams;
44 bufIter = bufIter + sizeof(int);
45 if (noParams >0) {
46 memcpy(bufIter, type, sizeof(int) * noParams);
47 bufIter = bufIter + sizeof(int)* noParams;
48 memcpy(bufIter, length, sizeof(int) * noParams);
49 bufIter = bufIter + sizeof(int)* noParams;
51 strcpy(bufIter, stmtString);
52 printDebug(DM_Network, "PacketPrepare::marshall ended\n");
53 return OK;
55 DbRetVal PacketPrepare::unmarshall()
57 printDebug(DM_Network, "PacketPrepare::unmarshall called\n");
58 stmtID = *(int*)buffer;
59 printDebug(DM_Network, "start of the buffer is %x\n", buffer);
60 char *bufIter = buffer + sizeof (int);
61 syncMode = *(int*)bufIter;
62 bufIter = bufIter + sizeof(int);
63 stmtLength = *(int*)bufIter;
64 bufIter = bufIter + sizeof(int);
65 noParams = *(int*)bufIter;
66 bufIter = bufIter + sizeof(int);
67 if (noParams >0) {
68 type = (int*) bufIter;
69 bufIter = bufIter + sizeof(int) * noParams;
70 length = (int*) bufIter;
71 bufIter = bufIter + sizeof(int) * noParams;
73 stmtString = bufIter;
74 printDebug(DM_Network, "stmtString ptr is %x\n", stmtString);
75 stmtString[stmtLength+1] = '\0';
76 printDebug(DM_Network, "PacketPrepare::unmarshall ended\n");
77 return OK;
79 DbRetVal PacketFree::marshall()
81 bufferSize = sizeof(int);
82 buffer = (char*) malloc(bufferSize);
83 *(int*)buffer = stmtID;
84 return OK;
86 DbRetVal PacketFree::unmarshall()
88 stmtID = *(int*)buffer;
89 return OK;
91 void PacketExecute::setParams(List list)
93 paramList = list;
94 noParams = list.size();
95 paramValues = new char*[noParams];
96 BindSqlField* bindField = NULL;
97 for (int i = 0 ; i < noParams; i++)
99 bindField = (BindSqlField*) paramList.get(i+1);
100 paramValues[i] = (char*) bindField->value;
102 return;
105 void SqlPacketExecute::setParams(List list)
107 paramList = list;
108 noParams = list.size();
109 if (!noParams) return;
110 paramValues = new char*[noParams];
111 BindSqlField* bindField = NULL;
112 for (int i = 0 ; i < noParams; i++)
114 bindField = (BindSqlField*) paramList.get(i+1);
115 paramValues[i] = (char*) bindField->value;
117 return;
120 void SqlPacketResultSet::setProjList(List list)
122 projList = list;
123 noProjs = list.size();
124 projValues = new char*[noProjs];
125 BindSqlProjectField* prjFld = NULL;
126 for (int i = 0 ; i < noProjs; i++)
128 prjFld = (BindSqlProjectField*) projList.get(i+1);
129 projValues[i] = (char*) prjFld->value;
131 return;
134 void PacketExecute::setStatementList(List stmtlist)
136 stmtList = stmtlist;
137 return;
140 void SqlPacketExecute::setStatementList(List stmtlist)
142 stmtList = stmtlist;
143 return;
146 DbRetVal PacketExecute::marshall()
148 bufferSize = sizeof(int)+ sizeof(int);
149 BindSqlField* bindField = NULL;
150 for (int i = 0 ; i < noParams; i++)
152 bindField = (BindSqlField*) paramList.get(i+1);
153 bufferSize = bufferSize + AllDataType::size(bindField->type, bindField->length);
155 buffer = (char*) malloc(bufferSize);
156 *(int*)buffer = stmtID;
157 char* bufIter = (char*) buffer + sizeof(int);
158 *(int*)bufIter = noParams;
159 bufIter = (char*) bufIter + sizeof(int);
160 for (int i = 0 ; i < noParams; i++)
162 bindField = (BindSqlField*) paramList.get(i+1);
163 AllDataType::copyVal(bufIter, bindField->value, bindField->type,bindField->length);
164 bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
166 return OK;
168 DbRetVal PacketExecute::unmarshall()
170 stmtID = *(int*)buffer;
171 char *bufIter = buffer + sizeof(int);
172 noParams = *(int*)bufIter;
173 bufIter = bufIter +sizeof(int);
174 ListIterator stmtIter = stmtList.getIterator();
175 NetworkStmt *stmt;
176 while (stmtIter.hasElement())
178 stmt = (NetworkStmt*) stmtIter.nextElement();
179 //TODO::Also check teh srcNetworkID
180 if (stmt->stmtID == stmtID ) break;
182 if (noParams == 0) return OK;
183 paramValues = new char*[noParams];
184 ListIterator paramIter = stmt->paramList.getIterator();
185 BindSqlField *bindField = NULL;
186 for (int i=0; i <noParams; i++)
188 paramValues[i] = bufIter;
189 bindField = (BindSqlField*) stmt->paramList.get(i+1);
190 bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
192 return OK;
195 void PacketCommit::setExecPackets(int tid, List list)
197 txnID = tid;
198 noOfStmts = list.size();
199 stmtBufSize = new int[noOfStmts];
200 stmtBuffer = new char*[noOfStmts];
201 PacketExecute* pkt = NULL;
202 int totalSize =0;
203 for (int i = 0 ; i < noOfStmts; i++)
205 pkt = (PacketExecute*) list.get(i+1);
206 if (pkt == NULL) printError(ErrSysFatal, "pkt is null.should never happen\n");
207 stmtBufSize[i] = pkt->getBufferSize();
208 stmtBuffer[i] = pkt->getMarshalledBuffer();
209 // *(int*)(((char*)stmtBuffer[i]) + 4));
210 totalSize = totalSize + stmtBufSize[i];
212 totalSize = sizeof(int) + sizeof(int) + noOfStmts * sizeof(int) +
213 totalSize;
214 bufferSize = totalSize;
215 return;
217 DbRetVal PacketCommit::marshall()
219 buffer = (char*) malloc(bufferSize);
220 *(int*)buffer = txnID;
221 char* bufIter = (char*) buffer + sizeof(int);
222 *(int*)bufIter = noOfStmts;
223 bufIter = (char*) bufIter + sizeof(int);
224 memcpy(bufIter, stmtBufSize, noOfStmts*sizeof(int));
225 bufIter = (char*) bufIter + noOfStmts* sizeof(int);
226 for (int i=0; i < noOfStmts; i++)
228 memcpy(bufIter, stmtBuffer[i], stmtBufSize[i]);
229 bufIter = bufIter + stmtBufSize[i];
231 return OK;
233 DbRetVal PacketCommit::unmarshall()
235 txnID = *(int*)buffer;
236 char *bufIter = buffer + sizeof(int);
237 noOfStmts = *(int*)bufIter;
238 bufIter = bufIter + sizeof(int);
239 stmtBufSize = new int[noOfStmts];
240 memcpy(stmtBufSize, bufIter, noOfStmts*sizeof(int));
241 bufIter = bufIter + noOfStmts * sizeof(int);
242 stmtBuffer = new char*[noOfStmts];
243 for (int i = 0 ; i <noOfStmts; i++)
245 stmtBuffer[i] = bufIter;
246 bufIter = bufIter + stmtBufSize[i];
249 //call unmarshall before calling this
250 void PacketCommit::getExecPacketList(List stmtList, List &list)
252 PacketExecute* pkt = NULL;
253 for (int i = 0 ; i < noOfStmts; i++)
255 pkt = new PacketExecute();
256 pkt->setBuffer(stmtBuffer[i]);
257 pkt->setBufferSize(stmtBufSize[i]);
258 pkt->setStatementList(stmtList);
259 pkt->unmarshall();
260 list.append(pkt);
264 DbRetVal SqlPacketConnect::marshall()
266 char *ptr = buffer; // moves over buffer
267 strncpy(ptr, userName, IDENTIFIER_LENGTH);
268 ptr = buffer+IDENTIFIER_LENGTH-1;
269 *ptr++ = '\0';
270 strncpy(ptr, passWord, IDENTIFIER_LENGTH);
271 ptr = ptr + IDENTIFIER_LENGTH-1;
272 *ptr++ = '\0';
273 *ptr = sqlApiImplType;
274 return OK;
277 DbRetVal SqlPacketConnect::unmarshall()
279 char *ptr = buffer;
280 strncpy(userName, ptr, IDENTIFIER_LENGTH);
281 strncpy(passWord, ptr + IDENTIFIER_LENGTH, IDENTIFIER_LENGTH);
282 sqlApiImplType = *(ptr + 2 * IDENTIFIER_LENGTH);
283 return OK;
286 DbRetVal SqlPacketPrepare::marshall()
288 printDebug(DM_Network, "PacketPrepare::marshall called\n");
289 bufferSize = sizeof(int) + strlen(stmtString) + 1;
290 printDebug(DM_Network, "Buffer size %d\n", bufferSize);
291 printDebug(DM_Network, "stmt %s size %d\n", stmtString, strlen(stmtString));
292 buffer = (char*) malloc(bufferSize);
293 *(int*)buffer = strlen(stmtString) + 1;
294 char *bufIter = buffer + sizeof(int);
295 strcpy(bufIter, stmtString);
296 printDebug(DM_Network, "PacketPrepare::marshall ended\n");
297 return OK;
300 DbRetVal SqlPacketPrepare::unmarshall()
302 printDebug(DM_Network, "PacketPrepare::unmarshall called\n");
303 printDebug(DM_Network, "start of the buffer is %x\n", buffer);
304 stmtLength = *(int*)buffer;
305 char *bufIter = buffer + sizeof(int);
306 stmtString = bufIter;
307 printDebug(DM_Network, "stmtString ptr is %x\n", stmtString);
308 stmtString[stmtLength] = '\0';
309 printDebug(DM_Network, "PacketPrepare::unmarshall ended\n");
310 return OK;
313 // the following marshall and unmarshall function is modified to accommodate
314 // the fix for MySQL Bug:1382
315 // SQLDescribeParam returns the same type information
316 // by sending type and length in exec packets.
317 DbRetVal SqlPacketExecute::marshall()
319 bufferSize = sizeof(int)+ sizeof(int);
320 BindSqlField* bindField = NULL;
321 for (int i = 0 ; i < noParams; i++)
323 bufferSize = bufferSize + 2 * sizeof(int);
324 bindField = (BindSqlField*) paramList.get(i+1);
325 if (bindField->type == typeBinary)
326 bufferSize = bufferSize + 2 * AllDataType::size(bindField->type, bindField->length);
327 else
328 bufferSize = bufferSize + AllDataType::size(bindField->type, bindField->length);
330 buffer = (char*) malloc(bufferSize);
331 *(int*)buffer = stmtID;
332 char* bufIter = (char*) buffer + sizeof(int);
333 *(int*)bufIter = noParams;
334 bufIter = (char*) bufIter + sizeof(int);
335 for (int i = 0 ; i < noParams; i++)
337 bindField = (BindSqlField*) paramList.get(i+1);
338 if (bindField->type == typeBinary) {
339 *(int *)bufIter = typeBinary;
340 bufIter = (char*) bufIter + sizeof(int);
341 *(int *)bufIter = AllDataType::size(bindField->type, bindField->length);
342 bufIter = (char*) bufIter + sizeof(int);
343 AllDataType::copyVal(bufIter, bindField->value, bindField->type, 2 * bindField->length);
344 bufIter = bufIter + 2 * AllDataType::size(bindField->type, bindField->length);
345 } else {
346 *(int *)bufIter = bindField->type;
347 bufIter = (char*) bufIter + sizeof(int);
348 *(int *)bufIter = AllDataType::size(bindField->type, bindField->length);
349 bufIter = (char*) bufIter + sizeof(int);
350 AllDataType::copyVal(bufIter, bindField->value, bindField->type, bindField->length);
351 bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
354 return OK;
357 DbRetVal SqlPacketExecute::unmarshall()
359 stmtID = *(int*)buffer;
360 char *bufIter = buffer + sizeof(int);
361 noParams = *(int*)bufIter;
362 bufIter = bufIter +sizeof(int);
363 ListIterator stmtIter = stmtList.getIterator();
364 NetworkStmt *stmt;
365 while (stmtIter.hasElement())
367 stmt = (NetworkStmt*) stmtIter.nextElement();
368 //TODO::Also check teh srcNetworkID
369 if (stmt->stmtID == stmtID ) break;
371 if (noParams == 0) return OK;
372 paramValues = new char*[noParams];
373 ListIterator paramIter = stmt->paramList.getIterator();
374 BindSqlField *bindField = NULL;
375 for (int i=0; i <noParams; i++)
377 bindField = (BindSqlField*) stmt->paramList.get(i+1);
378 bindField->type = (DataType) *(int *)bufIter;
379 bufIter = (char*) bufIter + sizeof(int);
380 bindField->length = *(int *)bufIter;
381 bufIter = (char*) bufIter + sizeof(int);
382 paramValues[i] = bufIter;
383 bindField->value = paramValues[i];
384 if (bindField->type == typeBinary)
385 bufIter = bufIter + 2 * AllDataType::size(bindField->type, bindField->length);
386 else
387 bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
389 return OK;
392 DbRetVal SqlPacketParamMetadata::marshall()
394 printDebug(DM_Network, "SqlPacketParamMetadata::marshall called\n");
395 bufferSize = sizeof(int) * 2;
396 printDebug(DM_Network, "NOOFPARAMS %d buffer size %d\n", noParams, bufferSize);
397 printDebug(DM_Network, "noParams is %d\n", noParams);
398 if (noParams >0)
399 bufferSize = bufferSize + sizeof(FieldInfo) * noParams;
400 buffer = (char*) malloc(bufferSize);
401 *(int*)buffer = stmtID;
402 char *bufIter = buffer + sizeof(int);
403 *(int*)bufIter = noParams;
404 bufIter = bufIter + sizeof(int);
405 ListIterator stmtIter = SqlNetworkHandler::stmtList.getIterator();
406 NetworkStmt *stmt;
407 while (stmtIter.hasElement())
409 stmt = (NetworkStmt*) stmtIter.nextElement();
410 if (stmt->stmtID == stmtID ) break;
412 FieldInfo *fldInfo = new FieldInfo();
413 for (int i=0; i <noParams; i++) {
414 BindSqlField *bindField = (BindSqlField*) stmt->paramList.get(i+1);
415 strcpy(fldInfo->fldName, bindField->fName);
416 fldInfo->type = bindField->type;
417 fldInfo->length = bindField->length;
418 fldInfo->offset = bindField->offset;
419 fldInfo->isNull = bindField->isNull;
420 fldInfo->isPrimary = bindField->isPrimary;
421 fldInfo->isDefault = bindField->isDefault;
422 fldInfo->isUnique = bindField->isUnique;
423 strcpy(fldInfo->defaultValueBuf, bindField->defaultValueBuf);
424 *(FieldInfo *) bufIter = *fldInfo;
425 bufIter += sizeof(FieldInfo);
427 delete fldInfo;
428 printDebug(DM_Network, "SqlPacketParamMetadata::marshall ended\n");
429 return OK;
432 DbRetVal SqlPacketParamMetadata::unmarshall()
434 printDebug(DM_Network, "SqlPacketParamMetadata::unmarshall called\n");
435 stmtID = *(int*)buffer;
436 printDebug(DM_Network, "start of the buffer is %x\n", buffer);
437 char *bufIter = buffer + sizeof (int);
438 noParams = *(int*)bufIter;
439 bufIter = bufIter + sizeof(int);
440 if(noParams > 0) data = bufIter;
441 printDebug(DM_Network, "SqlPacketParamMetadata::unmarshall ended\n");
442 return OK;
445 DbRetVal SqlPacketProjMetadata::marshall()
447 printDebug(DM_Network, "SqlPacketParamMetadata::marshall called\n");
448 bufferSize = sizeof(int) * 2;
449 printDebug(DM_Network, "NOOFPROJS %d buffer size %d\n", noProjs, bufferSize);
450 printDebug(DM_Network, "noParams is %d\n", noProjs);
451 if (noProjs >0)
452 bufferSize = bufferSize + sizeof(FieldInfo) * noProjs;
453 buffer = (char*) malloc(bufferSize);
454 *(int*)buffer = stmtID;
455 char *bufIter = buffer + sizeof(int);
456 *(int*)bufIter = noProjs;
457 bufIter = bufIter + sizeof(int);
458 ListIterator stmtIter = SqlNetworkHandler::stmtList.getIterator();
459 NetworkStmt *stmt;
460 while (stmtIter.hasElement())
462 stmt = (NetworkStmt*) stmtIter.nextElement();
463 if (stmt->stmtID == stmtID ) break;
465 FieldInfo *fldInfo = new FieldInfo();
466 for (int i=0; i < noProjs; i++) {
467 BindSqlProjectField *bindField = (BindSqlProjectField*) stmt->projList.get(i+1);
468 strcpy(fldInfo->fldName, bindField->fName);
469 fldInfo->type = bindField->type;
470 fldInfo->length = bindField->length;
471 fldInfo->offset = bindField->offset;
472 fldInfo->isNull = bindField->isNull;
473 fldInfo->isPrimary = bindField->isPrimary;
474 fldInfo->isDefault = bindField->isDefault;
475 fldInfo->isUnique = bindField->isUnique;
476 strcpy(fldInfo->defaultValueBuf, bindField->defaultValueBuf);
477 *(FieldInfo *) bufIter = *fldInfo;
478 bufIter += sizeof(FieldInfo);
480 delete fldInfo;
481 printDebug(DM_Network, "SqlPacketParamMetadata::marshall ended\n");
482 return OK;
485 DbRetVal SqlPacketProjMetadata::unmarshall()
487 printDebug(DM_Network, "SqlPacketProjMetadata::unmarshall called\n");
488 stmtID = *(int*)buffer;
489 printDebug(DM_Network, "start of the buffer is %x\n", buffer);
490 char *bufIter = buffer + sizeof (int);
491 noProjs = *(int*)bufIter;
492 bufIter = bufIter + sizeof(int);
493 if(noProjs > 0) data = bufIter;
494 printDebug(DM_Network, "SqlPacketProjMetadata::unmarshall ended\n");
495 return OK;
498 DbRetVal SqlPacketFetch::marshall()
500 bufferSize = sizeof(int);
501 buffer = (char*) malloc(bufferSize);
502 *(int*)buffer = stmtID;
503 return OK;
506 DbRetVal SqlPacketFetch::unmarshall()
508 stmtID = *(int *)buffer;
511 DbRetVal SqlPacketFree::marshall()
513 bufferSize = sizeof(int);
514 buffer = (char*) malloc(bufferSize);
515 *(int*)buffer = stmtID;
516 return OK;
519 DbRetVal SqlPacketFree::unmarshall()
521 stmtID = *(int *)buffer;
524 DbRetVal SqlPacketResultSet::marshall()
526 bufferSize = sizeof(int)+ sizeof(int);
527 BindSqlProjectField* prjFld = NULL;
528 for (int i = 0 ; i < noProjs; i++)
530 prjFld = (BindSqlProjectField*) projList.get(i+1);
531 bufferSize = bufferSize + AllDataType::size(prjFld->type, prjFld->length);
533 buffer = (char*) malloc(bufferSize);
534 *(int*)buffer = stmtID;
535 char* bufIter = (char*) buffer + sizeof(int);
536 *(int*)bufIter = noProjs;
537 bufIter = (char*) bufIter + sizeof(int);
538 for (int i = 0 ; i < noProjs; i++)
540 prjFld = (BindSqlProjectField*) projList.get(i+1);
541 AllDataType::copyVal(bufIter, prjFld->value, prjFld->type, prjFld->length);
542 bufIter = bufIter + AllDataType::size(prjFld->type, prjFld->length);
545 return OK;
548 DbRetVal SqlPacketResultSet::unmarshall()
550 stmtID = *(int*)buffer;
551 char *bufIter = buffer + sizeof(int);
552 noProjs = *(int*)bufIter;
553 bufIter = bufIter + sizeof(int);
554 BindSqlProjectField *prjFld = NULL;
555 for (int i=0; i <noProjs; i++)
557 prjFld = (BindSqlProjectField*) projList.get(i+1);
558 AllDataType::copyVal(prjFld->value, bufIter, prjFld->type, prjFld->length);
559 bufIter = bufIter + AllDataType::size(prjFld->type, prjFld->length);
561 return OK;
564 DbRetVal SqlPacketShowTables::marshall()
566 bufferSize = numOfTables * IDENTIFIER_LENGTH;
567 buffer = (char*) malloc(bufferSize);
568 char *bufIter = buffer;
569 ListIterator stmtIter = SqlNetworkHandler::stmtList.getIterator();
570 NetworkStmt *stmt;
571 while (stmtIter.hasElement())
573 stmt = (NetworkStmt*) stmtIter.nextElement();
574 if (stmt->stmtID == stmtID ) break;
576 Identifier *elem = NULL;
577 ListIterator tblIter = stmt->tableNamesList.getIterator();
578 while (tblIter.hasElement()) {
579 elem = (Identifier*) tblIter.nextElement();
580 strncpy(bufIter, elem->name, IDENTIFIER_LENGTH);
581 bufIter += IDENTIFIER_LENGTH;
583 return OK;
586 DbRetVal SqlPacketShowTables::unmarshall()
588 data = buffer;
589 return OK;