Version: 1.0.0
postgresql_resultset.cpp
Go to the documentation of this file.
1 #include "wx/database/wxprec.h"
2 
3 #if wxUSE_DATABASE_POSTGRESQL
4 
7 {
8  m_pInterface = pInterface;
9  m_pResult = NULL;
10  m_FieldLookupMap.clear();
11  m_nCurrentRow = -1;
12  m_nTotalRows = 0;
13  m_bBinaryResults = false;
14 }
15 
18 {
19  m_pInterface = pInterface;
20  m_pResult = pResult;
21  m_nCurrentRow = -1;
22  m_nTotalRows = m_pInterface->GetPQntuples()(m_pResult);
23  m_bBinaryResults = m_pInterface->GetPQbinaryTuples()(m_pResult);
24 
25  int nFields = m_pInterface->GetPQnfields()(m_pResult);
26  for (int i=0; i<nFields; i++)
27  {
28  wxString strField = ConvertFromUnicodeStream(m_pInterface->GetPQfname()(pResult, i));
29  strField.MakeUpper();
30  m_FieldLookupMap[strField] = i;
31  }
32 }
33 
35 {
36  Close();
37 }
38 
40 {
41  if (m_nTotalRows < 1)
42  return false;
43 
44  m_nCurrentRow++;
45 
46  return (m_nCurrentRow < m_nTotalRows);
47 }
48 
50 {
51  CloseMetaData();
52 
53  if (m_pResult != NULL)
54  {
56  m_pResult = NULL;
57  }
58  m_FieldLookupMap.clear();
59 }
60 
61 
62 // get field
64 {
65  // Don't use nField-1 here since GetResultLong will take care of that
66  return GetResultLong(nField);
67 }
68 
69 wxString wxPostgresResultSet::GetResultString(int nField)
70 {
71  wxString strValue = wxT("");
72  if (m_bBinaryResults)
73  {
74  wxLogError(_("Not implemented\n"));
75  }
76  else
77  {
78  if (nField != -1)
79  {
80  if (m_pInterface->GetPQgetisnull()(m_pResult, m_nCurrentRow, nField-1) != 1)
81  {
83  }
84  }
85  }
86 
87  return strValue;
88 }
89 
91 {
92  long nValue = 0;
93  if (m_bBinaryResults)
94  {
95  wxLogError(_("Not implemented\n"));
96  }
97  else
98  {
99  if (nField != -1)
100  {
101  if (m_pInterface->GetPQgetisnull()(m_pResult, m_nCurrentRow, nField-1) != 1)
102  {
103  wxString strValue = ConvertFromUnicodeStream(m_pInterface->GetPQgetvalue()(m_pResult, m_nCurrentRow, nField-1));
104  strValue.ToLong(&nValue);
105  }
106  }
107  }
108 
109  return nValue;
110 }
111 
112 bool wxPostgresResultSet::GetResultBool(int nField)
113 {
114  bool bValue = false;
115  if (m_bBinaryResults)
116  {
117  wxLogError(_("Not implemented\n"));
118  }
119  else
120  {
121  if (nField != -1)
122  {
123  if (m_pInterface->GetPQgetisnull()(m_pResult, m_nCurrentRow, nField-1) != 1)
124  {
125  wxString strValue = ConvertFromUnicodeStream(m_pInterface->GetPQgetvalue()(m_pResult, m_nCurrentRow, nField-1));
126  bValue = (strValue != _("0"));
127  }
128  }
129  }
130 
131  return bValue;
132 }
133 
134 wxDateTime wxPostgresResultSet::GetResultDate(int nField)
135 {
136  wxDateTime dateValue = wxInvalidDateTime;
137  // TIMESTAMP results should be the same in binary or text results
138  if (m_bBinaryResults)
139  {
140  if (m_pInterface->GetPQgetisnull()(m_pResult, m_nCurrentRow, nField-1) != 1)
141  {
142  wxString strDateValue = ConvertFromUnicodeStream(m_pInterface->GetPQgetvalue()(m_pResult, m_nCurrentRow, nField-1));
143  if (! dateValue.ParseDateTime(strDateValue))
144  {
145  if (dateValue.ParseDate(strDateValue) )
146  {
147  dateValue.SetHour(0);
148  dateValue.SetMinute(0);
149  dateValue.SetSecond(0);
150  dateValue.SetMillisecond(0);
151  }
152  else
153  {
154  dateValue = wxInvalidDateTime;
155  }
156  }
157  }
158  }
159  else
160  {
161  if (m_pInterface->GetPQgetisnull()(m_pResult, m_nCurrentRow, nField-1) != 1)
162  {
163  wxString strDateValue = ConvertFromUnicodeStream(m_pInterface->GetPQgetvalue()(m_pResult, m_nCurrentRow, nField-1));
164  if (! dateValue.ParseDateTime(strDateValue))
165  {
166  if (dateValue.ParseDate(strDateValue))
167  {
168  dateValue.SetHour(0);
169  dateValue.SetMinute(0);
170  dateValue.SetSecond(0);
171  dateValue.SetMillisecond(0);
172  }
173  else
174  {
175  dateValue = wxInvalidDateTime;
176  }
177  }
178  }
179  }
180 
181  return dateValue;
182 }
183 
184 void* wxPostgresResultSet::GetResultBlob(int nField, wxMemoryBuffer& Buffer)
185 {
186  //int nLength = m_pInterface->GetPQgetlength()(m_pResult, m_nCurrentRow, nIndex);
187  unsigned char* pBlob = (unsigned char*)m_pInterface->GetPQgetvalue()(m_pResult, m_nCurrentRow, nField-1);
188  size_t nUnescapedLength = 0;
189  unsigned char* pUnescapedBlob = m_pInterface->GetPQunescapeBytea()(pBlob, &nUnescapedLength);
190 
191  wxMemoryBuffer tempBuffer(nUnescapedLength);
192  void* pUnescapedBuffer = tempBuffer.GetWriteBuf(nUnescapedLength);
193  memcpy(pUnescapedBuffer, pUnescapedBlob, nUnescapedLength);
194  m_pInterface->GetPQfreemem()(pUnescapedBlob);
195  tempBuffer.UngetWriteBuf(nUnescapedLength);
196 
197  tempBuffer.SetBufSize(nUnescapedLength);
198  tempBuffer.SetDataLen(nUnescapedLength);
199  Buffer = tempBuffer;
200 
201  Buffer.UngetWriteBuf(nUnescapedLength);
202 
203  if (nUnescapedLength < 1)
204  return NULL;
205 
206  return Buffer.GetData();
207 }
208 
209 double wxPostgresResultSet::GetResultDouble(int nField)
210 {
211  double dblValue = 0;
212  if (m_bBinaryResults)
213  {
214  wxLogError(_("Not implemented\n"));
215  }
216  else
217  {
218  if (m_pInterface->GetPQgetisnull()(m_pResult, m_nCurrentRow, nField-1) != 1)
219  {
220  wxString strValue = ConvertFromUnicodeStream(m_pInterface->GetPQgetvalue()(m_pResult, m_nCurrentRow, nField-1));
221  strValue.ToDouble(&dblValue);
222  }
223  }
224 
225  return dblValue;
226 }
227 
228 bool wxPostgresResultSet::IsFieldNull(int nField)
229 {
230  return (m_pInterface->GetPQgetisnull()(m_pResult, m_nCurrentRow, nField-1) == 1);
231 }
232 
233 int wxPostgresResultSet::LookupField(const wxString& strField)
234 {
235  wxString strLocalCopy(strField);
236  strLocalCopy.MakeUpper();
237  StringToIntMap::iterator SearchIterator = m_FieldLookupMap.find(strLocalCopy);
238  if (SearchIterator == m_FieldLookupMap.end())
239  {
240  wxString msg(_("Field '") + strField + _("' not found in the resultset"));
241 #if wxUSE_DATABASE_EXCEPTIONS
242  wxDatabaseException error(wxDATABASE_FIELD_NOT_IN_RESULTSET, msg);
243  throw error;
244 #else
245  wxLogError(msg);
246 #endif
247  return -1;
248  }
249  else
250  {
251  return ((*SearchIterator).second+1); // Add +1 to make the result set 1-based rather than 0-based
252  }
253 }
254 
256 {
258  LogMetaDataForCleanup(pMetaData);
259  return pMetaData;
260 }
261 
262 #endif//wxUSE_DATABASE_POSTGRESQL
virtual bool GetResultBool(int nField)
Retrieve a boolean from the result set by the 1-based field index.
virtual long GetResultLong(int nField)
Retrieve a long from the result set by the 1-based field index.
PQunescapeByteaType GetPQunescapeBytea()
void LogMetaDataForCleanup(wxResultSetMetaData *pMetaData)
Add meta data object pointer to the list for "garbage collection".
Definition: resultset.h:70
virtual int LookupField(const wxString &strField)
wxDynamicPostgresInterface * m_pInterface
#define wxDATABASE_FIELD_NOT_IN_RESULTSET
Definition: errorcodes.h:12
virtual wxString GetResultString(int nField)
Retrieve a wxString from the result set by the 1-based field index.
PQbinaryTuplesType GetPQbinaryTuples()
virtual wxResultSetMetaData * GetMetaData()
Retrieve the MetaData associated with this result set.
void CloseMetaData()
Close all meta data objects that have been generated but not yet closed.
Definition: resultset.cpp:96
wxPostgresResultSet(wxDynamicPostgresInterface *pInterface)
virtual wxDateTime GetResultDate(int nField)
Retrieve a wxDateTime from the result set by the 1-based field index.
virtual ~wxPostgresResultSet()
virtual void * GetResultBlob(int nField, wxMemoryBuffer &Buffer)
Retrieve a BLOB from the result set by the 1-based field index.
virtual void Close()
Close the result set (call wxDatabase::CloseResultSet() instead on the result set)
virtual bool Next()
Move to the next record in the result set.
virtual int GetResultInt(int nField)
Retrieve an integer from the result set by the 1-based field index.
StringToIntMap m_FieldLookupMap
virtual wxString ConvertFromUnicodeStream(const char *inputBuffer)
virtual bool IsFieldNull(int nField)
Check if a field in the current result set record is NULL.
virtual double GetResultDouble(int nField)
Retrieve a double from the result set by the 1-based field index.