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 * 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>
23 DbRetVal
SqlNwStatement::executeDirect(char *stmtstr
)
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);
38 rv
= conn
->send(SQL_NW_PKT_EXECDIRECT
, pkt
->getMarshalledBuffer(), pkt
->getBufferSize());
40 conn
->setConnClosed(false);
41 printError(rv
, "Connection lost with the peer.");
46 if (rv
== ErrNoConnection
|| rv
== ErrPeerTimeOut
) {
47 conn
->setConnClosed(false);
48 printError(rv
, "Connection lost with the peer.");
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
;
61 DbRetVal
SqlNwStatement::prepare(char *stmtstr
)
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);
77 rv
= conn
->send(SQL_NW_PKT_PREPARE
, pkt
->getMarshalledBuffer(), pkt
->getBufferSize());
79 conn
->setConnClosed(false);
80 printError(rv
, "Connection lost with the peer.");
85 if (rv
== ErrNoConnection
|| rv
== ErrPeerTimeOut
) {
86 conn
->setConnClosed(false);
87 printError(rv
, "Connection lost with the peer.");
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
;
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);
109 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
110 conn
->setIsConnectedFlag(false);
112 return ErrNoConnection
;
114 // printf("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);
121 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
122 conn
->setIsConnectedFlag(false);
125 return ErrNoConnection
;
127 SqlPacketParamMetadata
*mdpkt
= new SqlPacketParamMetadata();
128 mdpkt
->setBuffer(buffer
);
130 noOfParams
= mdpkt
->noParams
;
131 //allocate null info for dml paramterized stmts to be filled up
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
);
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);
169 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
170 conn
->setIsConnectedFlag(false);
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);
181 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
182 conn
->setIsConnectedFlag(false);
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
);
221 DbRetVal
SqlNwStatement::execute(int &rowsAffected
)
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
);
236 rv
= conn
->send(SQL_NW_PKT_EXECUTE
, pkt
->getMarshalledBuffer(), pkt
->getBufferSize());
238 conn
->setConnClosed(false);
239 printError(rv
, "Connection lost with the peer.");
243 rv
= conn
->receive();
244 if (rv
== ErrNoConnection
|| rv
== ErrPeerTimeOut
) {
245 conn
->setConnClosed(false);
247 return ErrNoConnection
;
249 ResponsePacket
*rpkt
= (ResponsePacket
*) ((TCPClient
*)conn
->nwClient
)->respPkt
;
251 printError(rv
, "%s", rpkt
->errorString
);
255 rowsAffected
= rpkt
->rows
;
256 memset(nullInfoDml
, 0, os::align(pkt
->noParams
));
257 pkt
->setNullInfo(NULL
);
262 DbRetVal
SqlNwStatement::bindParam(int pos
, void* value
)
265 printError(ErrWarning
, "Deprecated. Use setParamXXX instead\n");
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
;
278 void* SqlNwStatement::fetch()
284 void* SqlNwStatement::fetch(DbRetVal
&ret
)
286 SqlNwConnection
*conn
= (SqlNwConnection
*)con
;
287 if (! conn
->isConOpen()) {
288 printError(ErrNoConnection
, "No connection present");
289 ret
= ErrNoConnection
;
292 if (!isPrepared
) return NULL
;
293 void *ptrToFirstField
= NULL
;
294 DbRetVal rv
= conn
->nwClient
->send(SQL_NW_PKT_FETCH
, getStmtID());
296 conn
->setConnClosed(false);
297 printError(rv
, "Connection lost with peer.");
298 ret
= ErrNoConnection
;
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);
309 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
310 conn
->setIsConnectedFlag(false);
311 ret
= ErrNoConnection
;
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);
322 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
323 conn
->setIsConnectedFlag(false);
324 ret
= ErrNoConnection
;
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
);
336 ptrToFirstField
= bindList
.get(1);
338 return ptrToFirstField
;
341 void* SqlNwStatement::fetchAndPrint(bool SQL
)
343 if (!isPrepared
) return NULL
;
345 BindSqlProjectField
*fld
= NULL
;
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");
354 else AllDataType::printVal(fld
->value
, fld
->type
, fld
->length
);
360 void* SqlNwStatement::next()
365 DbRetVal
SqlNwStatement::close()
367 if (!isPrepared
) return OK
;
372 void* SqlNwStatement::getFieldValuePtr( int pos
)
374 BindSqlProjectField
*fld
=(BindSqlProjectField
*) bindList
.get(pos
+1);
377 void SqlNwStatement::getProjFieldType(int *data
)
379 ListIterator biter
= bindList
.getIterator();
380 BindSqlProjectField
*elem
= NULL
;
382 while (biter
.hasElement())
384 elem
= (BindSqlProjectField
*) biter
.nextElement();
385 data
[i
++] = elem
->type
;
389 int SqlNwStatement::noOfProjFields()
391 if (!isPrepared
) return 0;
395 int SqlNwStatement::noOfParamFields()
397 if (!isPrepared
) return 0;
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
;
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
;
431 DbRetVal
SqlNwStatement::free()
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();
443 rv
= conn
->send(SQL_NW_PKT_FREE
, pkt
->getMarshalledBuffer(), pkt
->getBufferSize());
445 printError(rv
, "Connection lost with peer");
446 conn
->setConnClosed(false);
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
);
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
);
470 if (nullInfoSel
) { ::free(nullInfoSel
); nullInfoSel
= NULL
; }
471 if (nullInfoDml
) { ::free(nullInfoDml
); nullInfoDml
= NULL
; }
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
);
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
;
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
;
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
;
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
);
613 void SqlNwStatement::getPrimaryKeyFieldName(char *tablename
, char *pkfieldname
)
615 if (pkfieldname
== NULL
) return;
620 List
SqlNwStatement::getAllTableNames(DbRetVal
&ret
)
624 ListIterator tblIter
= tblNameList
.getIterator();
625 while (tblIter
.hasElement()) delete tblIter
.nextElement();
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
);
635 conn
->setConnClosed(false);
636 ret
= ErrNoConnection
;
639 rv
= conn
->receive();
640 if (rv
== ErrNoConnection
|| rv
== ErrPeerTimeOut
) {
641 conn
->setConnClosed(false);
642 ret
= ErrNoConnection
;
645 ResponsePacket
*rpkt
= (ResponsePacket
*)
646 ((TCPClient
*)conn
->nwClient
)->respPkt
;
647 int noOfTables
= rpkt
->rows
;
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);
655 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
656 conn
->setIsConnectedFlag(false);
657 ret
= ErrNoConnection
;
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);
667 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
668 conn
->setIsConnectedFlag(false);
669 ret
= ErrNoConnection
;
672 SqlPacketShowTables
*pkt
= new SqlPacketShowTables();
673 pkt
->setBuffer(buffer
);
674 rv
= pkt
->unmarshall();
675 char *ptr
= (char *)pkt
->data
;
677 Identifier
*id
= new Identifier();
678 strncpy(id
->name
, ptr
, IDENTIFIER_LENGTH
);
679 ptr
+= IDENTIFIER_LENGTH
;
681 tblNameList
.append(id
);
686 bool SqlNwStatement::isTablePresent(char *tblName
, DbRetVal
&ret
)
689 SqlNwConnection
*conn
= (SqlNwConnection
*)con
;
690 if (! conn
->isConOpen()) {
691 printError(ErrNoConnection
, "No connection present");
692 ret
= ErrNoConnection
;
696 SqlPacketIsTablePresent
*pkt
= new SqlPacketIsTablePresent();
697 pkt
->setTableName(tblName
);
699 rv
= conn
->send(SQL_NW_PKT_ISTABLEPRESENT
, pkt
->getMarshalledBuffer(),
700 pkt
->getBufferSize());
703 printError(ErrOS
, "Connection lost with peer.");
704 conn
->setConnClosed(false);
705 ret
= ErrNoConnection
;
709 rv
= conn
->receive();
710 if (rv
== ErrNoConnection
|| rv
== ErrPeerTimeOut
) {
711 conn
->setConnClosed(false);
712 ret
= ErrNoConnection
;
715 if (rv
!= OK
) return false;
719 void *SqlNwStatement::getLoadedRecords(char *tblName
, DbRetVal
&ret
)
722 SqlNwConnection
*conn
= (SqlNwConnection
*)con
;
723 if (! conn
->isConOpen()) {
724 printError(ErrNoConnection
, "No connection present");
725 ret
= ErrNoConnection
;
728 SqlPacketGetRecords
*pkt
= new SqlPacketGetRecords();
729 pkt
->setTableName(tblName
);
731 rv
= conn
->send(SQL_NW_PKT_GETRECORDS
, pkt
->getMarshalledBuffer(),
732 pkt
->getBufferSize());
734 printError(rv
, "Connection lost with peer.");
735 conn
->setConnClosed(false);
741 rv
= conn
->receive();
742 if (rv
== ErrNoConnection
|| rv
== ErrPeerTimeOut
) {
743 conn
->setConnClosed(false);
744 ret
= ErrNoConnection
;
747 ResponsePacket
*rpkt
= (ResponsePacket
*)
748 ((TCPClient
*)conn
->nwClient
)->respPkt
;
750 ret
= rpkt
->errRetVal
;
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);
760 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
761 conn
->setIsConnectedFlag(false);
762 ret
= ErrNoConnection
;
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);
772 ((TCPClient
*)(conn
->nwClient
))->sockfd
= -1;
773 conn
->setIsConnectedFlag(false);
774 ret
= ErrNoConnection
;
777 SqlPacketLoadRecords
*lpkt
= new SqlPacketLoadRecords();
778 lpkt
->setBuffer(buffer
);
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
);
789 bool SqlNwStatement::isFldNull(int pos
)
791 if (nullInfoSel
[pos
- 1]) return true;
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;