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
)
26 SqlGwConnection
*conn
= (SqlGwConnection
*) con
;
27 //conn->connectCSqlIfNotConnected();
28 //conn->connectAdapterIfNotConnected();
30 if (innerStmt
) rv
= innerStmt
->prepare(stmtstr
);
31 SqlLogStatement
*stmt
= (SqlLogStatement
*) innerStmt
;
32 mode
=CacheTableLoader::getTableMode((stmt
->getInnerStatement())->getTableName());
33 if ((rv
== OK
)&& ((mode
!=5 && mode
!=6)||innerStmt
->isSelect())) {
34 if (!stmt
->isCached
) {
35 stmtHdlr
= CSqlHandler
;
38 if (stmt
->mode
!= TABLE_OSYNC
) {
39 stmtHdlr
= CSqlHandler
;
42 stmtHdlr
= CSqlAndAdapterHandler
;
47 //TODO::add procedures also in the below checking
48 if (!strncasecmp(stmtstr
,"INSERT", 6) == 0 &&
49 !strncasecmp(stmtstr
, "UPDATE", 6) ==0 &&
50 !strncasecmp(stmtstr
, "SELECT", 6) ==0 &&
51 !strncasecmp(stmtstr
, "DELETE", 6) ==0) return rv
;
53 //prepare failed. means table not there in csql->uncached table
54 //or sql statement is complex and csql parser failed
55 if (adapter
) rv
= adapter
->prepare(stmtstr
);
57 printDebug(DM_Gateway
, "Handled by csql %d\n", shouldCSqlHandle());
58 if (!shouldCSqlHandle()) stmtHdlr
= AdapterHandler
;
59 else stmtHdlr
= CSqlAndAdapterHandler
;
60 printDebug(DM_Gateway
, "Handled %d\n", stmtHdlr
);
63 printError(ErrSysInit
, "Both csql and adapter could not prepare\n");
66 bool SqlGwStatement::shouldAdapterHandle()
68 if (stmtHdlr
== AdapterHandler
||
69 stmtHdlr
== CSqlAndAdapterHandler
) return true;
72 bool SqlGwStatement::shouldCSqlHandle()
74 SqlGwConnection
*conn
= (SqlGwConnection
*) con
;
75 if (stmtHdlr
== CSqlHandler
||
76 stmtHdlr
== CSqlAndAdapterHandler
) return true;
79 bool SqlGwStatement::isSelect()
82 if (adapter
&& shouldAdapterHandle()) retValue
= adapter
->isSelect();
83 if (innerStmt
&& shouldCSqlHandle()) retValue
= innerStmt
->isSelect();
87 DbRetVal
SqlGwStatement::execute(int &rowsAffected
)
90 SqlGwConnection
*conn
= (SqlGwConnection
*) con
;
91 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->execute(rowsAffected
);
92 if (rv
!= OK
) return rv
;
93 if (shouldAdapterHandle())
95 GwHandler hdlr
= conn
->getTxnHandler();
96 if (hdlr
== NoHandler
) conn
->setTxnHandler(AdapterHandler
);
97 if (hdlr
== CSqlHandler
) conn
->setTxnHandler(CSqlAndAdapterHandler
);
99 if (innerStmt
&& shouldCSqlHandle()) rv
= innerStmt
->execute(rowsAffected
);
100 if (shouldCSqlHandle())
102 GwHandler hdlr
= conn
->getTxnHandler();
103 if (hdlr
== NoHandler
) conn
->setTxnHandler(CSqlHandler
);
104 if (hdlr
== AdapterHandler
) conn
->setTxnHandler(CSqlAndAdapterHandler
);
109 DbRetVal
SqlGwStatement::bindParam(int pos
, void* value
)
112 printError(ErrWarning
, "Deprecated and does not replicate or cache");
116 DbRetVal
SqlGwStatement::bindField(int pos
, void* value
)
119 //TODO::this will never be handled by both. check the flag for this
120 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->bindField(pos
, value
);
121 if (rv
!= OK
) return rv
;
122 if (innerStmt
&& shouldCSqlHandle()) rv
= innerStmt
->bindField(pos
,value
);
126 void* SqlGwStatement::fetch()
128 //TODO::this will never be handled by both. check the flag for this
129 if (adapter
&& shouldAdapterHandle()) return adapter
->fetch();
130 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->fetch();
134 void* SqlGwStatement::fetch(DbRetVal
&rv
)
136 //TODO::this will never be handled by both. check the flag for this
137 if (adapter
&& shouldAdapterHandle()) return adapter
->fetch(rv
);
138 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->fetch(rv
);
142 void* SqlGwStatement::fetchAndPrint(bool SQL
)
144 //TODO::this will never be handled by both. check the flag for this
145 if (adapter
&& shouldAdapterHandle()) return adapter
->fetchAndPrint(SQL
);
146 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->fetchAndPrint(SQL
);
150 void* SqlGwStatement::next()
152 //TODO::this will never be handled by both. check the flag for this
153 if (adapter
&& shouldAdapterHandle()) return adapter
->next();
154 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->next();
158 DbRetVal
SqlGwStatement::close()
160 //TODO::this will never be handled by both. check the flag for this
161 if (adapter
&& shouldAdapterHandle()) return adapter
->close();
162 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->close();
166 void* SqlGwStatement::getFieldValuePtr( int pos
)
168 //TODO::this will never be handled by both. check the flag for this
169 if (adapter
&& shouldAdapterHandle()) return adapter
->getFieldValuePtr(pos
);
170 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->getFieldValuePtr(pos
);
174 int SqlGwStatement::noOfProjFields()
176 //TODO::this will never be handled by both. check the flag for this
177 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->noOfProjFields();
178 if (adapter
&& shouldAdapterHandle()) return adapter
->noOfProjFields();
182 int SqlGwStatement::noOfParamFields()
184 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->noOfParamFields();
185 if (adapter
&& shouldAdapterHandle()) return adapter
->noOfParamFields();
189 DbRetVal
SqlGwStatement::getProjFldInfo (int projpos
, FieldInfo
*&fInfo
)
191 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->getProjFldInfo(projpos
, fInfo
);
192 if (adapter
&& shouldAdapterHandle()) return adapter
->getProjFldInfo(projpos
, fInfo
);
196 DbRetVal
SqlGwStatement::getParamFldInfo (int parampos
, FieldInfo
*&fInfo
)
198 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->getParamFldInfo(parampos
, fInfo
);
199 if (adapter
&& shouldAdapterHandle()) return adapter
->getParamFldInfo(parampos
, fInfo
);
203 DbRetVal
SqlGwStatement::free()
206 if (adapter
&& shouldAdapterHandle()) rv
= adapter
->free();
207 if (innerStmt
&& shouldCSqlHandle()) rv
= innerStmt
->free();
210 void SqlGwStatement::setShortParam(int paramPos
, short value
)
212 if (adapter
&& shouldAdapterHandle()) adapter
->setShortParam(paramPos
, value
);
213 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setShortParam(paramPos
,value
);
216 void SqlGwStatement::setIntParam(int paramPos
, int value
)
218 if (adapter
&& shouldAdapterHandle()) adapter
->setIntParam(paramPos
, value
);
219 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setIntParam(paramPos
,value
);
223 void SqlGwStatement::setLongParam(int paramPos
, long value
)
225 if (adapter
&& shouldAdapterHandle()) adapter
->setLongParam(paramPos
, value
);
226 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setLongParam(paramPos
,value
);
230 void SqlGwStatement::setLongLongParam(int paramPos
, long long value
)
232 if (adapter
&& shouldAdapterHandle()) adapter
->setLongLongParam(paramPos
, value
);
233 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setLongLongParam(paramPos
,value
);
236 void SqlGwStatement::setByteIntParam(int paramPos
, ByteInt value
)
238 if (adapter
&& shouldAdapterHandle()) adapter
->setByteIntParam(paramPos
, value
);
239 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setByteIntParam(paramPos
,value
);
242 void SqlGwStatement::setFloatParam(int paramPos
, float value
)
244 if (adapter
&& shouldAdapterHandle()) adapter
->setFloatParam(paramPos
, value
);
245 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setFloatParam(paramPos
,value
);
248 void SqlGwStatement::setDoubleParam(int paramPos
, double value
)
250 if (adapter
&& shouldAdapterHandle()) adapter
->setDoubleParam(paramPos
, value
);
251 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setDoubleParam(paramPos
,value
);
255 void SqlGwStatement::setStringParam(int paramPos
, char *value
)
257 if (adapter
&& shouldAdapterHandle()) adapter
->setStringParam(paramPos
, value
);
258 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setStringParam(paramPos
,value
);
261 void SqlGwStatement::setDateParam(int paramPos
, Date value
)
263 if (adapter
&& shouldAdapterHandle()) adapter
->setDateParam(paramPos
, value
);
264 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setDateParam(paramPos
,value
);
267 void SqlGwStatement::setTimeParam(int paramPos
, Time value
)
269 if (adapter
&& shouldAdapterHandle()) adapter
->setTimeParam(paramPos
, value
);
270 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setTimeParam(paramPos
,value
);
273 void SqlGwStatement::setTimeStampParam(int paramPos
, TimeStamp value
)
275 if (adapter
&& shouldAdapterHandle()) adapter
->setTimeStampParam(paramPos
, value
);
276 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setTimeStampParam(paramPos
,value
);
279 void SqlGwStatement::setBinaryParam(int paramPos
, void *value
)
281 if (adapter
&& shouldAdapterHandle()) adapter
->setBinaryParam(paramPos
, value
);
282 if (innerStmt
&& shouldCSqlHandle()) innerStmt
->setBinaryParam(paramPos
,value
);
284 bool SqlGwStatement::isFldNull(int pos
)
286 if (adapter
&& shouldAdapterHandle()) return adapter
->isFldNull(pos
);
287 if (innerStmt
&& shouldCSqlHandle()) return innerStmt
->isFldNull(pos
);
290 void SqlGwStatement::setNull(int pos
)
292 if (adapter
&& shouldAdapterHandle()) adapter
->setNull(pos
);
293 if (innerStmt
&& shouldCSqlHandle()) innerStmt
-> setNull(pos
);