Version: 1.0.0
sqlite_preparedstatement.cpp
Go to the documentation of this file.
1 #include "wx/database/wxprec.h"
2 
3 #if wxUSE_DATABASE_SQLITE
4 
5 // ctor
8 {
9  m_pDatabase = pDatabase;
10 }
11 
12 wxSqlitePreparedStatement::wxSqlitePreparedStatement(sqlite3* pDatabase, sqlite3_stmt* pStatement)
14 {
15  m_pDatabase = pDatabase;
16  m_Statements.push_back(pStatement);
17 }
18 
19 wxSqlitePreparedStatement::wxSqlitePreparedStatement(sqlite3* pDatabase, wxSqliteStatementVector statements)
21 {
22  m_pDatabase = pDatabase;
23  m_Statements = statements;
24 }
25 
26 // dtor
28 {
29  Close();
30 }
31 
33 {
35 
36  wxSqliteStatementVector::iterator start = m_Statements.begin();
37  wxSqliteStatementVector::iterator stop = m_Statements.end();
38  while (start != stop)
39  {
40  if ((*start) != NULL)
41  {
42  sqlite3_finalize((sqlite3_stmt*)(*start));
43  (*start) = NULL;
44  //wxDELETE(*start);
45  }
46  start++;
47  }
48  m_Statements.Clear();
49 }
50 /*
51 void SqlitePreparedStatement::AddPreparedStatement(CppSQLite3Statement* pStatement)
52 {
53  m_Statements.push_back(pStatement);
54 }
55 */
56 void wxSqlitePreparedStatement::AddPreparedStatement(sqlite3_stmt* pStatement)
57 {
58  m_Statements.push_back(pStatement);
59 }
60 
61 // get field
62 void wxSqlitePreparedStatement::SetParamInt(int nPosition, int nValue)
63 {
65 
66  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
67  if (nIndex > -1)
68  {
69  sqlite3_reset(m_Statements[nIndex]);
70  int nReturn = sqlite3_bind_int(m_Statements[nIndex], nPosition, nValue);
71  if (nReturn != SQLITE_OK)
72  {
76  }
77  }
78 }
79 
80 void wxSqlitePreparedStatement::SetParamDouble(int nPosition, double dblValue)
81 {
83 
84  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
85  if (nIndex > -1)
86  {
87  sqlite3_reset(m_Statements[nIndex]);
88  int nReturn = sqlite3_bind_double(m_Statements[nIndex], nPosition, dblValue);
89  if (nReturn != SQLITE_OK)
90  {
94  }
95  }
96 }
97 
98 void wxSqlitePreparedStatement::SetParamString(int nPosition, const wxString& strValue)
99 {
100  ResetErrorCodes();
101 
102  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
103  if (nIndex > -1)
104  {
105  sqlite3_reset(m_Statements[nIndex]);
106  wxCharBuffer valueBuffer = ConvertToUnicodeStream(strValue);
107  int nReturn = sqlite3_bind_text(m_Statements[nIndex], nPosition, valueBuffer, -1, SQLITE_TRANSIENT);
108  if (nReturn != SQLITE_OK)
109  {
113  }
114  }
115 }
116 
117 void wxSqlitePreparedStatement::SetParamNull(int nPosition)
118 {
119  ResetErrorCodes();
120 
121  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
122  if (nIndex > -1)
123  {
124  sqlite3_reset(m_Statements[nIndex]);
125  int nReturn = sqlite3_bind_null(m_Statements[nIndex], nPosition);
126  if (nReturn != SQLITE_OK)
127  {
131  }
132  }
133 }
134 
135 void wxSqlitePreparedStatement::SetParamBlob(int nPosition, const void* pData, long nDataLength)
136 {
137  ResetErrorCodes();
138 
139  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
140  if (nIndex > -1)
141  {
142  sqlite3_reset(m_Statements[nIndex]);
143  int nReturn = sqlite3_bind_blob(m_Statements[nIndex], nPosition, (const void*)pData, nDataLength, SQLITE_STATIC);
144  if (nReturn != SQLITE_OK)
145  {
149  }
150  }
151 }
152 
153 void wxSqlitePreparedStatement::SetParamDate(int nPosition, const wxDateTime& dateValue)
154 {
155  ResetErrorCodes();
156 
157  if (dateValue.IsValid())
158  {
159  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
160  if (nIndex > -1)
161  {
162  sqlite3_reset(m_Statements[nIndex]);
163  wxCharBuffer valueBuffer = ConvertToUnicodeStream(dateValue.Format(_("%Y-%m-%d %H:%M:%S")));
164  int nReturn = sqlite3_bind_text(m_Statements[nIndex], nPosition, valueBuffer, -1, SQLITE_TRANSIENT);
165  if (nReturn != SQLITE_OK)
166  {
170  }
171  }
172  }
173  else
174  {
175  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
176  if (nIndex > -1)
177  {
178  sqlite3_reset(m_Statements[nIndex]);
179  int nReturn = sqlite3_bind_null(m_Statements[nIndex], nPosition);
180  if (nReturn != SQLITE_OK)
181  {
185  }
186  }
187  }
188 }
189 
190 void wxSqlitePreparedStatement::SetParamBool(int nPosition, bool bValue)
191 {
192  ResetErrorCodes();
193 
194  int nIndex = FindStatementAndAdjustPositionIndex(&nPosition);
195  if (nIndex > -1)
196  {
197  sqlite3_reset(m_Statements[nIndex]);
198  int nReturn = sqlite3_bind_int(m_Statements[nIndex], nPosition, (bValue ? 1 : 0));
199  if (nReturn != SQLITE_OK)
200  {
204  }
205  }
206 }
207 
209 {
210  ResetErrorCodes();
211 
212  int nReturn = 0;
213  wxSqliteStatementVector::iterator start = m_Statements.begin();
214  wxSqliteStatementVector::iterator stop = m_Statements.end();
215  while (start != stop)
216  {
217  nReturn += sqlite3_bind_parameter_count((sqlite3_stmt*)(*start));
218  start++;
219  }
220  return nReturn;
221 }
222 
224 {
225  ResetErrorCodes();
226 
227  wxSqliteStatementVector::iterator start = m_Statements.begin();
228  wxSqliteStatementVector::iterator stop = m_Statements.end();
229  while (start != stop)
230  {
231  int nReturn = sqlite3_step((sqlite3_stmt*)(*start));
232 
233  if (nReturn != SQLITE_ROW)
234  sqlite3_reset((sqlite3_stmt*)(*start));
235 
236  if ((nReturn != SQLITE_ROW) && (nReturn != SQLITE_DONE))
237  {
242  }
243  start++;
244  }
245 
246  return sqlite3_changes(m_pDatabase);
247 }
248 
250 {
251  ResetErrorCodes();
252 
253  if (m_Statements.size() > 1)
254  {
255  for (unsigned int i=0; i<m_Statements.size()-1; i++)
256  {
257  int nReturn = sqlite3_step(m_Statements[i]);
258 
259  if (nReturn != SQLITE_ROW)
260  sqlite3_reset(m_Statements[i]);
261 
262  if ((nReturn != SQLITE_ROW) && (nReturn != SQLITE_DONE))
263  {
264  wxLogError(_("Error with RunQueryWithResults\n"));
268  return NULL;
269  }
270  }
271  }
272  // Work off the assumption that only the last statement will return result
273 
274  wxSqliteResultSet* pResultSet = new wxSqliteResultSet(this);
275  if (pResultSet)
276  pResultSet->SetEncoding(GetEncoding());
277 
278  LogResultSetForCleanup(pResultSet);
279  return pResultSet;
280 }
281 
283 {
284  // Don't mess around if there's just one entry in the vector
285  if (m_Statements.size() == 0)
286  return 0;
287 
288  // Go through all the elements in the vector
289  // Get the number of parameters in each statement
290  // Adjust the nPosition for the the broken up statements
291  for (unsigned int i=0; i<m_Statements.size(); i++)
292  {
293  int nParametersInThisStatement = sqlite3_bind_parameter_count(m_Statements[i]);
294  if (*pPosition > nParametersInThisStatement)
295  {
296  *pPosition -= nParametersInThisStatement; // Decrement the position indicator by the number of parameters in this statement
297  }
298  else
299  {
300  // We're in the correct statement, return the index
301  return i;
302  }
303  }
304  return -1;
305 }
306 
307 #endif//wxUSE_DATABASE_SQLITE
virtual void SetParamDouble(int nPosition, double dblValue)
Set the parameter at the 1-based position to a double value.
virtual ~wxSqlitePreparedStatement()
virtual void SetParamBlob(int nPosition, const void *pData, long nDataLength)
Set the parameter at the 1-based position to a Blob value.
void SetErrorMessage(const wxString &strErrorMessage)
virtual void SetParamNull(int nPosition)
Set the parameter at the 1-based position to a NULL value.
virtual void SetParamDate(int nPosition, const wxDateTime &dateValue)
Set the parameter at the 1-based position to a wxDateTime value.
void LogResultSetForCleanup(wxDatabaseResultSet *pResultSet)
Add result set object pointer to the list for "garbage collection".
virtual int GetParameterCount()
void SetEncoding(wxFontEncoding encoding)
wxSqlitePreparedStatement(sqlite3 *pDatabase)
#define wxDATABASE_QUERY_RESULT_ERROR
Definition: errorcodes.h:21
virtual void SetParamString(int nPosition, const wxString &strValue)
Set the parameter at the 1-based position to a wxString value.
virtual void Close()
Close the result set (call wxDatabase::ClosePreparedStatement() instead on the statement)
wxSqliteStatementVector m_Statements
void CloseResultSets()
Close all result set objects that have been generated but not yet closed.
void SetErrorCode(int nErrorCode)
virtual void SetParamBool(int nPosition, bool bValue)
Set the parameter at the 1-based position to a boolean value.
static int TranslateErrorCode(int nCode)
void AddPreparedStatement(sqlite3_stmt *pStatement)
virtual wxString ConvertFromUnicodeStream(const char *inputBuffer)
const wxCSConv * GetEncoding()
wxDatabaseStatementHashSet m_Statements
Definition: database.h:201
virtual int RunQuery()
Run an insert, update, or delete query on the database.
virtual const wxCharBuffer ConvertToUnicodeStream(const wxString &inputString)
int FindStatementAndAdjustPositionIndex(int *pPosition)
virtual void SetParamInt(int nPosition, int nValue)
Set the parameter at the 1-based position to an int value.
virtual wxDatabaseResultSet * RunQueryWithResults()
Run an insert, update, or delete query on the database.