src/sqllog/SqlLogStatement.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Prabakaran Thirumalai   *
00003  *   praba_tuty@yahoo.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <SqlLogStatement.h>
00021 
00022 UniqueID SqlLogStatement::stmtUID;
00023 
00024 bool SqlLogStatement::isNonSelectDML(char *stmtstr)
00025 {
00026     if (strlen(stmtstr) <= 6) return false;
00027     if (strncasecmp(stmtstr,"INSERT", 6) == 0) return true;
00028     else if (strncasecmp(stmtstr, "UPDATE", 6) ==0) return true;
00029     else if (strncasecmp(stmtstr, "DELETE", 6) ==0) return true;
00030     return false;
00031 }
00032 
00033 DbRetVal SqlLogStatement::prepare(char *stmtstr)
00034 {
00035     DbRetVal rv = OK;
00036     if (innerStmt) rv = innerStmt->prepare(stmtstr);
00037     if (rv != OK) return rv;
00038 
00039     isCached = false;
00040     //check if it is INSERT UPDATE DELETE statement
00041     //if not, then no need to generate logs
00042     if (!isNonSelectDML(stmtstr)) { return rv;}
00043     if (!Conf::config.useReplication() && !Conf::config.useCache()) return OK;
00044     SqlLogConnection* logConn = (SqlLogConnection*)con;
00045     if (!logConn->isTableCached(innerStmt->getTableName())) return OK;
00046     isCached = true;
00047     mode = TABLE_OSYNC;//TEMP::support only OSYNC 
00048 
00049     sid  = SqlLogStatement::stmtUID.getID();
00050     //TODO::if connected to peer then only send this packet
00051     PacketPrepare *pkt = new PacketPrepare();
00052     pkt->stmtID= sid;
00053     pkt->syncMode = ASYNC;
00054     pkt->stmtString = stmtstr;
00055     pkt->noParams = innerStmt->noOfParamFields();
00056     FieldInfo *info = new FieldInfo();
00057     if (pkt->noParams > 0) {
00058       pkt->type = new int [pkt->noParams];
00059       pkt->length = new int [pkt->noParams];
00060       BindSqlField *bindField = NULL;
00061       for (int i = 0; i < innerStmt->noOfParamFields(); i++)
00062       {
00063         innerStmt->getParamFldInfo(i+1, info);
00064         bindField = new BindSqlField();
00065         bindField->type = info->type;
00066         bindField->length = info->length;
00067         pkt->type[i] =  info->type;
00068         pkt->length[i] =  info->length;
00069         bindField->value = AllDataType::alloc(info->type, info->length);
00070         paramList.append(bindField);
00071       }
00072     }
00073     pkt->marshall();
00074     /*logConn->connectIfNotConnected();
00075     //printf("Sending PREPARE packet of size %d\n", pkt->getBufferSize());
00076     rv = logConn->sendAndReceive(NW_PKT_PREPARE, pkt->getMarshalledBuffer(), pkt->getBufferSize());
00077     printf("RV from PREPARE SQLLOG %d\n", rv);
00078     if (rv != OK) { 
00079        logConn->addPreparePacket(pkt); 
00080        delete info; 
00081        return OK;
00082     }*/
00083     logConn->addPreparePacket(pkt); 
00084     delete info;
00085     return rv;
00086 }
00087 
00088 bool SqlLogStatement::isSelect()
00089 {
00090     if (innerStmt) return innerStmt->isSelect();
00091     return false;
00092 }
00093 
00094 DbRetVal SqlLogStatement::execute(int &rowsAffected)
00095 {
00096 
00097     SqlLogConnection* logConn = (SqlLogConnection*)con;
00098 
00099     DbRetVal rv = OK;
00100     if (innerStmt) rv = innerStmt->execute(rowsAffected);
00101     if (rv != OK) return rv;
00102 
00103     //no need to generate log if it does not actually modify the table
00104     if (rowsAffected == 0 ) return OK;
00105     if (!isCached) return OK;
00106     if (logConn->getSyncMode() == OSYNC) return OK;
00107 
00108     //printf("LOG:execute\n");
00109     PacketExecute *pkt = new PacketExecute();
00110     pkt->stmtID= sid;
00111     pkt->noParams = innerStmt->noOfParamFields();
00112     pkt->setParams(paramList);
00113     pkt->marshall();
00114     int *p = (int*)pkt->getMarshalledBuffer();
00115     //printf("After EXEC packet marshall %d %d size %d\n", *p, *(p+1),
00116     //                pkt->getBufferSize());
00117    // printf("EXEC pkt ptr is %x\n", pkt);
00118     logConn->addPacket(pkt);
00119     return rv;
00120 }
00121 
00122 DbRetVal SqlLogStatement::bindParam(int pos, void* value)
00123 {
00124     DbRetVal rv = OK;
00125     if (innerStmt) rv = innerStmt->bindParam(pos,value);
00126     if (rv != OK) return rv;
00127     printError(ErrWarning, "Deprecated and does not replicate or cache");
00128     return rv;
00129 }
00130 
00131 DbRetVal SqlLogStatement::bindField(int pos, void* value)
00132 {
00133     DbRetVal rv = OK;
00134     if (innerStmt) rv = innerStmt->bindField(pos,value);
00135     if (rv != OK) return rv;
00136     return rv;
00137 }
00138 void* SqlLogStatement::fetch()
00139 {
00140     if (innerStmt) return innerStmt->fetch();
00141     return NULL;
00142 }
00143 
00144 void* SqlLogStatement::fetchAndPrint(bool SQL)
00145 {
00146     if (innerStmt) return innerStmt->fetchAndPrint(SQL);
00147     return NULL;
00148 }
00149 
00150 void* SqlLogStatement::next()
00151 {
00152     if (innerStmt) return innerStmt->next();
00153     return NULL;
00154 }
00155 
00156 DbRetVal SqlLogStatement::close()
00157 {
00158     if (innerStmt) return innerStmt->close();
00159     return OK;
00160 }
00161 
00162 void* SqlLogStatement::getFieldValuePtr( int pos )
00163 {
00164     if (innerStmt) return innerStmt->getFieldValuePtr(pos);
00165     return NULL;
00166 }
00167 
00168 int SqlLogStatement::noOfProjFields()
00169 {
00170     if (innerStmt) return innerStmt->noOfProjFields();
00171     return 0;
00172 }
00173 
00174 int SqlLogStatement::noOfParamFields()
00175 {
00176     if (innerStmt) return innerStmt->noOfParamFields();
00177     return 0;
00178 }
00179 
00180 DbRetVal SqlLogStatement::getProjFldInfo (int projpos, FieldInfo *&fInfo)
00181 {
00182     if (innerStmt) return innerStmt->getProjFldInfo(projpos, fInfo);
00183     return OK;
00184 }
00185 
00186 DbRetVal SqlLogStatement::getParamFldInfo (int parampos, FieldInfo *&fInfo)
00187 {
00188     if (innerStmt) return innerStmt->getParamFldInfo(parampos, fInfo);
00189     return OK;
00190 }
00191 
00192 DbRetVal SqlLogStatement::free()
00193 {
00194     DbRetVal rv = OK;
00195     if (innerStmt) rv = innerStmt->free();
00196     //TODO::DEBUG::always innsrStmt->free() returns error
00197     //if (rv != OK) return rv;
00198     SqlLogConnection* logConn = (SqlLogConnection*)con;
00199     if (sid != 0 ) logConn->removePreparePacket(sid);
00200     if (!isCached) return rv;
00201 
00202     //TODO
00203     //If statement is freed before the txn commits, it will lead to issue 
00204     //incase of async mode. when the other site goes down and comes back,
00205     //it will not have the cached SqlStatement objects, so in that case 
00206     //we need to send all the prepare packets again, so we should not free
00207     //the statement straight away in client side as well as in server side
00208 
00209 
00210 
00211     /*PacketFree *pkt = new PacketFree();
00212     pkt->stmtID= sid;
00213     pkt->marshall();
00214     SqlLogConnection* logConn = (SqlLogConnection*)con;
00215     logConn->sendAndReceiveAllPeers(NW_PKT_FREE, pkt->getMarshalledBuffer(), pkt->getBufferSize());
00216     delete pkt;*/
00217     isCached= false;
00218     sid = 0;
00219     paramList.reset();
00220     return OK;
00221 }
00222 void SqlLogStatement::setShortParam(int paramPos, short value)
00223 {
00224     if (innerStmt) innerStmt->setShortParam(paramPos,value);
00225     SqlLogConnection* logConn = (SqlLogConnection*)con;
00226     if (logConn->getSyncMode() == OSYNC) return ;
00227     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00228     if (bindField->type != typeShort) return;
00229     *(short*)(bindField->value) = value;
00230     return;
00231 }
00232 void SqlLogStatement::setIntParam(int paramPos, int value)
00233 {
00234     if (innerStmt) innerStmt->setIntParam(paramPos,value);
00235     SqlLogConnection* logConn = (SqlLogConnection*)con;
00236     if (logConn->getSyncMode() == OSYNC) return ;
00237     if (!isCached) return;
00238     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00239     if (bindField->type != typeInt) return;
00240     *(int*)(bindField->value) = value;
00241     return;
00242 
00243 }
00244 void SqlLogStatement::setLongParam(int paramPos, long value)
00245 {
00246     if (innerStmt) innerStmt->setLongParam(paramPos,value);
00247     SqlLogConnection* logConn = (SqlLogConnection*)con;
00248     if (logConn->getSyncMode() == OSYNC) return ;
00249     if (!isCached) return;
00250     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00251     if (bindField->type != typeLong) return;
00252     *(long*)(bindField->value) = value;
00253     return;
00254 
00255 }
00256 void SqlLogStatement::setLongLongParam(int paramPos, long long value)
00257 {
00258     if (innerStmt) innerStmt->setLongLongParam(paramPos,value);
00259     SqlLogConnection* logConn = (SqlLogConnection*)con;
00260     if (logConn->getSyncMode() == OSYNC) return ;
00261     if (!isCached) return;
00262     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00263     if (bindField->type != typeLongLong) return;
00264     *(long long*)(bindField->value) = value;
00265     return;
00266 }
00267 void SqlLogStatement::setByteIntParam(int paramPos, ByteInt value)
00268 {
00269     if (innerStmt) innerStmt->setByteIntParam(paramPos,value);
00270     SqlLogConnection* logConn = (SqlLogConnection*)con;
00271     if (logConn->getSyncMode() == OSYNC) return ;
00272     if (!isCached) return;
00273     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00274     if (bindField->type != typeByteInt) return;
00275     *(char*)(bindField->value) = value;
00276 
00277 }
00278 void SqlLogStatement::setFloatParam(int paramPos, float value)
00279 {
00280     if (innerStmt) innerStmt->setFloatParam(paramPos,value);
00281     SqlLogConnection* logConn = (SqlLogConnection*)con;
00282     if (logConn->getSyncMode() == OSYNC) return ;
00283     if (!isCached) return;
00284     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00285     if (bindField->type != typeFloat) return;
00286     *(float*)(bindField->value) = value;
00287 
00288 }
00289 void SqlLogStatement::setDoubleParam(int paramPos, double value)
00290 {
00291     if (innerStmt) innerStmt->setDoubleParam(paramPos,value);
00292     SqlLogConnection* logConn = (SqlLogConnection*)con;
00293     if (logConn->getSyncMode() == OSYNC) return ;
00294     if (!isCached) return;
00295     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00296     if (bindField->type != typeDouble) return;
00297     *(double*)(bindField->value) = value;
00298 
00299 }
00300 void SqlLogStatement::setStringParam(int paramPos, char *value)
00301 {
00302     if (innerStmt) innerStmt->setStringParam(paramPos,value);
00303     SqlLogConnection* logConn = (SqlLogConnection*)con;
00304     if (logConn->getSyncMode() == OSYNC) return ;
00305     if (!isCached) return;
00306     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00307     if (bindField->type != typeString) return;
00308     char *dest = (char*)bindField->value;
00309     strncpy(dest, value, bindField->length);
00310     dest[ bindField->length - 1] ='\0';
00311     return;
00312 }
00313 void SqlLogStatement::setDateParam(int paramPos, Date value)
00314 {
00315     if (innerStmt) innerStmt->setDateParam(paramPos,value);
00316     SqlLogConnection* logConn = (SqlLogConnection*)con;
00317     if (logConn->getSyncMode() == OSYNC) return ;
00318     if (!isCached) return;
00319     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00320     if (bindField->type != typeDate) return;
00321     *(Date*)(bindField->value) = value;
00322 
00323 }
00324 void SqlLogStatement::setTimeParam(int paramPos, Time value)
00325 {
00326     if (innerStmt) innerStmt->setTimeParam(paramPos,value);
00327     SqlLogConnection* logConn = (SqlLogConnection*)con;
00328     if (logConn->getSyncMode() == OSYNC) return ;
00329     if (!isCached) return;
00330     BindSqlField *bindField = (BindSqlField*)paramList.get(paramPos);
00331     if (bindField->type != typeTime) return;
00332     *(Time*)(bindField->value) = value;
00333 
00334 }
00335 void SqlLogStatement::setTimeStampParam(int paramPos, TimeStamp value)
00336 {
00337     if (innerStmt) innerStmt->setTimeStampParam(paramPos,value);
00338     SqlLogConnection* logConn = (SqlLogConnection*)con;
00339     if (logConn->getSyncMode() == OSYNC) return ;
00340     if (!isCached) return;
00341     BindSqlField *bindField = (BindSqlField*) paramList.get(paramPos);
00342     if (bindField->type != typeTimeStamp) return;
00343     *(TimeStamp*)(bindField->value) = value;
00344 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7