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 ***************************************************************************/
20 #include <SqlLogStatement.h>
21 #include <TableConfig.h>
23 bool SqlLogStatement::isNonSelectDML(char *stmtstr
)
25 if (strlen(stmtstr
) <= 6) return false;
26 if (strncasecmp(stmtstr
,"INSERT", 6) == 0) return true;
27 else if (strncasecmp(stmtstr
, "UPDATE", 6) ==0) return true;
28 else if (strncasecmp(stmtstr
, "DELETE", 6) ==0) return true;
32 DbRetVal
SqlLogStatement::executeDirect(char *stmtstr
)
36 rv
= prepare(stmtstr
);
37 if (rv
!= OK
) return rv
;
39 if (rv
!= OK
) return rv
;
43 DbRetVal
SqlLogStatement::prepare(char *stmtstr
)
46 SqlLogConnection
*conn
= (SqlLogConnection
*)con
;
48 rv
= innerStmt
->prepare(stmtstr
);
49 if (rv
!= OK
) { isNonSelDML
= false; return rv
; }
50 if (innerStmt
->getStmtType() == SelectStatement
) {
51 isNonSelDML
= false; return rv
;
54 if (Conf::config
.useDurability()) {
55 if (strlen(stmtstr
) > 6 && ((strncasecmp(stmtstr
,"CREATE", 6) == 0) ||
56 (strncasecmp(stmtstr
,"DROP", 4) == 0))) {
57 sid
= SqlLogStatement::stmtUID
.getID(STMT_ID
);
58 printDebug(DM_SqlLog
, "CREATE|DROP: stmt id = %d\n", sid
);
59 conn
->fileLogPrepare(0, sid
, strlen(stmtstr
)+1, stmtstr
, NULL
);
64 if (!isNonSelectDML(stmtstr
)) { isNonSelDML
= false; return rv
;}
67 tblName
= innerStmt
->getTableName();
68 unsigned int mode
= TableConf::config
.getTableMode(tblName
);
69 if (TableConf::config
.isTableCached(mode
)) isCached
= true;
72 if (strncasecmp(stmtstr
,"CACHE", 5) == 0 ||
73 strncasecmp(stmtstr
,"UNCACHE", 7) == 0)
78 if ( (!Conf::config
.useCache() ||
79 (Conf::config
.useCache() && Conf::config
.getCacheMode()==SYNC_MODE
)) &&
80 !Conf::config
.useDurability())
87 int txnId
=conn
->getTxnID();
88 sid
= stmtUID
.getID(STMT_ID
);
89 printDebug(DM_SqlLog
, "stmt id = %d\n", sid
);
90 if ((Conf::config
.useCache() && Conf::config
.getCacheMode() == ASYNC_MODE
)
91 && !conn
->noMsgLog
&& isCached
)
92 conn
->msgPrepare(txnId
, sid
, strlen(stmtstr
) + 1, stmtstr
, tblName
);
93 if (Conf::config
.useDurability())
94 conn
->fileLogPrepare(txnId
, sid
, strlen(stmtstr
) + 1, stmtstr
, NULL
);
98 bool SqlLogStatement::isSelect()
100 if (innerStmt
) return innerStmt
->isSelect();
104 DbRetVal
SqlLogStatement::execute(int &rowsAffected
)
106 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
108 if (innerStmt
) rv
= innerStmt
->execute(rowsAffected
);
109 if (!isNonSelDML
) return rv
;
110 if ( rv
!= OK
) return rv
;
111 if (!needLog
) return rv
;
112 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (sizeof(ExecLogInfo
));
113 elInfo
->stmtId
= sid
;
114 printDebug(DM_SqlLog
, "Execute: stmtID: %d", elInfo
->stmtId
);
115 elInfo
->type
= EXECONLY
;
116 printDebug(DM_SqlLog
, "Execute: ExType: %d", elInfo
->type
);
117 logConn
->addExecLog(elInfo
);
118 printDebug(DM_SqlLog
, "Execute: elem address: %x", elInfo
);
119 int size
= 2 * sizeof(int);
120 logConn
->addToExecLogSize(size
);
121 printDebug(DM_SqlLog
, "Execute: log length: %d", size
);
125 DbRetVal
SqlLogStatement::bindParam(int pos
, void* value
)
128 if (innerStmt
) rv
= innerStmt
->bindParam(pos
,value
);
129 if (rv
!= OK
) return rv
;
130 printError(ErrWarning
, "Deprecated and does not replicate or cache");
134 DbRetVal
SqlLogStatement::bindField(int pos
, void* value
)
137 if (innerStmt
) rv
= innerStmt
->bindField(pos
,value
);
138 if (rv
!= OK
) return rv
;
141 void* SqlLogStatement::fetch()
143 if (innerStmt
) return innerStmt
->fetch();
147 void* SqlLogStatement::fetch(DbRetVal
&rv
)
149 if (innerStmt
) return innerStmt
->fetch(rv
);
153 void* SqlLogStatement::fetchAndPrint(bool SQL
)
155 if (innerStmt
) return innerStmt
->fetchAndPrint(SQL
);
159 void* SqlLogStatement::next()
161 if (innerStmt
) return innerStmt
->next();
165 DbRetVal
SqlLogStatement::close()
167 if (innerStmt
) return innerStmt
->close();
171 void* SqlLogStatement::getFieldValuePtr( int pos
)
173 if (innerStmt
) return innerStmt
->getFieldValuePtr(pos
);
176 void* SqlLogStatement::getFieldValuePtr( char *name
)
178 if (innerStmt
) return innerStmt
->getFieldValuePtr(name
);
182 void SqlLogStatement::getProjFieldType(int *data
)
184 if (innerStmt
) return innerStmt
->getProjFieldType(data
);
188 int SqlLogStatement::noOfProjFields()
190 if (innerStmt
) return innerStmt
->noOfProjFields();
194 int SqlLogStatement::noOfParamFields()
196 if (innerStmt
) return innerStmt
->noOfParamFields();
200 DbRetVal
SqlLogStatement::getProjFldInfo (int projpos
, FieldInfo
*&fInfo
)
202 if (innerStmt
) return innerStmt
->getProjFldInfo(projpos
, fInfo
);
206 DbRetVal
SqlLogStatement::getParamFldInfo (int parampos
, FieldInfo
*&fInfo
)
208 if (innerStmt
) return innerStmt
->getParamFldInfo(parampos
, fInfo
);
212 DbRetVal
SqlLogStatement::free()
215 if (!isPrepared
) return OK
;
216 if (innerStmt
) rv
= innerStmt
->free();
217 if (rv
!= OK
) return rv
;
218 if (!needLog
) { isPrepared
= false; return rv
; }
219 if (isNonSelDML
&& isCached
) {
220 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
221 if (sid
!= 0 ) logConn
->freeLogs(sid
);
228 void SqlLogStatement::setShortParam(int paramPos
, short value
)
230 if (innerStmt
) innerStmt
->setShortParam(paramPos
,value
);
231 if (!isNonSelDML
) return;
232 if (!needLog
) return;
233 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
234 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(short);
235 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
236 elInfo
->stmtId
= sid
;
237 printDebug(DM_SqlLog
, "setShort: stmtID: %d", elInfo
->stmtId
);
238 elInfo
->type
= SETPARAM
;
239 printDebug(DM_SqlLog
, "setShort: ExecTp: %d", elInfo
->type
);
240 elInfo
->pos
= paramPos
;
241 printDebug(DM_SqlLog
, "setShort: PrmPos: %d", elInfo
->pos
);
242 elInfo
->dataType
= typeShort
;
243 printDebug(DM_SqlLog
, "setShort: DtType: %d", elInfo
->dataType
);
244 elInfo
->len
= sizeof (short);
245 printDebug(DM_SqlLog
, "setShort: Length: %d", elInfo
->len
);
246 *(short *)&elInfo
->value
= value
;
247 printDebug(DM_SqlLog
, "setShort: Value : %d", *(short *)&elInfo
->value
);
248 logConn
->addExecLog(elInfo
);
249 printDebug(DM_SqlLog
, "appended elem addr: %x", elInfo
);
250 logConn
->addToExecLogSize(buffer
);
251 printDebug(DM_SqlLog
, "appended bufsize: %d", buffer
);
254 void SqlLogStatement::setIntParam(int paramPos
, int value
)
256 if (innerStmt
) innerStmt
->setIntParam(paramPos
,value
);
257 if (!isNonSelDML
) return;
258 if (!needLog
) return;
259 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
260 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(int);
261 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
262 elInfo
->stmtId
= sid
;
263 printDebug(DM_SqlLog
, "setInt: stmtID: %d", elInfo
->stmtId
);
264 elInfo
->type
= SETPARAM
;
265 printDebug(DM_SqlLog
, "setInt: ExecTp: %d", elInfo
->type
);
266 elInfo
->pos
= paramPos
;
267 printDebug(DM_SqlLog
, "setInt: PrmPos: %d", elInfo
->pos
);
268 elInfo
->dataType
= typeInt
;
269 printDebug(DM_SqlLog
, "setInt: DtType: %d", elInfo
->dataType
);
270 elInfo
->len
= sizeof(int);
271 printDebug(DM_SqlLog
, "setInt: Length: %d", elInfo
->len
);
272 *(int *)&elInfo
->value
= value
;
273 printDebug(DM_SqlLog
, "setInt: Value : %d", *(int *)&elInfo
->value
);
274 logConn
->addExecLog(elInfo
);
275 printDebug(DM_SqlLog
, "appended elem addr: %x", elInfo
);
276 logConn
->addToExecLogSize(buffer
);
277 printDebug(DM_SqlLog
, "appended bufsize: %d", buffer
);
280 void SqlLogStatement::setLongParam(int paramPos
, long value
)
282 if (innerStmt
) innerStmt
->setLongParam(paramPos
,value
);
283 if (!isNonSelDML
) return;
284 if (!needLog
) return;
285 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
286 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(long);
287 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
288 elInfo
->stmtId
= sid
;
289 elInfo
->type
= SETPARAM
;
290 elInfo
->pos
= paramPos
;
291 elInfo
->dataType
= typeLong
;
292 elInfo
->len
= sizeof(long);
293 *(long *)&elInfo
->value
= value
;
294 logConn
->addExecLog(elInfo
);
295 logConn
->addToExecLogSize(buffer
);
298 void SqlLogStatement::setLongLongParam(int paramPos
, long long value
)
300 if (innerStmt
) innerStmt
->setLongLongParam(paramPos
,value
);
301 if (!isNonSelDML
) return;
302 if (!needLog
) return;
303 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
304 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(long long);
305 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
306 elInfo
->stmtId
= sid
;
307 elInfo
->type
= SETPARAM
;
308 elInfo
->pos
= paramPos
;
309 elInfo
->dataType
= typeLongLong
;
310 elInfo
->len
= sizeof (long long);
311 *(long long *)&elInfo
->value
= value
;
312 logConn
->addExecLog(elInfo
);
313 logConn
->addToExecLogSize(buffer
);
316 void SqlLogStatement::setByteIntParam(int paramPos
, ByteInt value
)
318 if (innerStmt
) innerStmt
->setByteIntParam(paramPos
,value
);
319 if (!isNonSelDML
) return;
320 if (!needLog
) return;
321 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
322 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(ByteInt
);
323 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
324 elInfo
->stmtId
= sid
;
325 elInfo
->type
= SETPARAM
;
326 elInfo
->pos
= paramPos
;
327 elInfo
->dataType
= typeByteInt
;
328 elInfo
->len
= sizeof(ByteInt
);
329 *(ByteInt
*)&elInfo
->value
= value
;
330 logConn
->addExecLog(elInfo
);
331 logConn
->addToExecLogSize(buffer
);
334 void SqlLogStatement::setFloatParam(int paramPos
, float value
)
336 if (innerStmt
) innerStmt
->setFloatParam(paramPos
,value
);
337 if (!isNonSelDML
) return;
338 if (!needLog
) return;
339 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
340 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(float);
341 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
342 elInfo
->stmtId
= sid
;
343 elInfo
->type
= SETPARAM
;
344 elInfo
->pos
= paramPos
;
345 elInfo
->dataType
= typeFloat
;
346 elInfo
->len
= sizeof(float);
347 *(float *)&elInfo
->value
= value
;
348 logConn
->addExecLog(elInfo
);
349 logConn
->addToExecLogSize(buffer
);
352 void SqlLogStatement::setDoubleParam(int paramPos
, double value
)
354 if (innerStmt
) innerStmt
->setDoubleParam(paramPos
,value
);
355 if (!isNonSelDML
) return;
356 if (!needLog
) return;
357 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
358 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(double);
359 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
360 elInfo
->stmtId
= sid
;
361 elInfo
->type
= SETPARAM
;
362 elInfo
->pos
= paramPos
;
363 elInfo
->dataType
= typeDouble
;
364 elInfo
->len
= sizeof(double);
365 *(double *)&elInfo
->value
= value
;
366 logConn
->addExecLog(elInfo
);
367 logConn
->addToExecLogSize(buffer
);
370 void SqlLogStatement::setStringParam(int paramPos
, char *value
)
372 if (innerStmt
) innerStmt
->setStringParam(paramPos
,value
);
373 if (!isNonSelDML
) return;
374 if (!needLog
) return;
375 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
376 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + os::align(strlen(value
) + 1);
377 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
378 elInfo
->stmtId
= sid
;
379 elInfo
->type
= SETPARAM
;
380 elInfo
->pos
= paramPos
;
381 elInfo
->dataType
= typeString
;
382 elInfo
->len
= os::align(strlen(value
) + 1);
383 strcpy((char *) &elInfo
->value
, value
);
384 logConn
->addExecLog(elInfo
);
385 logConn
->addToExecLogSize(buffer
);
388 void SqlLogStatement::setDateParam(int paramPos
, Date value
)
390 if (innerStmt
) innerStmt
->setDateParam(paramPos
,value
);
391 if (!isNonSelDML
) return;
392 if (!needLog
) return;
393 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
394 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(Date
);
395 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
396 elInfo
->stmtId
= sid
;
397 elInfo
->type
= SETPARAM
;
398 elInfo
->pos
= paramPos
;
399 elInfo
->dataType
= typeDate
;
400 elInfo
->len
= sizeof(Date
);
401 *(Date
*)&elInfo
->value
= value
;
402 logConn
->addExecLog(elInfo
);
403 logConn
->addToExecLogSize(buffer
);
406 void SqlLogStatement::setTimeParam(int paramPos
, Time value
)
408 if (innerStmt
) innerStmt
->setTimeParam(paramPos
,value
);
409 if (!isNonSelDML
) return;
410 if (!needLog
) return;
411 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
412 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(Time
);
413 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
414 elInfo
->stmtId
= sid
;
415 elInfo
->type
= SETPARAM
;
416 elInfo
->pos
= paramPos
;
417 elInfo
->dataType
= typeTime
;
418 elInfo
->len
= sizeof (Time
);
419 *(Time
*)&elInfo
->value
= value
;
420 logConn
->addExecLog(elInfo
);
421 logConn
->addToExecLogSize(buffer
);
424 void SqlLogStatement::setTimeStampParam(int paramPos
, TimeStamp value
)
426 if (innerStmt
) innerStmt
->setTimeStampParam(paramPos
,value
);
427 if (!isNonSelDML
) return;
428 if (!needLog
) return;
429 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
430 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + sizeof(TimeStamp
);
431 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
432 elInfo
->stmtId
= sid
;
433 elInfo
->type
= SETPARAM
;
434 elInfo
->pos
= paramPos
;
435 elInfo
->dataType
= typeTimeStamp
;
436 elInfo
->len
= sizeof(TimeStamp
);
437 *(TimeStamp
*)&elInfo
->value
= value
;
438 logConn
->addExecLog(elInfo
);
439 logConn
->addToExecLogSize(buffer
);
442 void SqlLogStatement::setBinaryParam(int paramPos
, void *value
, int length
)
444 if (innerStmt
) innerStmt
->setBinaryParam(paramPos
,value
, length
);
445 if (!isNonSelDML
) return;
446 if (!needLog
) return;
447 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
448 int buffer
= sizeof(ExecLogInfo
) - sizeof(int) + os::align(length
);
449 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
450 elInfo
->stmtId
= sid
;
451 elInfo
->type
= SETPARAM
;
452 elInfo
->pos
= paramPos
;
453 elInfo
->dataType
= typeBinary
;
454 elInfo
->len
= os::align(length
);
455 memcpy(&elInfo
->value
, value
, length
);
456 logConn
->addExecLog(elInfo
);
457 logConn
->addToExecLogSize(buffer
);
460 bool SqlLogStatement::isFldNull(int pos
)
462 return innerStmt
->isFldNull(pos
);
464 void SqlLogStatement::setNull(int pos
)
466 if(innerStmt
) innerStmt
->setNull(pos
);
469 List
SqlLogStatement::getAllTableNames(DbRetVal
&ret
)
471 if(innerStmt
) return innerStmt
->getAllTableNames(ret
);
474 List
SqlLogStatement::getAllUserNames(DbRetVal
&ret
)
476 if(innerStmt
) return innerStmt
->getAllUserNames(ret
);