cleanup
[csql.git] / src / sqlnetwork / SqlNwConnection.cxx
blob5a5d2b2192cb15ac620a8a186a19e8b5aada5373
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 if (rv != OK) {
50 printError(rv, "Data could not be sent");
51 delete pkt;
52 delete nwClient;
53 nwClient = NULL;
54 return rv;
56 rv = nwClient->receive();
57 if (rv == ErrPeerTimeOut) {
58 delete pkt;
59 delete nwClient;
60 nwClient = NULL;
61 return ErrNoConnection;
63 ResponsePacket *rpkt = (ResponsePacket *) ((TCPClient *)nwClient)->respPkt;
64 char *ptr = (char *) &rpkt->retVal;
65 /* if (*ptr != 1) {
66 printError(ErrPeerResponse, "%s", rpkt->errorString);
67 delete pkt;
68 nwClient->disconnect();
69 delete nwClient;
70 nwClient = NULL;
71 return ErrPeerResponse;
72 }*/
73 isConnOpen = true;
74 delete pkt;
75 return rv;
77 DbRetVal SqlNwConnection::disconnect()
79 DbRetVal rv = OK;
80 if (! isConnOpen) {
81 printError(ErrNoConnection, "No connection present");
82 return ErrNoConnection;
84 rv = nwClient->disconnect();
85 isConnOpen=false;
86 delete nwClient;
87 nwClient = NULL;
88 return rv;
91 DbRetVal SqlNwConnection::beginTrans(IsolationLevel isoLevel, TransSyncMode mode)
93 if (! isConnOpen) {
94 printError(ErrNoConnection, "No connection present");
95 return ErrNoConnection;
97 return OK;
100 DbRetVal SqlNwConnection::commit()
102 if (! isConnOpen) {
103 printError(ErrNoConnection, "No connection present");
104 return ErrNoConnection;
106 DbRetVal rv = OK;
107 rv = nwClient->send(SQL_NW_PKT_COMMIT);
108 if (rv == ErrNoConnection) {
109 setConnClosed(false);
110 return rv;
112 rv = nwClient->receive();
113 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
114 setConnClosed(false);
115 return ErrNoConnection;
117 return rv;
120 DbRetVal SqlNwConnection::rollback()
122 if (! isConnOpen) {
123 printError(ErrNoConnection, "No connection present");
124 return ErrNoConnection;
126 DbRetVal rv = OK;
127 rv = nwClient->send(SQL_NW_PKT_ROLLBACK);
128 if (rv == ErrNoConnection) {
129 setConnClosed(false);
130 return rv;
132 rv = nwClient->receive();
133 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
134 setConnClosed(false);
135 return ErrNoConnection;
137 return rv;