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 DbRetVal
SqlGwStatement::prepare(char *stmtstr
)
25 DbRetVal rv
= OK
,ret
=OK
;
26 SqlGwConnection
*conn
= (SqlGwConnection
*) con
;
27 //conn->connectCSqlIfNotConnected();
28 //conn->connectAdapterIfNotConnected();
30 if (innerStmt
) rv
= innerStmt
->prepare(stmtstr
);
31 SqlLogStatement
*stmt
= (SqlLogStatement
*) innerStmt
;
32 bool isAllcachedTable
= true;
34 ListIterator titer
=(stmt
->getInnerStatement())->getTableNameList().getIterator();
35 while (titer
.hasElement())
37 TableName
*t
= (TableName
*)titer
.nextElement();
38 ret
= CacheTableLoader::isTableCached(t
->tblName
);
39 if(ret
!=OK
) isAllcachedTable
=false;
42 if(noOfTable
== 1){ isAllcachedTable
= true;}
43 mode
=CacheTableLoader::getTableMode((stmt
->getInnerStatement())->getTableName());
44 if ((rv
== OK
)&& ((mode
!=5 && mode
!=6)||innerStmt
->isSelect()) && isAllcachedTable
) {
45 if (!stmt
->isCached
) {
46 stmtHdlr
= CSqlHandler
;
49 if (stmt
->mode
!= TABLE_OSYNC
) {
50 stmtHdlr
= CSqlHandler
;
53 stmtHdlr
= CSqlAndAdapterHandler
;
58 //TODO::add procedures also in the below checking
59 if (!strncasecmp(stmtstr
,"INSERT", 6) == 0 &&
60 !strncasecmp(stmtstr
, "UPDATE", 6) ==0 &&
61 !strncasecmp(stmtstr
, "SELECT", 6) ==0 &&
62 !strncasecmp(stmtstr
, "DELETE", 6) ==0) return rv
;
64 //prepare failed. means table not there in csql->uncached table
65 //or sql statement is complex and csql parser failed
66 if (adapter
) rv
= adapter
->prepare(stmtstr
);
68 printDebug(DM_Gateway
, "Handled by csql %d\n", shouldCSqlHandle());
69 if (!shouldCSqlHandle()) stmtHdlr
= AdapterHandler
;
70 else stmtHdlr
= CSqlAndAdapterHandler
;
71 printDebug(DM_Gateway
, "Handled %d\n", stmtHdlr
);
74 printError(ErrSysInit
, "Both csql and adapter could not prepare\n");
77 bool SqlGwStatement::shouldAdapterHandle()
79 if (stmtHdlr
== AdapterHandler
||
80 stmtHdlr
== CSqlAndAdapterHandler
) return true;
83 bool SqlGwStatement::shouldCSqlHandle()
85 SqlGwConnection
*conn
= (SqlGwConnection
*) con
;
86 if (stmtHdlr
== CSqlHandler
||
87 stmtHdlr
== CSqlAndAdapterHandler
) return true;
90 bool SqlGwStatement::isSelect()
93 if (adapter
&& shouldAdapterHandle()) retValue
= adapter
->isSelect();
94 if (innerStmt
&& shouldCSqlHandle()) retValue
= innerStmt
->isSelect();
98 DbRetVal
SqlGwStatement::execute(int &rowsAffected
)
101 SqlGwConnection
*conn
= (SqlGwConnection
*) con
;
102 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->execute(rowsAffected
);
103 if (rv
!= OK
) return rv
;
104 if (shouldAdapterHandle())
106 GwHandler hdlr
= conn
->getTxnHandler();
107 if (hdlr
== NoHandler
) conn
->setTxnHandler(AdapterHandler
);
108 if (hdlr
== CSqlHandler
) conn
->setTxnHandler(CSqlAndAdapterHandler
);
110 if (innerStmt
&& shouldCSqlHandle()) rv
= innerStmt
->execute(rowsAffected
);
111 if (shouldCSqlHandle())
113 GwHandler hdlr
= conn
->getTxnHandler();
114 if (hdlr
== NoHandler
) conn
->setTxnHandler(CSqlHandler
);
115 if (hdlr
== AdapterHandler
) conn
->setTxnHandler(CSqlAndAdapterHandler
);
120 DbRetVal
SqlGwStatement::bindParam(int pos
, void* value
)
123 printError(ErrWarning
, "Deprecated and does not replicate or cache");
127 DbRetVal
SqlGwStatement::bindField(int pos
, void* value
)
130 //TODO::this will never be handled by both. check the flag for this
131 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->bindField(pos
, value
);
132 if (rv
!= OK
) return rv
;
133 if (innerStmt
&& shouldCSqlHandle()) rv
= innerStmt
->bindField(pos
,value
);
137 void* SqlGwStatement::fetch()
139 //TODO::this will never be handled by both. check the flag for this
140 if (adapter
&& shouldAdapterHandle()) return adapter
->fetch();
141 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->fetch();
145 void* SqlGwStatement::fetch(DbRetVal
&rv
)
147 //TODO::this will never be handled by both. check the flag for this
148 if (adapter
&& shouldAdapterHandle()) return adapter
->fetch(rv
);
149 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->fetch(rv
);
153 void* SqlGwStatement::fetchAndPrint(bool SQL
)
155 //TODO::this will never be handled by both. check the flag for this
156 if (adapter
&& shouldAdapterHandle()) return adapter
->fetchAndPrint(SQL
);
157 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->fetchAndPrint(SQL
);
161 void* SqlGwStatement::next()
163 //TODO::this will never be handled by both. check the flag for this
164 if (adapter
&& shouldAdapterHandle()) return adapter
->next();
165 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->next();
169 DbRetVal
SqlGwStatement::close()
171 //TODO::this will never be handled by both. check the flag for this
172 if (adapter
&& shouldAdapterHandle()) return adapter
->close();
173 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->close();
177 void* SqlGwStatement::getFieldValuePtr( int pos
)
179 //TODO::this will never be handled by both. check the flag for this
180 if (adapter
&& shouldAdapterHandle()) return adapter
->getFieldValuePtr(pos
);
181 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->getFieldValuePtr(pos
);
185 int SqlGwStatement::noOfProjFields()
187 //TODO::this will never be handled by both. check the flag for this
188 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->noOfProjFields();
189 if (adapter
&& shouldAdapterHandle()) return adapter
->noOfProjFields();
193 int SqlGwStatement::noOfParamFields()
195 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->noOfParamFields();
196 if (adapter
&& shouldAdapterHandle()) return adapter
->noOfParamFields();
200 DbRetVal
SqlGwStatement::getProjFldInfo (int projpos
, FieldInfo
*&fInfo
)
202 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->getProjFldInfo(projpos
, fInfo
);
203 if (adapter
&& shouldAdapterHandle()) return adapter
->getProjFldInfo(projpos
, fInfo
);
207 DbRetVal
SqlGwStatement::getParamFldInfo (int parampos
, FieldInfo
*&fInfo
)
209 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->getParamFldInfo(parampos
, fInfo
);
210 if (adapter
&& shouldAdapterHandle()) return adapter
->getParamFldInfo(parampos
, fInfo
);
214 DbRetVal
SqlGwStatement::free()
217 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->free();
218 if (innerStmt
&& shouldCSqlHandle()) rv
= innerStmt
->free();
221 void SqlGwStatement::setShortParam(int paramPos
, short value
)
223 if (adapter
&& shouldAdapterHandle()) adapter
->setShortParam(paramPos
, value
);
224 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setShortParam(paramPos
,value
);
227 void SqlGwStatement::setIntParam(int paramPos
, int value
)
229 if (adapter
&& shouldAdapterHandle()) adapter
->setIntParam(paramPos
, value
);
230 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setIntParam(paramPos
,value
);
234 void SqlGwStatement::setLongParam(int paramPos
, long value
)
236 if (adapter
&& shouldAdapterHandle()) adapter
->setLongParam(paramPos
, value
);
237 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setLongParam(paramPos
,value
);
241 void SqlGwStatement::setLongLongParam(int paramPos
, long long value
)
243 if (adapter
&& shouldAdapterHandle()) adapter
->setLongLongParam(paramPos
, value
);
244 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setLongLongParam(paramPos
,value
);
247 void SqlGwStatement::setByteIntParam(int paramPos
, ByteInt value
)
249 if (adapter
&& shouldAdapterHandle()) adapter
->setByteIntParam(paramPos
, value
);
250 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setByteIntParam(paramPos
,value
);
253 void SqlGwStatement::setFloatParam(int paramPos
, float value
)
255 if (adapter
&& shouldAdapterHandle()) adapter
->setFloatParam(paramPos
, value
);
256 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setFloatParam(paramPos
,value
);
259 void SqlGwStatement::setDoubleParam(int paramPos
, double value
)
261 if (adapter
&& shouldAdapterHandle()) adapter
->setDoubleParam(paramPos
, value
);
262 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setDoubleParam(paramPos
,value
);
266 void SqlGwStatement::setStringParam(int paramPos
, char *value
)
268 if (adapter
&& shouldAdapterHandle()) adapter
->setStringParam(paramPos
, value
);
269 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setStringParam(paramPos
,value
);
272 void SqlGwStatement::setDateParam(int paramPos
, Date value
)
274 if (adapter
&& shouldAdapterHandle()) adapter
->setDateParam(paramPos
, value
);
275 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setDateParam(paramPos
,value
);
278 void SqlGwStatement::setTimeParam(int paramPos
, Time value
)
280 if (adapter
&& shouldAdapterHandle()) adapter
->setTimeParam(paramPos
, value
);
281 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setTimeParam(paramPos
,value
);
284 void SqlGwStatement::setTimeStampParam(int paramPos
, TimeStamp value
)
286 if (adapter
&& shouldAdapterHandle()) adapter
->setTimeStampParam(paramPos
, value
);
287 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setTimeStampParam(paramPos
,value
);
290 void SqlGwStatement::setBinaryParam(int paramPos
, void *value
, int length
)
292 if (adapter
&& shouldAdapterHandle()) adapter
->setBinaryParam(paramPos
, value
, length
);
293 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setBinaryParam(paramPos
,value
, length
);
295 bool SqlGwStatement::isFldNull(int pos
)
297 if (adapter
&& shouldAdapterHandle()) return adapter
->isFldNull(pos
);
298 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->isFldNull(pos
);
301 bool SqlGwStatement::isFldNull(char *name
)
303 if (adapter
&& shouldAdapterHandle()) return adapter
->isFldNull(name
);
304 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->isFldNull(name
);
307 void SqlGwStatement::setNull(int pos
)
309 if (adapter
&& shouldAdapterHandle()) adapter
->setNull(pos
);
310 if (innerStmt
&& shouldCSqlHandle()) innerStmt
-> setNull(pos
);
312 List
SqlGwStatement::getAllTableNames(DbRetVal
&ret
)
314 printf("in csql\n"); if (innerStmt
) return innerStmt
->getAllTableNames(ret
);
315 printf("in Target Db\n"); if (adapter
) adapter
->getAllTableNames(ret
);