*** empty log message ***
[csql.git] / src / sqlnetwork / SqlNwConnection.cxx
blob9cf898736e3e233ba3894bbc567fbd11b770e881
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 <SqlNwConnection.h>
22 #include <Network.h>
23 #include <CSql.h>
25 DbRetVal SqlNwConnection::connect (char *user, char * pass)
27 DbRetVal rv = OK;
28 if (isConnOpen) {
29 printError(ErrAlready, "Connection is already open");
30 return ErrAlready;
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();
36 if (rv != OK) {
37 printError(rv, "Connection failed");
38 delete nwClient;
39 nwClient = NULL;
40 return rv;
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);
47 pkt->marshall();
48 rv = nwClient->send(SQL_NW_PKT_CONNECT, buffer, bufsize);
49 printDebug(DM_Network, "DEBUG:::NWCONNECT send connect pkt %d\n", rv);
50 if (rv != OK) {
51 printError(rv, "Data could not be sent");
52 delete pkt;
53 delete nwClient;
54 nwClient = NULL;
55 return rv;
57 rv = nwClient->receive();
58 printDebug(DM_Network, "DEBUG:::NWCONNECT receive connect pkt %d \n", rv);
59 if (rv == ErrPeerTimeOut) {
60 delete pkt;
61 delete nwClient;
62 nwClient = NULL;
63 return ErrNoConnection;
65 ResponsePacket *rpkt = (ResponsePacket *) ((TCPClient *)nwClient)->respPkt;
66 if (rpkt->errRetVal != OK) {
67 delete pkt;
68 delete nwClient;
69 nwClient = NULL;
70 return rpkt->errRetVal;
72 isConnOpen = true;
73 delete pkt;
74 return OK;
76 DbRetVal SqlNwConnection::disconnect()
78 DbRetVal rv = OK;
79 if (! isConnOpen) {
80 printError(ErrNoConnection, "No connection present");
81 return ErrNoConnection;
83 rv = nwClient->disconnect();
84 isConnOpen=false;
85 delete nwClient;
86 nwClient = NULL;
87 return rv;
90 DbRetVal SqlNwConnection::beginTrans(IsolationLevel isoLevel, TransSyncMode mode)
92 if (! isConnOpen) {
93 printError(ErrNoConnection, "No connection present");
94 return ErrNoConnection;
96 return OK;
99 DbRetVal SqlNwConnection::commit()
101 if (! isConnOpen) {
102 printError(ErrNoConnection, "No connection present");
103 return ErrNoConnection;
105 DbRetVal rv = OK;
106 rv = nwClient->send(SQL_NW_PKT_COMMIT);
107 if (rv == ErrNoConnection) {
108 setConnClosed(false);
109 return rv;
111 rv = nwClient->receive();
112 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
113 setConnClosed(false);
114 return ErrNoConnection;
116 return rv;
119 DbRetVal SqlNwConnection::rollback()
121 if (! isConnOpen) {
122 printError(ErrNoConnection, "No connection present");
123 return ErrNoConnection;
125 DbRetVal rv = OK;
126 rv = nwClient->send(SQL_NW_PKT_ROLLBACK);
127 if (rv == ErrNoConnection) {
128 setConnClosed(false);
129 return rv;
131 rv = nwClient->receive();
132 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
133 setConnClosed(false);
134 return ErrNoConnection;
136 return rv;