Bug in putting the prepare packet in the list
[csql.git] / src / network / SqlNetworkHandler.cxx
blob8871179cc63554d4df2b96899911c0fec156bbc9
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 <SqlNetworkHandler.h>
21 #include <AbsSqlConnection.h>
22 #include <SqlConnection.h>
23 #include <SqlOdbcConnection.h>
24 #include <AbsSqlStatement.h>
25 #include <SqlStatement.h>
26 #include <SqlOdbcStatement.h>
28 #include <SqlLogStatement.h>
30 List SqlNetworkHandler::stmtList;
31 AbsSqlConnection* SqlNetworkHandler::conn;
32 SqlApiImplType SqlNetworkHandler::type;
34 int SqlNetworkHandler::process(PacketHeader &header, char *buffer)
36 switch(header.packetType)
38 case NW_PKT_PREPARE:
39 return processPrepare(header, buffer);
40 break;
41 case NW_PKT_COMMIT:
42 return processCommit(header, buffer);
43 break;
47 int SqlNetworkHandler::processCommit(PacketHeader &header, char *buffer)
49 printf("COMMIT \n");
50 PacketCommit *pkt = new PacketCommit();
51 pkt->setBuffer(buffer);
52 pkt->setBufferSize(header.packetLength);
53 pkt->unmarshall();
54 List pktList;
55 pkt->getExecPacketList(stmtList, pktList);
56 DbRetVal rv = applyExecPackets(stmtList, pktList);
57 int response = 1;
58 if (rv != OK)
60 printf("Unable to apply the exec packets\n");
61 response =0;
63 return response;
66 int SqlNetworkHandler::processFree(PacketHeader &header, char *buffer)
68 PacketFree *pkt = new PacketFree();
69 pkt->setBuffer(buffer);
70 pkt->setBufferSize(header.packetLength);
71 pkt->unmarshall();
72 //printf("FREE %d \n", pkt->stmtID);
73 int response =1;
74 //This wont work for two statement executed in same transaction using same SqlStatement object using free.
75 //so do not delete now and put a flag 'readyfordelete' in NetworkStmt object and delete it during execute
77 ListIterator iter = stmtList.getIterator();
78 NetworkStmt *stmt, *removeStmt = NULL;
79 while (iter.hasElement())
81 stmt = (NetworkStmt*)iter.nextElement();
82 if (stmt->srcNetworkID == header.srcNetworkID
83 && stmt->stmtID == pkt->stmtID)
85 removeStmt = stmt;
86 break;
89 if (removeStmt) stmtList.remove(removeStmt);
90 else printf("Statement id %d not found in list \n", pkt->stmtID);
92 return response;
94 int SqlNetworkHandler::processPrepare(PacketHeader &header, char *buffer)
96 PacketPrepare *pkt = new PacketPrepare();
97 pkt->setBuffer(buffer);
98 pkt->setBufferSize(header.packetLength);
99 pkt->unmarshall();
100 printf("PREPARE %d %s\n", pkt->stmtID, pkt->stmtString);
101 //for (int i =0 ; i < pkt->noParams; i++)
102 //printf("PREPARE type %d length %d \n", pkt->type[i], pkt->length[i]);
103 int response =1;
104 //TODO::add it to the SqlStatement list
105 AbsSqlStatement *sqlstmt = SqlFactory::createStatement(type);
106 sqlstmt->setConnection(conn);
107 NetworkStmt *nwStmt = new NetworkStmt();
108 printf("nwstmt in prepare %x %x\n", nwStmt, sqlstmt);
109 printf("Statement string %s\n", pkt->stmtString);
110 nwStmt->srcNetworkID = header.srcNetworkID;
111 nwStmt->stmtID = pkt->stmtID;
112 nwStmt->stmt = sqlstmt;
113 DbRetVal rv = sqlstmt->prepare(pkt->stmtString);
114 if (rv != OK)
116 printError(ErrSysInit, "statement prepare failed\n");
117 response = 0;
118 return response;
120 BindSqlField *bindField = NULL;
121 //populate paramList
122 for (int i = 0; i < pkt->noParams; i++)
124 bindField = new BindSqlField();
125 bindField->type = (DataType) pkt->type[i];
126 bindField->length = pkt->length[i];
127 bindField->value = AllDataType::alloc(bindField->type,
128 bindField->length);
129 nwStmt->paramList.append(bindField);
130 printf("Adding element to paramList for type %d\n",bindField->type);
132 stmtList.append(nwStmt);
133 return response;
137 DbRetVal SqlNetworkHandler::applyExecPackets(List sList, List pList)
139 ListIterator stmtIter = sList.getIterator();
140 NetworkStmt *nwstmt;
141 DbRetVal rv = conn->beginTrans();
142 if (rv != OK) return rv;
143 ListIterator pktIter = pList.getIterator();
144 PacketExecute *pkt;
145 int i = 0;
146 BindSqlField *bindField;
147 while (pktIter.hasElement())
149 pkt = (PacketExecute*) pktIter.nextElement();
150 printf("EXEC packt ptr %x in apply \n", pkt);
151 stmtIter.reset();
152 bool found = false;
153 while (stmtIter.hasElement())
155 nwstmt = (NetworkStmt*) stmtIter.nextElement();
156 if (nwstmt->stmtID == pkt->stmtID) {found = true ; break;}
158 if (!found) {
159 printf("stmt not found in list. Negleting unreplicated table...\n");
160 continue;
162 printf("nwstmt ptr in apply %x\n", nwstmt);
163 ListIterator paramIter = nwstmt->paramList.getIterator();
164 i = 0;
165 while (paramIter.hasElement()) {
166 bindField = (BindSqlField*) paramIter.nextElement();
167 setParamValues(nwstmt->stmt, i+1, bindField->type, bindField->length, pkt->paramValues[i]);
168 printf("setting %d parameter of type %d\n", i, bindField->type);
169 i++;
171 int rows= 0;
172 DbRetVal rv = nwstmt->stmt->execute(rows);
173 printf("sqlHandler rv ois %d\n", rv);
174 if (rv != OK )
176 printf("sql execute failed with rv %d\n", rv);
177 //TODO::log all things like SQL statements to a file
178 SqlNetworkHandler::conn->rollback();
179 printError(ErrPeerExecFailed, "Transaction Rolledback\n");
180 return ErrPeerExecFailed;
183 SqlNetworkHandler::conn->commit();
184 printf("Transaction committed\n");
185 return OK;
188 void SqlNetworkHandler::setParamValues(AbsSqlStatement *stmt, int parampos, DataType type,
189 int length, char *value)
191 switch(type)
193 case typeInt:
194 stmt->setIntParam(parampos, *(int*)value);
195 break;
196 case typeLong:
197 stmt->setLongParam(parampos, *(long*)value);
198 break;
199 case typeLongLong:
200 stmt->setLongLongParam(parampos, *(long long*)value);
201 break;
202 case typeShort:
203 stmt->setShortParam(parampos, *(short*)value);
204 break;
205 case typeByteInt:
206 stmt->setByteIntParam(parampos, *(char*)value);
207 break;
208 case typeDouble:
209 stmt->setDoubleParam(parampos, *(double*)value);
210 break;
211 case typeFloat:
212 stmt->setFloatParam(parampos, *(float*)value);
213 break;
214 case typeDate:
215 stmt->setDateParam(parampos, *(Date*)value);
216 break;
217 case typeTime:
218 stmt->setTimeParam(parampos, *(Time*)value);
219 break;
220 case typeTimeStamp:
221 stmt->setTimeStampParam(parampos, *(TimeStamp*)value);
222 break;
223 case typeString:
225 char *d =(char*)value;
226 d[length-1] = '\0';
227 stmt->setStringParam(parampos, (char*)value);
228 break;
231 return;