Version: 1.0.0
database.cpp
Go to the documentation of this file.
1 #include "wx/database/wxprec.h"
2 
3 // ctor()
6  , m_typeName(wxEmptyString)
7  , m_isViaODBC(false)
8  , m_libraryPath(wxEmptyString)
9 {
10 }
11 
12 // dtor()
14 {
17 }
18 
19 int wxDatabase::RunQuery(const wxString& strQuery)
20 {
21  return RunQuery(strQuery, true);
22 }
23 
25 {
26  // Iterate through all of the result sets and close them all
27  wxDatabaseResultSetHashSet::iterator start = m_ResultSets.begin();
28  wxDatabaseResultSetHashSet::iterator stop = m_ResultSets.end();
29  while (start != stop)
30  {
31  wxLogDebug(_("ResultSet NOT closed and cleaned up by the wxDatabase dtor: Cleaning!"));
32  delete (*start);
33  start++;
34  }
35  m_ResultSets.clear();
36 }
37 
39 {
40  // Iterate through all of the statements and close them all
41  wxDatabaseStatementHashSet::iterator start = m_Statements.begin();
42  wxDatabaseStatementHashSet::iterator stop = m_Statements.end();
43  while (start != stop)
44  {
45  wxLogDebug(_("wxPreparedStatement NOT closed and cleaned up by the wxDatabase dtor"));
46  delete (*start);
47  start++;
48  }
49  m_Statements.clear();
50 }
51 
53 {
54  if (pResultSet != NULL)
55  {
56  // Check if we have this result set in our list
57  if (m_ResultSets.find(pResultSet) != m_ResultSets.end())
58  {
59  // Remove the result set pointer from the list and delete the pointer
60  delete pResultSet;
61  m_ResultSets.erase(pResultSet);
62  return true;
63  }
64 
65  // If not then iterate through all of the statements and see
66  // if any of them have the result set in their lists
67  wxDatabaseStatementHashSet::iterator it;
68  for( it = m_Statements.begin(); it != m_Statements.end(); ++it )
69  {
70  // If the statement knows about the result set then it will close the
71  // result set and return true, otherwise it will return false
72  wxPreparedStatement* pStatement = *it;
73  if (pStatement != NULL)
74  {
75  if (pStatement->CloseResultSet(pResultSet))
76  {
77  return true;
78  }
79  }
80  }
81 
82  // If we don't know about the result set and the statements don't
83  // know about it, the just delete it
84  delete pResultSet;
85  return true;
86  }
87  else
88  {
89  // Return false on NULL pointer
90  return false;
91  }
92 
93 }
94 
96 {
97  if (pStatement != NULL)
98  {
99  // See if we know about this pointer, if so then remove it from the list
100  if (m_Statements.find(pStatement) != m_Statements.end())
101  {
102  // Remove the statement pointer from the list and delete the pointer
103  delete pStatement;
104  m_Statements.erase(pStatement);
105  return true;
106  }
107 
108  // Otherwise just delete it
109  delete pStatement;
110  return true;
111  }
112  else
113  {
114  // Return false on NULL pointer
115  return false;
116  }
117 }
118 
119 
120 int wxDatabase::GetSingleResultInt(const wxString& strSQL, const wxString& strField, bool bRequireUniqueResult /*= true*/)
121 {
122  wxVariant variant(strField);
123  return GetSingleResultInt(strSQL, &variant, bRequireUniqueResult);
124 }
125 
126 int wxDatabase::GetSingleResultInt(const wxString& strSQL, int nField, bool bRequireUniqueResult /*= true*/)
127 {
128  wxVariant variant((long)nField);
129  return GetSingleResultInt(strSQL, &variant, bRequireUniqueResult);
130 }
131 
132 int wxDatabase::GetSingleResultInt(const wxString& strSQL, const wxVariant* field, bool bRequireUniqueResult /*= true*/)
133 {
134  bool valueRetrievedFlag = false;
135  int value = -1;
136 
137  wxDatabaseResultSet* pResult = NULL;
138 #if wxUSE_DATABASE_EXCEPTIONS
139  try
140  {
141 #endif
142  pResult = ExecuteQuery(strSQL);
143 
144  while (pResult->Next())
145  {
146  if (valueRetrievedFlag)
147  {
148  // Close the result set, reset the value and throw an exception
149  CloseResultSet(pResult);
150  pResult = NULL;
151  value = -1;
153  SetErrorMessage(wxT("A non-unique result was returned."));
155  return value;
156  }
157  else
158  {
159  if (field->IsType(_("string")))
160  value = pResult->GetResultInt(field->GetString());
161  else
162  value = pResult->GetResultInt(field->GetLong());
163  valueRetrievedFlag = true;
164 
165  // If the user isn't concerned about returning a unique result,
166  // then just exit after the first record is found
167  if (!bRequireUniqueResult)
168  break;
169  }
170  }
171 #if wxUSE_DATABASE_EXCEPTIONS
172  }
173  catch (wxDatabaseException& e)
174  {
175  if (pResult != NULL)
176  {
177  CloseResultSet(pResult);
178  pResult = NULL;
179  }
180 
181  throw e;
182  }
183 #endif
184 
185  if (pResult != NULL)
186  {
187  CloseResultSet(pResult);
188  pResult = NULL;
189  }
190 
191  // Make sure that a value was retrieved from the database
192  if (!valueRetrievedFlag)
193  {
194  value = -1;
196  SetErrorMessage(wxT("No result was returned."));
198  return value;
199  }
200 
201  return value;
202 }
203 
204 wxString wxDatabase::GetSingleResultString(const wxString& strSQL, int nField, bool bRequireUniqueResult /*= true*/)
205 {
206  wxVariant variant((long)nField);
207  return GetSingleResultString(strSQL, &variant, bRequireUniqueResult);
208 }
209 
210 wxString wxDatabase::GetSingleResultString(const wxString& strSQL, const wxString& strField, bool bRequireUniqueResult /*= true*/)
211 {
212  wxVariant variant(strField);
213  return GetSingleResultString(strSQL, &variant, bRequireUniqueResult);
214 }
215 
216 wxString wxDatabase::GetSingleResultString(const wxString& strSQL, const wxVariant* field, bool bRequireUniqueResult /*= true*/)
217 {
218  bool valueRetrievedFlag = false;
219  wxString value = wxEmptyString;
220 
221  wxDatabaseResultSet* pResult = NULL;
222 #if wxUSE_DATABASE_EXCEPTIONS
223  try
224  {
225 #endif
226  pResult = ExecuteQuery(strSQL);
227 
228  while (pResult->Next())
229  {
230  if (valueRetrievedFlag)
231  {
232  // Close the result set, reset the value and throw an exception
233  CloseResultSet(pResult);
234  pResult = NULL;
235  value = wxEmptyString;
237  SetErrorMessage(wxT("A non-unique result was returned."));
239  return value;
240  }
241  else
242  {
243  if (field->IsType(_("string")))
244  value = pResult->GetResultString(field->GetString());
245  else
246  value = pResult->GetResultString(field->GetLong());
247  valueRetrievedFlag = true;
248 
249  // If the user isn't concerned about returning a unique result,
250  // then just exit after the first record is found
251  if (!bRequireUniqueResult)
252  break;
253  }
254  }
255 #if wxUSE_DATABASE_EXCEPTIONS
256  }
257  catch (wxDatabaseException& e)
258  {
259  if (pResult != NULL)
260  {
261  CloseResultSet(pResult);
262  pResult = NULL;
263  }
264 
265  throw e;
266  }
267 #endif
268 
269  if (pResult != NULL)
270  {
271  CloseResultSet(pResult);
272  pResult = NULL;
273  }
274 
275  // Make sure that a value was retrieved from the database
276  if (!valueRetrievedFlag)
277  {
278  value = wxEmptyString;
280  SetErrorMessage(wxT("No result was returned."));
282  return value;
283  }
284 
285  return value;
286 }
287 
288 long wxDatabase::GetSingleResultLong(const wxString& strSQL, int nField, bool bRequireUniqueResult /*= true*/)
289 {
290  wxVariant variant((long)nField);
291  return GetSingleResultLong(strSQL, &variant, bRequireUniqueResult);
292 }
293 
294 long wxDatabase::GetSingleResultLong(const wxString& strSQL, const wxString& strField, bool bRequireUniqueResult /*= true*/)
295 {
296  wxVariant variant(strField);
297  return GetSingleResultLong(strSQL, &variant, bRequireUniqueResult);
298 }
299 
300 long wxDatabase::GetSingleResultLong(const wxString& strSQL, const wxVariant* field, bool bRequireUniqueResult /*= true*/)
301 {
302  bool valueRetrievedFlag = false;
303  long value = -1;
304 
305  wxDatabaseResultSet* pResult = NULL;
306 #if wxUSE_DATABASE_EXCEPTIONS
307  try
308  {
309 #endif
310  pResult = ExecuteQuery(strSQL);
311 
312  while (pResult->Next())
313  {
314  if (valueRetrievedFlag)
315  {
316  // Close the result set, reset the value and throw an exception
317  CloseResultSet(pResult);
318  pResult = NULL;
319  value = -1;
321  SetErrorMessage(wxT("A non-unique result was returned."));
323  return value;
324  }
325  else
326  {
327  if (field->IsType(_("string")))
328  value = pResult->GetResultLong(field->GetString());
329  else
330  value = pResult->GetResultLong(field->GetLong());
331  valueRetrievedFlag = true;
332 
333  // If the user isn't concerned about returning a unique result,
334  // then just exit after the first record is found
335  if (!bRequireUniqueResult)
336  break;
337  }
338  }
339 #if wxUSE_DATABASE_EXCEPTIONS
340  }
341  catch (wxDatabaseException& e)
342  {
343  if (pResult != NULL)
344  {
345  CloseResultSet(pResult);
346  pResult = NULL;
347  }
348 
349  throw e;
350  }
351 #endif
352 
353  if (pResult != NULL)
354  {
355  CloseResultSet(pResult);
356  pResult = NULL;
357  }
358 
359  // Make sure that a value was retrieved from the database
360  if (!valueRetrievedFlag)
361  {
362  value = -1;
364  SetErrorMessage(wxT("No result was returned."));
366  return value;
367  }
368 
369  return value;
370 }
371 
372 bool wxDatabase::GetSingleResultBool(const wxString& strSQL, int nField, bool bRequireUniqueResult /*= true*/)
373 {
374  wxVariant variant((long)nField);
375  return GetSingleResultBool(strSQL, &variant, bRequireUniqueResult);
376 }
377 
378 bool wxDatabase::GetSingleResultBool(const wxString& strSQL, const wxString& strField, bool bRequireUniqueResult /*= true*/)
379 {
380  wxVariant variant(strField);
381  return GetSingleResultBool(strSQL, &variant, bRequireUniqueResult);
382 }
383 
384 bool wxDatabase::GetSingleResultBool(const wxString& strSQL, const wxVariant* field, bool bRequireUniqueResult /*= true*/)
385 {
386  bool valueRetrievedFlag = false;
387  bool value = false;
388 
389  wxDatabaseResultSet* pResult = NULL;
390 #if wxUSE_DATABASE_EXCEPTIONS
391  try
392  {
393 #endif
394  pResult = ExecuteQuery(strSQL);
395 
396  while (pResult->Next())
397  {
398  if (valueRetrievedFlag)
399  {
400  // Close the result set, reset the value and throw an exception
401  CloseResultSet(pResult);
402  pResult = NULL;
403  value = false;
405  SetErrorMessage(wxT("A non-unique result was returned."));
407  return value;
408  }
409  else
410  {
411  if (field->IsType(_("string")))
412  value = pResult->GetResultBool(field->GetString());
413  else
414  value = pResult->GetResultBool(field->GetLong());
415  valueRetrievedFlag = true;
416 
417  // If the user isn't concerned about returning a unique result,
418  // then just exit after the first record is found
419  if (!bRequireUniqueResult)
420  break;
421  }
422  }
423 #if wxUSE_DATABASE_EXCEPTIONS
424  }
425  catch (wxDatabaseException& e)
426  {
427  if (pResult != NULL)
428  {
429  CloseResultSet(pResult);
430  pResult = NULL;
431  }
432 
433  throw e;
434  }
435 #endif
436 
437  if (pResult != NULL)
438  {
439  CloseResultSet(pResult);
440  pResult = NULL;
441  }
442 
443  // Make sure that a value was retrieved from the database
444  if (!valueRetrievedFlag)
445  {
446  value = false;
448  SetErrorMessage(wxT("No result was returned."));
450  return value;
451  }
452 
453  return value;
454 }
455 
456 wxDateTime wxDatabase::GetSingleResultDate(const wxString& strSQL, int nField, bool bRequireUniqueResult /*= true*/)
457 {
458  wxVariant variant((long)nField);
459  return GetSingleResultDate(strSQL, &variant, bRequireUniqueResult);
460 }
461 
462 wxDateTime wxDatabase::GetSingleResultDate(const wxString& strSQL, const wxString& strField, bool bRequireUniqueResult /*= true*/)
463 {
464  wxVariant variant(strField);
465  return GetSingleResultDate(strSQL, &variant, bRequireUniqueResult);
466 }
467 
468 wxDateTime wxDatabase::GetSingleResultDate(const wxString& strSQL, const wxVariant* field, bool bRequireUniqueResult /*= true*/)
469 {
470  bool valueRetrievedFlag = false;
471  wxDateTime value = wxInvalidDateTime;
472 
473  wxDatabaseResultSet* pResult = NULL;
474 #if wxUSE_DATABASE_EXCEPTIONS
475  try
476  {
477 #endif
478  pResult = ExecuteQuery(strSQL);
479 
480  while (pResult->Next())
481  {
482  if (valueRetrievedFlag)
483  {
484  // Close the result set, reset the value and throw an exception
485  CloseResultSet(pResult);
486  pResult = NULL;
487  value = wxInvalidDateTime;
489  SetErrorMessage(wxT("A non-unique result was returned."));
491  return value;
492  }
493  else
494  {
495  if (field->IsType(_("string")))
496  value = pResult->GetResultDate(field->GetString());
497  else
498  value = pResult->GetResultDate(field->GetLong());
499  valueRetrievedFlag = true;
500 
501  // If the user isn't concerned about returning a unique result,
502  // then just exit after the first record is found
503  if (!bRequireUniqueResult)
504  break;
505  }
506  }
507 #if wxUSE_DATABASE_EXCEPTIONS
508  }
509  catch (wxDatabaseException& e)
510  {
511  if (pResult != NULL)
512  {
513  CloseResultSet(pResult);
514  pResult = NULL;
515  }
516 
517  throw e;
518  }
519 #endif
520 
521  if (pResult != NULL)
522  {
523  CloseResultSet(pResult);
524  pResult = NULL;
525  }
526 
527  // Make sure that a value was retrieved from the database
528  if (!valueRetrievedFlag)
529  {
530  value = wxInvalidDateTime;
532  SetErrorMessage(wxT("No result was returned."));
534  return value;
535  }
536 
537  return value;
538 }
539 
540 void* wxDatabase::GetSingleResultBlob(const wxString& strSQL, int nField, wxMemoryBuffer& Buffer, bool bRequireUniqueResult /*= true*/)
541 {
542  wxVariant variant((long)nField);
543  return GetSingleResultBlob(strSQL, &variant, Buffer, bRequireUniqueResult);
544 }
545 
546 void* wxDatabase::GetSingleResultBlob(const wxString& strSQL, const wxString& strField, wxMemoryBuffer& Buffer, bool bRequireUniqueResult /*= true*/)
547 {
548  wxVariant variant(strField);
549  return GetSingleResultBlob(strSQL, &variant, Buffer, bRequireUniqueResult);
550 }
551 
552 void* wxDatabase::GetSingleResultBlob(const wxString& strSQL, const wxVariant* field, wxMemoryBuffer& Buffer, bool bRequireUniqueResult /*= true*/)
553 {
554  bool valueRetrievedFlag = false;
555  void* value = NULL;
556 
557  wxDatabaseResultSet* pResult = NULL;
558 #if wxUSE_DATABASE_EXCEPTIONS
559  try
560  {
561 #endif
562  pResult = ExecuteQuery(strSQL);
563 
564  while (pResult->Next())
565  {
566  if (valueRetrievedFlag)
567  {
568  // Close the result set, reset the value and throw an exception
569  CloseResultSet(pResult);
570  pResult = NULL;
571  value = NULL;
573  SetErrorMessage(wxT("A non-unique result was returned."));
575  return value;
576  }
577  else
578  {
579  if (field->IsType(_("string")))
580  value = pResult->GetResultBlob(field->GetString(), Buffer);
581  else
582  value = pResult->GetResultBlob(field->GetLong(), Buffer);
583  valueRetrievedFlag = true;
584 
585  // If the user isn't concerned about returning a unique result,
586  // then just exit after the first record is found
587  if (!bRequireUniqueResult)
588  break;
589  }
590  }
591 #if wxUSE_DATABASE_EXCEPTIONS
592  }
593  catch (wxDatabaseException& e)
594  {
595  if (pResult != NULL)
596  {
597  CloseResultSet(pResult);
598  pResult = NULL;
599  }
600 
601  throw e;
602  }
603 #endif
604 
605  if (pResult != NULL)
606  {
607  CloseResultSet(pResult);
608  pResult = NULL;
609  }
610 
611  // Make sure that a value was retrieved from the database
612  if (!valueRetrievedFlag)
613  {
614  value = NULL;
616  SetErrorMessage(wxT("No result was returned."));
618  return value;
619  }
620 
621  return value;
622 }
623 
624 double wxDatabase::GetSingleResultDouble(const wxString& strSQL, int nField, bool bRequireUniqueResult /*= true*/)
625 {
626  wxVariant variant((long)nField);
627  return GetSingleResultDouble(strSQL, &variant, bRequireUniqueResult);
628 }
629 
630 double wxDatabase::GetSingleResultDouble(const wxString& strSQL, const wxString& strField, bool bRequireUniqueResult /*= true*/)
631 {
632  wxVariant variant(strField);
633  return GetSingleResultDouble(strSQL, &variant, bRequireUniqueResult);
634 }
635 
636 double wxDatabase::GetSingleResultDouble(const wxString& strSQL, const wxVariant* field, bool bRequireUniqueResult /*= true*/)
637 {
638  bool valueRetrievedFlag = false;
639  double value = -1;
640 
641  wxDatabaseResultSet* pResult = NULL;
642 #if wxUSE_DATABASE_EXCEPTIONS
643  try
644  {
645 #endif
646  pResult = ExecuteQuery(strSQL);
647 
648  while (pResult->Next())
649  {
650  if (valueRetrievedFlag)
651  {
652  // Close the result set, reset the value and throw an exception
653  CloseResultSet(pResult);
654  pResult = NULL;
655  value = -1;
657  SetErrorMessage(wxT("A non-unique result was returned."));
659  return value;
660  }
661  else
662  {
663  if (field->IsType(_("string")))
664  value = pResult->GetResultDouble(field->GetString());
665  else
666  value = pResult->GetResultDouble(field->GetLong());
667  valueRetrievedFlag = true;
668 
669  // If the user isn't concerned about returning a unique result,
670  // then just exit after the first record is found
671  if (!bRequireUniqueResult)
672  break;
673  }
674  }
675 #if wxUSE_DATABASE_EXCEPTIONS
676  }
677  catch (wxDatabaseException& e)
678  {
679  if (pResult != NULL)
680  {
681  CloseResultSet(pResult);
682  pResult = NULL;
683  }
684 
685  throw e;
686  }
687 #endif
688 
689  if (pResult != NULL)
690  {
691  CloseResultSet(pResult);
692  pResult = NULL;
693  }
694 
695  // Make sure that a value was retrieved from the database
696  if (!valueRetrievedFlag)
697  {
698  value = -1;
700  SetErrorMessage(wxT("No result was returned."));
702  return value;
703  }
704 
705  return value;
706 }
707 
708 wxArrayInt wxDatabase::GetResultsArrayInt(const wxString& strSQL, int nField)
709 {
710  wxVariant variant((long)nField);
711  return GetResultsArrayInt(strSQL, &variant);
712 }
713 
714 wxArrayInt wxDatabase::GetResultsArrayInt(const wxString& strSQL, const wxString& strField)
715 {
716  wxVariant variant(strField);
717  return GetResultsArrayInt(strSQL, &variant);
718 }
719 
720 wxArrayInt wxDatabase::GetResultsArrayInt(const wxString& strSQL, const wxVariant* field)
721 {
722  wxArrayInt returnArray;
723 
724  wxDatabaseResultSet* pResult = NULL;
725 #if wxUSE_DATABASE_EXCEPTIONS
726  try
727  {
728 #endif
729  pResult = ExecuteQuery(strSQL);
730 
731  while (pResult->Next())
732  {
733  if (field->IsType(_("string")))
734  returnArray.Add(pResult->GetResultInt(field->GetString()));
735  else
736  returnArray.Add(pResult->GetResultInt(field->GetLong()));
737  }
738 #if wxUSE_DATABASE_EXCEPTIONS
739  }
740  catch (wxDatabaseException& e)
741  {
742  if (pResult != NULL)
743  {
744  CloseResultSet(pResult);
745  pResult = NULL;
746  }
747 
748  throw e;
749  }
750 #endif
751 
752  if (pResult != NULL)
753  {
754  CloseResultSet(pResult);
755  pResult = NULL;
756  }
757 
758  return returnArray;
759 }
760 
761 wxArrayString wxDatabase::GetResultsArrayString(const wxString& strSQL, int nField)
762 {
763  wxVariant variant((long)nField);
764  return GetResultsArrayString(strSQL, &variant);
765 }
766 
767 wxArrayString wxDatabase::GetResultsArrayString(const wxString& strSQL, const wxString& strField)
768 {
769  wxVariant variant(strField);
770  return GetResultsArrayString(strSQL, &variant);
771 }
772 
773 wxArrayString wxDatabase::GetResultsArrayString(const wxString& strSQL, const wxVariant* field)
774 {
775  wxArrayString returnArray;
776 
777  wxDatabaseResultSet* pResult = NULL;
778 #if wxUSE_DATABASE_EXCEPTIONS
779  try
780  {
781 #endif
782  pResult = ExecuteQuery(strSQL);
783 
784  while (pResult->Next())
785  {
786  if (field->IsType(_("string")))
787  returnArray.Add(pResult->GetResultString(field->GetString()));
788  else
789  returnArray.Add(pResult->GetResultString(field->GetLong()));
790  }
791 #if wxUSE_DATABASE_EXCEPTIONS
792  }
793  catch (wxDatabaseException& e)
794  {
795  if (pResult != NULL)
796  {
797  CloseResultSet(pResult);
798  pResult = NULL;
799  }
800 
801  throw e;
802  }
803 #endif
804 
805  if (pResult != NULL)
806  {
807  CloseResultSet(pResult);
808  pResult = NULL;
809  }
810 
811  return returnArray;
812 }
813 
814 wxArrayLong wxDatabase::GetResultsArrayLong(const wxString& strSQL, int nField)
815 {
816  wxVariant variant((long)nField);
817  return GetResultsArrayLong(strSQL, &variant);
818 }
819 
820 wxArrayLong wxDatabase::GetResultsArrayLong(const wxString& strSQL, const wxString& strField)
821 {
822  wxVariant variant(strField);
823  return GetResultsArrayLong(strSQL, &variant);
824 }
825 
826 wxArrayLong wxDatabase::GetResultsArrayLong(const wxString& strSQL, const wxVariant* field)
827 {
828  wxArrayLong returnArray;
829 
830  wxDatabaseResultSet* pResult = NULL;
831 #if wxUSE_DATABASE_EXCEPTIONS
832  try
833  {
834 #endif
835  pResult = ExecuteQuery(strSQL);
836 
837  while (pResult->Next())
838  {
839  if (field->IsType(_("string")))
840  returnArray.Add(pResult->GetResultLong(field->GetString()));
841  else
842  returnArray.Add(pResult->GetResultLong(field->GetLong()));
843  }
844 #if wxUSE_DATABASE_EXCEPTIONS
845  }
846  catch (wxDatabaseException& e)
847  {
848  if (pResult != NULL)
849  {
850  CloseResultSet(pResult);
851  pResult = NULL;
852  }
853 
854  throw e;
855  }
856 #endif
857 
858  if (pResult != NULL)
859  {
860  CloseResultSet(pResult);
861  pResult = NULL;
862  }
863 
864  return returnArray;
865 }
866 
867 wxArrayDouble wxDatabase::GetResultsArrayDouble(const wxString& strSQL, int nField)
868 {
869  wxVariant variant((long)nField);
870  return GetResultsArrayDouble(strSQL, &variant);
871 }
872 
873 wxArrayDouble wxDatabase::GetResultsArrayDouble(const wxString& strSQL, const wxString& strField)
874 {
875  wxVariant variant(strField);
876  return GetResultsArrayDouble(strSQL, &variant);
877 }
878 
879 wxArrayDouble wxDatabase::GetResultsArrayDouble(const wxString& strSQL, const wxVariant* field)
880 {
881  wxArrayDouble returnArray;
882 
883  wxDatabaseResultSet* pResult = NULL;
884 #if wxUSE_DATABASE_EXCEPTIONS
885  try
886  {
887 #endif
888  pResult = ExecuteQuery(strSQL);
889 
890  while (pResult->Next())
891  {
892  if (field->IsType(_("string")))
893  returnArray.Add(pResult->GetResultDouble(field->GetString()));
894  else
895  returnArray.Add(pResult->GetResultDouble(field->GetLong()));
896  }
897 #if wxUSE_DATABASE_EXCEPTIONS
898  }
899  catch (wxDatabaseException& e)
900  {
901  if (pResult != NULL)
902  {
903  CloseResultSet(pResult);
904  pResult = NULL;
905  }
906 
907  throw e;
908  }
909 #endif
910 
911  if (pResult != NULL)
912  {
913  CloseResultSet(pResult);
914  pResult = NULL;
915  }
916 
917  return returnArray;
918 }
919 
920 //AML start
921 
922 wxDatabase* wxDatabase::GetDatabase(wxConfigBase& config, wxString* err, const wxString& path)
923 {
924  long index;
925  wxString name;
926  wxDatabase* database = NULL;
927  config.SetPath(path);
928  for (bool continue_ = config.GetFirstGroup(name, index); continue_; continue_ = config.GetNextGroup(name, index))
929  {
930  database = GetDatabase(config, err, name); // can we recurse into this group?
931  if (database) break;
932 
933  config.SetPath("..");
934 
935  name.UpperCase();
936  if (false);
937 #if wxUSE_DATABASE_SQLITE
938  else
939  if (name.IsSameAs("SQLITE"))
940  {
941  database = GetSqliteDatabase(config, err);
942  }
943 #endif
944 #if wxUSE_DATABASE_POSTGRESQL
945  else
946  if (name.IsSameAs("POSTGRESQL"))
947  {
948  database = GetPostgresDatabase(config, err);
949  }
950 #endif
951 #if wxUSE_DATABASE_MYSQL
952  else
953  if (name.IsSameAs("MYSQL"))
954  {
955  database = GetMysqlDatabase(config, err);
956  }
957 #endif
958 #if wxUSE_DATABASE_ODBC
959  else
960  if (name.IsSameAs("ODBC"))
961  {
962  database = GetOdbcDatabase(config, err);
963  if (database) database->m_isViaODBC = true;
964  }
965 #endif
966 #if wxUSE_DATABASE_TDS
967  else
968  if (name.IsSameAs("TDS"))
969  {
970  database = GetTdsDatabase(config, err);
971  }
972 #endif
973  if (!database) continue;
974 
975  if (database->m_typeName.IsEmpty())
976  {
977  database->m_typeName = name;
978  }
979  }
980  return database;
981 }
982 
983 #if wxUSE_DATABASE_SQLITE
984 wxDatabase* wxDatabase::GetSqliteDatabase(wxConfigBase& config, wxString* err)
985 {
986  if (!config.HasGroup("SQLite"))
987  {
988  if (err) err->Append("/SQLite not defined");
989  return NULL;
990  }
991  config.SetPath("SQLite");
992 
993  wxString database;
994  if (!config.Read("database", &database))
995  {
996  if (err) err->Append("/SQLite/database not defined");
997  return NULL;
998  }
999 
1000  wxSqliteDatabase* pDatabase = new wxSqliteDatabase(database, true);
1001 
1002  wxString libraryPath;
1003  if (config.Read("library_path", &libraryPath))
1004  {
1005  pDatabase->m_libraryPath = libraryPath;
1006  }
1007 
1008  return pDatabase;
1009 }
1010 #endif
1011 
1012 #if wxUSE_DATABASE_POSTGRESQL
1013 wxDatabase* wxDatabase::GetPostgresDatabase(wxConfigBase& config, wxString* err)
1014 {
1015  if (!config.HasGroup("PostgreSQL"))
1016  {
1017  if (err) err->Append("/PostgreSQL not defined");
1018  return NULL;
1019  }
1020  config.SetPath("PostgreSQL");
1021 
1023  {
1024  if (err) err->Append("PostgreSQL database backend is not available");
1025  return NULL;
1026  }
1027 
1028  wxString database;
1029  if (!config.Read("database", &database))
1030  {
1031  if (err) err->Append("/PostgreSQL/database not defined");
1032  return NULL;
1033  }
1034 
1035  wxString server;
1036  wxString user;
1037  wxString password;
1038  int port;
1039 
1040  bool haveServerInfo = config.Read("server", &server, wxEmptyString);
1041  bool haveUserInfo = config.Read("user", &user, wxEmptyString);
1042  config.Read("password", &password, wxEmptyString);
1043  config.Read("port", &port, 5432);
1044 
1045  wxPostgresDatabase* pDatabase;
1046 
1047  if (haveServerInfo && haveUserInfo)
1048  pDatabase = new wxPostgresDatabase(server, port, database, user, password);
1049  else if (haveServerInfo && !haveUserInfo)
1050  pDatabase = new wxPostgresDatabase(server, database);
1051  else if (haveUserInfo && !haveServerInfo)
1052  pDatabase = new wxPostgresDatabase(database, user, password);
1053  else // We just have the database name
1054  pDatabase = new wxPostgresDatabase(database);
1055 
1056  wxString libraryPath;
1057  if (config.Read("library_path", &libraryPath))
1058  {
1059  pDatabase->m_libraryPath = libraryPath;
1060  }
1061 
1062  return pDatabase;
1063 }
1064 #endif
1065 
1066 #if wxUSE_DATABASE_MYSQL
1067 wxDatabase* wxDatabase::GetMysqlDatabase(wxConfigBase& config, wxString* err)
1068 {
1069  if (!config.HasGroup("MySQL"))
1070  {
1071  if (err) err->Append("/MySQL not defined");
1072  return NULL;
1073  }
1074  config.SetPath("MySQL");
1075 
1077  {
1078  if (err) err->Append("MySQL database backend is not available");
1079  return NULL;
1080  }
1081 
1082  wxString server;
1083  if (!config.Read("server", &server))
1084  {
1085  if (err) err->Append("/MySQL/server not defined");
1086  return NULL;
1087  }
1088  wxString database;
1089  if (!config.Read("database", &database))
1090  {
1091  if (err) err->Append("/MySQL/database not defined");
1092  return NULL;
1093  }
1094  wxString user;
1095  if (!config.Read("user", &user))
1096  {
1097  if (err) err->Append("/MySQL/user not defined");
1098  return NULL;
1099  }
1100  wxString password;
1101  if (!config.Read("password", &password))
1102  {
1103  if (err) err->Append("/MySQL/password not defined");
1104  return NULL;
1105  }
1106 
1107  wxMysqlDatabase* pDatabase = new wxMysqlDatabase(server, database, user, password);
1108 
1109  wxString libraryPath;
1110  if (config.Read("library_path", &libraryPath))
1111  {
1112  pDatabase->m_libraryPath = libraryPath;
1113  }
1114 
1115  return pDatabase;
1116 }
1117 #endif
1118 
1119 #if wxUSE_DATABASE_ODBC
1120 wxDatabase* wxDatabase::GetOdbcDatabase(wxConfigBase& config, wxString* err)
1121 {
1122  if (!config.HasGroup("ODBC"))
1123  {
1124  if (err) err->Append("/ODBC not defined");
1125  return NULL;
1126  }
1127  config.SetPath("ODBC");
1128 
1130  {
1131  if (err) err->Append("ODBC database backend is not available");
1132  return NULL;
1133  }
1134 
1135  wxString dbType;
1136  if (!config.Read("DbType", &dbType))
1137  {
1138  if (err) err->Append("/ODBC/DbType not defined");
1139  return NULL;
1140  }
1141 
1142  wxOdbcDatabase* pDatabase = new wxOdbcDatabase();
1143  pDatabase->m_typeName = dbType;
1144 
1145  wxString connection;
1146  wxString user;
1147  wxString password;
1148  if (config.Read("Connection", &connection))
1149  {
1150  pDatabase->Open(connection);
1151  }
1152  else
1153  {
1154  wxString DSN;
1155  if (!config.Read("DSN", &DSN, wxEmptyString))
1156  {
1157  if (err) err->Append("/ODBC/DSN not defined");
1158  return NULL;
1159  }
1160  if (config.Read("user", &user, wxEmptyString))
1161  {
1162  config.Read("password", &password, wxEmptyString);
1163  pDatabase->Open(DSN, user, password);
1164  }
1165  else
1166  {
1167  pDatabase->Open(DSN, wxEmptyString, wxEmptyString);
1168  }
1169  }
1170 
1171  wxString libraryPath;
1172  if (config.Read("library_path", &libraryPath))
1173  {
1174  pDatabase->m_libraryPath = libraryPath;
1175  }
1176 
1177  return pDatabase;
1178 }
1179 #endif
1180 
1181 #if wxUSE_DATABASE_TDS
1182 wxDatabase* wxDatabase::GetTdsDatabase(wxConfigBase& config, wxString* err)
1183 {
1184  if (!config.HasGroup("TDS"))
1185  {
1186  if (err) err->Append("/TDS not defined");
1187  return NULL;
1188  }
1189  config.SetPath("TDS");
1190 
1191  wxString freetds;
1192  if (!config.Read("freetds", &freetds))
1193  {
1194  if (err) err->Append("/TDS/freetds not defined. Defaulting to environment variable FREETDS");
1195  }
1196  wxString server;
1197  if (!config.Read("server", &server))
1198  {
1199  if (err) err->Append("/TDS/server not defined");
1200  return NULL;
1201  }
1202  wxString database;
1203  if (!config.Read("database", &database))
1204  {
1205  if (err) err->Append("/TDS/database not defined");
1206  return NULL;
1207  }
1208  wxString user;
1209  if (!config.Read("user", &user))
1210  {
1211  if (err) err->Append("/TDS/user not defined. Defaulting to n\"\"");
1212  }
1213  wxString password;
1214  if (!config.Read("password", &password))
1215  {
1216  if (err) err->Append("/TDS/password not defined. Defaulting to n\"\"");
1217  }
1218  wxString version;
1219  int tdsVersion = wxTdsDatabase::TDS_72;
1220  if (!config.Read("version", &version))
1221  {
1222  if (err) err->Append("/TDS/version not defined. Defaulting to 7.2");
1223  }
1224  else
1225  {
1226  if (version == "4.2")
1227  tdsVersion = wxTdsDatabase::TDS_42;
1228  else if (version == "4.6")
1229  tdsVersion = wxTdsDatabase::TDS_46;
1230  else if (version == "5.0")
1231  tdsVersion = wxTdsDatabase::TDS_50;
1232  else if (version == "7.0")
1233  tdsVersion = wxTdsDatabase::TDS_70;
1234  else if (version == "7.1")
1235  tdsVersion = wxTdsDatabase::TDS_71;
1236  else if (version == "7.2")
1237  tdsVersion = wxTdsDatabase::TDS_72;
1238  else if (version == "7.3")
1239  tdsVersion = wxTdsDatabase::TDS_73;
1240  else if (version == "7.4")
1241  tdsVersion = wxTdsDatabase::TDS_74;
1242  else if (version == "8.0")
1243  tdsVersion = wxTdsDatabase::TDS_80;
1244  }
1245 
1246  wxTdsDatabase* pDatabase = new wxTdsDatabase(freetds, server, database, user, password, tdsVersion);
1247 
1248  wxString libraryPath;
1249  if (config.Read("library_path", &libraryPath))
1250  {
1251  pDatabase->m_libraryPath = libraryPath;
1252  }
1253 
1254  return pDatabase;
1255 }
1256 #endif
1257 
1258 //AML end
static bool IsAvailable()
virtual wxDateTime GetSingleResultDate(const wxString &strSQL, int nField, bool bRequireUniqueResult=true)
Retrieve a single date/time value from a query If multiple records are returned from the query,...
Definition: database.cpp:456
virtual bool CloseResultSet(wxDatabaseResultSet *pResultSet)
Close a result set returned by the database or a prepared statement previously.
virtual double GetResultDouble(int nField)=0
Retrieve a double from the result set by the 1-based field index.
virtual void * GetSingleResultBlob(const wxString &strSQL, int nField, wxMemoryBuffer &Buffer, bool bRequireUniqueResult=true)
Retrieve a single Blob value from a query If multiple records are returned from the query,...
Definition: database.cpp:540
virtual wxArrayDouble GetResultsArrayDouble(const wxString &strSQL, int nField)
Definition: database.cpp:867
wxDatabaseResultSetHashSet m_ResultSets
Definition: database.h:200
#define wxDATABASE_NO_ROWS_FOUND
Definition: errorcodes.h:13
virtual wxString GetResultString(int nField)=0
Retrieve a wxString from the result set by the 1-based field index.
wxDatabaseResultSet * ExecuteQuery(const wxString &strQuery)
See RunQueryWithResults.
Definition: database.h:64
#define wxDATABASE_NON_UNIQUE_RESULTSET
Definition: errorcodes.h:14
virtual int GetSingleResultInt(const wxString &strSQL, int nField, bool bRequireUniqueResult=true)
With the GetSingleResultX API, two additional exception types are thrown: wxDATABASE_NO_ROWS_FOUND - ...
Definition: database.cpp:126
static wxDatabase * GetDatabase(wxConfigBase &config, wxString *err=NULL, const wxString &path="/")
Get an instance of the first valid database specified in config.
Definition: database.cpp:922
bool m_isViaODBC
Definition: database.h:204
virtual bool GetSingleResultBool(const wxString &strSQL, int nField, bool bRequireUniqueResult=true)
Retrieve a single bool value from a query If multiple records are returned from the query,...
Definition: database.cpp:372
virtual int RunQuery(const wxString &strQuery)
Run an insert, update, or delete query on the database.
Definition: database.cpp:19
virtual int GetResultInt(int nField)=0
Retrieve an integer from the result set by the 1-based field index.
virtual bool CloseStatement(wxPreparedStatement *pStatement)
Close a prepared statement previously prepared by the database.
Definition: database.cpp:95
wxDatabase()
Constructor.
Definition: database.cpp:4
virtual ~wxDatabase()
Destructor.
Definition: database.cpp:13
virtual long GetResultLong(int nField)=0
Retrieve a long from the result set by the 1-based field index.
virtual wxArrayString GetResultsArrayString(const wxString &strSQL, int nField)
Definition: database.cpp:761
void SetErrorMessage(const wxString &strErrorMessage)
virtual bool Next()=0
Move to the next record in the result set.
virtual wxDateTime GetResultDate(int nField)=0
Retrieve a wxDateTime from the result set by the 1-based field index.
virtual void * GetResultBlob(int nField, wxMemoryBuffer &Buffer)=0
Retrieve a BLOB from the result set by the 1-based field index.
wxString m_typeName
Definition: database.h:203
static bool IsAvailable()
virtual wxArrayInt GetResultsArrayInt(const wxString &strSQL, int nField)
Retrieve all the values of one field in a result set.
Definition: database.cpp:708
virtual wxArrayLong GetResultsArrayLong(const wxString &strSQL, int nField)
Definition: database.cpp:814
static bool IsAvailable()
void SetErrorCode(int nErrorCode)
virtual bool Open()
void CloseStatements()
Close all prepared statement objects that have been generated but not yet closed.
Definition: database.cpp:38
virtual bool GetResultBool(int nField)=0
Retrieve a boolean from the result set by the 1-based field index.
wxDatabaseStatementHashSet m_Statements
Definition: database.h:201
virtual long GetSingleResultLong(const wxString &strSQL, int nField, bool bRequireUniqueResult=true)
Retrieve a single long value from a query If multiple records are returned from the query,...
Definition: database.cpp:288
virtual double GetSingleResultDouble(const wxString &strSQL, int nField, bool bRequireUniqueResult=true)
Retrieve a single double value from a query If multiple records are returned from the query,...
Definition: database.cpp:624
virtual wxString GetSingleResultString(const wxString &strSQL, int nField, bool bRequireUniqueResult=true)
Retrieve a single string value from a query If multiple records are returned from the query,...
Definition: database.cpp:204
void CloseResultSets()
Close all result set objects that have been generated but not yet closed.
Definition: database.cpp:24
virtual bool CloseResultSet(wxDatabaseResultSet *pResultSet)
Close a result set returned by the database or a prepared statement previously.
Definition: database.cpp:52