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 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 wxWindows
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 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
)
275 return wxGRID_VALUE_STRING
;
277 return wxGRID_VALUE_NUMBER
;
279 return wxGRID_VALUE_NUMBER
;
281 return wxGRID_VALUE_NUMBER
;
283 return wxGRID_VALUE_NUMBER
;
285 return wxGRID_VALUE_FLOAT
;
287 return wxGRID_VALUE_FLOAT
;
289 return wxGRID_VALUE_DATETIME
;
291 return wxGRID_VALUE_DATETIME
;
292 case SQL_C_TIMESTAMP
:
293 return wxGRID_VALUE_DATETIME
;
295 return wxGRID_VALUE_STRING
;
300 return m_ColInfo
[col
].wxtypename
;
303 wxFAIL_MSG (_T("unknown column"));
307 bool wxDbGridTableBase::CanGetValueAs(int row
, int col
, const wxString
& typeName
)
309 wxLogDebug(wxT("CanGetValueAs() on %i,%i"),row
,col
);
310 //Is this needed? As it will be validated on GetValueAsXXXX
313 if (typeName
== wxGRID_VALUE_STRING
)
315 //FIXME ummm What about blob field etc.
319 if (m_data
->IsColNull(m_ColInfo
[col
].DbCol
))
324 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
326 //If a virtual column then we can't find it's type. we have to
327 // return FALSE to get using wxVariant.
330 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
332 if (typeName
== wxGRID_VALUE_DATETIME
)
334 if ((sqltype
== SQL_C_DATE
) ||
335 (sqltype
== SQL_C_TIME
) ||
336 (sqltype
== SQL_C_TIMESTAMP
))
342 if (typeName
== wxGRID_VALUE_NUMBER
)
344 if ((sqltype
== SQL_C_SSHORT
) ||
345 (sqltype
== SQL_C_USHORT
) ||
346 (sqltype
== SQL_C_SLONG
) ||
347 (sqltype
== SQL_C_ULONG
))
353 if (typeName
== wxGRID_VALUE_FLOAT
)
355 if ((sqltype
== SQL_C_SSHORT
) ||
356 (sqltype
== SQL_C_USHORT
) ||
357 (sqltype
== SQL_C_SLONG
) ||
358 (sqltype
== SQL_C_ULONG
) ||
359 (sqltype
== SQL_C_FLOAT
) ||
360 (sqltype
== SQL_C_DOUBLE
))
369 bool wxDbGridTableBase::CanSetValueAs(int row
, int col
, const wxString
& typeName
)
371 if (typeName
== wxGRID_VALUE_STRING
)
373 //FIXME ummm What about blob field etc.
377 if (!(m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].Updateable
))
382 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
384 //If a virtual column then we can't find it's type. we have to faulse to
385 //get using wxVairent.
389 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
390 if (typeName
== wxGRID_VALUE_DATETIME
)
392 if ((sqltype
== SQL_C_DATE
) ||
393 (sqltype
== SQL_C_TIME
) ||
394 (sqltype
== SQL_C_TIMESTAMP
))
400 if (typeName
== wxGRID_VALUE_NUMBER
)
402 if ((sqltype
== SQL_C_SSHORT
) ||
403 (sqltype
== SQL_C_USHORT
) ||
404 (sqltype
== SQL_C_SLONG
) ||
405 (sqltype
== SQL_C_ULONG
))
411 if (typeName
== wxGRID_VALUE_FLOAT
)
413 if ((sqltype
== SQL_C_SSHORT
) ||
414 (sqltype
== SQL_C_USHORT
) ||
415 (sqltype
== SQL_C_SLONG
) ||
416 (sqltype
== SQL_C_ULONG
) ||
417 (sqltype
== SQL_C_FLOAT
) ||
418 (sqltype
== SQL_C_DOUBLE
))
427 long wxDbGridTableBase::GetValueAsLong(int row
, int col
)
431 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
433 wxFAIL_MSG (_T("You can not use GetValueAsLong for virtual columns"));
436 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
437 if ((sqltype
== SQL_C_SSHORT
) ||
438 (sqltype
== SQL_C_USHORT
) ||
439 (sqltype
== SQL_C_SLONG
) ||
440 (sqltype
== SQL_C_ULONG
))
442 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
443 return val
.GetLong();
445 wxFAIL_MSG (_T("unknown column, "));
449 double wxDbGridTableBase::GetValueAsDouble(int row
, int col
)
451 wxLogDebug(wxT("GetValueAsDouble() on %i,%i"),row
,col
);
454 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
456 wxFAIL_MSG (_T("You can not use GetValueAsDouble for virtual columns"));
459 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
460 if ((sqltype
== SQL_C_SSHORT
) ||
461 (sqltype
== SQL_C_USHORT
) ||
462 (sqltype
== SQL_C_SLONG
) ||
463 (sqltype
== SQL_C_ULONG
) ||
464 (sqltype
== SQL_C_FLOAT
) ||
465 (sqltype
== SQL_C_DOUBLE
))
467 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
468 return val
.GetDouble();
470 wxFAIL_MSG (_T("unknown column"));
474 bool wxDbGridTableBase::GetValueAsBool(int row
, int col
)
476 wxLogDebug(wxT("GetValueAsBool() on %i,%i"),row
,col
);
479 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
481 wxFAIL_MSG (_T("You can not use GetValueAsBool for virtual columns"));
484 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
485 if ((sqltype
== SQL_C_SSHORT
) ||
486 (sqltype
== SQL_C_USHORT
) ||
487 (sqltype
== SQL_C_SLONG
) ||
488 (sqltype
== SQL_C_ULONG
))
490 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
491 return val
.GetBool();
493 wxFAIL_MSG (_T("unknown column, "));
497 void* wxDbGridTableBase::GetValueAsCustom(int row
, int col
, const wxString
& typeName
)
499 wxLogDebug(wxT("GetValueAsCustom() on %i,%i"),row
,col
);
502 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
504 wxFAIL_MSG (_T("You can not use GetValueAsCustom for virtual columns"));
507 if (m_data
->IsColNull(m_ColInfo
[col
].DbCol
))
510 if (typeName
== wxGRID_VALUE_DATETIME
)
512 wxDbColDef
*pColDefs
= m_data
->GetColDefs();
513 int sqltype
= pColDefs
[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
515 if ((sqltype
== SQL_C_DATE
) ||
516 (sqltype
== SQL_C_TIME
) ||
517 (sqltype
== SQL_C_TIMESTAMP
))
519 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
520 return new wxDateTime(val
.GetDateTime());
523 wxFAIL_MSG (_T("unknown column data type "));
528 void wxDbGridTableBase::SetValueAsCustom(int row
, int col
, const wxString
& typeName
, void* value
)
530 wxLogDebug(wxT("SetValueAsCustom() on %i,%i"),row
,col
);
533 if (m_data
->GetNumberOfColumns() <= m_ColInfo
[col
].DbCol
)
535 wxFAIL_MSG (_T("You can not use SetValueAsCustom for virtual columns"));
539 if (typeName
== wxGRID_VALUE_DATETIME
)
541 int sqltype
= m_data
->GetColDefs()[(m_ColInfo
[col
].DbCol
)].SqlCtype
;
542 if ((sqltype
== SQL_C_DATE
) ||
543 (sqltype
== SQL_C_TIME
) ||
544 (sqltype
== SQL_C_TIMESTAMP
))
546 //FIXME: you can't dynamic_cast from (void *)
547 //wxDateTime *date = wxDynamicCast(value, wxDateTime);
548 wxDateTime
*date
= (wxDateTime
*)value
;
551 wxFAIL_MSG (_T("Failed to convert data"));
555 m_rowmodified
= TRUE
;
556 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
559 wxFAIL_MSG (_T("unknown column data type"));
564 wxString
wxDbGridTableBase::GetColLabelValue(int col
)
566 if (GetNumberCols() > col
)
568 return m_ColInfo
[col
].Title
;
570 wxFAIL_MSG (_T("unknown column"));
574 bool wxDbGridTableBase::IsEmptyCell(int row
, int col
)
576 wxLogDebug(wxT("IsEmtpyCell on %i,%i"),row
,col
);
579 return m_data
->IsColNull(m_ColInfo
[col
].DbCol
);
583 wxString
wxDbGridTableBase::GetValue(int row
, int col
)
585 wxLogDebug(wxT("GetValue() on %i,%i"),row
,col
);
588 wxVariant val
= m_data
->GetCol(m_ColInfo
[col
].DbCol
);
589 wxLogDebug(wxT("\tReturning \"%s\"\n"),val
.GetString().c_str());
591 return val
.GetString();
595 void wxDbGridTableBase::SetValue(int row
, int col
,const wxString
& value
)
597 wxLogDebug(wxT("SetValue() on %i,%i"),row
,col
);
600 wxVariant
val(value
);
602 m_rowmodified
= TRUE
;
603 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
607 void wxDbGridTableBase::SetValueAsLong(int row
, int col
, long value
)
609 wxLogDebug(wxT("SetValueAsLong() on %i,%i"),row
,col
);
612 wxVariant
val(value
);
614 m_rowmodified
= TRUE
;
615 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
619 void wxDbGridTableBase::SetValueAsDouble(int row
, int col
, double value
)
621 wxLogDebug(wxT("SetValueAsDouble() on %i,%i"),row
,col
);
624 wxVariant
val(value
);
626 m_rowmodified
= TRUE
;
627 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
632 void wxDbGridTableBase::SetValueAsBool(int row
, int col
, bool value
)
634 wxLogDebug(wxT("SetValueAsBool() on %i,%i"),row
,col
);
637 wxVariant
val(value
);
639 m_rowmodified
= TRUE
;
640 m_data
->SetCol(m_ColInfo
[col
].DbCol
,val
);
644 void wxDbGridTableBase::ValidateRow(int row
)
646 wxLogDebug(wxT("ValidateRow(%i) currently on row (%i). Array count = %i"),row
,m_row
,m_keys
.GetCount());
652 //We add to row as Count is unsigned!
653 if ((unsigned)(row
+1) > m_keys
.GetCount())
655 wxLogDebug(wxT("\trow key unknown"));
656 // Extend Array, iterate through data filling with keys
657 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_QUERY
);
659 for (trow
= m_keys
.GetCount(); trow
<= row
; trow
++)
661 wxLogDebug(wxT("Fetching row %i.."), trow
);
662 bool ret
= m_data
->GetNext();
664 wxLogDebug(wxT(" ...success=(%i)"),ret
);
665 GenericKey k
= m_data
->GetKey();
672 wxLogDebug(wxT("\trow key known centering data"));
673 GenericKey k
= m_keys
.Item(row
);
674 m_data
->SetRowMode(wxDbTable::WX_ROW_MODE_INDIVIDUAL
);
675 m_data
->ClearMemberVars();
677 if (!m_data
->QueryOnKeyFields())
679 wxDbLogExtendedErrorMsg("ODBC error during Query()\n\n", m_data
->GetDb(),__FILE__
,__LINE__
);
686 m_rowmodified
= FALSE
;
689 bool wxDbGridTableBase::Writeback() const
697 wxLogDebug(wxT("\trow key unknown"));
699 // FIXME: this code requires dbtable support for record status
701 switch (m_data
->get_ModifiedStatus())
703 case wxDbTable::UpdatePending
:
704 result
= m_data
->Update();
706 case wxDbTable::InsertPending
:
707 result
= (m_data
->Insert() == SQL_SUCCESS
);
714 wxLogDebug(wxT("WARNING : Row writeback not implemented "));
719 #include "wx/arrimpl.cpp"
721 WX_DEFINE_EXPORTED_OBJARRAY(keyarray
);
723 #endif // #if wxUSE_NEW_GRID
724 #endif // #if wxUSE_ODBC