windows changes
[csql.git] / src / sqlnetwork / SqlNwStatement.cxx
blob6e3097ba14fd4894e5a5509b30f57496c2c50269
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 <SqlNwStatement.h>
21 #include <Network.h>
23 DbRetVal SqlNwStatement::executeDirect(char *stmtstr)
25 DbRetVal rv = OK;
26 isSel = false;
27 SqlNwConnection *conn = (SqlNwConnection*)con;
28 if (! conn->isConOpen()) {
29 printError(ErrNoConnection, "No connection present");
30 return ErrNoConnection;
32 if (isPrepared) free();
33 if (nullInfoSel) { ::free(nullInfoSel); nullInfoSel = NULL; }
34 SqlPacketPrepare *pkt = new SqlPacketPrepare();
35 pkt->stmtString = stmtstr;
36 pkt->stmtLength = os::align(strlen(stmtstr) + 1);
37 pkt->marshall();
38 rv = conn->send(SQL_NW_PKT_EXECDIRECT, pkt->getMarshalledBuffer(), pkt->getBufferSize());
39 if (rv != OK) {
40 conn->setConnClosed(false);
41 printError(rv, "Connection lost with the peer.");
42 delete pkt;
43 return rv;
45 rv = conn->receive();
46 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
47 conn->setConnClosed(false);
48 printError(rv, "Connection lost with the peer.");
49 delete pkt;
50 return ErrNoConnection;
52 ResponsePacket *rpkt = (ResponsePacket *)
53 ((TCPClient *)conn->nwClient)->respPkt;
54 if (rv != OK) { delete pkt; return rv; }
55 char *ptr = (char *) &rpkt->retVal;
56 isPrepared = true;
57 delete pkt;
58 return rv;
61 DbRetVal SqlNwStatement::prepare(char *stmtstr)
63 DbRetVal rv = OK;
64 isSel = false;
65 SqlNwConnection *conn = (SqlNwConnection*)con;
66 if (! conn->isConOpen()) {
67 printError(ErrNoConnection, "No connection present");
68 return ErrNoConnection;
70 if (isPrepared) free();
71 if (nullInfoSel) { ::free(nullInfoSel); nullInfoSel = NULL; }
72 if (nullInfoDml) { ::free(nullInfoDml); nullInfoDml = NULL; }
73 SqlPacketPrepare *pkt = new SqlPacketPrepare();
74 pkt->stmtString = stmtstr;
75 pkt->stmtLength = os::align(strlen(stmtstr) + 1);
76 pkt->marshall();
77 rv = conn->send(SQL_NW_PKT_PREPARE, pkt->getMarshalledBuffer(), pkt->getBufferSize());
78 if (rv != OK) {
79 conn->setConnClosed(false);
80 printError(rv, "Connection lost with the peer.");
81 delete pkt;
82 return rv;
84 rv = conn->receive();
85 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
86 conn->setConnClosed(false);
87 printError(rv, "Connection lost with the peer.");
88 delete pkt;
89 return ErrNoConnection;
91 ResponsePacket *rpkt = (ResponsePacket *)
92 ((TCPClient *)conn->nwClient)->respPkt;
93 if (rv != OK) { delete pkt; return rv; }
94 char *ptr = (char *) &rpkt->retVal;
95 if (rpkt->isSelect) isSel = true; else isSel = false;
96 plan =(ResultSetPlan) *ptr ;
97 int params = *(ptr + 2);
98 int proj = *(ptr + 3);
99 stmtID = rpkt->stmtID;
100 char *buffer = NULL;
101 if (params) {
102 PacketHeader header;
103 int fd = ((TCPClient *)(conn->nwClient))->sockfd;
104 int numbytes = os::recv(fd, &header, sizeof(PacketHeader), 0);
105 if (numbytes == -1) {
106 printError(ErrOS, "Error reading from socket\n");
107 conn->setConnClosed(false);
108 os::closeSocket(fd);
109 ((TCPClient *)(conn->nwClient))->sockfd = -1;
110 conn->setIsConnectedFlag(false);
111 delete pkt;
112 return ErrNoConnection;
114 printDebug(DM_Network, "HEADER says packet type is %d\n", header.packetType);
115 buffer = (char*) malloc(header.packetLength);
116 numbytes = os::recv(fd,buffer,header.packetLength,0);
117 if (numbytes == -1) {
118 printError(ErrOS, "Error reading from socket\n");
119 conn->setConnClosed(false);
120 os::closeSocket(fd);
121 ((TCPClient *)(conn->nwClient))->sockfd = -1;
122 conn->setIsConnectedFlag(false);
123 delete pkt;
124 ::free(buffer);
125 return ErrNoConnection;
127 SqlPacketParamMetadata *mdpkt = new SqlPacketParamMetadata();
128 mdpkt->setBuffer(buffer);
129 mdpkt->unmarshall();
130 noOfParams = mdpkt->noParams;
131 //allocate null info for dml paramterized stmts to be filled up
132 //during set null
133 int nullInfoLen = os::align(noOfParams);
134 nullInfoDml = (char *) malloc(nullInfoLen);
135 memset(nullInfoDml, 0, nullInfoLen);
137 BindSqlField *bindField=NULL;
138 FieldInfo *fldInfo = new FieldInfo();
139 char *ptr = (char *) mdpkt->data;
140 for (int i=0; i < noOfParams; i++) {
141 *fldInfo = *(FieldInfo *) ptr;
142 bindField = new BindSqlField();
143 strcpy(bindField->fName, fldInfo->fldName);
144 bindField->type = fldInfo->type;
145 bindField->length = fldInfo->length;
146 bindField->offset = fldInfo->offset;
147 strcpy(bindField->defaultValueBuf, fldInfo->defaultValueBuf);
148 bindField->isNull = fldInfo->isNull;
149 bindField->isPrimary = fldInfo->isPrimary;
150 bindField->isDefault = fldInfo->isDefault;
151 bindField->isUnique = fldInfo->isUnique;
152 if (bindField->type == typeBinary)
153 bindField->value = AllDataType::alloc(bindField->type, 2 * bindField->length);
154 else bindField->value = AllDataType::alloc(bindField->type, bindField->length);
155 paramList.append(bindField);
156 ptr += sizeof (FieldInfo);
158 delete mdpkt;
159 delete fldInfo;
161 if (proj) {
162 PacketHeader header;
163 int fd = ((TCPClient *)(conn->nwClient))->sockfd;
164 int numbytes = os::recv(fd, &header, sizeof(PacketHeader), 0);
165 if (numbytes == -1) {
166 printError(ErrOS, "Error reading from socket\n");
167 conn->setConnClosed(false);
168 os::closeSocket(fd);
169 ((TCPClient *)(conn->nwClient))->sockfd = -1;
170 conn->setIsConnectedFlag(false);
171 delete pkt;
172 return ErrNoConnection;
174 // printf("HEADER says packet type is %d\n", header.packetType);
175 buffer = (char*) malloc(header.packetLength);
176 numbytes = os::recv(fd,buffer,header.packetLength,0);
177 if (numbytes == -1) {
178 printError(ErrOS, "Error reading from socket\n");
179 conn->setConnClosed(false);
180 os::closeSocket(fd);
181 ((TCPClient *)(conn->nwClient))->sockfd = -1;
182 conn->setIsConnectedFlag(false);
183 ::free (buffer);
184 delete pkt;
185 return ErrNoConnection;
187 SqlPacketProjMetadata *prjmdpkt = new SqlPacketProjMetadata();
188 prjmdpkt->setBuffer(buffer);
189 prjmdpkt->unmarshall();
190 noOfProjs = prjmdpkt->noProjs;
191 BindSqlProjectField *prjFld=NULL;
192 FieldInfo *fldInfo = new FieldInfo();
193 char *ptr = (char *) prjmdpkt->data;
194 for (int i=0; i < noOfProjs; i++) {
195 *fldInfo = *(FieldInfo *) ptr;
196 prjFld = new BindSqlProjectField();
197 strcpy(prjFld->fName, fldInfo->fldName);
198 prjFld->type = fldInfo->type;
199 prjFld->length = fldInfo->length;
200 prjFld->offset = fldInfo->offset;
201 strcpy(prjFld->defaultValueBuf, fldInfo->defaultValueBuf);
202 prjFld->aType = fldInfo->aType;
203 prjFld->isNull = fldInfo->isNull;
204 prjFld->isPrimary = fldInfo->isPrimary;
205 prjFld->isDefault = fldInfo->isDefault;
206 prjFld->isUnique = fldInfo->isUnique;
207 if (prjFld->type == typeBinary)
208 prjFld->value = AllDataType::alloc(prjFld->type, 2 * prjFld->length);
209 else prjFld->value = AllDataType::alloc(prjFld->type, prjFld->length);
210 bindList.append(prjFld);
211 ptr += sizeof (FieldInfo);
213 delete prjmdpkt;
214 delete fldInfo;
216 isPrepared = true;
217 delete pkt;
218 return rv;
221 DbRetVal SqlNwStatement::execute(int &rowsAffected)
223 DbRetVal rv = OK;
224 SqlNwConnection *conn = (SqlNwConnection*)con;
225 if (! conn->isConOpen()) {
226 printError(ErrNoConnection, "No connection present");
227 return ErrNoConnection;
229 if (!isPrepared) return ErrNotPrepared;
230 SqlPacketExecute *pkt = new SqlPacketExecute();
231 pkt->stmtID = getStmtID();
232 pkt->noParams=paramList.size();
233 pkt->setParams(paramList);
234 pkt->setNullInfo(nullInfoDml);
235 pkt->marshall();
236 rv = conn->send(SQL_NW_PKT_EXECUTE, pkt->getMarshalledBuffer(), pkt->getBufferSize());
237 if (rv != OK) {
238 conn->setConnClosed(false);
239 printError(rv, "Connection lost with the peer.");
240 delete pkt;
241 return rv;
243 rv = conn->receive();
244 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
245 conn->setConnClosed(false);
246 delete pkt;
247 return ErrNoConnection;
249 ResponsePacket *rpkt = (ResponsePacket *) ((TCPClient *)conn->nwClient)->respPkt;
250 if (rv != OK) {
251 printError(rv, "%s", rpkt->errorString);
252 delete pkt;
253 return rv;
255 rowsAffected = rpkt->rows;
256 memset(nullInfoDml, 0, os::align(pkt->noParams));
257 pkt->setNullInfo(NULL);
258 delete pkt;
259 return rv;
262 DbRetVal SqlNwStatement::bindParam(int pos, void* value)
264 DbRetVal rv = OK;
265 printError(ErrWarning, "Deprecated. Use setParamXXX instead\n");
266 return rv;
269 DbRetVal SqlNwStatement::bindField(int pos, void* value)
271 if (!isPrepared) return OK;
272 BindSqlProjectField *prjFld = (BindSqlProjectField *) bindList.get(pos);
273 if(prjFld->value) { ::free(prjFld->value); prjFld->isFreed = true; }
274 prjFld->value = value;
275 return OK;
278 void* SqlNwStatement::fetch()
280 DbRetVal rv = OK;
281 return fetch(rv);
284 void* SqlNwStatement::fetch(DbRetVal &ret)
286 SqlNwConnection *conn = (SqlNwConnection*)con;
287 if (! conn->isConOpen()) {
288 printError(ErrNoConnection, "No connection present");
289 ret = ErrNoConnection;
290 return NULL;
292 if (!isPrepared) return NULL;
293 void *ptrToFirstField = NULL;
294 DbRetVal rv = conn->nwClient->send(SQL_NW_PKT_FETCH, getStmtID());
295 if (rv != OK) {
296 conn->setConnClosed(false);
297 printError(rv, "Connection lost with peer.");
298 ret = ErrNoConnection;
299 return NULL;
301 int rowLength=0;
302 int dummy =0;
303 int fd = ((TCPClient *)(conn->nwClient))->sockfd;
304 int numbytes = os::recv(fd, &rowLength, 4, 0);
305 if (numbytes == -1) {
306 printError(ErrOS, "Connection lost with peer\n");
307 conn->setConnClosed(false);
308 os::closeSocket(fd);
309 ((TCPClient *)(conn->nwClient))->sockfd = -1;
310 conn->setIsConnectedFlag(false);
311 ret = ErrNoConnection;
312 return NULL;
314 if (rowLength ==1 ) return NULL;
315 else if (rowLength == 2) {ret = ErrUnknown; return NULL; }
316 char *rowBuffer = (char*) malloc(rowLength);
317 numbytes = os::recv(fd,rowBuffer,rowLength,0);
318 if (numbytes == -1) {
319 printError(ErrOS, "Connection lost with peer\n");
320 conn->setConnClosed(false);
321 os::closeSocket(fd);
322 ((TCPClient *)(conn->nwClient))->sockfd = -1;
323 conn->setIsConnectedFlag(false);
324 ret = ErrNoConnection;
325 return NULL;
327 SqlPacketResultSet *rspkt = new SqlPacketResultSet();
328 rspkt->setBuffer(rowBuffer);
329 rspkt->setProjList(bindList);
330 rspkt->noProjs = bindList.size();
331 if (nullInfoSel == NULL)
332 nullInfoSel = (char *) malloc(os::align(rspkt->noProjs));
333 memset(nullInfoSel, 0, os::align(rspkt->noProjs));
334 rspkt->setNullInfo(nullInfoSel);
335 rspkt->unmarshall();
336 ptrToFirstField = bindList.get(1);
337 delete rspkt;
338 return ptrToFirstField;
341 void* SqlNwStatement::fetchAndPrint(bool SQL)
343 if (!isPrepared) return NULL;
344 void *tuple = NULL;
345 BindSqlProjectField *fld = NULL;
346 tuple = fetch();
347 if (NULL == tuple) return NULL;
348 for(int i = 0; i < noOfProjs; i++) {
349 fld = (BindSqlProjectField *) bindList.get(i + 1);
350 if (isFldNull(i+1)) {
351 if (fld->aType == AGG_COUNT) printf("0");
352 else printf("NULL");
354 else AllDataType::printVal(fld->value, fld->type, fld->length);
355 printf("\t");
357 return tuple;
360 void* SqlNwStatement::next()
362 return fetch();
365 DbRetVal SqlNwStatement::close()
367 if (!isPrepared) return OK;
368 //TODO
369 return OK;
372 void* SqlNwStatement::getFieldValuePtr( int pos )
374 BindSqlProjectField *fld=(BindSqlProjectField *) bindList.get(pos+1);
375 return fld->value;
377 void SqlNwStatement::getProjFieldType(int *data)
379 ListIterator biter = bindList.getIterator();
380 BindSqlProjectField *elem = NULL;
381 int i = 1;
382 while (biter.hasElement())
384 elem = (BindSqlProjectField*) biter.nextElement();
385 data[i++] = elem->type;
389 int SqlNwStatement::noOfProjFields()
391 if (!isPrepared) return 0;
392 return noOfProjs;
395 int SqlNwStatement::noOfParamFields()
397 if (!isPrepared) return 0;
398 return noOfParams;
401 DbRetVal SqlNwStatement::getProjFldInfo (int projpos, FieldInfo *&fInfo)
403 BindSqlProjectField *prjFld = (BindSqlProjectField *) bindList.get(projpos);
404 strcpy(fInfo->fldName, prjFld->fName);
405 fInfo->type = prjFld->type;
406 fInfo->length = prjFld->length;
407 fInfo->offset = prjFld->offset;
408 strcpy(fInfo->defaultValueBuf, prjFld->defaultValueBuf);
409 fInfo->isNull = prjFld->isNull;
410 fInfo->isPrimary = prjFld->isPrimary;
411 fInfo->isDefault = prjFld->isDefault;
412 fInfo->isUnique = prjFld->isUnique;
413 return OK;
416 DbRetVal SqlNwStatement::getParamFldInfo (int parampos, FieldInfo *&fInfo)
418 BindSqlField *bindField = (BindSqlField *) paramList.get(parampos);
419 strcpy(fInfo->fldName, bindField->fName);
420 fInfo->type = bindField->type;
421 fInfo->length = bindField->length;
422 fInfo->offset = bindField->offset;
423 strcpy(fInfo->defaultValueBuf, bindField->defaultValueBuf);
424 fInfo->isNull = bindField->isNull;
425 fInfo->isPrimary = bindField->isPrimary;
426 fInfo->isDefault = bindField->isDefault;
427 fInfo->isUnique = bindField->isUnique;
428 return OK;
431 DbRetVal SqlNwStatement::free()
433 DbRetVal rv = OK;
434 SqlNwConnection *conn = (SqlNwConnection*)con;
435 if (! conn->isConOpen()) {
436 printError(ErrNoConnection, "No connection present");
437 return ErrNoConnection;
439 if (!isPrepared) return OK;
440 SqlPacketFree *pkt = new SqlPacketFree();
441 pkt->stmtID = getStmtID();
442 pkt->marshall();
443 rv = conn->send(SQL_NW_PKT_FREE, pkt->getMarshalledBuffer(), pkt->getBufferSize());
444 if (rv != OK) {
445 printError(rv, "Connection lost with peer");
446 conn->setConnClosed(false);
447 delete pkt;
448 return rv;
450 delete pkt;
451 rv = conn->receive();
452 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
453 conn->setConnClosed(false);
454 return ErrNoConnection;
456 else if (rv != OK) return rv;
457 ListIterator itprm = paramList.getIterator();
458 BindSqlField *fld = NULL;
459 while((fld = (BindSqlField *) itprm.nextElement()) != NULL) {
460 if (fld->value) ::free(fld->value);
461 delete fld;
463 paramList.reset();
464 ListIterator itprj = bindList.getIterator();
465 BindSqlProjectField *pfld = NULL;
466 while((pfld = (BindSqlProjectField *) itprj.nextElement()) != NULL) {
467 if(pfld->value && !pfld->isFreed) ::free(pfld->value);
468 delete pfld;
470 if (nullInfoSel) { ::free(nullInfoSel); nullInfoSel = NULL; }
471 if (nullInfoDml) { ::free(nullInfoDml); nullInfoDml = NULL; }
472 bindList.reset();
474 isPrepared = false;
475 return rv;
478 // In all the following setXXXParam functions type and length fields are
479 // reinitialized to accommodate fix for MySQL bug #1382
480 // SQLDescribeParam returns the same type information
482 void SqlNwStatement::setShortParam(int paramPos, short value)
484 if (!isPrepared) return;
485 if (paramPos <= 0) return;
486 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
487 bindField->type = typeShort;
488 bindField->length = AllDataType::size(typeShort);
489 *(short *) bindField->value = value;
490 return;
493 void SqlNwStatement::setIntParam(int paramPos, int value)
495 if (!isPrepared) return ;
496 if (paramPos <= 0) return;
497 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
498 bindField->type = typeInt;
499 bindField->length = sizeof(int);
500 *(int *) bindField->value = value;
501 return;
504 void SqlNwStatement::setLongParam(int paramPos, long value)
506 if (!isPrepared) return ;
507 if (paramPos <= 0) return;
508 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
509 bindField->type = typeLong;
510 bindField->length = sizeof(long);
511 *(long *) bindField->value = value;
512 return;
516 void SqlNwStatement::setLongLongParam(int paramPos, long long value)
518 if (!isPrepared) return ;
519 if (paramPos <= 0) return;
520 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
521 bindField->type = typeLongLong;
522 bindField->length = AllDataType::size(typeLongLong);
523 *(long long *) bindField->value = value;
524 return;
527 void SqlNwStatement::setByteIntParam(int paramPos, ByteInt value)
529 if (!isPrepared) return ;
530 if (paramPos <= 0) return;
531 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
532 bindField->type = typeByteInt;
533 bindField->length = AllDataType::size(typeByteInt);
534 *(ByteInt *) bindField->value = value;
535 return;
538 void SqlNwStatement::setFloatParam(int paramPos, float value)
540 if (!isPrepared) return ;
541 if (paramPos <= 0) return;
542 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
543 bindField->type = typeFloat;
544 bindField->length = AllDataType::size(typeFloat);
545 *(float *) bindField->value = value;
546 return;
549 void SqlNwStatement::setDoubleParam(int paramPos, double value)
551 if (!isPrepared) return ;
552 if (paramPos <= 0) return;
553 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
554 bindField->type = typeDouble;
555 bindField->length = AllDataType::size(typeDouble);
556 *(double *) bindField->value = value;
557 return;
560 void SqlNwStatement::setStringParam(int paramPos, char *value)
562 if (!isPrepared) return ;
563 if (paramPos <= 0) return;
564 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
565 bindField->type = typeString;
566 strncpy((char *) bindField->value, value, bindField->length);
567 return;
570 void SqlNwStatement::setDateParam(int paramPos, Date value)
572 if (!isPrepared) return ;
573 if (paramPos <= 0) return;
574 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
575 bindField->type = typeDate;
576 bindField->length = sizeof(Date);
577 *(Date *)bindField->value = value;
578 return;
581 void SqlNwStatement::setTimeParam(int paramPos, Time value)
583 if (!isPrepared) return ;
584 if (paramPos <= 0) return;
585 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
586 bindField->type = typeTime;
587 bindField->length = sizeof(Time);
588 * (Time *) bindField->value = value;
589 return;
592 void SqlNwStatement::setTimeStampParam(int paramPos, TimeStamp value)
594 if (!isPrepared) return ;
595 if (paramPos <= 0) return;
596 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
597 bindField->type = typeTimeStamp;
598 bindField->length = sizeof(TimeStamp);
599 *(TimeStamp *) bindField->value = value;
600 return;
603 void SqlNwStatement::setBinaryParam(int paramPos, void *value, int length)
605 if (!isPrepared) return;
606 if (paramPos <= 0) return;
607 BindSqlField *bindField = (BindSqlField *) paramList.get(paramPos);
608 bindField->type = typeBinary;
609 memcpy(bindField->value, value, 2 * bindField->length);
610 return;
613 void SqlNwStatement::getPrimaryKeyFieldName(char *tablename, char *pkfieldname)
615 if (pkfieldname == NULL) return;
616 //TODO
617 return;
620 List SqlNwStatement::getAllTableNames(DbRetVal &ret)
622 DbRetVal rv = OK;
624 ListIterator tblIter = tblNameList.getIterator();
625 while (tblIter.hasElement()) delete tblIter.nextElement();
626 tblNameList.reset();
628 SqlNwConnection *conn = (SqlNwConnection*)con;
629 if (! conn->isConOpen()) {
630 printError(ErrNoConnection, "No connection present");
631 ret = ErrNoConnection;
633 rv = conn->send(SQL_NW_PKT_SHOWTABLES);
634 if (rv != OK) {
635 conn->setConnClosed(false);
636 ret = ErrNoConnection;
637 return tblNameList;
639 rv = conn->receive();
640 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
641 conn->setConnClosed(false);
642 ret = ErrNoConnection;
643 return tblNameList;
645 ResponsePacket *rpkt = (ResponsePacket *)
646 ((TCPClient *)conn->nwClient)->respPkt;
647 int noOfTables = rpkt->rows;
648 PacketHeader header;
649 int fd = ((TCPClient *)(conn->nwClient))->sockfd;
650 int numbytes = os::recv(fd, &header, sizeof(PacketHeader), 0);
651 if (numbytes == -1) {
652 printError(ErrOS, "Connection lost with peer.");
653 conn->setConnClosed(false);
654 os::closeSocket(fd);
655 ((TCPClient *)(conn->nwClient))->sockfd = -1;
656 conn->setIsConnectedFlag(false);
657 ret = ErrNoConnection;
658 return tblNameList;
660 // printf("HEADER says packet type is %d\n", header.packetType);
661 char *buffer = (char*) malloc(header.packetLength);
662 numbytes = os::recv(fd,buffer,header.packetLength,0);
663 if (numbytes == -1) {
664 printError(ErrOS, "Connection lost with peer.");
665 conn->setConnClosed(false);
666 os::closeSocket(fd);
667 ((TCPClient *)(conn->nwClient))->sockfd = -1;
668 conn->setIsConnectedFlag(false);
669 ret = ErrNoConnection;
670 return tblNameList;
672 SqlPacketShowTables *pkt = new SqlPacketShowTables();
673 pkt->setBuffer(buffer);
674 rv = pkt->unmarshall();
675 char *ptr = (char *)pkt->data;
676 while (noOfTables) {
677 Identifier *id = new Identifier();
678 strncpy(id->name, ptr, IDENTIFIER_LENGTH);
679 ptr += IDENTIFIER_LENGTH;
680 noOfTables--;
681 tblNameList.append(id);
683 return tblNameList;
686 bool SqlNwStatement::isTablePresent(char *tblName, DbRetVal &ret)
688 DbRetVal rv = OK;
689 SqlNwConnection *conn = (SqlNwConnection*)con;
690 if (! conn->isConOpen()) {
691 printError(ErrNoConnection, "No connection present");
692 ret = ErrNoConnection;
693 return false;
696 SqlPacketIsTablePresent *pkt = new SqlPacketIsTablePresent();
697 pkt->setTableName(tblName);
698 pkt->marshall();
699 rv = conn->send(SQL_NW_PKT_ISTABLEPRESENT, pkt->getMarshalledBuffer(),
700 pkt->getBufferSize());
701 if (rv != OK) {
702 delete pkt;
703 printError(ErrOS, "Connection lost with peer.");
704 conn->setConnClosed(false);
705 ret = ErrNoConnection;
706 return false;
708 delete pkt;
709 rv = conn->receive();
710 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
711 conn->setConnClosed(false);
712 ret = ErrNoConnection;
713 return false;
715 if (rv != OK) return false;
716 return true;
719 void *SqlNwStatement::getLoadedRecords(char *tblName, DbRetVal &ret)
721 DbRetVal rv = OK;
722 SqlNwConnection *conn = (SqlNwConnection*)con;
723 if (! conn->isConOpen()) {
724 printError(ErrNoConnection, "No connection present");
725 ret = ErrNoConnection;
726 return NULL;
728 SqlPacketGetRecords *pkt = new SqlPacketGetRecords();
729 pkt->setTableName(tblName);
730 pkt->marshall();
731 rv = conn->send(SQL_NW_PKT_GETRECORDS, pkt->getMarshalledBuffer(),
732 pkt->getBufferSize());
733 if (rv != OK) {
734 printError(rv, "Connection lost with peer.");
735 conn->setConnClosed(false);
736 ret = rv;
737 delete pkt;
738 return NULL;
740 delete pkt;
741 rv = conn->receive();
742 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
743 conn->setConnClosed(false);
744 ret = ErrNoConnection;
745 return NULL;
747 ResponsePacket *rpkt = (ResponsePacket *)
748 ((TCPClient *)conn->nwClient)->respPkt;
749 if (!rpkt->rows) {
750 ret = rpkt->errRetVal;
751 return NULL;
753 PacketHeader header;
754 int fd = ((TCPClient *)(conn->nwClient))->sockfd;
755 int numbytes = os::recv(fd, &header, sizeof(PacketHeader), 0);
756 if (numbytes == -1) {
757 printError(ErrOS, "Connection lost with peer");
758 conn->setConnClosed(false);
759 os::closeSocket(fd);
760 ((TCPClient *)(conn->nwClient))->sockfd = -1;
761 conn->setIsConnectedFlag(false);
762 ret = ErrNoConnection;
763 return NULL;
765 // printf("HEADER says packet length is %d\n", header.packetLength);
766 char *buffer = (char*) malloc(header.packetLength);
767 numbytes = os::recv(fd,buffer,header.packetLength,0);
768 if (numbytes == -1) {
769 printError(ErrOS, "Connection lost with peer");
770 conn->setConnClosed(false);
771 os::closeSocket(fd);
772 ((TCPClient *)(conn->nwClient))->sockfd = -1;
773 conn->setIsConnectedFlag(false);
774 ret = ErrNoConnection;
775 return NULL;
777 SqlPacketLoadRecords *lpkt = new SqlPacketLoadRecords();
778 lpkt->setBuffer(buffer);
779 lpkt->unmarshall();
780 char * data = (char *) lpkt->getMarshalledBuffer();
781 int pages = *(int *) data;
782 int sizeToCopy = sizeof(int) + pages * PAGE_SIZE;
783 char *dataToReturn = (char *) malloc(sizeToCopy);
784 memcpy(dataToReturn, data, sizeToCopy);
785 delete lpkt;
786 return dataToReturn;
789 bool SqlNwStatement::isFldNull(int pos)
791 if (nullInfoSel[pos - 1]) return true;
792 else return false;
795 bool SqlNwStatement::isFldNull(char *fname)
797 for (int i=0; i < bindList.size(); i++) {
798 BindSqlProjectField *fld = (BindSqlProjectField *) bindList.get(i+1);
799 if (strcmp(fname, fld->fName) == 0) return isFldNull(i+1);
803 void SqlNwStatement::setNull(int fldPos)
805 if (nullInfoDml) nullInfoDml[fldPos-1] = 1;
807 long long SqlNwStatement::getLastInsertedVal(DbRetVal &rv)
809 DbRetVal ret = OK;
810 SqlNwConnection *conn = (SqlNwConnection*)con;
811 if (! conn->isConOpen()) {
812 printError(ErrNoConnection, "No connection present");
813 rv = ErrNoConnection;
814 return 0;
816 rv = conn->nwClient->send(SQL_NW_PKT_LASTAIVAL, getStmtID());
817 if (rv != OK) {
818 printError(rv, "Connection lost with peer.");
819 conn->setConnClosed(false);
820 return 0;
822 rv = conn->receive();
823 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
824 conn->setConnClosed(false);
825 return 0;
827 ResponsePacket *rpkt = (ResponsePacket *)
828 ((TCPClient *)conn->nwClient)->respPkt;
829 return rpkt->lastAutoIncVal;