1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
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. *
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. *
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 ***************************************************************************/
23 #include <SqlLogStatement.h>
25 DbRetVal
PacketPrepare::marshall()
27 bufferSize
= sizeof(int) * 4 + strlen(stmtString
) + 1;
28 printf("NOOFPARAMS %d size %d\n", noParams
, bufferSize
);
29 printf("stmt length %s size %d\n", stmtString
, strlen(stmtString
));
30 printf("noParams is %d\n", noParams
);
32 bufferSize
= bufferSize
+ 2 * sizeof(int) * noParams
;
33 buffer
= (char*) malloc(bufferSize
);
34 printf("start of the buffer %x\n", buffer
);
35 *(int*)buffer
= stmtID
;
36 char *bufIter
= buffer
+ sizeof(int);
37 *(int*)bufIter
= syncMode
;
38 bufIter
= bufIter
+ sizeof(int);
39 *(int*)bufIter
= strlen(stmtString
);
40 bufIter
= bufIter
+ sizeof(int);
41 *(int*)bufIter
= noParams
;
42 bufIter
= bufIter
+ sizeof(int);
44 memcpy(bufIter
, type
, sizeof(int) * noParams
);
45 bufIter
= bufIter
+ sizeof(int)* noParams
;
46 memcpy(bufIter
, length
, sizeof(int) * noParams
);
47 bufIter
= bufIter
+ sizeof(int)* noParams
;
49 printf("stmtString is in %x\n", bufIter
);
50 strcpy(bufIter
, stmtString
);
53 DbRetVal
PacketPrepare::unmarshall()
55 stmtID
= *(int*)buffer
;
56 printf("start of the buffer is %x\n", buffer
);
57 char *bufIter
= buffer
+ sizeof (int);
58 syncMode
= *(int*)bufIter
;
59 bufIter
= bufIter
+ sizeof(int);
60 stmtLength
= *(int*)bufIter
;
61 bufIter
= bufIter
+ sizeof(int);
62 noParams
= *(int*)bufIter
;
63 bufIter
= bufIter
+ sizeof(int);
65 type
= (int*) bufIter
;
66 bufIter
= bufIter
+ sizeof(int) * noParams
;
67 length
= (int*) bufIter
;
68 bufIter
= bufIter
+ sizeof(int) * noParams
;
71 printf("stmtString ptr is %x\n", stmtString
);
72 stmtString
[stmtLength
+1] = '\0';
75 DbRetVal
PacketFree::marshall()
77 bufferSize
= sizeof(int);
78 buffer
= (char*) malloc(bufferSize
);
79 *(int*)buffer
= stmtID
;
80 //printf("PacketFree marshall with stmtid %d\n", stmtID);
83 DbRetVal
PacketFree::unmarshall()
85 stmtID
= *(int*)buffer
;
88 void PacketExecute::setParams(List list
)
91 noParams
= list
.size();
92 paramValues
= new char*[noParams
];
93 BindSqlField
* bindField
= NULL
;
94 for (int i
= 0 ; i
< noParams
; i
++)
96 bindField
= (BindSqlField
*) paramList
.get(i
+1);
97 paramValues
[i
] = (char*) bindField
->value
;
101 void PacketExecute::setStatementList(List stmtlist
)
106 DbRetVal
PacketExecute::marshall()
108 bufferSize
= sizeof(int)+ sizeof(int);
109 BindSqlField
* bindField
= NULL
;
110 //printf("noParams %d\n", noParams);
111 for (int i
= 0 ; i
< noParams
; i
++)
113 bindField
= (BindSqlField
*) paramList
.get(i
+1);
114 bufferSize
= bufferSize
+ AllDataType::size(bindField
->type
, bindField
->length
);
116 buffer
= (char*) malloc(bufferSize
);
117 *(int*)buffer
= stmtID
;
118 char* bufIter
= (char*) buffer
+ sizeof(int);
119 *(int*)bufIter
= noParams
;
120 bufIter
= (char*) bufIter
+ sizeof(int);
121 for (int i
= 0 ; i
< noParams
; i
++)
123 bindField
= (BindSqlField
*) paramList
.get(i
+1);
124 AllDataType::copyVal(bufIter
, bindField
->value
, bindField
->type
,bindField
->length
);
125 bufIter
= bufIter
+ AllDataType::size(bindField
->type
, bindField
->length
);
127 //printf("PacketExecute marshall with stmtid %d\n", stmtID);
130 DbRetVal
PacketExecute::unmarshall()
132 stmtID
= *(int*)buffer
;
133 char *bufIter
= buffer
+ sizeof(int);
134 noParams
= *(int*)bufIter
;
135 bufIter
= bufIter
+sizeof(int);
136 ListIterator stmtIter
= stmtList
.getIterator();
138 while (stmtIter
.hasElement())
140 stmt
= (NetworkStmt
*) stmtIter
.nextElement();
141 //TODO::Also check teh srcNetworkID
142 if (stmt
->stmtID
== stmtID
) break;
144 //printf("PKTEXEC stmtid %d params %d\n", stmtID, noParams);
145 if (noParams
== 0) return OK
;
146 paramValues
= new char*[noParams
];
147 ListIterator paramIter
= stmt
->paramList
.getIterator();
148 BindSqlField
*bindField
= NULL
;
149 for (int i
=0; i
<noParams
; i
++)
151 paramValues
[i
] = bufIter
;
152 bindField
= (BindSqlField
*) stmt
->paramList
.get(i
+1);
153 bufIter
= bufIter
+ AllDataType::size(bindField
->type
, bindField
->length
);
158 void PacketCommit::setExecPackets(int tid
, List list
)
161 noOfStmts
= list
.size();
162 //printf("totalstmts %d\n", noOfStmts);
163 stmtBufSize
= new int[noOfStmts
];
164 stmtBuffer
= new char*[noOfStmts
];
165 PacketExecute
* pkt
= NULL
;
167 for (int i
= 0 ; i
< noOfStmts
; i
++)
169 pkt
= (PacketExecute
*) list
.get(i
+1);
170 //printf("pkt ptr from PACKETCommit is %x\n", pkt);
171 if (pkt
== NULL
) printf("pkt is null.should never happen\n");
172 stmtBufSize
[i
] = pkt
->getBufferSize();
173 //printf("Inside loop exec pkt size is %d\n", stmtBufSize[i]);
174 stmtBuffer
[i
] = pkt
->getMarshalledBuffer();
175 //printf("Inside loop exec %d %d\n", *(int*)stmtBuffer[i],
176 // *(int*)(((char*)stmtBuffer[i]) + 4));
177 totalSize
= totalSize
+ stmtBufSize
[i
];
178 //printf(" stmt no %d totalSize = %d\n", i+1, totalSize);
180 totalSize
= sizeof(int) + sizeof(int) + noOfStmts
* sizeof(int) +
182 bufferSize
= totalSize
;
185 DbRetVal
PacketCommit::marshall()
187 //printf("Marshalling commit packet %d %d\n", txnID, noOfStmts);
188 buffer
= (char*) malloc(bufferSize
);
189 *(int*)buffer
= txnID
;
190 char* bufIter
= (char*) buffer
+ sizeof(int);
191 *(int*)bufIter
= noOfStmts
;
192 bufIter
= (char*) bufIter
+ sizeof(int);
193 memcpy(bufIter
, stmtBufSize
, noOfStmts
*sizeof(int));
194 bufIter
= (char*) bufIter
+ noOfStmts
* sizeof(int);
195 for (int i
=0; i
< noOfStmts
; i
++)
197 //printf("stmtbufsize %d\n", stmtBufSize[i]);
198 memcpy(bufIter
, stmtBuffer
[i
], stmtBufSize
[i
]);
199 bufIter
= bufIter
+ stmtBufSize
[i
];
203 DbRetVal
PacketCommit::unmarshall()
205 txnID
= *(int*)buffer
;
206 char *bufIter
= buffer
+ sizeof(int);
207 noOfStmts
= *(int*)bufIter
;
208 bufIter
= bufIter
+ sizeof(int);
209 stmtBufSize
= new int[noOfStmts
];
210 memcpy(stmtBufSize
, bufIter
, noOfStmts
*sizeof(int));
211 bufIter
= bufIter
+ noOfStmts
* sizeof(int);
212 stmtBuffer
= new char*[noOfStmts
];
213 for (int i
= 0 ; i
<noOfStmts
; i
++)
215 stmtBuffer
[i
] = bufIter
;
216 bufIter
= bufIter
+ stmtBufSize
[i
];
217 //printf("stmtbufsize %d\n", stmtBufSize[i]);
220 //call unmarshall before calling this
221 void PacketCommit::getExecPacketList(List stmtList
, List
&list
)
223 PacketExecute
* pkt
= NULL
;
224 for (int i
= 0 ; i
< noOfStmts
; i
++)
226 pkt
= new PacketExecute();
227 pkt
->setBuffer(stmtBuffer
[i
]);
228 pkt
->setBufferSize(stmtBufSize
[i
]);
229 pkt
->setStatementList(stmtList
);