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 ***************************************************************************/
21 #include <SqlNwConnection.h>
25 DbRetVal
SqlNwConnection::connect (char *user
, char * pass
)
29 printError(ErrAlready
, "Connection is already open");
32 if (nwClient
== NULL
) nwClient
= new TCPClient();
33 int bufsize
= 2 * IDENTIFIER_LENGTH
+ sizeof(int);
34 nwClient
->setHost(hostname
, port
, 123);
35 rv
= nwClient
->connect();
37 printError(rv
, "Connection failed");
42 SqlPacketConnect
*pkt
= new SqlPacketConnect();
43 pkt
->setConnParam(user
, pass
, sqlApiImplType
);
44 pkt
->setBufferSize(bufsize
);
45 char * buffer
= (char *) malloc(bufsize
);
46 pkt
->setBuffer(buffer
);
48 rv
= nwClient
->send(SQL_NW_PKT_CONNECT
, buffer
, bufsize
);
50 printError(rv
, "Data could not be sent");
56 rv
= nwClient
->receive();
57 if (rv
== ErrPeerTimeOut
) {
61 return ErrNoConnection
;
63 ResponsePacket
*rpkt
= (ResponsePacket
*) ((TCPClient
*)nwClient
)->respPkt
;
64 char *ptr
= (char *) &rpkt
->retVal
;
66 printError(ErrPeerResponse, "%s", rpkt->errorString);
68 nwClient->disconnect();
71 return ErrPeerResponse;
77 DbRetVal
SqlNwConnection::disconnect()
81 printError(ErrNoConnection
, "No connection present");
82 return ErrNoConnection
;
84 rv
= nwClient
->disconnect();
91 DbRetVal
SqlNwConnection::beginTrans(IsolationLevel isoLevel
, TransSyncMode mode
)
94 printError(ErrNoConnection
, "No connection present");
95 return ErrNoConnection
;
100 DbRetVal
SqlNwConnection::commit()
103 printError(ErrNoConnection
, "No connection present");
104 return ErrNoConnection
;
107 rv
= nwClient
->send(SQL_NW_PKT_COMMIT
);
108 if (rv
== ErrNoConnection
) {
109 setConnClosed(false);
112 rv
= nwClient
->receive();
113 if (rv
== ErrNoConnection
|| rv
== ErrPeerTimeOut
) {
114 setConnClosed(false);
115 return ErrNoConnection
;
120 DbRetVal
SqlNwConnection::rollback()
123 printError(ErrNoConnection
, "No connection present");
124 return ErrNoConnection
;
127 rv
= nwClient
->send(SQL_NW_PKT_ROLLBACK
);
128 if (rv
== ErrNoConnection
) {
129 setConnClosed(false);
132 rv
= nwClient
->receive();
133 if (rv
== ErrNoConnection
|| rv
== ErrPeerTimeOut
) {
134 setConnClosed(false);
135 return ErrNoConnection
;