src/sql/SqlStatement.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 "Statement.h"
00021 #include <SqlStatement.h>
00022 #include <dmllex.h>
00023 
00024 char *lexInput;
00025 extern ParsedData *parsedData;
00026 
00027 int yyparse ();
00028 
00029 SqlStatement::SqlStatement()
00030 {
00031     sqlCon = NULL;
00032     stmt = NULL;
00033     isPrepd = false;
00034 }
00035 void SqlStatement::setConnection(AbsSqlConnection *conn)
00036 {
00037     sqlCon = (SqlConnection*)conn;
00038     con = conn;
00039 
00040 }
00041 void SqlStatement::setSqlConnection(SqlConnection *conn)
00042 {
00043     sqlCon = conn;
00044 }
00045 
00046 DbRetVal SqlStatement::prepare(char *stmtstr)
00047 {
00048     DbRetVal rv = OK;
00049     if (! sqlCon->isConnectionOpen()) {
00050         printError(ErrNotOpen, "Connection not open");
00051         return ErrNotOpen;
00052     }
00053     if(isPrepared()) free();
00054     lexInput = stmtstr;
00055     parsedData = &pData;
00056     yy_scan_string( stmtstr );
00057     int rc = yyparse();
00058     if (rc != 0) 
00059     {
00060         free();
00061         parsedData = NULL;
00062         yyrestart(yyin);
00063         return ErrSyntaxError;
00064     }
00065     stmt = StatementFactory::getStatement(parsedData);
00066     stmt->setDbMgr(sqlCon->getConnObject().getDatabaseManager());
00067     rv = stmt->resolve();
00068     if (rv != OK)
00069     {
00070         free();
00071         parsedData = NULL;
00072         yyrestart(yyin);
00073         return rv;
00074     }
00075     parsedData = NULL;
00076     yyrestart(yyin);
00077     isPrepd = true;
00078     return OK;
00079 }
00080 
00081 char*  SqlStatement::getTableName()
00082 {
00083     return pData.getTableName();
00084 }
00085 
00086 bool SqlStatement::isSelect()
00087 {
00088     if (pData.getStmtType() == SelectStatement) return true;
00089     return false;
00090 }
00091 
00092 bool SqlStatement::isPrepared() { return isPrepd; }
00093 
00094 DbRetVal SqlStatement::execute(int &rowsAffected)
00095 {
00096     DbRetVal rv = OK;
00097     if (! sqlCon->isConnectionOpen()) {
00098         printError(ErrNotOpen, "Connection not open");
00099         return ErrNotOpen;
00100     }
00101     if (! isPrepared()) {
00102         printError(ErrNotPrepared, "Statement Not Prepared");
00103         return ErrNotPrepared;
00104     }
00105     rv = stmt->execute(rowsAffected);
00106     return rv;
00107 }
00108 
00109 void* SqlStatement::fetch()
00110 {
00111     if (! sqlCon->isConnectionOpen()) {
00112         printError(ErrNotOpen, "Connection not open");
00113         return NULL;
00114     }
00115     if (! isPrepared()) {
00116         printError(ErrNotPrepared, "Statement Not Prepared");
00117         return NULL;
00118     }
00119     if (pData.getStmtType() != SelectStatement) return NULL;
00120     SelStatement *selStmt = (SelStatement*) stmt; 
00121     return selStmt->fetch();
00122 }
00123 
00124 void* SqlStatement::fetch(DbRetVal &rv)
00125 {
00126     if (! sqlCon->isConnectionOpen()) {
00127         printError(ErrNotOpen, "Connection not open");
00128         return NULL;
00129     }
00130     if (! isPrepared()) {
00131         printError(ErrNotPrepared, "Statement Not Prepared");
00132         return NULL;
00133     }
00134     if (pData.getStmtType() != SelectStatement) return NULL;
00135     SelStatement *selStmt = (SelStatement*) stmt; 
00136     return selStmt->fetch(rv);
00137 }
00138 
00139 void* SqlStatement::fetchAndPrint(bool SQL)
00140 {
00141     if (! sqlCon->isConnectionOpen()) {
00142         printError(ErrNotOpen, "Connection not open");
00143         return NULL;
00144     }
00145     if (! isPrepared()) {
00146         printError(ErrNotPrepared, "Statement Not Prepared");
00147         return NULL;
00148     }
00149     if (pData.getStmtType() != SelectStatement) return NULL;
00150     SelStatement *selStmt = (SelStatement*) stmt; 
00151     return selStmt->fetchAndPrint(SQL);
00152 }
00153 
00154 DbRetVal SqlStatement::bindParam(int pos, void* value)
00155 {
00156     DbRetVal rv = OK;
00157     rv = stmt->setParam(pos, value);
00158     return rv;
00159 }
00160 
00161 DbRetVal SqlStatement::bindField(int pos, void* value)
00162 {
00163     DbRetVal rv = OK;
00164     if (pData.getStmtType() != SelectStatement) return ErrBadCall;
00165     SelStatement *selStmt = (SelStatement*) stmt; 
00166     rv = selStmt->setBindField(pos, value);
00167     return rv;
00168 }
00169 void* SqlStatement::next()
00170 {
00171     if (pData.getStmtType() != SelectStatement) return 0;
00172     SelStatement *selStmt = (SelStatement*) stmt; 
00173     return( (void*) selStmt->next() );
00174 }
00175 
00176 DbRetVal SqlStatement::close()
00177 {
00178     if (pData.getStmtType() != SelectStatement) return OK;
00179     SelStatement *selStmt = (SelStatement*) stmt; 
00180     return selStmt->close();
00181 }
00182 void* SqlStatement::getParamValuePtr( int pos )
00183 {
00184     //if (pData.getStmtType() != SelectStatement) return 0;
00185     DmlStatement *dmlStmt = (DmlStatement*) stmt;
00186     return( (void*) dmlStmt->getParamValuePtr( pos ) );
00187 }
00188 
00189 char* SqlStatement::getFieldName( int pos )
00190 {
00191     if (pData.getStmtType() != SelectStatement) return 0;
00192     SelStatement *selStmt = (SelStatement*) stmt;
00193     return( (char*) selStmt->getFieldName( pos ) );
00194 }
00195 
00196 DataType SqlStatement::getFieldType( int pos )
00197 {
00198     if (pData.getStmtType() != SelectStatement) return typeUnknown;
00199     SelStatement *selStmt = (SelStatement*) stmt;
00200     return( (DataType) selStmt->getFieldType( pos ) );
00201 }
00202 int SqlStatement::getFieldLength( int pos )
00203 {
00204     if (pData.getStmtType() != SelectStatement) return 0;
00205     SelStatement *selStmt = (SelStatement*) stmt;
00206     return( (int) selStmt->getFieldLength( pos ) );
00207 }
00208 
00209 void* SqlStatement::getFieldValuePtr( int pos )
00210 {
00211     if (pData.getStmtType() != SelectStatement) return 0;
00212     SelStatement *selStmt = (SelStatement*) stmt; 
00213     return( (void*) selStmt->getFieldValuePtr( pos ) );
00214 }
00215 
00216 int SqlStatement::noOfProjFields()
00217 {
00218     if (pData.getStmtType() != SelectStatement) return 0;
00219     SelStatement *selStmt = (SelStatement*) stmt; 
00220     return selStmt->noOfProjFields();
00221 }
00222 
00223 int SqlStatement::noOfParamFields()
00224 {
00225     return stmt->noOfParamFields();
00226 }
00227 
00228 DbRetVal SqlStatement::getProjFldInfo (int projpos, FieldInfo *&fInfo)
00229 {
00230     DbRetVal rv = OK;
00231     if (pData.getStmtType() != SelectStatement) return ErrBadCall;
00232     SelStatement *selStmt = (SelStatement*) stmt; 
00233     rv = selStmt->getProjFldInfo(projpos, fInfo);
00234     return rv;
00235 }
00236 
00237 DbRetVal SqlStatement::getParamFldInfo (int parampos, FieldInfo *&fInfo)
00238 {
00239     DbRetVal rv = OK;
00240     if (pData.getStmtType() ==SelectStatement ||
00241         pData.getStmtType() ==InsertStatement ||
00242         pData.getStmtType() ==UpdateStatement ||
00243         pData.getStmtType() ==DeleteStatement)
00244     {
00245  
00246         DmlStatement *dmlStmt = (DmlStatement*) stmt; 
00247         rv = dmlStmt->getParamFldInfo(parampos, fInfo);
00248     }
00249     return rv;
00250 }
00251 
00252 DbRetVal SqlStatement::free()
00253 {
00254     delete stmt;
00255     stmt = NULL;
00256     pData.reset();
00257     isPrepd = false;
00258     return OK;
00259 }
00260 
00261 void SqlStatement::setShortParam(int paramPos, short value)
00262 {
00263     stmt->setShortParam(paramPos, value);
00264 }
00265 void SqlStatement::setIntParam(int paramPos, int value)
00266 {
00267     stmt->setIntParam(paramPos, value);
00268 }
00269 void SqlStatement::setLongParam(int paramPos, long value)
00270 {
00271     stmt->setLongParam(paramPos, value);
00272 }
00273 void SqlStatement::setLongLongParam(int paramPos, long long value)
00274 {
00275     stmt->setLongLongParam(paramPos, value);
00276 }
00277 void SqlStatement::setByteIntParam(int paramPos, ByteInt value)
00278 {
00279     stmt->setByteIntParam(paramPos, value);
00280 }
00281 void SqlStatement::setFloatParam(int paramPos, float value)
00282 {
00283     stmt->setFloatParam(paramPos, value);
00284 }
00285 void SqlStatement::setDoubleParam(int paramPos, double value)
00286 {
00287     stmt->setDoubleParam(paramPos, value);
00288 }
00289 void SqlStatement::setStringParam(int paramPos, char *value)
00290 {
00291     stmt->setStringParam(paramPos, value);
00292 }
00293 void SqlStatement::setDateParam(int paramPos, Date value)
00294 {
00295     stmt->setDateParam(paramPos, value);
00296 }
00297 void SqlStatement::setTimeParam(int paramPos, Time value)
00298 {
00299     stmt->setTimeParam(paramPos, value);
00300 }
00301 void SqlStatement::setTimeStampParam(int paramPos, TimeStamp value)
00302 {
00303     stmt->setTimeStampParam(paramPos, value);
00304 }
00305 

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