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 #include "wx/wxprec.h"
25 #include "wx/textctrl.h"
29 #include "wx/generic/gridctrl.h"
30 #include "wx/dbgrid.h"
32 // DLL options compatibility check:
34 WX_CHECK_BUILD_OPTIONS("wxDbGrid")
37 wxDbGridCellAttrProvider::wxDbGridCellAttrProvider()
43 wxDbGridCellAttrProvider::wxDbGridCellAttrProvider(wxDbTable
*tab
, wxDbGridColInfoBase
* ColInfo
)
49 wxDbGridCellAttrProvider::~wxDbGridCellAttrProvider()
53 wxGridCellAttr
*wxDbGridCellAttrProvider::GetAttr(int row
, int col
,
54 wxGridCellAttr::wxAttrKind kind
) const
56 wxGridCellAttr
*attr
= wxGridCellAttrProvider::GetAttr(row
,col
,kind
);
58 if (m_data
&& m_ColInfo
&& (m_data
->GetNumberOfColumns() > m_ColInfo
[col
].DbCol
))
60 //FIXME: this test could.
61 // ??::InsertPending == m_data->get_ModifiedStatus()
62 // and if InsertPending use colDef[].InsertAllowed
63 if (!(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].Updateable
))
67 case (wxGridCellAttr::Any
):
70 attr
= new wxGridCellAttr
;
71 // Store so we don't keep creating / deleting this...
72 wxDbGridCellAttrProvider
* self
= wxConstCast(this, wxDbGridCellAttrProvider
) ;
74 self
->SetColAttr(attr
, col
);
79 //We now must check what we were returned. and do the right thing (tm)
80 wxGridCellAttr::wxAttrKind attrkind
= attr
->GetKind();
81 if ((attrkind
== (wxGridCellAttr::Default
)) || (attrkind
== (wxGridCellAttr::Cell
)) ||
82 (attrkind
== (wxGridCellAttr::Col
)))
84 wxGridCellAttr
*attrtomerge
= attr
;
85 attr
= new wxGridCellAttr
;
86 attr
->SetKind(wxGridCellAttr::Merged
);
87 attr
->MergeWith(attrtomerge
);
89 attrtomerge
->DecRef();
94 case (wxGridCellAttr::Col
):
95 //As we must have a Coll, and were setting Coll attributes
96 // we can based on wxdbTable's so just set RO if attr valid
99 attr
= new wxGridCellAttr
;
100 wxDbGridCellAttrProvider
* self
= wxConstCast(this, wxDbGridCellAttrProvider
) ;
102 self
->SetColAttr(attr
, col
);
108 // wxGridCellAttr::Cell - Not required, will inherit on merge from row.
109 // wxGridCellAttr::Row - If wxDbtable ever supports row locking could add
110 // support to make RO on a row basis also.
111 // wxGridCellAttr::Default - Don't edit this ! or all cell with a attr will become readonly
112 // wxGridCellAttr::Merged - This should never be asked for.
121 void wxDbGridCellAttrProvider::AssignDbTable(wxDbTable
*tab
)
126 wxDbGridTableBase::wxDbGridTableBase(wxDbTable
*tab
, wxDbGridColInfo
* ColInfo
,
127 int count
, bool takeOwnership
) :
130 m_dbowner(takeOwnership
),
134 if (count
== wxUSE_QUERY
)
136 m_rowtotal
= m_data
? m_data
->Count() : 0;
142 // m_keys.Size(m_rowtotal);
146 m_nocols
= ColInfo
->Length();
147 m_ColInfo
= new wxDbGridColInfoBase
[m_nocols
];
149 wxDbGridColInfo
*ptr
= ColInfo
;
151 while (ptr
&& i
< m_nocols
)
153 m_ColInfo
[i
] = ptr
->m_data
;
160 wxLogDebug(wxT("NoCols over length after traversing %i items"),i
);
164 wxLogDebug(wxT("NoCols under length after traversing %i items"),i
);
170 wxDbGridTableBase::~wxDbGridTableBase()
172 wxDbGridCellAttrProvider
*provider
;
174 //Can't check for update here as
176 //FIXME: should i remove m_ColInfo and m_data from m_attrProvider if a wxDbGridAttrProvider
177 // if ((provider = dynamic_cast<wxDbGridCellAttrProvider *>(GetAttrProvider())))
178 // Using C casting for now until we can support dynamic_cast with wxWidgets
179 provider
= (wxDbGridCellAttrProvider
*)(GetAttrProvider());
182 provider
->AssignDbTable(NULL
);
193 bool wxDbGridTableBase::CanHaveAttributes()
195 if (!GetAttrProvider())
197 // use the default attr provider by default
198 SetAttrProvider(new wxDbGridCellAttrProvider(m_data
, m_ColInfo
));
204 bool wxDbGridTableBase::AssignDbTable(wxDbTable
*tab
, int count
, bool takeOwnership
)
206 wxDbGridCellAttrProvider
*provider
;
208 //Remove Information from grid about old data
211 wxGrid
*grid
= GetView();
213 grid
->ClearSelection();
214 if (grid
->IsCellEditControlEnabled())
216 grid
->DisableCellEditControl();
218 wxGridTableMessage
msg(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED
,0,m_rowtotal
);
219 grid
->ProcessTableMessage(msg
);
222 //reset our internals...
230 //FIXME: Remove dynamic_cast before sumision to wxwin
231 // if ((provider = dynamic_cast<wxDbGridCellAttrProvider *> (GetAttrProvider())))
232 // Using C casting for now until we can support dynamic_cast with wxWidgets
233 provider
= (wxDbGridCellAttrProvider
*)(GetAttrProvider());
236 provider
->AssignDbTable(m_data
);
239 if (count
== wxUSE_QUERY
)
241 m_rowtotal
= m_data
? m_data
->Count() : 0;
249 //Add Information to grid about new data
252 wxGrid
* grid
= GetView();
253 wxGridTableMessage
msg(this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED
, m_rowtotal
);
254 grid
->ProcessTableMessage(msg
);
257 m_dbowner
= takeOwnership
;
258 m_rowmodified
= false;
262 wxString
wxDbGridTableBase::GetTypeName(int WXUNUSED(row
), int col
)
264 if (GetNumberCols() > col
)
266 if (m_ColInfo
[col
].wxtypename
== wxGRID_VALUE_DBAUTO
)
268 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
270 wxFAIL_MSG (_T("You can not use wxGRID_VALUE_DBAUTO for virtual columns"));
272 switch(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
)
278 return wxGRID_VALUE_STRING
;
281 return wxGRID_VALUE_NUMBER
;
283 return wxGRID_VALUE_NUMBER
;
286 return wxGRID_VALUE_NUMBER
;
288 return wxGRID_VALUE_NUMBER
;
290 return wxGRID_VALUE_FLOAT
;
292 return wxGRID_VALUE_FLOAT
;
294 return wxGRID_VALUE_DATETIME
;
296 return wxGRID_VALUE_DATETIME
;
297 case SQL_C_TIMESTAMP
:
298 return wxGRID_VALUE_DATETIME
;
300 return wxGRID_VALUE_STRING
;
305 return m_ColInfo
[col
].wxtypename
;
308 wxFAIL_MSG (_T("unknown column"));
312 bool wxDbGridTableBase::CanGetValueAs(int row
, int col
, const wxString
& typeName
)
314 wxLogDebug(wxT("CanGetValueAs() on %i,%i"),row
,col
);
315 //Is this needed? As it will be validated on GetValueAsXXXX
318 if (typeName
== wxGRID_VALUE_STRING
)
320 //FIXME ummm What about blob field etc.
324 if (m_data
->IsColNull((UWORD
)m_ColInfo
[col
].DbCol
))
329 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
331 //If a virtual column then we can't find it's type. we have to
332 // return false to get using wxVariant.
335 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
337 if (typeName
== wxGRID_VALUE_DATETIME
)
339 if ((sqltype
== SQL_C_DATE
) ||
340 (sqltype
== SQL_C_TIME
) ||
341 (sqltype
== SQL_C_TIMESTAMP
))
347 if (typeName
== wxGRID_VALUE_NUMBER
)
349 if ((sqltype
== SQL_C_SSHORT
) ||
350 (sqltype
== SQL_C_USHORT
) ||
351 (sqltype
== SQL_C_SLONG
) ||
352 (sqltype
== SQL_C_ULONG
))
358 if (typeName
== wxGRID_VALUE_FLOAT
)
360 if ((sqltype
== SQL_C_SSHORT
) ||
361 (sqltype
== SQL_C_USHORT
) ||
362 (sqltype
== SQL_C_SLONG
) ||
363 (sqltype
== SQL_C_ULONG
) ||
364 (sqltype
== SQL_C_FLOAT
) ||
365 (sqltype
== SQL_C_DOUBLE
))
374 bool wxDbGridTableBase::CanSetValueAs(int WXUNUSED(row
), int col
, const wxString
& typeName
)
376 if (typeName
== wxGRID_VALUE_STRING
)
378 //FIXME ummm What about blob field etc.
382 if (!(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].Updateable
))
387 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
389 //If a virtual column then we can't find it's type. we have to faulse to
390 //get using wxVairent.
394 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
395 if (typeName
== wxGRID_VALUE_DATETIME
)
397 if ((sqltype
== SQL_C_DATE
) ||
398 (sqltype
== SQL_C_TIME
) ||
399 (sqltype
== SQL_C_TIMESTAMP
))
405 if (typeName
== wxGRID_VALUE_NUMBER
)
407 if ((sqltype
== SQL_C_SSHORT
) ||
408 (sqltype
== SQL_C_USHORT
) ||
409 (sqltype
== SQL_C_SLONG
) ||
410 (sqltype
== SQL_C_ULONG
))
416 if (typeName
== wxGRID_VALUE_FLOAT
)
418 if ((sqltype
== SQL_C_SSHORT
) ||
419 (sqltype
== SQL_C_USHORT
) ||
420 (sqltype
== SQL_C_SLONG
) ||
421 (sqltype
== SQL_C_ULONG
) ||
422 (sqltype
== SQL_C_FLOAT
) ||
423 (sqltype
== SQL_C_DOUBLE
))
432 long wxDbGridTableBase::GetValueAsLong(int row
, int col
)
436 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
438 wxFAIL_MSG (_T("You can not use GetValueAsLong for virtual columns"));
441 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
442 if ((sqltype
== SQL_C_SSHORT
) ||
443 (sqltype
== SQL_C_USHORT
) ||
444 (sqltype
== SQL_C_SLONG
) ||
445 (sqltype
== SQL_C_ULONG
))
447 wxVariant val
= m_data
->GetColumn(m_ColInfo
[col
].DbCol
);
448 return val
.GetLong();
450 wxFAIL_MSG (_T("unknown column, "));
454 double wxDbGridTableBase::GetValueAsDouble(int row
, int col
)
456 wxLogDebug(wxT("GetValueAsDouble() on %i,%i"),row
,col
);
459 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
461 wxFAIL_MSG (_T("You can not use GetValueAsDouble for virtual columns"));
464 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
465 if ((sqltype
== SQL_C_SSHORT
) ||
466 (sqltype
== SQL_C_USHORT
) ||
467 (sqltype
== SQL_C_SLONG
) ||
468 (sqltype
== SQL_C_ULONG
) ||
469 (sqltype
== SQL_C_FLOAT
) ||
470 (sqltype
== SQL_C_DOUBLE
))
472 wxVariant val
= m_data
->GetColumn(m_ColInfo
[col
].DbCol
);
473 return val
.GetDouble();
475 wxFAIL_MSG (_T("unknown column"));
479 bool wxDbGridTableBase::GetValueAsBool(int row
, int col
)
481 wxLogDebug(wxT("GetValueAsBool() on %i,%i"),row
,col
);
484 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
486 wxFAIL_MSG (_T("You can not use GetValueAsBool for virtual columns"));
489 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
490 if ((sqltype
== SQL_C_SSHORT
) ||
491 (sqltype
== SQL_C_USHORT
) ||
492 (sqltype
== SQL_C_SLONG
) ||
493 (sqltype
== SQL_C_ULONG
))
495 wxVariant val
= m_data
->GetColumn(m_ColInfo
[col
].DbCol
);
496 return val
.GetBool();
498 wxFAIL_MSG (_T("unknown column, "));
502 void* wxDbGridTableBase::GetValueAsCustom(int row
, int col
, const wxString
& typeName
)
504 wxLogDebug(wxT("GetValueAsCustom() on %i,%i"),row
,col
);
507 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
509 wxFAIL_MSG (_T("You can not use GetValueAsCustom for virtual columns"));
512 if (m_data
->IsColNull((UWORD
)m_ColInfo
[col
].DbCol
))
515 if (typeName
== wxGRID_VALUE_DATETIME
)
517 wxDbColDef
*pColDefs
= m_data
->GetColDefs();
518 int sqltype
= pColDefs
[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
520 if ((sqltype
== SQL_C_DATE
) ||
521 (sqltype
== SQL_C_TIME
) ||
522 (sqltype
== SQL_C_TIMESTAMP
))
524 wxVariant val
= m_data
->GetColumn(m_ColInfo
[col
].DbCol
);
525 return new wxDateTime(val
.GetDateTime());
528 wxFAIL_MSG (_T("unknown column data type "));
533 void wxDbGridTableBase::SetValueAsCustom(int row
, int col
, const wxString
& typeName
, void* value
)
535 wxLogDebug(wxT("SetValueAsCustom() on %i,%i"),row
,col
);
538 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
540 wxFAIL_MSG (_T("You can not use SetValueAsCustom for virtual columns"));
544 if (typeName
== wxGRID_VALUE_DATETIME
)
546 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
547 if ((sqltype
== SQL_C_DATE
) ||
548 (sqltype
== SQL_C_TIME
) ||
549 (sqltype
== SQL_C_TIMESTAMP
))
551 //FIXME: you can't dynamic_cast from (void *)
552 //wxDateTime *date = wxDynamicCast(value, wxDateTime);
553 wxDateTime
*date
= (wxDateTime
*)value
;
556 wxFAIL_MSG (_T("Failed to convert data"));
560 m_rowmodified
= true;
561 m_data
->SetColumn(m_ColInfo
[col
].DbCol
,val
);
564 wxFAIL_MSG (_T("unknown column data type"));
569 wxString
wxDbGridTableBase::GetColLabelValue(int col
)
571 if (GetNumberCols() > col
)
573 return m_ColInfo
[col
].Title
;
575 wxFAIL_MSG (_T("unknown column"));
579 bool wxDbGridTableBase::IsEmptyCell(int row
, int col
)
581 wxLogDebug(wxT("IsEmtpyCell on %i,%i"),row
,col
);
584 return m_data
->IsColNull((UWORD
)m_ColInfo
[col
].DbCol
);
588 wxString
wxDbGridTableBase::GetValue(int row
, int col
)
590 wxLogDebug(wxT("GetValue() on %i,%i"),row
,col
);
593 wxVariant val
= m_data
->GetColumn(m_ColInfo
[col
].DbCol
);
594 wxLogDebug(wxT("\tReturning \"%s\"\n"),val
.GetString().c_str());
596 return val
.GetString();
600 void wxDbGridTableBase::SetValue(int row
, int col
,const wxString
& value
)
602 wxLogDebug(wxT("SetValue() on %i,%i"),row
,col
);
605 wxVariant
val(value
);
607 m_rowmodified
= true;
608 m_data
->SetColumn(m_ColInfo
[col
].DbCol
,val
);
612 void wxDbGridTableBase::SetValueAsLong(int row
, int col
, long value
)
614 wxLogDebug(wxT("SetValueAsLong() on %i,%i"),row
,col
);
617 wxVariant
val(value
);
619 m_rowmodified
= true;
620 m_data
->SetColumn(m_ColInfo
[col
].DbCol
,val
);
624 void wxDbGridTableBase::SetValueAsDouble(int row
, int col
, double value
)
626 wxLogDebug(wxT("SetValueAsDouble() on %i,%i"),row
,col
);
629 wxVariant
val(value
);
631 m_rowmodified
= true;
632 m_data
->SetColumn(m_ColInfo
[col
].DbCol
,val
);
637 void wxDbGridTableBase::SetValueAsBool(int row
, int col
, bool value
)
639 wxLogDebug(wxT("SetValueAsBool() on %i,%i"),row
,col
);
642 wxVariant
val(value
);
644 m_rowmodified
= true;
645 m_data
->SetColumn(m_ColInfo
[col
].DbCol
,val
);
649 void wxDbGridTableBase::ValidateRow(int row
)
651 wxLogDebug(wxT("ValidateRow(%i) currently on row (%i). Array count = %i"),row
,m_row
,m_keys
.GetCount());
657 //We add to row as Count is unsigned!
658 if ((unsigned)(row
+1) > m_keys
.GetCount())
660 wxLogDebug(wxT("\trow key unknown"));
661 // Extend Array, iterate through data filling with keys
662 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_QUERY
);
664 for (trow
= m_keys
.GetCount(); trow
<= row
; trow
++)
666 wxLogDebug(wxT("Fetching row %i.."), trow
);
667 bool ret
= m_data
->GetNext();
669 wxLogDebug(wxT(" ...success=(%i)"),ret
);
670 GenericKey k
= m_data
->GetKey();
677 wxLogDebug(wxT("\trow key known centering data"));
678 GenericKey k
= m_keys
.Item(row
);
679 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_INDIVIDUAL
);
680 m_data
->ClearMemberVars();
682 if (!m_data
->QueryOnKeyFields())
684 wxDbLogExtendedErrorMsg(_T("ODBC error during Query()\n\n"), m_data
->GetDb(),__TFILE__
,__LINE__
);
691 m_rowmodified
= false;
694 bool wxDbGridTableBase::Writeback() const
702 wxLogDebug(wxT("\trow key unknown"));
704 // FIXME: this code requires dbtable support for record status
706 switch (m_data
->get_ModifiedStatus())
708 case wxDbTable::UpdatePending
:
709 result
= m_data
->Update();
711 case wxDbTable::InsertPending
:
712 result
= (m_data
->Insert() == SQL_SUCCESS
);
719 wxLogDebug(wxT("WARNING : Row writeback not implemented "));
724 #include "wx/arrimpl.cpp"
726 WX_DEFINE_EXPORTED_OBJARRAY(keyarray
);
728 #endif // #if wxUSE_GRID
729 #endif // #if wxUSE_ODBC