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 <SqlGwStatement.h>
21 #include <SqlLogStatement.h>
22 #include <CacheTableLoader.h>
23 #include <TableConfig.h>
25 DbRetVal
SqlGwStatement::prepare(char *stmtstr
)
27 DbRetVal rv
= OK
,ret
=OK
;
28 SqlGwConnection
*conn
= (SqlGwConnection
*) con
;
29 //conn->connectCSqlIfNotConnected();
30 //conn->connectAdapterIfNotConnected();
32 if (innerStmt
) rv
= innerStmt
->prepare(stmtstr
);
33 // SqlLogStatement *stmt = (SqlLogStatement*) innerStmt;
34 bool isAllcachedTableForJoin
= true;
36 ListIterator titer
=innerStmt
->getTableNameList().getIterator();
37 while (titer
.hasElement())
39 TableName
*t
= (TableName
*)titer
.nextElement();
40 ret
= TableConf::config
.isTableCached(t
->tblName
);
41 if(ret
!=OK
) isAllcachedTableForJoin
=false;
44 if(noOfTable
== 1){ isAllcachedTableForJoin
= true;}
45 mode
= TableConf::config
.getTableMode(innerStmt
->getTableName());
46 /* if((mode==2||mode==6) && !innerStmt->isSelect())
48 printError(ErrReadOnlyCache, "Partial Cache Condition Violation for Non select Dml statement\n");
49 return ErrReadOnlyCache;
51 if ((rv
== OK
)&& ((mode
!=5 && mode
!=6)||innerStmt
->isSelect()) && isAllcachedTableForJoin
)
53 stmtHdlr
= CSqlHandler
;
54 if(mode
!=1) return rv
;
57 //TODO::add procedures also in the below checking
58 if (!strncasecmp(stmtstr
,"INSERT", 6) == 0 &&
59 !strncasecmp(stmtstr
, "UPDATE", 6) ==0 &&
60 !strncasecmp(stmtstr
, "SELECT", 6) ==0 &&
61 !strncasecmp(stmtstr
, "DELETE", 6) ==0) return rv
;
63 printDebug(DM_Gateway
, "Handled by csql %d\n", shouldCSqlHandle());
65 if (!shouldCSqlHandle()) stmtHdlr
= AdapterHandler
;
66 if ( shouldCSqlHandle() && !innerStmt
->isSelect() )
67 stmtHdlr
= CSqlAndAdapterHandler
;
68 printDebug(DM_Gateway
, "Handled %d\n", stmtHdlr
);
69 //prepare failed. means table not there in csql->uncached table
70 //or sql statement is complex and csql parser failed
71 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->prepare(stmtstr
);
73 printError(ErrBadCall
, "Both adapter and csql could not prepare");
76 bool SqlGwStatement::shouldAdapterHandle()
78 if (stmtHdlr
== AdapterHandler
|| stmtHdlr
== CSqlAndAdapterHandler
) return true;
81 bool SqlGwStatement::shouldCSqlHandle()
83 SqlGwConnection
*conn
= (SqlGwConnection
*) con
;
84 if (stmtHdlr
== CSqlHandler
|| stmtHdlr
== CSqlAndAdapterHandler
) return true;
87 bool SqlGwStatement::isSelect()
90 if (adapter
&& shouldAdapterHandle()) retValue
= adapter
->isSelect();
91 if (innerStmt
&& shouldCSqlHandle()) retValue
= innerStmt
->isSelect();
95 DbRetVal
SqlGwStatement::execute(int &rowsAffected
)
98 SqlGwConnection
*conn
= (SqlGwConnection
*) con
;
99 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->execute(rowsAffected
);
100 if (rv
!= OK
) return rv
;
101 if (shouldAdapterHandle())
103 GwHandler hdlr
= conn
->getTxnHandler();
104 if (hdlr
== NoHandler
) conn
->setTxnHandler(AdapterHandler
);
105 if (hdlr
== CSqlHandler
) conn
->setTxnHandler(CSqlAndAdapterHandler
);
107 if (innerStmt
&& shouldCSqlHandle()) rv
= innerStmt
->execute(rowsAffected
);
108 if (shouldCSqlHandle())
110 GwHandler hdlr
= conn
->getTxnHandler();
111 if (hdlr
== NoHandler
) conn
->setTxnHandler(CSqlHandler
);
112 if (hdlr
== AdapterHandler
) conn
->setTxnHandler(CSqlAndAdapterHandler
);
117 DbRetVal
SqlGwStatement::bindParam(int pos
, void* value
)
120 printError(ErrWarning
, "Deprecated and does not replicate or cache");
124 DbRetVal
SqlGwStatement::bindField(int pos
, void* value
)
127 //TODO::this will never be handled by both. check the flag for this
128 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->bindField(pos
, value
);
129 if (rv
!= OK
) return rv
;
130 if (innerStmt
&& shouldCSqlHandle()) rv
= innerStmt
->bindField(pos
,value
);
134 void* SqlGwStatement::fetch()
136 //TODO::this will never be handled by both. check the flag for this
137 if (adapter
&& shouldAdapterHandle()) return adapter
->fetch();
138 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->fetch();
142 void* SqlGwStatement::fetch(DbRetVal
&rv
)
144 //TODO::this will never be handled by both. check the flag for this
145 if (adapter
&& shouldAdapterHandle()) return adapter
->fetch(rv
);
146 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->fetch(rv
);
150 void* SqlGwStatement::fetchAndPrint(bool SQL
)
152 //TODO::this will never be handled by both. check the flag for this
153 if (adapter
&& shouldAdapterHandle()) return adapter
->fetchAndPrint(SQL
);
154 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->fetchAndPrint(SQL
);
158 void* SqlGwStatement::next()
160 //TODO::this will never be handled by both. check the flag for this
161 if (adapter
&& shouldAdapterHandle()) return adapter
->next();
162 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->next();
166 DbRetVal
SqlGwStatement::close()
168 //TODO::this will never be handled by both. check the flag for this
169 if (adapter
&& shouldAdapterHandle()) return adapter
->close();
170 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->close();
174 void* SqlGwStatement::getFieldValuePtr( int pos
)
176 //TODO::this will never be handled by both. check the flag for this
177 if (adapter
&& shouldAdapterHandle()) return adapter
->getFieldValuePtr(pos
);
178 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->getFieldValuePtr(pos
);
182 int SqlGwStatement::noOfProjFields()
184 //TODO::this will never be handled by both. check the flag for this
185 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->noOfProjFields();
186 if (adapter
&& shouldAdapterHandle()) return adapter
->noOfProjFields();
190 int SqlGwStatement::noOfParamFields()
192 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->noOfParamFields();
193 if (adapter
&& shouldAdapterHandle()) return adapter
->noOfParamFields();
197 DbRetVal
SqlGwStatement::getProjFldInfo (int projpos
, FieldInfo
*&fInfo
)
199 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->getProjFldInfo(projpos
, fInfo
);
200 if (adapter
&& shouldAdapterHandle()) return adapter
->getProjFldInfo(projpos
, fInfo
);
204 DbRetVal
SqlGwStatement::getParamFldInfo (int parampos
, FieldInfo
*&fInfo
)
206 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->getParamFldInfo(parampos
, fInfo
);
207 if (adapter
&& shouldAdapterHandle()) return adapter
->getParamFldInfo(parampos
, fInfo
);
211 DbRetVal
SqlGwStatement::free()
214 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->free();
215 if (innerStmt
&& shouldCSqlHandle()) rv
= innerStmt
->free();
218 void SqlGwStatement::setShortParam(int paramPos
, short value
)
220 if (adapter
&& shouldAdapterHandle()) adapter
->setShortParam(paramPos
, value
);
221 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setShortParam(paramPos
,value
);
224 void SqlGwStatement::setIntParam(int paramPos
, int value
)
226 if (adapter
&& shouldAdapterHandle()) adapter
->setIntParam(paramPos
, value
);
227 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setIntParam(paramPos
,value
);
231 void SqlGwStatement::setLongParam(int paramPos
, long value
)
233 if (adapter
&& shouldAdapterHandle()) adapter
->setLongParam(paramPos
, value
);
234 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setLongParam(paramPos
,value
);
238 void SqlGwStatement::setLongLongParam(int paramPos
, long long value
)
240 if (adapter
&& shouldAdapterHandle()) adapter
->setLongLongParam(paramPos
, value
);
241 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setLongLongParam(paramPos
,value
);
244 void SqlGwStatement::setByteIntParam(int paramPos
, ByteInt value
)
246 if (adapter
&& shouldAdapterHandle()) adapter
->setByteIntParam(paramPos
, value
);
247 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setByteIntParam(paramPos
,value
);
250 void SqlGwStatement::setFloatParam(int paramPos
, float value
)
252 if (adapter
&& shouldAdapterHandle()) adapter
->setFloatParam(paramPos
, value
);
253 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setFloatParam(paramPos
,value
);
256 void SqlGwStatement::setDoubleParam(int paramPos
, double value
)
258 if (adapter
&& shouldAdapterHandle()) adapter
->setDoubleParam(paramPos
, value
);
259 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setDoubleParam(paramPos
,value
);
263 void SqlGwStatement::setStringParam(int paramPos
, char *value
)
265 if (adapter
&& shouldAdapterHandle()) adapter
->setStringParam(paramPos
, value
);
266 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setStringParam(paramPos
,value
);
269 void SqlGwStatement::setDateParam(int paramPos
, Date value
)
271 if (adapter
&& shouldAdapterHandle()) adapter
->setDateParam(paramPos
, value
);
272 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setDateParam(paramPos
,value
);
275 void SqlGwStatement::setTimeParam(int paramPos
, Time value
)
277 if (adapter
&& shouldAdapterHandle()) adapter
->setTimeParam(paramPos
, value
);
278 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setTimeParam(paramPos
,value
);
281 void SqlGwStatement::setTimeStampParam(int paramPos
, TimeStamp value
)
283 if (adapter
&& shouldAdapterHandle()) adapter
->setTimeStampParam(paramPos
, value
);
284 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setTimeStampParam(paramPos
,value
);
287 void SqlGwStatement::setBinaryParam(int paramPos
, void *value
, int length
)
289 if (adapter
&& shouldAdapterHandle()) adapter
->setBinaryParam(paramPos
, value
, length
);
290 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setBinaryParam(paramPos
,value
, length
);
292 bool SqlGwStatement::isFldNull(int pos
)
294 if (adapter
&& shouldAdapterHandle()) return adapter
->isFldNull(pos
);
295 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->isFldNull(pos
);
298 bool SqlGwStatement::isFldNull(char *name
)
300 if (adapter
&& shouldAdapterHandle()) return adapter
->isFldNull(name
);
301 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->isFldNull(name
);
304 void SqlGwStatement::setNull(int pos
)
306 if (adapter
&& shouldAdapterHandle()) adapter
->setNull(pos
);
307 if (innerStmt
&& shouldCSqlHandle()) innerStmt
-> setNull(pos
);
309 List
SqlGwStatement::getAllTableNames(DbRetVal
&ret
)
311 printf("in csql\n"); if (innerStmt
) return innerStmt
->getAllTableNames(ret
);
312 printf("in Target Db\n"); if (adapter
) adapter
->getAllTableNames(ret
);