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
); if (rv
!= OK
) return rv
;
37 rv
= execute(rows
); if (rv
!= OK
) return rv
;
41 DbRetVal
SqlLogStatement::prepare(char *stmtstr
)
44 SqlLogConnection
*conn
= (SqlLogConnection
*)con
;
46 rv
= innerStmt
->prepare(stmtstr
);
47 if (rv
!= OK
) { isNonSelDML
= false; return rv
; }
48 if (innerStmt
->getStmtType() == SelectStatement
) {
49 isNonSelDML
= false; return rv
;
52 bool hasParam
= false;
53 if (innerStmt
->noOfParamFields() >0) hasParam
= true;
55 if (Conf::config
.useDurability()) {
56 if (strlen(stmtstr
) > 6 && ((strncasecmp(stmtstr
,"CREATE", 6) == 0) ||
57 (strncasecmp(stmtstr
,"DROP", 4) == 0) ||
58 (strncasecmp(stmtstr
,"RENAME", 6) == 0) ||
59 (strncasecmp(stmtstr
,"ALTER", 5) == 0))) {
60 sid
= SqlLogStatement::stmtUID
.getID(STMT_ID
);
61 printDebug(DM_SqlLog
, "CREATE|DROP: stmt id = %d\n", sid
);
62 conn
->fileLogPrepare(0, sid
, strlen(stmtstr
)+1, stmtstr
, NULL
, hasParam
);
67 if (!isNonSelectDML(stmtstr
)) { isNonSelDML
= false; return rv
;}
70 tblName
= innerStmt
->getTableName();
71 unsigned int mode
= TableConf::config
.getTableMode(tblName
);
72 if (TableConf::config
.isTableCached(mode
)) isCached
= true;
75 if (strncasecmp(stmtstr
,"CACHE", 5) == 0 ||
76 strncasecmp(stmtstr
,"UNCACHE", 7) == 0) {
80 if (conn
->isNoLogRequired()) { isNonSelDML
= false; return OK
; }
82 needLog
= true; isNonSelDML
= true;
83 int txnId
=conn
->getTxnID();
84 sid
= stmtUID
.getID(STMT_ID
);
85 printDebug(DM_SqlLog
, "stmt id = %d\n", sid
);
86 if (conn
->isMsgQReqd() && !conn
->noMsgLog
&& isCached
)
87 conn
->msgPrepare(txnId
, sid
, strlen(stmtstr
) + 1, stmtstr
,
89 if (conn
->isFileLogReqd())
90 conn
->fileLogPrepare(txnId
, sid
, strlen(stmtstr
) + 1, stmtstr
,
92 if (conn
->isOfflineLogReqd() && !conn
->noOfflineLog
)
93 conn
->offlineLogPrepare(txnId
, sid
, strlen(stmtstr
) + 1, stmtstr
,
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 bool hasParam
= false;
217 if (innerStmt
->noOfParamFields() >0) hasParam
= true;
218 rv
= innerStmt
->free();
220 if (!isPrepared
) return OK
;
221 if (!needLog
) { isPrepared
= false; return rv
; }
222 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
223 if (sid
!= 0) logConn
->freeLogs(sid
, hasParam
);
229 void SqlLogStatement::setShortParam(int paramPos
, short value
)
231 if (innerStmt
) innerStmt
->setShortParam(paramPos
,value
);
232 if (!isNonSelDML
) return;
233 if (!needLog
) return;
234 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
235 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(short);
236 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
237 elInfo
->stmtId
= sid
;
238 printDebug(DM_SqlLog
, "setShort: stmtID: %d", elInfo
->stmtId
);
239 elInfo
->type
= SETPARAM
;
240 printDebug(DM_SqlLog
, "setShort: ExecTp: %d", elInfo
->type
);
241 elInfo
->pos
= paramPos
;
242 printDebug(DM_SqlLog
, "setShort: PrmPos: %d", elInfo
->pos
);
244 printDebug(DM_SqlLog
, "setShort: isNull: %d", elInfo
->isNull
);
245 elInfo
->dataType
= typeShort
;
246 printDebug(DM_SqlLog
, "setShort: DtType: %d", elInfo
->dataType
);
247 elInfo
->len
= sizeof (short);
248 printDebug(DM_SqlLog
, "setShort: Length: %d", elInfo
->len
);
249 *(short *)&elInfo
->value
= value
;
250 printDebug(DM_SqlLog
, "setShort: Value : %d", *(short *)&elInfo
->value
);
251 logConn
->addExecLog(elInfo
);
252 printDebug(DM_SqlLog
, "appended elem addr: %x", elInfo
);
253 logConn
->addToExecLogSize(buffer
);
254 printDebug(DM_SqlLog
, "appended bufsize: %d", buffer
);
257 void SqlLogStatement::setIntParam(int paramPos
, int value
)
259 if (innerStmt
) innerStmt
->setIntParam(paramPos
,value
);
260 if (!isNonSelDML
) return;
261 if (!needLog
) return;
262 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
263 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(int);
264 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
265 elInfo
->stmtId
= sid
;
266 printDebug(DM_SqlLog
, "setInt: stmtID: %d", elInfo
->stmtId
);
267 elInfo
->type
= SETPARAM
;
268 printDebug(DM_SqlLog
, "setInt: ExecTp: %d", elInfo
->type
);
269 elInfo
->pos
= paramPos
;
270 printDebug(DM_SqlLog
, "setInt: PrmPos: %d", elInfo
->pos
);
272 printDebug(DM_SqlLog
, "setInt: isNull: %d", elInfo
->isNull
);
273 elInfo
->dataType
= typeInt
;
274 printDebug(DM_SqlLog
, "setInt: DtType: %d", elInfo
->dataType
);
275 elInfo
->len
= sizeof(int);
276 printDebug(DM_SqlLog
, "setInt: Length: %d", elInfo
->len
);
277 *(int *)&elInfo
->value
= value
;
278 printDebug(DM_SqlLog
, "setInt: Value : %d", *(int *)&elInfo
->value
);
279 logConn
->addExecLog(elInfo
);
280 printDebug(DM_SqlLog
, "appended elem addr: %x", elInfo
);
281 logConn
->addToExecLogSize(buffer
);
282 printDebug(DM_SqlLog
, "appended bufsize: %d", buffer
);
285 void SqlLogStatement::setLongParam(int paramPos
, long value
)
287 if (innerStmt
) innerStmt
->setLongParam(paramPos
,value
);
288 if (!isNonSelDML
) return;
289 if (!needLog
) return;
290 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
291 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(long);
292 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
293 elInfo
->stmtId
= sid
;
294 elInfo
->type
= SETPARAM
;
295 elInfo
->pos
= paramPos
;
297 elInfo
->dataType
= typeLong
;
298 elInfo
->len
= sizeof(long);
299 *(long *)&elInfo
->value
= value
;
300 logConn
->addExecLog(elInfo
);
301 logConn
->addToExecLogSize(buffer
);
304 void SqlLogStatement::setLongLongParam(int paramPos
, long long value
)
306 if (innerStmt
) innerStmt
->setLongLongParam(paramPos
,value
);
307 if (!isNonSelDML
) return;
308 if (!needLog
) return;
309 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
310 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(long long);
311 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
312 elInfo
->stmtId
= sid
;
313 elInfo
->type
= SETPARAM
;
314 elInfo
->pos
= paramPos
;
316 elInfo
->dataType
= typeLongLong
;
317 elInfo
->len
= sizeof (long long);
318 *(long long *)&elInfo
->value
= value
;
319 logConn
->addExecLog(elInfo
);
320 logConn
->addToExecLogSize(buffer
);
323 void SqlLogStatement::setByteIntParam(int paramPos
, ByteInt value
)
325 if (innerStmt
) innerStmt
->setByteIntParam(paramPos
,value
);
326 if (!isNonSelDML
) return;
327 if (!needLog
) return;
328 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
329 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(ByteInt
);
330 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
331 elInfo
->stmtId
= sid
;
332 elInfo
->type
= SETPARAM
;
333 elInfo
->pos
= paramPos
;
335 elInfo
->dataType
= typeByteInt
;
336 elInfo
->len
= sizeof(ByteInt
);
337 *(ByteInt
*)&elInfo
->value
= value
;
338 logConn
->addExecLog(elInfo
);
339 logConn
->addToExecLogSize(buffer
);
342 void SqlLogStatement::setFloatParam(int paramPos
, float value
)
344 if (innerStmt
) innerStmt
->setFloatParam(paramPos
,value
);
345 if (!isNonSelDML
) return;
346 if (!needLog
) return;
347 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
348 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(float);
349 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
350 elInfo
->stmtId
= sid
;
351 elInfo
->type
= SETPARAM
;
352 elInfo
->pos
= paramPos
;
354 elInfo
->dataType
= typeFloat
;
355 elInfo
->len
= sizeof(float);
356 *(float *)&elInfo
->value
= value
;
357 logConn
->addExecLog(elInfo
);
358 logConn
->addToExecLogSize(buffer
);
361 void SqlLogStatement::setDoubleParam(int paramPos
, double value
)
363 if (innerStmt
) innerStmt
->setDoubleParam(paramPos
,value
);
364 if (!isNonSelDML
) return;
365 if (!needLog
) return;
366 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
367 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(double);
368 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
369 elInfo
->stmtId
= sid
;
370 elInfo
->type
= SETPARAM
;
371 elInfo
->pos
= paramPos
;
373 elInfo
->dataType
= typeDouble
;
374 elInfo
->len
= sizeof(double);
375 *(double *)&elInfo
->value
= value
;
376 logConn
->addExecLog(elInfo
);
377 logConn
->addToExecLogSize(buffer
);
380 void SqlLogStatement::setStringParam(int paramPos
, char *value
)
382 if (innerStmt
) innerStmt
->setStringParam(paramPos
,value
);
383 if (!isNonSelDML
) return;
384 if (!needLog
) return;
385 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
386 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + os::align(strlen(value
) + 1);
387 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
388 elInfo
->stmtId
= sid
;
389 elInfo
->type
= SETPARAM
;
390 elInfo
->pos
= paramPos
;
392 elInfo
->dataType
= typeString
;
393 elInfo
->len
= os::align(strlen(value
) + 1);
394 strcpy((char *) &elInfo
->value
, value
);
395 logConn
->addExecLog(elInfo
);
396 logConn
->addToExecLogSize(buffer
);
399 void SqlLogStatement::setDateParam(int paramPos
, Date value
)
401 if (innerStmt
) innerStmt
->setDateParam(paramPos
,value
);
402 if (!isNonSelDML
) return;
403 if (!needLog
) return;
404 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
405 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(Date
);
406 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
407 elInfo
->stmtId
= sid
;
408 elInfo
->type
= SETPARAM
;
409 elInfo
->pos
= paramPos
;
411 elInfo
->dataType
= typeDate
;
412 elInfo
->len
= sizeof(Date
);
413 *(Date
*)&elInfo
->value
= value
;
414 logConn
->addExecLog(elInfo
);
415 logConn
->addToExecLogSize(buffer
);
418 void SqlLogStatement::setTimeParam(int paramPos
, Time value
)
420 if (innerStmt
) innerStmt
->setTimeParam(paramPos
,value
);
421 if (!isNonSelDML
) return;
422 if (!needLog
) return;
423 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
424 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(Time
);
425 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
426 elInfo
->stmtId
= sid
;
427 elInfo
->type
= SETPARAM
;
428 elInfo
->pos
= paramPos
;
430 elInfo
->dataType
= typeTime
;
431 elInfo
->len
= sizeof (Time
);
432 *(Time
*)&elInfo
->value
= value
;
433 logConn
->addExecLog(elInfo
);
434 logConn
->addToExecLogSize(buffer
);
437 void SqlLogStatement::setTimeStampParam(int paramPos
, TimeStamp value
)
439 if (innerStmt
) innerStmt
->setTimeStampParam(paramPos
,value
);
440 if (!isNonSelDML
) return;
441 if (!needLog
) return;
442 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
443 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + sizeof(TimeStamp
);
444 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
445 elInfo
->stmtId
= sid
;
446 elInfo
->type
= SETPARAM
;
447 elInfo
->pos
= paramPos
;
449 elInfo
->dataType
= typeTimeStamp
;
450 elInfo
->len
= sizeof(TimeStamp
);
451 *(TimeStamp
*)&elInfo
->value
= value
;
452 logConn
->addExecLog(elInfo
);
453 logConn
->addToExecLogSize(buffer
);
456 void SqlLogStatement::setBinaryParam(int paramPos
, void *value
, int length
)
458 if (innerStmt
) innerStmt
->setBinaryParam(paramPos
,value
, length
);
459 if (!isNonSelDML
) return;
460 if (!needLog
) return;
461 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
462 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) + os::align(length
);
463 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
464 elInfo
->stmtId
= sid
;
465 elInfo
->type
= SETPARAM
;
466 elInfo
->pos
= paramPos
;
468 elInfo
->dataType
= typeBinary
;
469 elInfo
->len
= os::align(length
);
470 memcpy(&elInfo
->value
, value
, length
);
471 logConn
->addExecLog(elInfo
);
472 logConn
->addToExecLogSize(buffer
);
475 bool SqlLogStatement::isFldNull(int pos
)
477 if (innerStmt
) return innerStmt
->isFldNull(pos
);
480 void SqlLogStatement::setNull(int pos
)
482 if(innerStmt
) innerStmt
->setNull(pos
);
483 if (!isNonSelDML
) return;
484 if (!needLog
) return;
485 SqlLogConnection
* logConn
= (SqlLogConnection
*)con
;
486 int buffer
= sizeof(ExecLogInfo
) - sizeof(void *) - 2 * sizeof(int);
487 ExecLogInfo
*elInfo
= (ExecLogInfo
*) malloc (buffer
);
488 elInfo
->stmtId
= sid
;
489 elInfo
->type
= SETPARAM
;
492 logConn
->addExecLog(elInfo
);
493 logConn
->addToExecLogSize(buffer
);
497 List
SqlLogStatement::getAllTableNames(DbRetVal
&ret
)
499 if(innerStmt
) return innerStmt
->getAllTableNames(ret
);
502 List
SqlLogStatement::getAllUserNames(DbRetVal
&ret
)
504 if(innerStmt
) return innerStmt
->getAllUserNames(ret
);