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 ///////////////////////////////////////////////////////////////////////////////
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"
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 wxWindows
179 if ((provider
= (wxDbGridCellAttrProvider
*)(GetAttrProvider())))
181 provider
->AssignDbTable(NULL
);
192 bool wxDbGridTableBase::CanHaveAttributes()
194 if (!GetAttrProvider())
196 // use the default attr provider by default
197 SetAttrProvider(new wxDbGridCellAttrProvider(m_data
, m_ColInfo
));
203 bool wxDbGridTableBase::AssignDbTable(wxDbTable
*tab
, int count
, bool takeOwnership
)
205 wxDbGridCellAttrProvider
*provider
;
207 //Remove Information from grid about old data
210 wxGrid
*grid
= GetView();
212 grid
->ClearSelection();
213 if (grid
->IsCellEditControlEnabled())
215 grid
->DisableCellEditControl();
217 wxGridTableMessage
msg(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED
,0,m_rowtotal
);
218 grid
->ProcessTableMessage(msg
);
221 //reset our internals...
229 //FIXME: Remove dynamic_cast before sumision to wxwin
230 // if ((provider = dynamic_cast<wxDbGridCellAttrProvider *> (GetAttrProvider())))
231 // Using C casting for now until we can support dynamic_cast with wxWindows
232 if ((provider
= (wxDbGridCellAttrProvider
*)(GetAttrProvider())))
234 provider
->AssignDbTable(m_data
);
237 if (count
== wxUSE_QUERY
)
239 m_rowtotal
= m_data
? m_data
->Count() : 0;
247 //Add Information to grid about new data
250 wxGrid
* grid
= GetView();
251 wxGridTableMessage
msg(this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED
, m_rowtotal
);
252 grid
->ProcessTableMessage(msg
);
255 m_dbowner
= takeOwnership
;
256 m_rowmodified
= false;
260 wxString
wxDbGridTableBase::GetTypeName(int row
, int col
)
265 if (GetNumberCols() > col
)
267 if (m_ColInfo
[col
].wxtypename
== wxGRID_VALUE_DBAUTO
)
269 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
271 wxFAIL_MSG (_T("You can not use wxGRID_VALUE_DBAUTO for virtual columns"));
273 switch(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
)
276 return wxGRID_VALUE_STRING
;
279 return wxGRID_VALUE_NUMBER
;
282 return wxGRID_VALUE_NUMBER
;
285 return wxGRID_VALUE_NUMBER
;
288 return wxGRID_VALUE_NUMBER
;
291 return wxGRID_VALUE_FLOAT
;
294 return wxGRID_VALUE_FLOAT
;
297 return wxGRID_VALUE_DATETIME
;
300 return wxGRID_VALUE_DATETIME
;
302 case SQL_C_TIMESTAMP
:
303 return wxGRID_VALUE_DATETIME
;
306 return wxGRID_VALUE_STRING
;
312 return m_ColInfo
[col
].wxtypename
;
315 wxFAIL_MSG (_T("unknown column"));
319 bool wxDbGridTableBase::CanGetValueAs(int row
, int col
, const wxString
& typeName
)
321 wxLogDebug(wxT("CanGetValueAs() on %i,%i"),row
,col
);
322 //Is this needed? As it will be validated on GetValueAsXXXX
325 if (typeName
== wxGRID_VALUE_STRING
)
327 //FIXME ummm What about blob field etc.
331 if (m_data
->IsColNull(m_ColInfo
[col
].DbCol
))
336 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
338 //If a virtual column then we can't find it's type. we have to
339 // return false to get using wxVariant.
342 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
344 if (typeName
== wxGRID_VALUE_DATETIME
)
346 if ((sqltype
== SQL_C_DATE
) ||
347 (sqltype
== SQL_C_TIME
) ||
348 (sqltype
== SQL_C_TIMESTAMP
))
354 if (typeName
== wxGRID_VALUE_NUMBER
)
356 if ((sqltype
== SQL_C_SSHORT
) ||
357 (sqltype
== SQL_C_USHORT
) ||
358 (sqltype
== SQL_C_SLONG
) ||
359 (sqltype
== SQL_C_ULONG
))
365 if (typeName
== wxGRID_VALUE_FLOAT
)
367 if ((sqltype
== SQL_C_SSHORT
) ||
368 (sqltype
== SQL_C_USHORT
) ||
369 (sqltype
== SQL_C_SLONG
) ||
370 (sqltype
== SQL_C_ULONG
) ||
371 (sqltype
== SQL_C_FLOAT
) ||
372 (sqltype
== SQL_C_DOUBLE
))
381 bool wxDbGridTableBase::CanSetValueAs(int row
, int col
, const wxString
& typeName
)
383 //Is this needed? As will be validated on SetValueAsXXXX
386 if (m_data
->IsColNull(m_ColInfo
[col
].DbCol
))
391 if (typeName
== wxGRID_VALUE_STRING
)
393 //FIXME ummm What about blob field etc.
397 if (!(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].Updateable
))
402 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
404 //If a virtual column then we can't find it's type. we have to faulse to
405 //get using wxVairent.
409 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
410 if (typeName
== wxGRID_VALUE_DATETIME
)
412 if ((sqltype
== SQL_C_DATE
) ||
413 (sqltype
== SQL_C_TIME
) ||
414 (sqltype
== SQL_C_TIMESTAMP
))
420 if (typeName
== wxGRID_VALUE_NUMBER
)
422 if ((sqltype
== SQL_C_SSHORT
) ||
423 (sqltype
== SQL_C_USHORT
) ||
424 (sqltype
== SQL_C_SLONG
) ||
425 (sqltype
== SQL_C_ULONG
))
431 if (typeName
== wxGRID_VALUE_FLOAT
)
433 if ((sqltype
== SQL_C_SSHORT
) ||
434 (sqltype
== SQL_C_USHORT
) ||
435 (sqltype
== SQL_C_SLONG
) ||
436 (sqltype
== SQL_C_ULONG
) ||
437 (sqltype
== SQL_C_FLOAT
) ||
438 (sqltype
== SQL_C_DOUBLE
))
447 long wxDbGridTableBase::GetValueAsLong(int row
, int col
)
451 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
453 wxFAIL_MSG (_T("You can not use GetValueAsLong for virtual columns"));
456 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
457 if ((sqltype
== SQL_C_SSHORT
) ||
458 (sqltype
== SQL_C_USHORT
) ||
459 (sqltype
== SQL_C_SLONG
) ||
460 (sqltype
== SQL_C_ULONG
))
462 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
463 return val
.GetLong();
465 wxFAIL_MSG (_T("unknown column, "));
469 double wxDbGridTableBase::GetValueAsDouble(int row
, int col
)
471 wxLogDebug(wxT("GetValueAsDouble() on %i,%i"),row
,col
);
474 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
476 wxFAIL_MSG (_T("You can not use GetValueAsDouble for virtual columns"));
479 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
480 if ((sqltype
== SQL_C_SSHORT
) ||
481 (sqltype
== SQL_C_USHORT
) ||
482 (sqltype
== SQL_C_SLONG
) ||
483 (sqltype
== SQL_C_ULONG
) ||
484 (sqltype
== SQL_C_FLOAT
) ||
485 (sqltype
== SQL_C_DOUBLE
))
487 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
488 return val
.GetDouble();
490 wxFAIL_MSG (_T("unknown column"));
494 bool wxDbGridTableBase::GetValueAsBool(int row
, int col
)
496 wxLogDebug(wxT("GetValueAsBool() on %i,%i"),row
,col
);
499 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
501 wxFAIL_MSG (_T("You can not use GetValueAsBool for virtual columns"));
504 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
505 if ((sqltype
== SQL_C_SSHORT
) ||
506 (sqltype
== SQL_C_USHORT
) ||
507 (sqltype
== SQL_C_SLONG
) ||
508 (sqltype
== SQL_C_ULONG
))
510 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
511 return val
.GetBool();
513 wxFAIL_MSG (_T("unknown column, "));
517 void* wxDbGridTableBase::GetValueAsCustom(int row
, int col
, const wxString
& typeName
)
519 wxLogDebug(wxT("GetValueAsCustom() on %i,%i"),row
,col
);
522 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
524 wxFAIL_MSG (_T("You can not use GetValueAsCustom for virtual columns"));
527 if (m_data
->IsColNull(m_ColInfo
[col
].DbCol
))
530 if (typeName
== wxGRID_VALUE_DATETIME
)
532 wxDbColDef
*pColDefs
= m_data
->GetColDefs();
533 int sqltype
= pColDefs
[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
535 if ((sqltype
== SQL_C_DATE
) ||
536 (sqltype
== SQL_C_TIME
) ||
537 (sqltype
== SQL_C_TIMESTAMP
))
539 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
540 return new wxDateTime(val
.GetDateTime());
543 wxFAIL_MSG (_T("unknown column data type "));
548 void wxDbGridTableBase::SetValueAsCustom(int row
, int col
, const wxString
& typeName
, void* value
)
550 wxLogDebug(wxT("SetValueAsCustom() on %i,%i"),row
,col
);
553 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
555 wxFAIL_MSG (_T("You can not use SetValueAsCustom for virtual columns"));
559 if (typeName
== wxGRID_VALUE_DATETIME
)
561 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
562 if ((sqltype
== SQL_C_DATE
) ||
563 (sqltype
== SQL_C_TIME
) ||
564 (sqltype
== SQL_C_TIMESTAMP
))
566 //FIXME: you can't dynamic_cast from (void *)
567 //wxDateTime *date = wxDynamicCast(value, wxDateTime);
568 wxDateTime
*date
= (wxDateTime
*)value
;
571 wxFAIL_MSG (_T("Failed to convert data"));
575 m_rowmodified
= true;
576 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
579 wxFAIL_MSG (_T("unknown column data type"));
584 wxString
wxDbGridTableBase::GetColLabelValue(int col
)
586 if (GetNumberCols() > col
)
588 return m_ColInfo
[col
].Title
;
590 wxFAIL_MSG (_T("unknown column"));
594 bool wxDbGridTableBase::IsEmptyCell(int row
, int col
)
596 wxLogDebug(wxT("IsEmtpyCell on %i,%i"),row
,col
);
599 return m_data
->IsColNull(m_ColInfo
[col
].DbCol
);
603 wxString
wxDbGridTableBase::GetValue(int row
, int col
)
605 wxLogDebug(wxT("GetValue() on %i,%i"),row
,col
);
608 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
609 wxLogDebug(wxT("\tReturning \"%s\"\n"),val
.GetString().c_str());
611 return val
.GetString();
615 void wxDbGridTableBase::SetValue(int row
, int col
,const wxString
& value
)
617 wxLogDebug(wxT("SetValue() on %i,%i"),row
,col
);
620 wxVariant
val(value
);
622 m_rowmodified
= true;
623 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
627 void wxDbGridTableBase::SetValueAsLong(int row
, int col
, long value
)
629 wxLogDebug(wxT("SetValueAsLong() on %i,%i"),row
,col
);
632 wxVariant
val(value
);
634 m_rowmodified
= true;
635 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
639 void wxDbGridTableBase::SetValueAsDouble(int row
, int col
, double value
)
641 wxLogDebug(wxT("SetValueAsDouble() on %i,%i"),row
,col
);
644 wxVariant
val(value
);
646 m_rowmodified
= true;
647 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
652 void wxDbGridTableBase::SetValueAsBool(int row
, int col
, bool value
)
654 wxLogDebug(wxT("SetValueAsBool() on %i,%i"),row
,col
);
657 wxVariant
val(value
);
659 m_rowmodified
= true;
660 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
664 void wxDbGridTableBase::ValidateRow(int row
)
666 wxLogDebug(wxT("ValidateRow(%i) currently on row (%i). Array count = %i"),row
,m_row
,m_keys
.GetCount());
672 //We add to row as Count is unsigned!
673 if ((unsigned)(row
+1) > m_keys
.GetCount())
675 wxLogDebug(wxT("\trow key unknown"));
676 // Extend Array, iterate through data filling with keys
677 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_QUERY
);
679 for (trow
= m_keys
.GetCount(); trow
<= row
; trow
++)
681 wxLogDebug(wxT("Fetching row %i.."), trow
);
682 bool ret
= m_data
->GetNext();
684 wxLogDebug(wxT(" ...success=(%i)"),ret
);
685 GenericKey k
= m_data
->GetKey();
692 wxLogDebug(wxT("\trow key known centering data"));
693 GenericKey k
= m_keys
.Item(row
);
694 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_INDIVIDUAL
);
695 m_data
->ClearMemberVars();
697 if (!m_data
->QueryOnKeyFields())
699 wxDbLogExtendedErrorMsg("ODBC error during Query()\n\n", m_data
->GetDb(),__FILE__
,__LINE__
);
706 m_rowmodified
= false;
709 bool wxDbGridTableBase::Writeback() const
717 wxLogDebug(wxT("\trow key unknown"));
719 // FIXME: this code requires dbtable support for record status
721 switch (m_data
->get_ModifiedStatus())
723 case wxDbTable::UpdatePending
:
724 result
= m_data
->Update();
726 case wxDbTable::InsertPending
:
727 result
= (m_data
->Insert() == SQL_SUCCESS
);
734 wxLogDebug(wxT("WARNING : Row writeback not implemented "));
739 #include <wx/arrimpl.cpp>
741 WX_DEFINE_OBJARRAY(keyarray
);
743 #endif // #if wxUSE_NEW_GRID
744 #endif // #if wxUSE_ODBC