Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

MysqlPreparedStatement.cpp

Go to the documentation of this file.
00001 #include "../include/MysqlPreparedStatement.h"
00002 #include "../include/MysqlDatabaseLayer.h"
00003 #include "../include/DatabaseErrorCodes.h"
00004 
00005 MysqlPreparedStatement::MysqlPreparedStatement()
00006  : PreparedStatement()
00007 {
00008   m_Statements.clear();
00009 }
00010 
00011 MysqlPreparedStatement::MysqlPreparedStatement(MYSQL_STMT* pStatement)
00012  : PreparedStatement()
00013 {
00014   AddPreparedStatement(pStatement);
00015 }
00016 
00017 MysqlPreparedStatement::~MysqlPreparedStatement()
00018 {
00019   Close();
00020 }
00021 
00022 
00023 void MysqlPreparedStatement::Close()
00024 {
00025   CloseResultSets();
00026 
00027   // Free the statements
00028   MysqlStatementWrapperArray::iterator start = m_Statements.begin();
00029   MysqlStatementWrapperArray::iterator stop = m_Statements.end();
00030 
00031   while (start != stop)
00032   {
00033     if ((*start) != NULL)
00034     {
00035       delete (*start);
00036       (*start) = NULL;
00037     }
00038     start++;
00039   }
00040 }
00041 
00042 void MysqlPreparedStatement::AddPreparedStatement(MYSQL_STMT* pStatement)
00043 {
00044   MysqlPreparedStatementWrapper* pStatementWrapper = new MysqlPreparedStatementWrapper(pStatement);
00045   if (pStatementWrapper)
00046     pStatementWrapper->SetEncoding(GetEncoding());
00047   m_Statements.push_back(pStatementWrapper);
00048 }
00049 
00050 // get field
00051 void MysqlPreparedStatement::SetParamInt(int nPosition, int nValue)
00052 {
00053   int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
00054   if (nIndex > -1)
00055   {
00056     m_Statements[nIndex]->SetParam(nPosition, nValue);
00057   }
00058 }
00059 
00060 void MysqlPreparedStatement::SetParamDouble(int nPosition, double dblValue)
00061 {
00062   int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
00063   if (nIndex > -1)
00064   {
00065     m_Statements[nIndex]->SetParam(nPosition, dblValue);
00066   }
00067 }
00068 
00069 void MysqlPreparedStatement::SetParamString(int nPosition, const wxString& strValue)
00070 {
00071   int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
00072   if (nIndex > -1)
00073   {
00074     m_Statements[nIndex]->SetParam(nPosition, strValue);
00075   }
00076 }
00077 
00078 void MysqlPreparedStatement::SetParamNull(int nPosition)
00079 {
00080   int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
00081   if (nIndex > -1)
00082   {
00083     m_Statements[nIndex]->SetParam(nPosition);
00084   }
00085 }
00086 
00087 void MysqlPreparedStatement::SetParamBlob(int nPosition, const void* pData, long nDataLength)
00088 {
00089   int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
00090   if (nIndex > -1)
00091   {
00092     m_Statements[nIndex]->SetParam(nPosition, pData, nDataLength);
00093   }
00094 }
00095 
00096 void MysqlPreparedStatement::SetParamDate(int nPosition, const wxDateTime& dateValue)
00097 {
00098   int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
00099   if (nIndex > -1)
00100   {
00101     m_Statements[nIndex]->SetParam(nPosition, dateValue);
00102   }
00103 }
00104 
00105 void MysqlPreparedStatement::SetParamBool(int nPosition, bool bValue)
00106 {
00107   int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
00108   if (nIndex > -1)
00109   {
00110     m_Statements[nIndex]->SetParam(nPosition, bValue);
00111   }
00112 }
00113 
00114 int MysqlPreparedStatement::GetParameterCount()
00115 {
00116   MysqlStatementWrapperArray::iterator start = m_Statements.begin();
00117   MysqlStatementWrapperArray::iterator stop = m_Statements.end();
00118 
00119   int nParameters = 0;
00120   while (start != stop)
00121   {
00122     nParameters += (*start)->GetParameterCount();
00123     start++;
00124   }
00125   return nParameters;
00126 }
00127 
00128 void MysqlPreparedStatement::RunQuery()
00129 {
00130   MysqlStatementWrapperArray::iterator start = m_Statements.begin();
00131   MysqlStatementWrapperArray::iterator stop = m_Statements.end();
00132 
00133   while (start != stop)
00134   {
00135     (*start)->RunQuery();
00136     if ((*start)->GetErrorCode() != DATABASE_LAYER_OK)
00137     {
00138       SetErrorCode((*start)->GetErrorCode());
00139       SetErrorMessage((*start)->GetErrorMessage());
00140       ThrowDatabaseException();
00141       return;
00142     }
00143     start++;
00144   }
00145 }
00146 
00147 DatabaseResultSet* MysqlPreparedStatement::RunQueryWithResults()
00148 {
00149   if (m_Statements.size() > 0)
00150   {
00151     for (unsigned int i=0; i<(m_Statements.size()-1); i++)
00152     {
00153       MysqlPreparedStatementWrapper* pStatement = m_Statements[i];
00154       pStatement->RunQuery();
00155       if (pStatement->GetErrorCode() != DATABASE_LAYER_OK)
00156       {
00157         SetErrorCode(pStatement->GetErrorCode());
00158         SetErrorMessage(pStatement->GetErrorMessage());
00159         ThrowDatabaseException();
00160         return NULL;
00161       }
00162     }
00163 
00164     MysqlPreparedStatementWrapper* pLastStatement = m_Statements[m_Statements.size()-1];
00165     DatabaseResultSet* pResults = pLastStatement->RunQueryWithResults();
00166     if (pLastStatement->GetErrorCode() != DATABASE_LAYER_OK)
00167     {
00168       SetErrorCode(pLastStatement->GetErrorCode());
00169       SetErrorMessage(pLastStatement->GetErrorMessage());
00170       ThrowDatabaseException();
00171     }
00172     LogResultSetForCleanup(pResults);
00173     return pResults;
00174   }
00175   else
00176     return NULL;
00177 }
00178 
00179 int MysqlPreparedStatement::FindStatementAndAdjustPositionIndex(int* pPosition)
00180 {
00181   if (m_Statements.size() == 0)
00182     return 0;
00183     
00184   // Go through all the elements in the vector
00185   // Get the number of parameters in each statement
00186   // Adjust the nPosition for the the broken up statements
00187   for (unsigned int i=0; i<m_Statements.size(); i++)
00188   {
00189     int nParametersInThisStatement = m_Statements[i]->GetParameterCount();
00190 
00191     if (*pPosition > nParametersInThisStatement)
00192     {
00193       *pPosition -= nParametersInThisStatement;    // Decrement the position indicator by the number of parameters in this statement
00194     }
00195     else
00196     {
00197       // We're in the correct statement, return the index
00198       return i;
00199     }
00200   }
00201   return -1;
00202 }
00203 

Generated on Sat May 13 17:31:34 2006 for databaselayer by  doxygen 1.4.1