]>
Commit | Line | Data |
---|---|---|
32a2907b GT |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: dbgrid.cpp | |
3 | // Purpose: Displays a wxDbTable in a wxGrid. | |
4 | // Author: Roger Gammans, Paul Gammans | |
eae630f1 DW |
5 | // Modified by: |
6 | // Created: | |
32a2907b GT |
7 | // RCS-ID: $Id$ |
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 | /////////////////////////////////////////////////////////////////////////////// | |
13 | ||
14 | #ifdef __GNUG__ | |
15 | #pragma implementation "dbgrid.h" | |
16 | #endif | |
17 | ||
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | ||
25 | #if wxUSE_ODBC | |
26 | #if wxUSE_NEW_GRID | |
27 | ||
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/textctrl.h" | |
30 | #include "wx/dc.h" | |
31 | #endif // WX_PRECOMP | |
32 | ||
33 | #include "wx/generic/gridctrl.h" | |
34 | #include "wx/dbgrid.h" | |
35 | ||
36 | ||
37 | wxDbGridCellAttrProvider::wxDbGridCellAttrProvider() | |
38 | { | |
39 | m_data=NULL; | |
40 | m_ColInfo=NULL; | |
41 | } | |
42 | ||
43 | wxDbGridCellAttrProvider::wxDbGridCellAttrProvider(wxDbTable *tab, wxDbGridColInfoBase* ColInfo) | |
44 | { | |
45 | m_data=tab; | |
46 | m_ColInfo=ColInfo; | |
47 | } | |
48 | ||
49 | wxDbGridCellAttrProvider::~wxDbGridCellAttrProvider() | |
50 | { | |
51 | } | |
52 | ||
53 | wxGridCellAttr *wxDbGridCellAttrProvider::GetAttr(int row, int col, | |
54 | wxGridCellAttr::wxAttrKind kind) const | |
55 | { | |
56 | wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row,col,kind); | |
57 | ||
58 | if (m_data && m_ColInfo && (m_data->GetNumberOfColumns() > m_ColInfo[col].DbCol)) | |
59 | { | |
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)) | |
64 | { | |
65 | switch(kind) | |
66 | { | |
67 | case (wxGridCellAttr::Any): | |
68 | if (!attr) | |
69 | { | |
70 | attr = new wxGridCellAttr; | |
71 | // Store so we don't keep creating / deleting this... | |
72 | wxDbGridCellAttrProvider * self = wxConstCast(this, wxDbGridCellAttrProvider) ; | |
73 | attr->IncRef(); | |
74 | self->SetColAttr(attr, col); | |
75 | attr->SetReadOnly(); | |
76 | } | |
77 | else | |
78 | { | |
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))) | |
83 | { | |
84 | wxGridCellAttr *attrtomerge = attr; | |
85 | attr = new wxGridCellAttr; | |
86 | attr->SetKind(wxGridCellAttr::Merged); | |
87 | attr->MergeWith(attrtomerge); | |
88 | attr->SetReadOnly(); | |
89 | attrtomerge->DecRef(); | |
90 | } | |
91 | attr->SetReadOnly(); | |
92 | } | |
93 | break; | |
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 | |
97 | if (!attr) | |
98 | { | |
99 | attr = new wxGridCellAttr; | |
100 | wxDbGridCellAttrProvider * self = wxConstCast(this, wxDbGridCellAttrProvider) ; | |
101 | attr->IncRef(); | |
102 | self->SetColAttr(attr, col); | |
103 | } | |
104 | attr->SetReadOnly(); | |
105 | break; | |
106 | default: | |
107 | //Dont add RO for... | |
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. | |
113 | break; | |
114 | } | |
115 | } | |
eae630f1 | 116 | |
32a2907b GT |
117 | } |
118 | return attr; | |
119 | } | |
120 | ||
121 | void wxDbGridCellAttrProvider::AssignDbTable(wxDbTable *tab) | |
122 | { | |
123 | m_data = tab; | |
124 | } | |
125 | ||
126 | wxDbGridTableBase::wxDbGridTableBase(wxDbTable *tab, wxDbGridColInfo* ColInfo, | |
127 | int count, bool takeOwnership) : | |
eae630f1 | 128 | m_keys(), |
32a2907b GT |
129 | m_data(tab), |
130 | m_dbowner(takeOwnership), | |
a144affe | 131 | m_rowmodified(FALSE) |
32a2907b GT |
132 | { |
133 | ||
134 | if (count == wxUSE_QUERY) | |
135 | { | |
136 | m_rowtotal = m_data ? m_data->Count() : 0; | |
137 | } | |
138 | else | |
139 | { | |
140 | m_rowtotal = count; | |
141 | } | |
142 | // m_keys.Size(m_rowtotal); | |
143 | m_row = -1; | |
144 | if (ColInfo) | |
145 | { | |
146 | m_nocols = ColInfo->Length(); | |
147 | m_ColInfo = new wxDbGridColInfoBase[m_nocols]; | |
148 | //Do Copy. | |
149 | wxDbGridColInfo *ptr = ColInfo; | |
150 | int i =0; | |
151 | while (ptr && i < m_nocols) | |
152 | { | |
153 | m_ColInfo[i] = ptr->m_data; | |
154 | ptr = ptr->m_next; | |
155 | i++; | |
156 | } | |
157 | #ifdef __WXDEBUG__ | |
158 | if (ptr) | |
159 | { | |
160 | wxLogDebug(wxT("NoCols over length after traversing %i items"),i); | |
161 | } | |
162 | if (i < m_nocols) | |
163 | { | |
164 | wxLogDebug(wxT("NoCols under length after traversing %i items"),i); | |
eae630f1 | 165 | } |
32a2907b GT |
166 | #endif |
167 | } | |
168 | } | |
169 | ||
170 | wxDbGridTableBase::~wxDbGridTableBase() | |
171 | { | |
172 | wxDbGridCellAttrProvider *provider; | |
173 | ||
eae630f1 | 174 | //Can't check for update here as |
32a2907b GT |
175 | |
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 | |
eae630f1 DW |
179 | provider = (wxDbGridCellAttrProvider *)(GetAttrProvider()); |
180 | if (provider) | |
32a2907b GT |
181 | { |
182 | provider->AssignDbTable(NULL); | |
183 | } | |
184 | delete [] m_ColInfo; | |
eae630f1 | 185 | |
32a2907b GT |
186 | Writeback(); |
187 | if (m_dbowner) | |
188 | { | |
189 | delete m_data; | |
190 | } | |
191 | } | |
192 | ||
193 | bool wxDbGridTableBase::CanHaveAttributes() | |
194 | { | |
195 | if (!GetAttrProvider()) | |
196 | { | |
197 | // use the default attr provider by default | |
198 | SetAttrProvider(new wxDbGridCellAttrProvider(m_data, m_ColInfo)); | |
199 | } | |
a144affe | 200 | return TRUE; |
32a2907b GT |
201 | } |
202 | ||
203 | ||
204 | bool wxDbGridTableBase::AssignDbTable(wxDbTable *tab, int count, bool takeOwnership) | |
205 | { | |
206 | wxDbGridCellAttrProvider *provider; | |
207 | ||
208 | //Remove Information from grid about old data | |
209 | if (GetView()) | |
210 | { | |
211 | wxGrid *grid = GetView(); | |
212 | grid->BeginBatch(); | |
213 | grid->ClearSelection(); | |
214 | if (grid->IsCellEditControlEnabled()) | |
215 | { | |
216 | grid->DisableCellEditControl(); | |
217 | } | |
218 | wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED,0,m_rowtotal); | |
219 | grid->ProcessTableMessage(msg); | |
220 | } | |
221 | ||
222 | //reset our internals... | |
223 | Writeback(); | |
224 | if (m_dbowner) | |
225 | { | |
226 | delete m_data; | |
227 | } | |
228 | m_keys.Empty(); | |
229 | m_data = tab; | |
eae630f1 | 230 | //FIXME: Remove dynamic_cast before sumision to wxwin |
32a2907b GT |
231 | // if ((provider = dynamic_cast<wxDbGridCellAttrProvider *> (GetAttrProvider()))) |
232 | // Using C casting for now until we can support dynamic_cast with wxWindows | |
eae630f1 DW |
233 | provider = (wxDbGridCellAttrProvider *)(GetAttrProvider()); |
234 | if (provider) | |
32a2907b GT |
235 | { |
236 | provider->AssignDbTable(m_data); | |
237 | } | |
eae630f1 | 238 | |
32a2907b GT |
239 | if (count == wxUSE_QUERY) |
240 | { | |
241 | m_rowtotal = m_data ? m_data->Count() : 0; | |
242 | } | |
243 | else | |
244 | { | |
245 | m_rowtotal = count; | |
246 | } | |
247 | m_row = -1; | |
248 | ||
249 | //Add Information to grid about new data | |
250 | if (GetView()) | |
251 | { | |
252 | wxGrid * grid = GetView(); | |
253 | wxGridTableMessage msg(this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_rowtotal); | |
254 | grid->ProcessTableMessage(msg); | |
255 | grid->EndBatch(); | |
256 | } | |
257 | m_dbowner = takeOwnership; | |
a144affe GT |
258 | m_rowmodified = FALSE; |
259 | return TRUE; | |
32a2907b GT |
260 | } |
261 | ||
262 | wxString wxDbGridTableBase::GetTypeName(int row, int col) | |
263 | { | |
32a2907b GT |
264 | if (GetNumberCols() > col) |
265 | { | |
266 | if (m_ColInfo[col].wxtypename == wxGRID_VALUE_DBAUTO) | |
267 | { | |
268 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
269 | { | |
270 | wxFAIL_MSG (_T("You can not use wxGRID_VALUE_DBAUTO for virtual columns")); | |
271 | } | |
272 | switch(m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype) | |
273 | { | |
274 | case SQL_C_CHAR: | |
275 | return wxGRID_VALUE_STRING; | |
32a2907b GT |
276 | case SQL_C_SSHORT: |
277 | return wxGRID_VALUE_NUMBER; | |
32a2907b GT |
278 | case SQL_C_USHORT: |
279 | return wxGRID_VALUE_NUMBER; | |
32a2907b GT |
280 | case SQL_C_SLONG: |
281 | return wxGRID_VALUE_NUMBER; | |
32a2907b GT |
282 | case SQL_C_ULONG: |
283 | return wxGRID_VALUE_NUMBER; | |
32a2907b GT |
284 | case SQL_C_FLOAT: |
285 | return wxGRID_VALUE_FLOAT; | |
32a2907b GT |
286 | case SQL_C_DOUBLE: |
287 | return wxGRID_VALUE_FLOAT; | |
32a2907b GT |
288 | case SQL_C_DATE: |
289 | return wxGRID_VALUE_DATETIME; | |
32a2907b GT |
290 | case SQL_C_TIME: |
291 | return wxGRID_VALUE_DATETIME; | |
32a2907b GT |
292 | case SQL_C_TIMESTAMP: |
293 | return wxGRID_VALUE_DATETIME; | |
32a2907b GT |
294 | default: |
295 | return wxGRID_VALUE_STRING; | |
32a2907b GT |
296 | } |
297 | } | |
298 | else | |
299 | { | |
300 | return m_ColInfo[col].wxtypename; | |
301 | } | |
302 | } | |
303 | wxFAIL_MSG (_T("unknown column")); | |
304 | return wxString(); | |
305 | } | |
306 | ||
307 | bool wxDbGridTableBase::CanGetValueAs(int row, int col, const wxString& typeName) | |
308 | { | |
309 | wxLogDebug(wxT("CanGetValueAs() on %i,%i"),row,col); | |
310 | //Is this needed? As it will be validated on GetValueAsXXXX | |
311 | ValidateRow(row); | |
312 | ||
313 | if (typeName == wxGRID_VALUE_STRING) | |
314 | { | |
315 | //FIXME ummm What about blob field etc. | |
a144affe | 316 | return TRUE; |
32a2907b GT |
317 | } |
318 | ||
319 | if (m_data->IsColNull(m_ColInfo[col].DbCol)) | |
320 | { | |
a144affe | 321 | return FALSE; |
32a2907b GT |
322 | } |
323 | ||
324 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
325 | { | |
eae630f1 | 326 | //If a virtual column then we can't find it's type. we have to |
a144affe GT |
327 | // return FALSE to get using wxVariant. |
328 | return FALSE; | |
32a2907b GT |
329 | } |
330 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
eae630f1 | 331 | |
32a2907b GT |
332 | if (typeName == wxGRID_VALUE_DATETIME) |
333 | { | |
334 | if ((sqltype == SQL_C_DATE) || | |
eae630f1 | 335 | (sqltype == SQL_C_TIME) || |
32a2907b GT |
336 | (sqltype == SQL_C_TIMESTAMP)) |
337 | { | |
a144affe | 338 | return TRUE; |
32a2907b | 339 | } |
a144affe | 340 | return FALSE; |
32a2907b GT |
341 | } |
342 | if (typeName == wxGRID_VALUE_NUMBER) | |
343 | { | |
344 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 345 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
346 | (sqltype == SQL_C_SLONG) || |
347 | (sqltype == SQL_C_ULONG)) | |
348 | { | |
a144affe | 349 | return TRUE; |
32a2907b | 350 | } |
a144affe | 351 | return FALSE; |
32a2907b GT |
352 | } |
353 | if (typeName == wxGRID_VALUE_FLOAT) | |
354 | { | |
355 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 356 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
357 | (sqltype == SQL_C_SLONG) || |
358 | (sqltype == SQL_C_ULONG) || | |
359 | (sqltype == SQL_C_FLOAT) || | |
360 | (sqltype == SQL_C_DOUBLE)) | |
361 | { | |
a144affe | 362 | return TRUE; |
32a2907b | 363 | } |
a144affe | 364 | return FALSE; |
32a2907b | 365 | } |
a144affe | 366 | return FALSE; |
32a2907b GT |
367 | } |
368 | ||
369 | bool wxDbGridTableBase::CanSetValueAs(int row, int col, const wxString& typeName) | |
370 | { | |
371 | //Is this needed? As will be validated on SetValueAsXXXX | |
372 | ValidateRow(row); | |
373 | ||
374 | if (m_data->IsColNull(m_ColInfo[col].DbCol)) | |
375 | { | |
a144affe | 376 | return FALSE; |
32a2907b GT |
377 | } |
378 | ||
379 | if (typeName == wxGRID_VALUE_STRING) | |
380 | { | |
381 | //FIXME ummm What about blob field etc. | |
a144affe | 382 | return TRUE; |
32a2907b GT |
383 | } |
384 | ||
385 | if (!(m_data->GetColDefs()[(m_ColInfo[col].DbCol)].Updateable)) | |
386 | { | |
a144affe | 387 | return FALSE; |
32a2907b GT |
388 | } |
389 | ||
390 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
391 | { | |
eae630f1 | 392 | //If a virtual column then we can't find it's type. we have to faulse to |
32a2907b | 393 | //get using wxVairent. |
a144affe | 394 | return FALSE; |
32a2907b GT |
395 | } |
396 | ||
397 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
398 | if (typeName == wxGRID_VALUE_DATETIME) | |
399 | { | |
400 | if ((sqltype == SQL_C_DATE) || | |
eae630f1 | 401 | (sqltype == SQL_C_TIME) || |
32a2907b GT |
402 | (sqltype == SQL_C_TIMESTAMP)) |
403 | { | |
a144affe | 404 | return TRUE; |
32a2907b | 405 | } |
a144affe | 406 | return FALSE; |
32a2907b GT |
407 | } |
408 | if (typeName == wxGRID_VALUE_NUMBER) | |
409 | { | |
410 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 411 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
412 | (sqltype == SQL_C_SLONG) || |
413 | (sqltype == SQL_C_ULONG)) | |
414 | { | |
a144affe | 415 | return TRUE; |
32a2907b | 416 | } |
a144affe | 417 | return FALSE; |
32a2907b GT |
418 | } |
419 | if (typeName == wxGRID_VALUE_FLOAT) | |
420 | { | |
421 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 422 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
423 | (sqltype == SQL_C_SLONG) || |
424 | (sqltype == SQL_C_ULONG) || | |
425 | (sqltype == SQL_C_FLOAT) || | |
426 | (sqltype == SQL_C_DOUBLE)) | |
427 | { | |
a144affe | 428 | return TRUE; |
32a2907b | 429 | } |
a144affe | 430 | return FALSE; |
32a2907b | 431 | } |
a144affe | 432 | return FALSE; |
32a2907b GT |
433 | } |
434 | ||
435 | long wxDbGridTableBase::GetValueAsLong(int row, int col) | |
436 | { | |
437 | ValidateRow(row); | |
438 | ||
439 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
440 | { | |
441 | wxFAIL_MSG (_T("You can not use GetValueAsLong for virtual columns")); | |
442 | return 0; | |
443 | } | |
444 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
445 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 446 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
447 | (sqltype == SQL_C_SLONG) || |
448 | (sqltype == SQL_C_ULONG)) | |
449 | { | |
450 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
451 | return val.GetLong(); | |
452 | } | |
453 | wxFAIL_MSG (_T("unknown column, ")); | |
454 | return 0; | |
455 | } | |
456 | ||
457 | double wxDbGridTableBase::GetValueAsDouble(int row, int col) | |
458 | { | |
459 | wxLogDebug(wxT("GetValueAsDouble() on %i,%i"),row,col); | |
460 | ValidateRow(row); | |
461 | ||
462 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
463 | { | |
464 | wxFAIL_MSG (_T("You can not use GetValueAsDouble for virtual columns")); | |
465 | return 0.0; | |
466 | } | |
467 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
468 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 469 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
470 | (sqltype == SQL_C_SLONG) || |
471 | (sqltype == SQL_C_ULONG) || | |
472 | (sqltype == SQL_C_FLOAT) || | |
473 | (sqltype == SQL_C_DOUBLE)) | |
474 | { | |
475 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
476 | return val.GetDouble(); | |
477 | } | |
478 | wxFAIL_MSG (_T("unknown column")); | |
479 | return 0.0; | |
480 | } | |
481 | ||
482 | bool wxDbGridTableBase::GetValueAsBool(int row, int col) | |
483 | { | |
484 | wxLogDebug(wxT("GetValueAsBool() on %i,%i"),row,col); | |
485 | ValidateRow(row); | |
486 | ||
487 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
488 | { | |
489 | wxFAIL_MSG (_T("You can not use GetValueAsBool for virtual columns")); | |
490 | return 0; | |
491 | } | |
492 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
493 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 494 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
495 | (sqltype == SQL_C_SLONG) || |
496 | (sqltype == SQL_C_ULONG)) | |
497 | { | |
498 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
499 | return val.GetBool(); | |
500 | } | |
501 | wxFAIL_MSG (_T("unknown column, ")); | |
502 | return 0; | |
503 | } | |
504 | ||
505 | void* wxDbGridTableBase::GetValueAsCustom(int row, int col, const wxString& typeName) | |
506 | { | |
507 | wxLogDebug(wxT("GetValueAsCustom() on %i,%i"),row,col); | |
508 | ValidateRow(row); | |
eae630f1 | 509 | |
32a2907b GT |
510 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) |
511 | { | |
512 | wxFAIL_MSG (_T("You can not use GetValueAsCustom for virtual columns")); | |
513 | return NULL; | |
514 | } | |
515 | if (m_data->IsColNull(m_ColInfo[col].DbCol)) | |
516 | return NULL; | |
517 | ||
518 | if (typeName == wxGRID_VALUE_DATETIME) | |
519 | { | |
520 | wxDbColDef *pColDefs = m_data->GetColDefs(); | |
521 | int sqltype = pColDefs[(m_ColInfo[col].DbCol)].SqlCtype; | |
522 | ||
523 | if ((sqltype == SQL_C_DATE) || | |
524 | (sqltype == SQL_C_TIME) || | |
525 | (sqltype == SQL_C_TIMESTAMP)) | |
526 | { | |
527 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
528 | return new wxDateTime(val.GetDateTime()); | |
529 | } | |
530 | } | |
531 | wxFAIL_MSG (_T("unknown column data type ")); | |
532 | return NULL; | |
533 | } | |
534 | ||
535 | ||
536 | void wxDbGridTableBase::SetValueAsCustom(int row, int col, const wxString& typeName, void* value) | |
537 | { | |
538 | wxLogDebug(wxT("SetValueAsCustom() on %i,%i"),row,col); | |
539 | ValidateRow(row); | |
540 | ||
541 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
542 | { | |
543 | wxFAIL_MSG (_T("You can not use SetValueAsCustom for virtual columns")); | |
544 | return; | |
545 | } | |
546 | ||
547 | if (typeName == wxGRID_VALUE_DATETIME) | |
548 | { | |
549 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
550 | if ((sqltype == SQL_C_DATE) || | |
551 | (sqltype == SQL_C_TIME) || | |
552 | (sqltype == SQL_C_TIMESTAMP)) | |
553 | { | |
554 | //FIXME: you can't dynamic_cast from (void *) | |
555 | //wxDateTime *date = wxDynamicCast(value, wxDateTime); | |
556 | wxDateTime *date = (wxDateTime *)value; | |
557 | if (!date) | |
558 | { | |
559 | wxFAIL_MSG (_T("Failed to convert data")); | |
560 | return; | |
561 | } | |
562 | wxVariant val(date); | |
a144affe | 563 | m_rowmodified = TRUE; |
32a2907b GT |
564 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
565 | } | |
566 | } | |
567 | wxFAIL_MSG (_T("unknown column data type")); | |
568 | return ; | |
569 | } | |
570 | ||
571 | ||
572 | wxString wxDbGridTableBase::GetColLabelValue(int col) | |
573 | { | |
574 | if (GetNumberCols() > col) | |
575 | { | |
576 | return m_ColInfo[col].Title; | |
577 | } | |
578 | wxFAIL_MSG (_T("unknown column")); | |
579 | return wxString(); | |
580 | } | |
581 | ||
582 | bool wxDbGridTableBase::IsEmptyCell(int row, int col) | |
583 | { | |
584 | wxLogDebug(wxT("IsEmtpyCell on %i,%i"),row,col); | |
585 | ||
586 | ValidateRow(row); | |
587 | return m_data->IsColNull(m_ColInfo[col].DbCol); | |
588 | } | |
589 | ||
590 | ||
591 | wxString wxDbGridTableBase::GetValue(int row, int col) | |
592 | { | |
593 | wxLogDebug(wxT("GetValue() on %i,%i"),row,col); | |
eae630f1 | 594 | |
32a2907b GT |
595 | ValidateRow(row); |
596 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
597 | wxLogDebug(wxT("\tReturning \"%s\"\n"),val.GetString().c_str()); | |
eae630f1 DW |
598 | |
599 | return val.GetString(); | |
32a2907b GT |
600 | } |
601 | ||
602 | ||
603 | void wxDbGridTableBase::SetValue(int row, int col,const wxString& value) | |
604 | { | |
605 | wxLogDebug(wxT("SetValue() on %i,%i"),row,col); | |
606 | ||
607 | ValidateRow(row); | |
608 | wxVariant val(value); | |
609 | ||
a144affe | 610 | m_rowmodified = TRUE; |
32a2907b GT |
611 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
612 | } | |
613 | ||
614 | ||
615 | void wxDbGridTableBase::SetValueAsLong(int row, int col, long value) | |
616 | { | |
617 | wxLogDebug(wxT("SetValueAsLong() on %i,%i"),row,col); | |
618 | ||
619 | ValidateRow(row); | |
620 | wxVariant val(value); | |
621 | ||
a144affe | 622 | m_rowmodified = TRUE; |
32a2907b GT |
623 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
624 | } | |
625 | ||
626 | ||
627 | void wxDbGridTableBase::SetValueAsDouble(int row, int col, double value) | |
628 | { | |
629 | wxLogDebug(wxT("SetValueAsDouble() on %i,%i"),row,col); | |
630 | ||
631 | ValidateRow(row); | |
632 | wxVariant val(value); | |
633 | ||
a144affe | 634 | m_rowmodified = TRUE; |
32a2907b GT |
635 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
636 | ||
637 | } | |
638 | ||
639 | ||
640 | void wxDbGridTableBase::SetValueAsBool(int row, int col, bool value) | |
641 | { | |
642 | wxLogDebug(wxT("SetValueAsBool() on %i,%i"),row,col); | |
643 | ||
644 | ValidateRow(row); | |
645 | wxVariant val(value); | |
646 | ||
a144affe | 647 | m_rowmodified = TRUE; |
32a2907b GT |
648 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
649 | } | |
650 | ||
651 | ||
652 | void wxDbGridTableBase::ValidateRow(int row) | |
653 | { | |
654 | wxLogDebug(wxT("ValidateRow(%i) currently on row (%i). Array count = %i"),row,m_row,m_keys.GetCount()); | |
655 | ||
656 | if (row == m_row) | |
657 | return; | |
658 | Writeback(); | |
659 | ||
660 | //We add to row as Count is unsigned! | |
661 | if ((unsigned)(row+1) > m_keys.GetCount()) | |
662 | { | |
663 | wxLogDebug(wxT("\trow key unknown")); | |
664 | // Extend Array, iterate through data filling with keys | |
665 | m_data->SetRowMode(wxDbTable::WX_ROW_MODE_QUERY); | |
666 | int trow; | |
667 | for (trow = m_keys.GetCount(); trow <= row; trow++) | |
668 | { | |
669 | wxLogDebug(wxT("Fetching row %i.."), trow); | |
670 | bool ret = m_data->GetNext(); | |
671 | ||
672 | wxLogDebug(wxT(" ...success=(%i)"),ret); | |
673 | GenericKey k = m_data->GetKey(); | |
674 | m_keys.Add(k); | |
675 | } | |
676 | m_row = row; | |
677 | } | |
678 | else | |
679 | { | |
680 | wxLogDebug(wxT("\trow key known centering data")); | |
681 | GenericKey k = m_keys.Item(row); | |
682 | m_data->SetRowMode(wxDbTable::WX_ROW_MODE_INDIVIDUAL); | |
683 | m_data->ClearMemberVars(); | |
684 | m_data->SetKey(k); | |
685 | if (!m_data->QueryOnKeyFields()) | |
686 | { | |
687 | wxDbLogExtendedErrorMsg("ODBC error during Query()\n\n", m_data->GetDb(),__FILE__,__LINE__); | |
688 | } | |
689 | ||
690 | m_data->GetNext(); | |
691 | ||
692 | m_row = row; | |
693 | } | |
a144affe | 694 | m_rowmodified = FALSE; |
32a2907b GT |
695 | } |
696 | ||
697 | bool wxDbGridTableBase::Writeback() const | |
698 | { | |
699 | if (!m_rowmodified) | |
700 | { | |
a144affe | 701 | return TRUE; |
32a2907b GT |
702 | } |
703 | ||
a144affe | 704 | bool result=TRUE; |
32a2907b GT |
705 | wxLogDebug(wxT("\trow key unknown")); |
706 | ||
eae630f1 | 707 | // FIXME: this code requires dbtable support for record status |
32a2907b GT |
708 | #if 0 |
709 | switch (m_data->get_ModifiedStatus()) | |
710 | { | |
711 | case wxDbTable::UpdatePending: | |
712 | result = m_data->Update(); | |
713 | break; | |
714 | case wxDbTable::InsertPending: | |
715 | result = (m_data->Insert() == SQL_SUCCESS); | |
716 | break; | |
717 | default: | |
718 | //Nothing | |
719 | break; | |
720 | } | |
721 | #else | |
722 | wxLogDebug(wxT("WARNING : Row writeback not implemented ")); | |
723 | #endif | |
724 | return result; | |
725 | } | |
726 | ||
4498031c | 727 | #include "wx/arrimpl.cpp" |
32a2907b | 728 | |
12353a27 | 729 | WX_DEFINE_EXPORTED_OBJARRAY(keyarray); |
32a2907b GT |
730 | |
731 | #endif // #if wxUSE_NEW_GRID | |
732 | #endif // #if wxUSE_ODBC | |
733 |