SqlNwConnection and statement can be created by SqlFactory class.
[csql.git] / src / sqlnetwork / SqlNwConnection.cxx
blobfbade5549ca5d312c627f6afc258f7196c2218a4
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 <SqlNwConnection.h>
21 #include <Network.h>
22 #include<os.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 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 return rv;
40 SqlPacketConnect *pkt = new SqlPacketConnect();
41 pkt->setConnParam(user, pass, sqlApiImplType);
42 pkt->setBufferSize(bufsize);
43 char * buffer = (char *) malloc(bufsize);
44 pkt->setBuffer(buffer);
45 pkt->marshall();
46 rv = send(SQL_NW_PKT_CONNECT, buffer, bufsize);
47 if (rv != OK) {
48 printError(rv, "Data could not be sent");
49 return rv;
51 int response = 0;
52 rv = nwClient->receive();
53 ResponsePacket *rpkt = (ResponsePacket *) ((TCPClient *)nwClient)->respPkt;
54 char *ptr = (char *) &rpkt->retVal;
55 if (*ptr != 1) {
56 printError(ErrPeerResponse, "%s", rpkt->errorString);
57 nwClient->disconnect();
58 delete nwClient;
59 delete pkt;
60 return ErrPeerResponse;
62 isConnOpen = true;
63 delete pkt;
64 return rv;
66 DbRetVal SqlNwConnection::disconnect()
68 DbRetVal rv = OK;
69 if (! isConnOpen) {
70 printError(ErrNoConnection, "No connection present");
71 return ErrNoConnection;
73 rv = nwClient->disconnect();
74 isConnOpen=false;
75 delete nwClient;
76 return rv;
79 DbRetVal SqlNwConnection::beginTrans(IsolationLevel isoLevel, TransSyncMode mode)
81 if (! isConnOpen) {
82 printError(ErrNoConnection, "No connection present");
83 return ErrNoConnection;
85 return OK;
88 DbRetVal SqlNwConnection::commit()
90 if (! isConnOpen) {
91 printError(ErrNoConnection, "No connection present");
92 return ErrNoConnection;
94 DbRetVal rv = OK;
95 rv = nwClient->send(SQL_NW_PKT_COMMIT, NULL, 0);
96 int response = 0;
97 return nwClient->receive();
100 DbRetVal SqlNwConnection::rollback()
102 if (! isConnOpen) {
103 printError(ErrNoConnection, "No connection present");
104 return ErrNoConnection;
106 DbRetVal rv = OK;
107 rv = nwClient->send(SQL_NW_PKT_ROLLBACK, NULL, 0);
108 int response = 0;
109 return nwClient->receive();