From edca7a82055fc201fad1bba36742807d08cb6192 Mon Sep 17 00:00:00 2001 From: George Tasker Date: Sun, 10 Jun 2001 17:04:00 +0000 Subject: [PATCH] wxDbGrid additions - Added support for ODBC date types git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@10499 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/variant.h | 35 +++++++ src/common/variant.cpp | 201 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 234 insertions(+), 2 deletions(-) diff --git a/include/wx/variant.h b/include/wx/variant.h index 0e3d7388a1..34eb32c400 100644 --- a/include/wx/variant.h +++ b/include/wx/variant.h @@ -26,6 +26,12 @@ #include "wx/date.h" #endif // time/date +#include "wx/datetime.h" + +#if wxUSE_ODBC + #include "wx/db.h" // will #include sqltypes.h +#endif //ODBC + #include "wx/ioswrap.h" /* @@ -98,6 +104,15 @@ public: #endif wxVariant(void* ptr, const wxString& name = wxEmptyString); // void* (general purpose) wxVariant(wxVariantData* data, const wxString& name = wxEmptyString); // User-defined data +//TODO: Need to document + wxVariant(const wxDateTime& val, const wxString& name = wxEmptyString); // Date +#if wxUSE_ODBC + wxVariant(const DATE_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime + wxVariant(const TIME_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime + wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name = wxEmptyString); // DateTime +#endif +//TODO: End of Need to document + wxVariant(const wxVariant& variant); ~wxVariant(); @@ -105,6 +120,17 @@ public: // Assignment void operator= (const wxVariant& variant); +//TODO: Need to document + bool operator== (const wxDateTime& value) const; + bool operator!= (const wxDateTime& value) const; + void operator= (const wxDateTime& value) ; +#if wxUSE_ODBC + void operator= (const DATE_STRUCT* value) ; + void operator= (const TIME_STRUCT* value) ; + void operator= (const TIMESTAMP_STRUCT* value) ; +#endif +//TODO: End of Need to document + // Assignment using data, e.g. // myVariant = new wxStringVariantData("hello"); void operator= (wxVariantData* variantData); @@ -168,6 +194,9 @@ public: inline operator wxDate () const { return GetDate(); } #endif inline operator void* () const { return GetVoidPtr(); } +//TODO: Need to document + inline operator wxDateTime () const { return GetDateTime(); } +//TODO: End of Need to document // Accessors // Sets/gets name @@ -206,6 +235,9 @@ public: wxDate GetDate() const ; #endif void* GetVoidPtr() const ; +//TODO: Need to document + wxDateTime GetDateTime() const ; +//TODO: End of Need to document // Operations // Make NULL (i.e. delete the data) @@ -242,6 +274,9 @@ protected: bool Convert(wxTime* value) const; bool Convert(wxDate* value) const; #endif +//TODO: Need to document + bool Convert(wxDateTime* value) const; +//TODO: End of Need to document // Attributes protected: diff --git a/src/common/variant.cpp b/src/common/variant.cpp index 0ada278e3e..cab5e3ead1 100644 --- a/src/common/variant.cpp +++ b/src/common/variant.cpp @@ -1076,6 +1076,102 @@ bool wxVariantDataVoidPtr::Read(wxString& WXUNUSED(str)) return FALSE; } +/* + * wxVariantDataDateTime + */ + +class wxVariantDataDateTime: public wxVariantData +{ + DECLARE_DYNAMIC_CLASS(wxVariantDataDateTime) + +public: + wxVariantDataDateTime() { } + wxVariantDataDateTime(const wxDateTime& value) { m_value = value; } +#if wxUSE_ODBC + wxVariantDataDateTime(const TIME_STRUCT* valptr) + { m_value = wxDateTime(valptr->hour, valptr->minute, valptr->second); } + wxVariantDataDateTime(const DATE_STRUCT* valptr) + { m_value = wxDateTime(valptr->day, (wxDateTime::Month) (valptr->month - 1),valptr->year); } + wxVariantDataDateTime(const TIMESTAMP_STRUCT* valptr) + { m_value = wxDateTime(valptr->day, (wxDateTime::Month) (valptr->month - 1), valptr->year, + valptr->hour, valptr->minute, valptr->second, valptr->fraction ); } +#endif //ODBC + + inline wxDateTime GetValue() const { return m_value; } + inline void SetValue(const wxDateTime& value) { m_value = value; } + + virtual void Copy(wxVariantData& data); + virtual bool Eq(wxVariantData& data) const; +#if wxUSE_STD_IOSTREAM + virtual bool Write(ostream& str) const; +#endif + virtual bool Write(wxString& str) const; +#if wxUSE_STD_IOSTREAM + virtual bool Read(istream& str); +#endif + virtual bool Read(wxString& str); + virtual wxString GetType() const { return wxT("datetime"); }; + virtual wxVariantData* Clone() { return new wxVariantDataDateTime; } + +protected: + wxDateTime m_value; +}; + + +IMPLEMENT_DYNAMIC_CLASS(wxVariantDataDateTime, wxVariantData) + +void wxVariantDataDateTime::Copy(wxVariantData& data) +{ + wxASSERT_MSG( (data.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Copy: Can't copy to this type of data") ); + + wxVariantDataDateTime& otherData = (wxVariantDataDateTime&) data; + + otherData.m_value = m_value; +} + + +bool wxVariantDataDateTime::Eq(wxVariantData& data) const +{ + wxASSERT_MSG( (data.GetType() == wxT("datetime")), wxT("wxVariantDataDateTime::Eq: argument mismatch") ); + + wxVariantDataDateTime& otherData = (wxVariantDataDateTime&) data; + + return (otherData.m_value == m_value); +} + + +#if wxUSE_STD_IOSTREAM +bool wxVariantDataDateTime::Write(ostream& str) const +{ + // Not implemented + return FALSE; +} +#endif + + +bool wxVariantDataDateTime::Write(wxString& str) const +{ + str = m_value.Format(); + return TRUE; +} + + +#if wxUSE_STD_IOSTREAM +bool wxVariantDataDateTime::Read(istream& WXUNUSED(str)) +{ + // Not implemented + return FALSE; +} +#endif + + +bool wxVariantDataDateTime::Read(wxString& str) +{ + if(! m_value.ParseDateTime(str)) + return FALSE; + return TRUE; +} + /* * wxVariant @@ -1160,6 +1256,32 @@ wxVariant::wxVariant(void* val, const wxString& name) // Void ptr m_name = name; } +wxVariant::wxVariant(const wxDateTime& val, const wxString& name) // Date +{ + m_data = new wxVariantDataDateTime(val); + m_name = name; +} + +#if wxUSE_ODBC +wxVariant::wxVariant(const TIME_STRUCT* valptr, const wxString& name) // Date +{ + m_data = new wxVariantDataDateTime(valptr); + m_name = name; +} + +wxVariant::wxVariant(const TIMESTAMP_STRUCT* valptr, const wxString& name) // Date +{ + m_data = new wxVariantDataDateTime(valptr); + m_name = name; +} + +wxVariant::wxVariant(const DATE_STRUCT* valptr, const wxString& name) // Date +{ + m_data = new wxVariantDataDateTime(valptr); + m_name = name; +} +#endif + wxVariant::wxVariant(const wxVariant& variant) { if (!variant.IsNull()) @@ -1527,6 +1649,61 @@ void wxVariant::operator= (void* value) } } +bool wxVariant::operator== (const wxDateTime& value) const +{ + wxDateTime thisValue; + if (!Convert(&thisValue)) + return FALSE; + + return value.IsEqualTo(thisValue); +} + +bool wxVariant::operator!= (const wxDateTime& value) const +{ + return (!((*this) == value)); +} + +void wxVariant::operator= (const wxDateTime& value) +{ + if (GetType() == wxT("datetime")) + { + ((wxVariantDataDateTime*)GetData())->SetValue(value); + } + else + { + if (m_data) + delete m_data; + m_data = new wxVariantDataDateTime(value); + } +} + + +#if wxUSE_ODBC +void wxVariant::operator= (const DATE_STRUCT* value) +{ + if (m_data) + delete m_data; + m_data = new wxVariantDataDateTime(value); +} + + +void wxVariant::operator= (const TIME_STRUCT* value) +{ + if (m_data) + delete m_data; + m_data = new wxVariantDataDateTime(value); +} + + +void wxVariant::operator= (const TIMESTAMP_STRUCT* value) +{ + if (m_data) + delete m_data; + m_data = new wxVariantDataDateTime(value); +} + +#endif + // Treat a list variant as an array wxVariant wxVariant::operator[] (size_t idx) const { @@ -1709,6 +1886,17 @@ void* wxVariant::GetVoidPtr() const return (void*) ((wxVariantDataVoidPtr*) m_data)->GetValue(); } +wxDateTime wxVariant::GetDateTime() const +{ + wxDateTime value; + if (!Convert(& value)) + { + wxFAIL_MSG(wxT("Could not convert to a datetime")); + } + + return value; +} + wxList& wxVariant::GetList() const { wxASSERT( (GetType() == wxT("list")) ); @@ -1901,6 +2089,15 @@ bool wxVariant::Convert(wxDate* value) const return TRUE; } -#endif - // wxUSE_TIMEDATE +#endif // wxUSE_TIMEDATE +bool wxVariant::Convert(wxDateTime* value) const +{ + wxString type(GetType()); + if (type == wxT("datetime")) + *value = ((wxVariantDataDateTime*)GetData())->GetValue(); + else + return FALSE; + + return TRUE; +}