Version: 1.0.0
mysql_preparedstatement.cpp
Go to the documentation of this file.
1 #include "wx/database/wxprec.h"
2 
3 #if wxUSE_DATABASE_MYSQL
4 
7 {
8  m_pInterface = pInterface;
9  m_Statements.clear();
10 }
11 
14 {
15  m_pInterface = pInterface;
16  AddPreparedStatement(pStatement);
17 }
18 
20 {
21  Close();
22 }
23 
24 
26 {
28 
29  // Free the statements
30  MysqlStatementWrapperArray::iterator start = m_Statements.begin();
31  MysqlStatementWrapperArray::iterator stop = m_Statements.end();
32 
33  while (start != stop)
34  {
35  if ((*start) != NULL)
36  {
38  wxDELETE(pWrapper);
39  (*start) = NULL;
40  }
41  start++;
42  }
43 }
44 
45 void wxMysqlPreparedStatement::AddPreparedStatement(MYSQL_STMT* pStatement)
46 {
48  if (pStatementWrapper)
49  pStatementWrapper->SetEncoding(GetEncoding());
50  m_Statements.push_back(pStatementWrapper);
51 }
52 
53 // get field
54 void wxMysqlPreparedStatement::SetParamInt(int nPosition, int nValue)
55 {
56  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
57  if (nIndex > -1)
58  {
59  m_Statements[nIndex]->SetParam(nPosition, nValue);
60  }
61 }
62 
63 void wxMysqlPreparedStatement::SetParamDouble(int nPosition, double dblValue)
64 {
65  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
66  if (nIndex > -1)
67  {
68  m_Statements[nIndex]->SetParam(nPosition, dblValue);
69  }
70 }
71 
72 void wxMysqlPreparedStatement::SetParamString(int nPosition, const wxString& strValue)
73 {
74  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
75  if (nIndex > -1)
76  {
77  m_Statements[nIndex]->SetParam(nPosition, strValue);
78  }
79 }
80 
82 {
83  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
84  if (nIndex > -1)
85  {
86  m_Statements[nIndex]->SetParam(nPosition);
87  }
88 }
89 
90 void wxMysqlPreparedStatement::SetParamBlob(int nPosition, const void* pData, long nDataLength)
91 {
92  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
93  if (nIndex > -1)
94  {
95  m_Statements[nIndex]->SetParam(nPosition, pData, nDataLength);
96  }
97 }
98 
99 void wxMysqlPreparedStatement::SetParamDate(int nPosition, const wxDateTime& dateValue)
100 {
101  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
102  if (nIndex > -1)
103  {
104  m_Statements[nIndex]->SetParam(nPosition, dateValue);
105  }
106 }
107 
108 void wxMysqlPreparedStatement::SetParamBool(int nPosition, bool bValue)
109 {
110  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
111  if (nIndex > -1)
112  {
113  m_Statements[nIndex]->SetParam(nPosition, bValue);
114  }
115 }
116 
118 {
119  MysqlStatementWrapperArray::iterator start = m_Statements.begin();
120  MysqlStatementWrapperArray::iterator stop = m_Statements.end();
121 
122  int nParameters = 0;
123  while (start != stop)
124  {
125  nParameters += ((wxMysqlPreparedStatementWrapper*)(*start))->GetParameterCount();
126  start++;
127  }
128  return nParameters;
129 }
130 
132 {
133  MysqlStatementWrapperArray::iterator start = m_Statements.begin();
134  MysqlStatementWrapperArray::iterator stop = m_Statements.end();
135 
136  int nRows = -1;
137  while (start != stop)
138  {
139  nRows = ((wxMysqlPreparedStatementWrapper*)(*start))->RunQuery();
141  {
146  }
147  start++;
148  }
149  return nRows;
150 }
151 
153 {
154  if (m_Statements.size() > 0)
155  {
156  for (unsigned int i=0; i<(m_Statements.size()-1); i++)
157  {
159  pStatement->RunQuery();
160  if (pStatement->GetErrorCode() != wxDATABASE_OK)
161  {
162  SetErrorCode(pStatement->GetErrorCode());
163  SetErrorMessage(pStatement->GetErrorMessage());
165  return NULL;
166  }
167  }
168 
169  wxMysqlPreparedStatementWrapper* pLastStatement = m_Statements[m_Statements.size()-1];
170  wxDatabaseResultSet* pResults = pLastStatement->RunQueryWithResults();
171  if (pLastStatement->GetErrorCode() != wxDATABASE_OK)
172  {
173  SetErrorCode(pLastStatement->GetErrorCode());
174  SetErrorMessage(pLastStatement->GetErrorMessage());
176  }
177  LogResultSetForCleanup(pResults);
178  return pResults;
179  }
180  else
181  return NULL;
182 }
183 
185 {
186  if (m_Statements.size() == 0)
187  return 0;
188 
189  // Go through all the elements in the vector
190  // Get the number of parameters in each statement
191  // Adjust the nPosition for the the broken up statements
192  for (unsigned int i=0; i<m_Statements.size(); i++)
193  {
194  int nParametersInThisStatement = m_Statements[i]->GetParameterCount();
195 
196  if (*pPosition > nParametersInThisStatement)
197  {
198  *pPosition -= nParametersInThisStatement; // Decrement the position indicator by the number of parameters in this statement
199  }
200  else
201  {
202  // We're in the correct statement, return the index
203  return i;
204  }
205  }
206  return -1;
207 }
208 
209 #endif//wxUSE_DATABASE_MYSQL
virtual int GetParameterCount()
virtual void SetParamString(int nPosition, const wxString &strValue)
Set the parameter at the 1-based position to a wxString value.
virtual void SetParamNull(int nPosition)
Set the parameter at the 1-based position to a NULL value.
wxDatabaseResultSet * RunQueryWithResults()
virtual void SetParamDate(int nPosition, const wxDateTime &dateValue)
Set the parameter at the 1-based position to a wxDateTime value.
virtual int RunQuery()
Run an insert, update, or delete query on the database.
const wxString & GetErrorMessage()
#define wxDATABASE_OK
Definition: errorcodes.h:4
virtual void Close()
Close the result set (call wxDatabase::ClosePreparedStatement() instead on the statement)
virtual ~wxMysqlPreparedStatement()
void SetErrorMessage(const wxString &strErrorMessage)
void LogResultSetForCleanup(wxDatabaseResultSet *pResultSet)
Add result set object pointer to the list for "garbage collection".
virtual void SetParamBool(int nPosition, bool bValue)
Set the parameter at the 1-based position to a boolean value.
wxMysqlDynamicInterface * m_pInterface
void SetEncoding(wxFontEncoding encoding)
#define wxDATABASE_QUERY_RESULT_ERROR
Definition: errorcodes.h:21
virtual void SetParamInt(int nPosition, int nValue)
Set the parameter at the 1-based position to an int value.
void AddPreparedStatement(MYSQL_STMT *pStatement)
int FindStatementAndAdjustPositionIndex(int *pPosition)
void CloseResultSets()
Close all result set objects that have been generated but not yet closed.
void SetErrorCode(int nErrorCode)
const wxCSConv * GetEncoding()
MysqlStatementWrapperArray m_Statements
virtual void SetParamBlob(int nPosition, const void *pData, long nDataLength)
Set the parameter at the 1-based position to a Blob value.
wxMysqlPreparedStatement(wxMysqlDynamicInterface *pInterface)
virtual wxDatabaseResultSet * RunQueryWithResults()
Run an insert, update, or delete query on the database.
virtual void SetParamDouble(int nPosition, double dblValue)
Set the parameter at the 1-based position to a double value.