Version: 1.0.0
sqlite_resultset.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_pStatement = NULL;
10  m_pSqliteStatement = NULL;
11  m_bManageStatement = false;
12 }
13 
14 wxSqliteResultSet::wxSqliteResultSet(wxSqlitePreparedStatement* pStatement, bool bManageStatement /*= false*/)
16 {
17  m_pStatement = pStatement;
18  m_pSqliteStatement = m_pStatement->GetLastStatement();
19  m_bManageStatement = bManageStatement;
20 
21  // Populate field lookup map
22  int nFieldCount = sqlite3_column_count(m_pSqliteStatement);
23  for (int i=0; i<nFieldCount; i++)
24  {
25  wxString strField = ConvertFromUnicodeStream(sqlite3_column_name(m_pSqliteStatement, i));
26  m_FieldLookupMap[strField] = i;
27  }
28 }
29 
30 // dtor
32 {
33  Close();
34 }
35 
36 
38 {
39  CloseMetaData();
40 
42  {
43  if (m_pStatement != NULL)
44  {
46  wxDELETE(m_pStatement);
47  }
48  }
49 }
50 
51 
53 {
54  if (m_pSqliteStatement == NULL)
56  int nReturn = sqlite3_step(m_pSqliteStatement);
57 
58  if (nReturn != SQLITE_ROW)
59  sqlite3_reset(m_pSqliteStatement);
60 
61  if ((nReturn != SQLITE_ROW) && (nReturn != SQLITE_DONE))
62  {
63  wxLogError(_("Error with RunQueryWithResults\n"));
65 #if SQLITE_VERSION_NUMBER>=3002002
66  // sqlite3_db_handle wasn't added to the SQLite3 API until version 3.2.2
67  SetErrorMessage(ConvertFromUnicodeStream(sqlite3_errmsg(sqlite3_db_handle(m_pSqliteStatement))));
68 #else
69  SetErrorMessage(_("Unknown error advancing result set"));
70 #endif
72  return false;
73  }
74 
75  return (nReturn == SQLITE_ROW);
76 }
77 
78 
79 // get field
80 int wxSqliteResultSet::GetResultInt(int nField)
81 {
82  int nValue = -1;
83  if (m_pSqliteStatement == NULL)
85  nValue = sqlite3_column_int(m_pSqliteStatement, nField-1);
86 
87  return nValue;
88 }
89 
90 wxString wxSqliteResultSet::GetResultString(int nField)
91 {
92  wxString strValue = wxT("");
93  if (m_pSqliteStatement == NULL)
95  strValue = ConvertFromUnicodeStream((const char*)(sqlite3_column_text(m_pSqliteStatement, nField-1)));
96 
97  return strValue;
98 }
99 
100 long wxSqliteResultSet::GetResultLong(int nField)
101 {
102  long nValue = -1;
103  if (m_pSqliteStatement == NULL)
105  nValue = sqlite3_column_int(m_pSqliteStatement, nField-1);
106 
107  return nValue;
108 }
109 
110 bool wxSqliteResultSet::GetResultBool(int nField)
111 {
112  int nValue = 0;
113  if (m_pSqliteStatement == NULL)
115  nValue = sqlite3_column_int(m_pSqliteStatement, nField-1);
116 
117  return (nValue != 0);
118 }
119 
120 wxDateTime wxSqliteResultSet::GetResultDate(int nField)
121 {
122  // Don't use nField-1 here since GetResultString will take care of that
123  wxString strDate = GetResultString(nField);
124  wxDateTime date;
125  // First check for the 2-digit year format
126  if (date.ParseFormat(strDate, wxT("%m/%d/%y %H:%M:%S")))
127  {
128  return date;
129  }
130  else if (date.ParseDateTime(strDate) )
131  {
132  return date;
133  }
134  else if (date.ParseDate(strDate))
135  {
136  return date;
137  }
138  else
139  {
140  return wxInvalidDateTime;
141  }
142 }
143 
144 double wxSqliteResultSet::GetResultDouble(int nField)
145 {
146  double dblValue = -1;
147  if (m_pSqliteStatement == NULL)
149  dblValue = sqlite3_column_double(m_pSqliteStatement, nField-1);
150 
151  return dblValue;
152 }
153 
154 void* wxSqliteResultSet::GetResultBlob(int nField, wxMemoryBuffer& Buffer)
155 {
156  int nLength = 0;
157  if (m_pSqliteStatement == NULL)
159  nLength = sqlite3_column_bytes(m_pSqliteStatement, nField-1);
160  if (nLength < 1)
161  {
162  wxMemoryBuffer tempBuffer(0);
163  tempBuffer.SetDataLen(0);
164  tempBuffer.SetBufSize(0);
165  Buffer = tempBuffer;
166 
167  return NULL;
168  }
169 
170  const void* pBlob = sqlite3_column_blob(m_pSqliteStatement, nField-1);
171 
172  wxMemoryBuffer tempBuffer(nLength);
173  void* pBuffer = tempBuffer.GetWriteBuf(nLength);
174  memcpy(pBuffer, pBlob, nLength);
175  tempBuffer.UngetWriteBuf(nLength);
176  tempBuffer.SetDataLen(nLength);
177  tempBuffer.SetBufSize(nLength);
178 
179  Buffer = tempBuffer;
180 
181  return Buffer.GetData();
182 }
183 
184 bool wxSqliteResultSet::IsFieldNull(int nField)
185 {
186  if (m_pSqliteStatement == NULL)
188  return (NULL == sqlite3_column_text(m_pSqliteStatement, nField-1));
189 }
190 
191 int wxSqliteResultSet::LookupField(const wxString& strField)
192 {
193  StringToIntMap::iterator SearchIterator = m_FieldLookupMap.find(strField);
194  if (SearchIterator == m_FieldLookupMap.end())
195  {
196  wxString msg(_("Field '") + strField + _("' not found in the resultset"));
197 #if wxUSE_DATABASE_EXCEPTIONS
198  wxDatabaseException error(wxDATABASE_FIELD_NOT_IN_RESULTSET, msg);
199  throw error;
200 #else
201  wxLogError(msg);
202 #endif
203  //return -1;
204  }
205  else
206  {
207  return ((*SearchIterator).second + 1); // Add +1 to make the result set 1-based rather than 0-based
208  }
209 }
210 
212 {
214  LogMetaDataForCleanup(pMetaData);
215  return pMetaData;
216 }
217 
218 #endif//wxUSE_DATABASE_SQLITE
virtual bool Next()
Move to the next record in the result set.
wxSqlitePreparedStatement * m_pStatement
virtual bool IsFieldNull(int nField)
Check if a field in the current result set record is NULL.
virtual void Close()
Close the result set (call wxDatabase::CloseResultSet() instead on the result set)
virtual wxDateTime GetResultDate(int nField)
Retrieve a wxDateTime from the result set by the 1-based field index.
StringToIntMap m_FieldLookupMap
virtual long GetResultLong(int nField)
Retrieve a long from the result set by the 1-based field index.
void LogMetaDataForCleanup(wxResultSetMetaData *pMetaData)
Add meta data object pointer to the list for "garbage collection".
Definition: resultset.h:70
virtual wxResultSetMetaData * GetMetaData()
Retrieve the MetaData associated with this result set.
#define wxDATABASE_FIELD_NOT_IN_RESULTSET
Definition: errorcodes.h:12
virtual double GetResultDouble(int nField)
Retrieve a double from the result set by the 1-based field index.
virtual int GetResultInt(int nField)
Retrieve an integer from the result set by the 1-based field index.
virtual void * GetResultBlob(int nField, wxMemoryBuffer &Buffer)
Retrieve a BLOB from the result set by the 1-based field index.
sqlite3_stmt * m_pSqliteStatement
void CloseMetaData()
Close all meta data objects that have been generated but not yet closed.
Definition: resultset.cpp:96
void SetErrorMessage(const wxString &strErrorMessage)
virtual void Close()
Close the result set (call wxDatabase::ClosePreparedStatement() instead on the statement)
virtual wxString GetResultString(int nField)
Retrieve a wxString from the result set by the 1-based field index.
virtual bool GetResultBool(int nField)
Retrieve a boolean from the result set by the 1-based field index.
void SetErrorCode(int nErrorCode)
static int TranslateErrorCode(int nCode)
virtual wxString ConvertFromUnicodeStream(const char *inputBuffer)
virtual ~wxSqliteResultSet()
virtual int LookupField(const wxString &strField)