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
)
279 return wxGRID_VALUE_STRING
;
281 return wxGRID_VALUE_NUMBER
;
283 return wxGRID_VALUE_NUMBER
;
285 return wxGRID_VALUE_NUMBER
;
287 return wxGRID_VALUE_NUMBER
;
289 return wxGRID_VALUE_FLOAT
;
291 return wxGRID_VALUE_FLOAT
;
293 return wxGRID_VALUE_DATETIME
;
295 return wxGRID_VALUE_DATETIME
;
296 case SQL_C_TIMESTAMP
:
297 return wxGRID_VALUE_DATETIME
;
299 return wxGRID_VALUE_STRING
;
304 return m_ColInfo
[col
].wxtypename
;
307 wxFAIL_MSG (_T("unknown column"));
311 bool wxDbGridTableBase::CanGetValueAs(int row
, int col
, const wxString
& typeName
)
313 wxLogDebug(wxT("CanGetValueAs() on %i,%i"),row
,col
);
314 //Is this needed? As it will be validated on GetValueAsXXXX
317 if (typeName
== wxGRID_VALUE_STRING
)
319 //FIXME ummm What about blob field etc.
323 if (m_data
->IsColNull(m_ColInfo
[col
].DbCol
))
328 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
330 //If a virtual column then we can't find it's type. we have to
331 // return FALSE to get using wxVariant.
334 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
336 if (typeName
== wxGRID_VALUE_DATETIME
)
338 if ((sqltype
== SQL_C_DATE
) ||
339 (sqltype
== SQL_C_TIME
) ||
340 (sqltype
== SQL_C_TIMESTAMP
))
346 if (typeName
== wxGRID_VALUE_NUMBER
)
348 if ((sqltype
== SQL_C_SSHORT
) ||
349 (sqltype
== SQL_C_USHORT
) ||
350 (sqltype
== SQL_C_SLONG
) ||
351 (sqltype
== SQL_C_ULONG
))
357 if (typeName
== wxGRID_VALUE_FLOAT
)
359 if ((sqltype
== SQL_C_SSHORT
) ||
360 (sqltype
== SQL_C_USHORT
) ||
361 (sqltype
== SQL_C_SLONG
) ||
362 (sqltype
== SQL_C_ULONG
) ||
363 (sqltype
== SQL_C_FLOAT
) ||
364 (sqltype
== SQL_C_DOUBLE
))
373 bool wxDbGridTableBase::CanSetValueAs(int WXUNUSED(row
), int col
, const wxString
& typeName
)
375 if (typeName
== wxGRID_VALUE_STRING
)
377 //FIXME ummm What about blob field etc.
381 if (!(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].Updateable
))
386 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
388 //If a virtual column then we can't find it's type. we have to faulse to
389 //get using wxVairent.
393 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
394 if (typeName
== wxGRID_VALUE_DATETIME
)
396 if ((sqltype
== SQL_C_DATE
) ||
397 (sqltype
== SQL_C_TIME
) ||
398 (sqltype
== SQL_C_TIMESTAMP
))
404 if (typeName
== wxGRID_VALUE_NUMBER
)
406 if ((sqltype
== SQL_C_SSHORT
) ||
407 (sqltype
== SQL_C_USHORT
) ||
408 (sqltype
== SQL_C_SLONG
) ||
409 (sqltype
== SQL_C_ULONG
))
415 if (typeName
== wxGRID_VALUE_FLOAT
)
417 if ((sqltype
== SQL_C_SSHORT
) ||
418 (sqltype
== SQL_C_USHORT
) ||
419 (sqltype
== SQL_C_SLONG
) ||
420 (sqltype
== SQL_C_ULONG
) ||
421 (sqltype
== SQL_C_FLOAT
) ||
422 (sqltype
== SQL_C_DOUBLE
))
431 long wxDbGridTableBase::GetValueAsLong(int row
, int col
)
435 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
437 wxFAIL_MSG (_T("You can not use GetValueAsLong for virtual columns"));
440 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
441 if ((sqltype
== SQL_C_SSHORT
) ||
442 (sqltype
== SQL_C_USHORT
) ||
443 (sqltype
== SQL_C_SLONG
) ||
444 (sqltype
== SQL_C_ULONG
))
446 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
447 return val
.GetLong();
449 wxFAIL_MSG (_T("unknown column, "));
453 double wxDbGridTableBase::GetValueAsDouble(int row
, int col
)
455 wxLogDebug(wxT("GetValueAsDouble() on %i,%i"),row
,col
);
458 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
460 wxFAIL_MSG (_T("You can not use GetValueAsDouble for virtual columns"));
463 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
464 if ((sqltype
== SQL_C_SSHORT
) ||
465 (sqltype
== SQL_C_USHORT
) ||
466 (sqltype
== SQL_C_SLONG
) ||
467 (sqltype
== SQL_C_ULONG
) ||
468 (sqltype
== SQL_C_FLOAT
) ||
469 (sqltype
== SQL_C_DOUBLE
))
471 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
472 return val
.GetDouble();
474 wxFAIL_MSG (_T("unknown column"));
478 bool wxDbGridTableBase::GetValueAsBool(int row
, int col
)
480 wxLogDebug(wxT("GetValueAsBool() on %i,%i"),row
,col
);
483 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
485 wxFAIL_MSG (_T("You can not use GetValueAsBool for virtual columns"));
488 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
489 if ((sqltype
== SQL_C_SSHORT
) ||
490 (sqltype
== SQL_C_USHORT
) ||
491 (sqltype
== SQL_C_SLONG
) ||
492 (sqltype
== SQL_C_ULONG
))
494 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
495 return val
.GetBool();
497 wxFAIL_MSG (_T("unknown column, "));
501 void* wxDbGridTableBase::GetValueAsCustom(int row
, int col
, const wxString
& typeName
)
503 wxLogDebug(wxT("GetValueAsCustom() on %i,%i"),row
,col
);
506 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
508 wxFAIL_MSG (_T("You can not use GetValueAsCustom for virtual columns"));
511 if (m_data
->IsColNull(m_ColInfo
[col
].DbCol
))
514 if (typeName
== wxGRID_VALUE_DATETIME
)
516 wxDbColDef
*pColDefs
= m_data
->GetColDefs();
517 int sqltype
= pColDefs
[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
519 if ((sqltype
== SQL_C_DATE
) ||
520 (sqltype
== SQL_C_TIME
) ||
521 (sqltype
== SQL_C_TIMESTAMP
))
523 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
524 return new wxDateTime(val
.GetDateTime());
527 wxFAIL_MSG (_T("unknown column data type "));
532 void wxDbGridTableBase::SetValueAsCustom(int row
, int col
, const wxString
& typeName
, void* value
)
534 wxLogDebug(wxT("SetValueAsCustom() on %i,%i"),row
,col
);
537 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
539 wxFAIL_MSG (_T("You can not use SetValueAsCustom for virtual columns"));
543 if (typeName
== wxGRID_VALUE_DATETIME
)
545 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
546 if ((sqltype
== SQL_C_DATE
) ||
547 (sqltype
== SQL_C_TIME
) ||
548 (sqltype
== SQL_C_TIMESTAMP
))
550 //FIXME: you can't dynamic_cast from (void *)
551 //wxDateTime *date = wxDynamicCast(value, wxDateTime);
552 wxDateTime
*date
= (wxDateTime
*)value
;
555 wxFAIL_MSG (_T("Failed to convert data"));
559 m_rowmodified
= TRUE
;
560 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
563 wxFAIL_MSG (_T("unknown column data type"));
568 wxString
wxDbGridTableBase::GetColLabelValue(int col
)
570 if (GetNumberCols() > col
)
572 return m_ColInfo
[col
].Title
;
574 wxFAIL_MSG (_T("unknown column"));
578 bool wxDbGridTableBase::IsEmptyCell(int row
, int col
)
580 wxLogDebug(wxT("IsEmtpyCell on %i,%i"),row
,col
);
583 return m_data
->IsColNull(m_ColInfo
[col
].DbCol
);
587 wxString
wxDbGridTableBase::GetValue(int row
, int col
)
589 wxLogDebug(wxT("GetValue() on %i,%i"),row
,col
);
592 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
593 wxLogDebug(wxT("\tReturning \"%s\"\n"),val
.GetString().c_str());
595 return val
.GetString();
599 void wxDbGridTableBase::SetValue(int row
, int col
,const wxString
& value
)
601 wxLogDebug(wxT("SetValue() on %i,%i"),row
,col
);
604 wxVariant
val(value
);
606 m_rowmodified
= TRUE
;
607 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
611 void wxDbGridTableBase::SetValueAsLong(int row
, int col
, long value
)
613 wxLogDebug(wxT("SetValueAsLong() on %i,%i"),row
,col
);
616 wxVariant
val(value
);
618 m_rowmodified
= TRUE
;
619 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
623 void wxDbGridTableBase::SetValueAsDouble(int row
, int col
, double value
)
625 wxLogDebug(wxT("SetValueAsDouble() on %i,%i"),row
,col
);
628 wxVariant
val(value
);
630 m_rowmodified
= TRUE
;
631 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
636 void wxDbGridTableBase::SetValueAsBool(int row
, int col
, bool value
)
638 wxLogDebug(wxT("SetValueAsBool() on %i,%i"),row
,col
);
641 wxVariant
val(value
);
643 m_rowmodified
= TRUE
;
644 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
648 void wxDbGridTableBase::ValidateRow(int row
)
650 wxLogDebug(wxT("ValidateRow(%i) currently on row (%i). Array count = %i"),row
,m_row
,m_keys
.GetCount());
656 //We add to row as Count is unsigned!
657 if ((unsigned)(row
+1) > m_keys
.GetCount())
659 wxLogDebug(wxT("\trow key unknown"));
660 // Extend Array, iterate through data filling with keys
661 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_QUERY
);
663 for (trow
= m_keys
.GetCount(); trow
<= row
; trow
++)
665 wxLogDebug(wxT("Fetching row %i.."), trow
);
666 bool ret
= m_data
->GetNext();
668 wxLogDebug(wxT(" ...success=(%i)"),ret
);
669 GenericKey k
= m_data
->GetKey();
676 wxLogDebug(wxT("\trow key known centering data"));
677 GenericKey k
= m_keys
.Item(row
);
678 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_INDIVIDUAL
);
679 m_data
->ClearMemberVars();
681 if (!m_data
->QueryOnKeyFields())
683 wxDbLogExtendedErrorMsg(_T("ODBC error during Query()\n\n"), m_data
->GetDb(),__TFILE__
,__LINE__
);
690 m_rowmodified
= FALSE
;
693 bool wxDbGridTableBase::Writeback() const
701 wxLogDebug(wxT("\trow key unknown"));
703 // FIXME: this code requires dbtable support for record status
705 switch (m_data
->get_ModifiedStatus())
707 case wxDbTable::UpdatePending
:
708 result
= m_data
->Update();
710 case wxDbTable::InsertPending
:
711 result
= (m_data
->Insert() == SQL_SUCCESS
);
718 wxLogDebug(wxT("WARNING : Row writeback not implemented "));
723 #include "wx/arrimpl.cpp"
725 WX_DEFINE_EXPORTED_OBJARRAY(keyarray
);
727 #endif // #if wxUSE_GRID
728 #endif // #if wxUSE_ODBC