1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Displays a wxDbTable in a wxGrid.
4 // Author: Roger Gammans, Paul Gammans
8 // Copyright: (c) 1999 The Computer Surgery (roger@computer-surgery.co.uk)
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11 // Branched From : dbgrid.cpp,v 1.18 2000/12/19 13:00:58
12 ///////////////////////////////////////////////////////////////////////////////
14 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
15 #pragma implementation "dbgrid.h"
18 #include "wx/wxprec.h"
29 #include "wx/textctrl.h"
33 #include "wx/generic/gridctrl.h"
34 #include "wx/dbgrid.h"
36 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxDbGrid")
41 wxDbGridCellAttrProvider::wxDbGridCellAttrProvider()
47 wxDbGridCellAttrProvider::wxDbGridCellAttrProvider(wxDbTable
*tab
, wxDbGridColInfoBase
* ColInfo
)
53 wxDbGridCellAttrProvider::~wxDbGridCellAttrProvider()
57 wxGridCellAttr
*wxDbGridCellAttrProvider::GetAttr(int row
, int col
,
58 wxGridCellAttr::wxAttrKind kind
) const
60 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
,col
,kind
);
62 if (m_data
&& m_ColInfo
&& (m_data
->GetNumberOfColumns() > m_ColInfo
[col
].DbCol
))
64 //FIXME: this test could.
65 // ??::InsertPending == m_data->get_ModifiedStatus()
66 // and if InsertPending use colDef[].InsertAllowed
67 if (!(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].Updateable
))
71 case (wxGridCellAttr::Any
):
74 attr
= new wxGridCellAttr
;
75 // Store so we don't keep creating / deleting this...
76 wxDbGridCellAttrProvider
* self
= wxConstCast(this, wxDbGridCellAttrProvider
) ;
78 self
->SetColAttr(attr
, col
);
83 //We now must check what we were returned. and do the right thing (tm)
84 wxGridCellAttr::wxAttrKind attrkind
= attr
->GetKind();
85 if ((attrkind
== (wxGridCellAttr::Default
)) || (attrkind
== (wxGridCellAttr::Cell
)) ||
86 (attrkind
== (wxGridCellAttr::Col
)))
88 wxGridCellAttr
*attrtomerge
= attr
;
89 attr
= new wxGridCellAttr
;
90 attr
->SetKind(wxGridCellAttr::Merged
);
91 attr
->MergeWith(attrtomerge
);
93 attrtomerge
->DecRef();
98 case (wxGridCellAttr::Col
):
99 //As we must have a Coll, and were setting Coll attributes
100 // we can based on wxdbTable's so just set RO if attr valid
103 attr
= new wxGridCellAttr
;
104 wxDbGridCellAttrProvider
* self
= wxConstCast(this, wxDbGridCellAttrProvider
) ;
106 self
->SetColAttr(attr
, col
);
112 // wxGridCellAttr::Cell - Not required, will inherit on merge from row.
113 // wxGridCellAttr::Row - If wxDbtable ever supports row locking could add
114 // support to make RO on a row basis also.
115 // wxGridCellAttr::Default - Don't edit this ! or all cell with a attr will become readonly
116 // wxGridCellAttr::Merged - This should never be asked for.
125 void wxDbGridCellAttrProvider::AssignDbTable(wxDbTable
*tab
)
130 wxDbGridTableBase::wxDbGridTableBase(wxDbTable
*tab
, wxDbGridColInfo
* ColInfo
,
131 int count
, bool takeOwnership
) :
134 m_dbowner(takeOwnership
),
138 if (count
== wxUSE_QUERY
)
140 m_rowtotal
= m_data
? m_data
->Count() : 0;
146 // m_keys.Size(m_rowtotal);
150 m_nocols
= ColInfo
->Length();
151 m_ColInfo
= new wxDbGridColInfoBase
[m_nocols
];
153 wxDbGridColInfo
*ptr
= ColInfo
;
155 while (ptr
&& i
< m_nocols
)
157 m_ColInfo
[i
] = ptr
->m_data
;
164 wxLogDebug(wxT("NoCols over length after traversing %i items"),i
);
168 wxLogDebug(wxT("NoCols under length after traversing %i items"),i
);
174 wxDbGridTableBase::~wxDbGridTableBase()
176 wxDbGridCellAttrProvider
*provider
;
178 //Can't check for update here as
180 //FIXME: should i remove m_ColInfo and m_data from m_attrProvider if a wxDbGridAttrProvider
181 // if ((provider = dynamic_cast<wxDbGridCellAttrProvider *>(GetAttrProvider())))
182 // Using C casting for now until we can support dynamic_cast with wxWidgets
183 provider
= (wxDbGridCellAttrProvider
*)(GetAttrProvider());
186 provider
->AssignDbTable(NULL
);
197 bool wxDbGridTableBase::CanHaveAttributes()
199 if (!GetAttrProvider())
201 // use the default attr provider by default
202 SetAttrProvider(new wxDbGridCellAttrProvider(m_data
, m_ColInfo
));
208 bool wxDbGridTableBase::AssignDbTable(wxDbTable
*tab
, int count
, bool takeOwnership
)
210 wxDbGridCellAttrProvider
*provider
;
212 //Remove Information from grid about old data
215 wxGrid
*grid
= GetView();
217 grid
->ClearSelection();
218 if (grid
->IsCellEditControlEnabled())
220 grid
->DisableCellEditControl();
222 wxGridTableMessage
msg(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED
,0,m_rowtotal
);
223 grid
->ProcessTableMessage(msg
);
226 //reset our internals...
234 //FIXME: Remove dynamic_cast before sumision to wxwin
235 // if ((provider = dynamic_cast<wxDbGridCellAttrProvider *> (GetAttrProvider())))
236 // Using C casting for now until we can support dynamic_cast with wxWidgets
237 provider
= (wxDbGridCellAttrProvider
*)(GetAttrProvider());
240 provider
->AssignDbTable(m_data
);
243 if (count
== wxUSE_QUERY
)
245 m_rowtotal
= m_data
? m_data
->Count() : 0;
253 //Add Information to grid about new data
256 wxGrid
* grid
= GetView();
257 wxGridTableMessage
msg(this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED
, m_rowtotal
);
258 grid
->ProcessTableMessage(msg
);
261 m_dbowner
= takeOwnership
;
262 m_rowmodified
= false;
266 wxString
wxDbGridTableBase::GetTypeName(int WXUNUSED(row
), int col
)
268 if (GetNumberCols() > col
)
270 if (m_ColInfo
[col
].wxtypename
== wxGRID_VALUE_DBAUTO
)
272 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
274 wxFAIL_MSG (_T("You can not use wxGRID_VALUE_DBAUTO for virtual columns"));
276 switch(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
)
280 return wxGRID_VALUE_STRING
;
283 return wxGRID_VALUE_NUMBER
;
285 return wxGRID_VALUE_NUMBER
;
288 return wxGRID_VALUE_NUMBER
;
290 return wxGRID_VALUE_NUMBER
;
292 return wxGRID_VALUE_FLOAT
;
294 return wxGRID_VALUE_FLOAT
;
296 return wxGRID_VALUE_DATETIME
;
298 return wxGRID_VALUE_DATETIME
;
299 case SQL_C_TIMESTAMP
:
300 return wxGRID_VALUE_DATETIME
;
302 return wxGRID_VALUE_STRING
;
307 return m_ColInfo
[col
].wxtypename
;
310 wxFAIL_MSG (_T("unknown column"));
314 bool wxDbGridTableBase::CanGetValueAs(int row
, int col
, const wxString
& typeName
)
316 wxLogDebug(wxT("CanGetValueAs() on %i,%i"),row
,col
);
317 //Is this needed? As it will be validated on GetValueAsXXXX
320 if (typeName
== wxGRID_VALUE_STRING
)
322 //FIXME ummm What about blob field etc.
326 if (m_data
->IsColNull((UWORD
)m_ColInfo
[col
].DbCol
))
331 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
333 //If a virtual column then we can't find it's type. we have to
334 // return false to get using wxVariant.
337 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
339 if (typeName
== wxGRID_VALUE_DATETIME
)
341 if ((sqltype
== SQL_C_DATE
) ||
342 (sqltype
== SQL_C_TIME
) ||
343 (sqltype
== SQL_C_TIMESTAMP
))
349 if (typeName
== wxGRID_VALUE_NUMBER
)
351 if ((sqltype
== SQL_C_SSHORT
) ||
352 (sqltype
== SQL_C_USHORT
) ||
353 (sqltype
== SQL_C_SLONG
) ||
354 (sqltype
== SQL_C_ULONG
))
360 if (typeName
== wxGRID_VALUE_FLOAT
)
362 if ((sqltype
== SQL_C_SSHORT
) ||
363 (sqltype
== SQL_C_USHORT
) ||
364 (sqltype
== SQL_C_SLONG
) ||
365 (sqltype
== SQL_C_ULONG
) ||
366 (sqltype
== SQL_C_FLOAT
) ||
367 (sqltype
== SQL_C_DOUBLE
))
376 bool wxDbGridTableBase::CanSetValueAs(int WXUNUSED(row
), int col
, const wxString
& typeName
)
378 if (typeName
== wxGRID_VALUE_STRING
)
380 //FIXME ummm What about blob field etc.
384 if (!(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].Updateable
))
389 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
391 //If a virtual column then we can't find it's type. we have to faulse to
392 //get using wxVairent.
396 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
397 if (typeName
== wxGRID_VALUE_DATETIME
)
399 if ((sqltype
== SQL_C_DATE
) ||
400 (sqltype
== SQL_C_TIME
) ||
401 (sqltype
== SQL_C_TIMESTAMP
))
407 if (typeName
== wxGRID_VALUE_NUMBER
)
409 if ((sqltype
== SQL_C_SSHORT
) ||
410 (sqltype
== SQL_C_USHORT
) ||
411 (sqltype
== SQL_C_SLONG
) ||
412 (sqltype
== SQL_C_ULONG
))
418 if (typeName
== wxGRID_VALUE_FLOAT
)
420 if ((sqltype
== SQL_C_SSHORT
) ||
421 (sqltype
== SQL_C_USHORT
) ||
422 (sqltype
== SQL_C_SLONG
) ||
423 (sqltype
== SQL_C_ULONG
) ||
424 (sqltype
== SQL_C_FLOAT
) ||
425 (sqltype
== SQL_C_DOUBLE
))
434 long wxDbGridTableBase::GetValueAsLong(int row
, int col
)
438 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
440 wxFAIL_MSG (_T("You can not use GetValueAsLong for virtual columns"));
443 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
444 if ((sqltype
== SQL_C_SSHORT
) ||
445 (sqltype
== SQL_C_USHORT
) ||
446 (sqltype
== SQL_C_SLONG
) ||
447 (sqltype
== SQL_C_ULONG
))
449 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
450 return val
.GetLong();
452 wxFAIL_MSG (_T("unknown column, "));
456 double wxDbGridTableBase::GetValueAsDouble(int row
, int col
)
458 wxLogDebug(wxT("GetValueAsDouble() on %i,%i"),row
,col
);
461 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
463 wxFAIL_MSG (_T("You can not use GetValueAsDouble for virtual columns"));
466 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
467 if ((sqltype
== SQL_C_SSHORT
) ||
468 (sqltype
== SQL_C_USHORT
) ||
469 (sqltype
== SQL_C_SLONG
) ||
470 (sqltype
== SQL_C_ULONG
) ||
471 (sqltype
== SQL_C_FLOAT
) ||
472 (sqltype
== SQL_C_DOUBLE
))
474 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
475 return val
.GetDouble();
477 wxFAIL_MSG (_T("unknown column"));
481 bool wxDbGridTableBase::GetValueAsBool(int row
, int col
)
483 wxLogDebug(wxT("GetValueAsBool() on %i,%i"),row
,col
);
486 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
488 wxFAIL_MSG (_T("You can not use GetValueAsBool for virtual columns"));
491 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
492 if ((sqltype
== SQL_C_SSHORT
) ||
493 (sqltype
== SQL_C_USHORT
) ||
494 (sqltype
== SQL_C_SLONG
) ||
495 (sqltype
== SQL_C_ULONG
))
497 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
498 return val
.GetBool();
500 wxFAIL_MSG (_T("unknown column, "));
504 void* wxDbGridTableBase::GetValueAsCustom(int row
, int col
, const wxString
& typeName
)
506 wxLogDebug(wxT("GetValueAsCustom() on %i,%i"),row
,col
);
509 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
511 wxFAIL_MSG (_T("You can not use GetValueAsCustom for virtual columns"));
514 if (m_data
->IsColNull((UWORD
)m_ColInfo
[col
].DbCol
))
517 if (typeName
== wxGRID_VALUE_DATETIME
)
519 wxDbColDef
*pColDefs
= m_data
->GetColDefs();
520 int sqltype
= pColDefs
[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
522 if ((sqltype
== SQL_C_DATE
) ||
523 (sqltype
== SQL_C_TIME
) ||
524 (sqltype
== SQL_C_TIMESTAMP
))
526 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
527 return new wxDateTime(val
.GetDateTime());
530 wxFAIL_MSG (_T("unknown column data type "));
535 void wxDbGridTableBase::SetValueAsCustom(int row
, int col
, const wxString
& typeName
, void* value
)
537 wxLogDebug(wxT("SetValueAsCustom() on %i,%i"),row
,col
);
540 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
542 wxFAIL_MSG (_T("You can not use SetValueAsCustom for virtual columns"));
546 if (typeName
== wxGRID_VALUE_DATETIME
)
548 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
549 if ((sqltype
== SQL_C_DATE
) ||
550 (sqltype
== SQL_C_TIME
) ||
551 (sqltype
== SQL_C_TIMESTAMP
))
553 //FIXME: you can't dynamic_cast from (void *)
554 //wxDateTime *date = wxDynamicCast(value, wxDateTime);
555 wxDateTime
*date
= (wxDateTime
*)value
;
558 wxFAIL_MSG (_T("Failed to convert data"));
562 m_rowmodified
= true;
563 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
566 wxFAIL_MSG (_T("unknown column data type"));
571 wxString
wxDbGridTableBase::GetColLabelValue(int col
)
573 if (GetNumberCols() > col
)
575 return m_ColInfo
[col
].Title
;
577 wxFAIL_MSG (_T("unknown column"));
581 bool wxDbGridTableBase::IsEmptyCell(int row
, int col
)
583 wxLogDebug(wxT("IsEmtpyCell on %i,%i"),row
,col
);
586 return m_data
->IsColNull((UWORD
)m_ColInfo
[col
].DbCol
);
590 wxString
wxDbGridTableBase::GetValue(int row
, int col
)
592 wxLogDebug(wxT("GetValue() on %i,%i"),row
,col
);
595 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
596 wxLogDebug(wxT("\tReturning \"%s\"\n"),val
.GetString().c_str());
598 return val
.GetString();
602 void wxDbGridTableBase::SetValue(int row
, int col
,const wxString
& value
)
604 wxLogDebug(wxT("SetValue() on %i,%i"),row
,col
);
607 wxVariant
val(value
);
609 m_rowmodified
= true;
610 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
614 void wxDbGridTableBase::SetValueAsLong(int row
, int col
, long value
)
616 wxLogDebug(wxT("SetValueAsLong() on %i,%i"),row
,col
);
619 wxVariant
val(value
);
621 m_rowmodified
= true;
622 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
626 void wxDbGridTableBase::SetValueAsDouble(int row
, int col
, double value
)
628 wxLogDebug(wxT("SetValueAsDouble() on %i,%i"),row
,col
);
631 wxVariant
val(value
);
633 m_rowmodified
= true;
634 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
639 void wxDbGridTableBase::SetValueAsBool(int row
, int col
, bool value
)
641 wxLogDebug(wxT("SetValueAsBool() on %i,%i"),row
,col
);
644 wxVariant
val(value
);
646 m_rowmodified
= true;
647 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
651 void wxDbGridTableBase::ValidateRow(int row
)
653 wxLogDebug(wxT("ValidateRow(%i) currently on row (%i). Array count = %i"),row
,m_row
,m_keys
.GetCount());
659 //We add to row as Count is unsigned!
660 if ((unsigned)(row
+1) > m_keys
.GetCount())
662 wxLogDebug(wxT("\trow key unknown"));
663 // Extend Array, iterate through data filling with keys
664 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_QUERY
);
666 for (trow
= m_keys
.GetCount(); trow
<= row
; trow
++)
668 wxLogDebug(wxT("Fetching row %i.."), trow
);
669 bool ret
= m_data
->GetNext();
671 wxLogDebug(wxT(" ...success=(%i)"),ret
);
672 GenericKey k
= m_data
->GetKey();
679 wxLogDebug(wxT("\trow key known centering data"));
680 GenericKey k
= m_keys
.Item(row
);
681 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_INDIVIDUAL
);
682 m_data
->ClearMemberVars();
684 if (!m_data
->QueryOnKeyFields())
686 wxDbLogExtendedErrorMsg(_T("ODBC error during Query()\n\n"), m_data
->GetDb(),__TFILE__
,__LINE__
);
693 m_rowmodified
= false;
696 bool wxDbGridTableBase::Writeback() const
704 wxLogDebug(wxT("\trow key unknown"));
706 // FIXME: this code requires dbtable support for record status
708 switch (m_data
->get_ModifiedStatus())
710 case wxDbTable::UpdatePending
:
711 result
= m_data
->Update();
713 case wxDbTable::InsertPending
:
714 result
= (m_data
->Insert() == SQL_SUCCESS
);
721 wxLogDebug(wxT("WARNING : Row writeback not implemented "));
726 #include "wx/arrimpl.cpp"
728 WX_DEFINE_EXPORTED_OBJARRAY(keyarray
);
730 #endif // #if wxUSE_GRID
731 #endif // #if wxUSE_ODBC