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 printDebug(DM_Network
, "PacketPrepare::marshall called\n");
28 bufferSize
= sizeof(int) * 4 + strlen(stmtString
) + 1;
29 printDebug(DM_Network
, "NOOFPARAMS %d buffer size %d\n", noParams
, bufferSize
);
30 printDebug(DM_Network
, "stmt %s size %d\n", stmtString
, strlen(stmtString
));
31 printDebug(DM_Network
, "noParams is %d\n", noParams
);
33 bufferSize
= bufferSize
+ 2 * sizeof(int) * noParams
;
34 buffer
= (char*) malloc(bufferSize
);
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 strcpy(bufIter
, stmtString
);
50 printDebug(DM_Network
, "PacketPrepare::marshall ended\n");
53 DbRetVal
PacketPrepare::unmarshall()
55 printDebug(DM_Network
, "PacketPrepare::unmarshall called\n");
56 stmtID
= *(int*)buffer
;
57 printDebug(DM_Network
, "start of the buffer is %x\n", buffer
);
58 char *bufIter
= buffer
+ sizeof (int);
59 syncMode
= *(int*)bufIter
;
60 bufIter
= bufIter
+ sizeof(int);
61 stmtLength
= *(int*)bufIter
;
62 bufIter
= bufIter
+ sizeof(int);
63 noParams
= *(int*)bufIter
;
64 bufIter
= bufIter
+ sizeof(int);
66 type
= (int*) bufIter
;
67 bufIter
= bufIter
+ sizeof(int) * noParams
;
68 length
= (int*) bufIter
;
69 bufIter
= bufIter
+ sizeof(int) * noParams
;
72 printDebug(DM_Network
, "stmtString ptr is %x\n", stmtString
);
73 stmtString
[stmtLength
+1] = '\0';
74 printDebug(DM_Network
, "PacketPrepare::unmarshall ended\n");
77 DbRetVal
PacketFree::marshall()
79 bufferSize
= sizeof(int);
80 buffer
= (char*) malloc(bufferSize
);
81 *(int*)buffer
= stmtID
;
84 DbRetVal
PacketFree::unmarshall()
86 stmtID
= *(int*)buffer
;
89 void PacketExecute::setParams(List list
)
92 noParams
= list
.size();
93 paramValues
= new char*[noParams
];
94 BindSqlField
* bindField
= NULL
;
95 for (int i
= 0 ; i
< noParams
; i
++)
97 bindField
= (BindSqlField
*) paramList
.get(i
+1);
98 paramValues
[i
] = (char*) bindField
->value
;
102 void PacketExecute::setStatementList(List stmtlist
)
107 DbRetVal
PacketExecute::marshall()
109 bufferSize
= sizeof(int)+ sizeof(int);
110 BindSqlField
* bindField
= NULL
;
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
);
129 DbRetVal
PacketExecute::unmarshall()
131 stmtID
= *(int*)buffer
;
132 char *bufIter
= buffer
+ sizeof(int);
133 noParams
= *(int*)bufIter
;
134 bufIter
= bufIter
+sizeof(int);
135 ListIterator stmtIter
= stmtList
.getIterator();
137 while (stmtIter
.hasElement())
139 stmt
= (NetworkStmt
*) stmtIter
.nextElement();
140 //TODO::Also check teh srcNetworkID
141 if (stmt
->stmtID
== stmtID
) break;
143 if (noParams
== 0) return OK
;
144 paramValues
= new char*[noParams
];
145 ListIterator paramIter
= stmt
->paramList
.getIterator();
146 BindSqlField
*bindField
= NULL
;
147 for (int i
=0; i
<noParams
; i
++)
149 paramValues
[i
] = bufIter
;
150 bindField
= (BindSqlField
*) stmt
->paramList
.get(i
+1);
151 bufIter
= bufIter
+ AllDataType::size(bindField
->type
, bindField
->length
);
156 void PacketCommit::setExecPackets(int tid
, List list
)
159 noOfStmts
= list
.size();
160 stmtBufSize
= new int[noOfStmts
];
161 stmtBuffer
= new char*[noOfStmts
];
162 PacketExecute
* pkt
= NULL
;
164 for (int i
= 0 ; i
< noOfStmts
; i
++)
166 pkt
= (PacketExecute
*) list
.get(i
+1);
167 if (pkt
== NULL
) printError(ErrSysFatal
, "pkt is null.should never happen\n");
168 stmtBufSize
[i
] = pkt
->getBufferSize();
169 stmtBuffer
[i
] = pkt
->getMarshalledBuffer();
170 // *(int*)(((char*)stmtBuffer[i]) + 4));
171 totalSize
= totalSize
+ stmtBufSize
[i
];
173 totalSize
= sizeof(int) + sizeof(int) + noOfStmts
* sizeof(int) +
175 bufferSize
= totalSize
;
178 DbRetVal
PacketCommit::marshall()
180 buffer
= (char*) malloc(bufferSize
);
181 *(int*)buffer
= txnID
;
182 char* bufIter
= (char*) buffer
+ sizeof(int);
183 *(int*)bufIter
= noOfStmts
;
184 bufIter
= (char*) bufIter
+ sizeof(int);
185 memcpy(bufIter
, stmtBufSize
, noOfStmts
*sizeof(int));
186 bufIter
= (char*) bufIter
+ noOfStmts
* sizeof(int);
187 for (int i
=0; i
< noOfStmts
; i
++)
189 memcpy(bufIter
, stmtBuffer
[i
], stmtBufSize
[i
]);
190 bufIter
= bufIter
+ stmtBufSize
[i
];
194 DbRetVal
PacketCommit::unmarshall()
196 txnID
= *(int*)buffer
;
197 char *bufIter
= buffer
+ sizeof(int);
198 noOfStmts
= *(int*)bufIter
;
199 bufIter
= bufIter
+ sizeof(int);
200 stmtBufSize
= new int[noOfStmts
];
201 memcpy(stmtBufSize
, bufIter
, noOfStmts
*sizeof(int));
202 bufIter
= bufIter
+ noOfStmts
* sizeof(int);
203 stmtBuffer
= new char*[noOfStmts
];
204 for (int i
= 0 ; i
<noOfStmts
; i
++)
206 stmtBuffer
[i
] = bufIter
;
207 bufIter
= bufIter
+ stmtBufSize
[i
];
210 //call unmarshall before calling this
211 void PacketCommit::getExecPacketList(List stmtList
, List
&list
)
213 PacketExecute
* pkt
= NULL
;
214 for (int i
= 0 ; i
< noOfStmts
; i
++)
216 pkt
= new PacketExecute();
217 pkt
->setBuffer(stmtBuffer
[i
]);
218 pkt
->setBufferSize(stmtBufSize
[i
]);
219 pkt
->setStatementList(stmtList
);