adding test scripts
[csql.git] / src / sqlnetwork / SqlNwConnection.cxx
blob3f35c2a8daea1880ccccb41a3df864da2fda9444
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 if (rpkt->errRetVal != OK) {
65 delete pkt;
66 delete nwClient;
67 nwClient = NULL;
68 return rpkt->errRetVal;
70 isConnOpen = true;
71 delete pkt;
72 return OK;
74 DbRetVal SqlNwConnection::disconnect()
76 DbRetVal rv = OK;
77 if (! isConnOpen) {
78 printError(ErrNoConnection, "No connection present");
79 return ErrNoConnection;
81 rv = nwClient->disconnect();
82 isConnOpen=false;
83 delete nwClient;
84 nwClient = NULL;
85 return rv;
88 DbRetVal SqlNwConnection::beginTrans(IsolationLevel isoLevel, TransSyncMode mode)
90 if (! isConnOpen) {
91 printError(ErrNoConnection, "No connection present");
92 return ErrNoConnection;
94 return OK;
97 DbRetVal SqlNwConnection::commit()
99 if (! isConnOpen) {
100 printError(ErrNoConnection, "No connection present");
101 return ErrNoConnection;
103 DbRetVal rv = OK;
104 rv = nwClient->send(SQL_NW_PKT_COMMIT);
105 if (rv == ErrNoConnection) {
106 setConnClosed(false);
107 return rv;
109 rv = nwClient->receive();
110 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
111 setConnClosed(false);
112 return ErrNoConnection;
114 return rv;
117 DbRetVal SqlNwConnection::rollback()
119 if (! isConnOpen) {
120 printError(ErrNoConnection, "No connection present");
121 return ErrNoConnection;
123 DbRetVal rv = OK;
124 rv = nwClient->send(SQL_NW_PKT_ROLLBACK);
125 if (rv == ErrNoConnection) {
126 setConnClosed(false);
127 return rv;
129 rv = nwClient->receive();
130 if (rv == ErrNoConnection || rv == ErrPeerTimeOut) {
131 setConnClosed(false);
132 return ErrNoConnection;
134 return rv;