]>
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 | { | |
264 | if (col == 8) | |
265 | int breakpoint = 1; | |
266 | ||
267 | if (GetNumberCols() > col) | |
268 | { | |
269 | if (m_ColInfo[col].wxtypename == wxGRID_VALUE_DBAUTO) | |
270 | { | |
271 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
272 | { | |
273 | wxFAIL_MSG (_T("You can not use wxGRID_VALUE_DBAUTO for virtual columns")); | |
274 | } | |
275 | switch(m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype) | |
276 | { | |
277 | case SQL_C_CHAR: | |
278 | return wxGRID_VALUE_STRING; | |
32a2907b GT |
279 | case SQL_C_SSHORT: |
280 | return wxGRID_VALUE_NUMBER; | |
32a2907b GT |
281 | case SQL_C_USHORT: |
282 | return wxGRID_VALUE_NUMBER; | |
32a2907b GT |
283 | case SQL_C_SLONG: |
284 | return wxGRID_VALUE_NUMBER; | |
32a2907b GT |
285 | case SQL_C_ULONG: |
286 | return wxGRID_VALUE_NUMBER; | |
32a2907b GT |
287 | case SQL_C_FLOAT: |
288 | return wxGRID_VALUE_FLOAT; | |
32a2907b GT |
289 | case SQL_C_DOUBLE: |
290 | return wxGRID_VALUE_FLOAT; | |
32a2907b GT |
291 | case SQL_C_DATE: |
292 | return wxGRID_VALUE_DATETIME; | |
32a2907b GT |
293 | case SQL_C_TIME: |
294 | return wxGRID_VALUE_DATETIME; | |
32a2907b GT |
295 | case SQL_C_TIMESTAMP: |
296 | return wxGRID_VALUE_DATETIME; | |
32a2907b GT |
297 | default: |
298 | return wxGRID_VALUE_STRING; | |
32a2907b GT |
299 | } |
300 | } | |
301 | else | |
302 | { | |
303 | return m_ColInfo[col].wxtypename; | |
304 | } | |
305 | } | |
306 | wxFAIL_MSG (_T("unknown column")); | |
307 | return wxString(); | |
308 | } | |
309 | ||
310 | bool wxDbGridTableBase::CanGetValueAs(int row, int col, const wxString& typeName) | |
311 | { | |
312 | wxLogDebug(wxT("CanGetValueAs() on %i,%i"),row,col); | |
313 | //Is this needed? As it will be validated on GetValueAsXXXX | |
314 | ValidateRow(row); | |
315 | ||
316 | if (typeName == wxGRID_VALUE_STRING) | |
317 | { | |
318 | //FIXME ummm What about blob field etc. | |
a144affe | 319 | return TRUE; |
32a2907b GT |
320 | } |
321 | ||
322 | if (m_data->IsColNull(m_ColInfo[col].DbCol)) | |
323 | { | |
a144affe | 324 | return FALSE; |
32a2907b GT |
325 | } |
326 | ||
327 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
328 | { | |
eae630f1 | 329 | //If a virtual column then we can't find it's type. we have to |
a144affe GT |
330 | // return FALSE to get using wxVariant. |
331 | return FALSE; | |
32a2907b GT |
332 | } |
333 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
eae630f1 | 334 | |
32a2907b GT |
335 | if (typeName == wxGRID_VALUE_DATETIME) |
336 | { | |
337 | if ((sqltype == SQL_C_DATE) || | |
eae630f1 | 338 | (sqltype == SQL_C_TIME) || |
32a2907b GT |
339 | (sqltype == SQL_C_TIMESTAMP)) |
340 | { | |
a144affe | 341 | return TRUE; |
32a2907b | 342 | } |
a144affe | 343 | return FALSE; |
32a2907b GT |
344 | } |
345 | if (typeName == wxGRID_VALUE_NUMBER) | |
346 | { | |
347 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 348 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
349 | (sqltype == SQL_C_SLONG) || |
350 | (sqltype == SQL_C_ULONG)) | |
351 | { | |
a144affe | 352 | return TRUE; |
32a2907b | 353 | } |
a144affe | 354 | return FALSE; |
32a2907b GT |
355 | } |
356 | if (typeName == wxGRID_VALUE_FLOAT) | |
357 | { | |
358 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 359 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
360 | (sqltype == SQL_C_SLONG) || |
361 | (sqltype == SQL_C_ULONG) || | |
362 | (sqltype == SQL_C_FLOAT) || | |
363 | (sqltype == SQL_C_DOUBLE)) | |
364 | { | |
a144affe | 365 | return TRUE; |
32a2907b | 366 | } |
a144affe | 367 | return FALSE; |
32a2907b | 368 | } |
a144affe | 369 | return FALSE; |
32a2907b GT |
370 | } |
371 | ||
372 | bool wxDbGridTableBase::CanSetValueAs(int row, int col, const wxString& typeName) | |
373 | { | |
374 | //Is this needed? As will be validated on SetValueAsXXXX | |
375 | ValidateRow(row); | |
376 | ||
377 | if (m_data->IsColNull(m_ColInfo[col].DbCol)) | |
378 | { | |
a144affe | 379 | return FALSE; |
32a2907b GT |
380 | } |
381 | ||
382 | if (typeName == wxGRID_VALUE_STRING) | |
383 | { | |
384 | //FIXME ummm What about blob field etc. | |
a144affe | 385 | return TRUE; |
32a2907b GT |
386 | } |
387 | ||
388 | if (!(m_data->GetColDefs()[(m_ColInfo[col].DbCol)].Updateable)) | |
389 | { | |
a144affe | 390 | return FALSE; |
32a2907b GT |
391 | } |
392 | ||
393 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
394 | { | |
eae630f1 | 395 | //If a virtual column then we can't find it's type. we have to faulse to |
32a2907b | 396 | //get using wxVairent. |
a144affe | 397 | return FALSE; |
32a2907b GT |
398 | } |
399 | ||
400 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
401 | if (typeName == wxGRID_VALUE_DATETIME) | |
402 | { | |
403 | if ((sqltype == SQL_C_DATE) || | |
eae630f1 | 404 | (sqltype == SQL_C_TIME) || |
32a2907b GT |
405 | (sqltype == SQL_C_TIMESTAMP)) |
406 | { | |
a144affe | 407 | return TRUE; |
32a2907b | 408 | } |
a144affe | 409 | return FALSE; |
32a2907b GT |
410 | } |
411 | if (typeName == wxGRID_VALUE_NUMBER) | |
412 | { | |
413 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 414 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
415 | (sqltype == SQL_C_SLONG) || |
416 | (sqltype == SQL_C_ULONG)) | |
417 | { | |
a144affe | 418 | return TRUE; |
32a2907b | 419 | } |
a144affe | 420 | return FALSE; |
32a2907b GT |
421 | } |
422 | if (typeName == wxGRID_VALUE_FLOAT) | |
423 | { | |
424 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 425 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
426 | (sqltype == SQL_C_SLONG) || |
427 | (sqltype == SQL_C_ULONG) || | |
428 | (sqltype == SQL_C_FLOAT) || | |
429 | (sqltype == SQL_C_DOUBLE)) | |
430 | { | |
a144affe | 431 | return TRUE; |
32a2907b | 432 | } |
a144affe | 433 | return FALSE; |
32a2907b | 434 | } |
a144affe | 435 | return FALSE; |
32a2907b GT |
436 | } |
437 | ||
438 | long wxDbGridTableBase::GetValueAsLong(int row, int col) | |
439 | { | |
440 | ValidateRow(row); | |
441 | ||
442 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
443 | { | |
444 | wxFAIL_MSG (_T("You can not use GetValueAsLong for virtual columns")); | |
445 | return 0; | |
446 | } | |
447 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
448 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 449 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
450 | (sqltype == SQL_C_SLONG) || |
451 | (sqltype == SQL_C_ULONG)) | |
452 | { | |
453 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
454 | return val.GetLong(); | |
455 | } | |
456 | wxFAIL_MSG (_T("unknown column, ")); | |
457 | return 0; | |
458 | } | |
459 | ||
460 | double wxDbGridTableBase::GetValueAsDouble(int row, int col) | |
461 | { | |
462 | wxLogDebug(wxT("GetValueAsDouble() on %i,%i"),row,col); | |
463 | ValidateRow(row); | |
464 | ||
465 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
466 | { | |
467 | wxFAIL_MSG (_T("You can not use GetValueAsDouble for virtual columns")); | |
468 | return 0.0; | |
469 | } | |
470 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
471 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 472 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
473 | (sqltype == SQL_C_SLONG) || |
474 | (sqltype == SQL_C_ULONG) || | |
475 | (sqltype == SQL_C_FLOAT) || | |
476 | (sqltype == SQL_C_DOUBLE)) | |
477 | { | |
478 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
479 | return val.GetDouble(); | |
480 | } | |
481 | wxFAIL_MSG (_T("unknown column")); | |
482 | return 0.0; | |
483 | } | |
484 | ||
485 | bool wxDbGridTableBase::GetValueAsBool(int row, int col) | |
486 | { | |
487 | wxLogDebug(wxT("GetValueAsBool() on %i,%i"),row,col); | |
488 | ValidateRow(row); | |
489 | ||
490 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
491 | { | |
492 | wxFAIL_MSG (_T("You can not use GetValueAsBool for virtual columns")); | |
493 | return 0; | |
494 | } | |
495 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
496 | if ((sqltype == SQL_C_SSHORT) || | |
eae630f1 | 497 | (sqltype == SQL_C_USHORT) || |
32a2907b GT |
498 | (sqltype == SQL_C_SLONG) || |
499 | (sqltype == SQL_C_ULONG)) | |
500 | { | |
501 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
502 | return val.GetBool(); | |
503 | } | |
504 | wxFAIL_MSG (_T("unknown column, ")); | |
505 | return 0; | |
506 | } | |
507 | ||
508 | void* wxDbGridTableBase::GetValueAsCustom(int row, int col, const wxString& typeName) | |
509 | { | |
510 | wxLogDebug(wxT("GetValueAsCustom() on %i,%i"),row,col); | |
511 | ValidateRow(row); | |
eae630f1 | 512 | |
32a2907b GT |
513 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) |
514 | { | |
515 | wxFAIL_MSG (_T("You can not use GetValueAsCustom for virtual columns")); | |
516 | return NULL; | |
517 | } | |
518 | if (m_data->IsColNull(m_ColInfo[col].DbCol)) | |
519 | return NULL; | |
520 | ||
521 | if (typeName == wxGRID_VALUE_DATETIME) | |
522 | { | |
523 | wxDbColDef *pColDefs = m_data->GetColDefs(); | |
524 | int sqltype = pColDefs[(m_ColInfo[col].DbCol)].SqlCtype; | |
525 | ||
526 | if ((sqltype == SQL_C_DATE) || | |
527 | (sqltype == SQL_C_TIME) || | |
528 | (sqltype == SQL_C_TIMESTAMP)) | |
529 | { | |
530 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
531 | return new wxDateTime(val.GetDateTime()); | |
532 | } | |
533 | } | |
534 | wxFAIL_MSG (_T("unknown column data type ")); | |
535 | return NULL; | |
536 | } | |
537 | ||
538 | ||
539 | void wxDbGridTableBase::SetValueAsCustom(int row, int col, const wxString& typeName, void* value) | |
540 | { | |
541 | wxLogDebug(wxT("SetValueAsCustom() on %i,%i"),row,col); | |
542 | ValidateRow(row); | |
543 | ||
544 | if (m_data->GetNumberOfColumns() <= m_ColInfo[col].DbCol) | |
545 | { | |
546 | wxFAIL_MSG (_T("You can not use SetValueAsCustom for virtual columns")); | |
547 | return; | |
548 | } | |
549 | ||
550 | if (typeName == wxGRID_VALUE_DATETIME) | |
551 | { | |
552 | int sqltype = m_data->GetColDefs()[(m_ColInfo[col].DbCol)].SqlCtype; | |
553 | if ((sqltype == SQL_C_DATE) || | |
554 | (sqltype == SQL_C_TIME) || | |
555 | (sqltype == SQL_C_TIMESTAMP)) | |
556 | { | |
557 | //FIXME: you can't dynamic_cast from (void *) | |
558 | //wxDateTime *date = wxDynamicCast(value, wxDateTime); | |
559 | wxDateTime *date = (wxDateTime *)value; | |
560 | if (!date) | |
561 | { | |
562 | wxFAIL_MSG (_T("Failed to convert data")); | |
563 | return; | |
564 | } | |
565 | wxVariant val(date); | |
a144affe | 566 | m_rowmodified = TRUE; |
32a2907b GT |
567 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
568 | } | |
569 | } | |
570 | wxFAIL_MSG (_T("unknown column data type")); | |
571 | return ; | |
572 | } | |
573 | ||
574 | ||
575 | wxString wxDbGridTableBase::GetColLabelValue(int col) | |
576 | { | |
577 | if (GetNumberCols() > col) | |
578 | { | |
579 | return m_ColInfo[col].Title; | |
580 | } | |
581 | wxFAIL_MSG (_T("unknown column")); | |
582 | return wxString(); | |
583 | } | |
584 | ||
585 | bool wxDbGridTableBase::IsEmptyCell(int row, int col) | |
586 | { | |
587 | wxLogDebug(wxT("IsEmtpyCell on %i,%i"),row,col); | |
588 | ||
589 | ValidateRow(row); | |
590 | return m_data->IsColNull(m_ColInfo[col].DbCol); | |
591 | } | |
592 | ||
593 | ||
594 | wxString wxDbGridTableBase::GetValue(int row, int col) | |
595 | { | |
596 | wxLogDebug(wxT("GetValue() on %i,%i"),row,col); | |
eae630f1 | 597 | |
32a2907b GT |
598 | ValidateRow(row); |
599 | wxVariant val = m_data->GetCol(m_ColInfo[col].DbCol); | |
600 | wxLogDebug(wxT("\tReturning \"%s\"\n"),val.GetString().c_str()); | |
eae630f1 DW |
601 | |
602 | return val.GetString(); | |
32a2907b GT |
603 | } |
604 | ||
605 | ||
606 | void wxDbGridTableBase::SetValue(int row, int col,const wxString& value) | |
607 | { | |
608 | wxLogDebug(wxT("SetValue() on %i,%i"),row,col); | |
609 | ||
610 | ValidateRow(row); | |
611 | wxVariant val(value); | |
612 | ||
a144affe | 613 | m_rowmodified = TRUE; |
32a2907b GT |
614 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
615 | } | |
616 | ||
617 | ||
618 | void wxDbGridTableBase::SetValueAsLong(int row, int col, long value) | |
619 | { | |
620 | wxLogDebug(wxT("SetValueAsLong() on %i,%i"),row,col); | |
621 | ||
622 | ValidateRow(row); | |
623 | wxVariant val(value); | |
624 | ||
a144affe | 625 | m_rowmodified = TRUE; |
32a2907b GT |
626 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
627 | } | |
628 | ||
629 | ||
630 | void wxDbGridTableBase::SetValueAsDouble(int row, int col, double value) | |
631 | { | |
632 | wxLogDebug(wxT("SetValueAsDouble() on %i,%i"),row,col); | |
633 | ||
634 | ValidateRow(row); | |
635 | wxVariant val(value); | |
636 | ||
a144affe | 637 | m_rowmodified = TRUE; |
32a2907b GT |
638 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
639 | ||
640 | } | |
641 | ||
642 | ||
643 | void wxDbGridTableBase::SetValueAsBool(int row, int col, bool value) | |
644 | { | |
645 | wxLogDebug(wxT("SetValueAsBool() on %i,%i"),row,col); | |
646 | ||
647 | ValidateRow(row); | |
648 | wxVariant val(value); | |
649 | ||
a144affe | 650 | m_rowmodified = TRUE; |
32a2907b GT |
651 | m_data->SetCol(m_ColInfo[col].DbCol,val); |
652 | } | |
653 | ||
654 | ||
655 | void wxDbGridTableBase::ValidateRow(int row) | |
656 | { | |
657 | wxLogDebug(wxT("ValidateRow(%i) currently on row (%i). Array count = %i"),row,m_row,m_keys.GetCount()); | |
658 | ||
659 | if (row == m_row) | |
660 | return; | |
661 | Writeback(); | |
662 | ||
663 | //We add to row as Count is unsigned! | |
664 | if ((unsigned)(row+1) > m_keys.GetCount()) | |
665 | { | |
666 | wxLogDebug(wxT("\trow key unknown")); | |
667 | // Extend Array, iterate through data filling with keys | |
668 | m_data->SetRowMode(wxDbTable::WX_ROW_MODE_QUERY); | |
669 | int trow; | |
670 | for (trow = m_keys.GetCount(); trow <= row; trow++) | |
671 | { | |
672 | wxLogDebug(wxT("Fetching row %i.."), trow); | |
673 | bool ret = m_data->GetNext(); | |
674 | ||
675 | wxLogDebug(wxT(" ...success=(%i)"),ret); | |
676 | GenericKey k = m_data->GetKey(); | |
677 | m_keys.Add(k); | |
678 | } | |
679 | m_row = row; | |
680 | } | |
681 | else | |
682 | { | |
683 | wxLogDebug(wxT("\trow key known centering data")); | |
684 | GenericKey k = m_keys.Item(row); | |
685 | m_data->SetRowMode(wxDbTable::WX_ROW_MODE_INDIVIDUAL); | |
686 | m_data->ClearMemberVars(); | |
687 | m_data->SetKey(k); | |
688 | if (!m_data->QueryOnKeyFields()) | |
689 | { | |
690 | wxDbLogExtendedErrorMsg("ODBC error during Query()\n\n", m_data->GetDb(),__FILE__,__LINE__); | |
691 | } | |
692 | ||
693 | m_data->GetNext(); | |
694 | ||
695 | m_row = row; | |
696 | } | |
a144affe | 697 | m_rowmodified = FALSE; |
32a2907b GT |
698 | } |
699 | ||
700 | bool wxDbGridTableBase::Writeback() const | |
701 | { | |
702 | if (!m_rowmodified) | |
703 | { | |
a144affe | 704 | return TRUE; |
32a2907b GT |
705 | } |
706 | ||
a144affe | 707 | bool result=TRUE; |
32a2907b GT |
708 | wxLogDebug(wxT("\trow key unknown")); |
709 | ||
eae630f1 | 710 | // FIXME: this code requires dbtable support for record status |
32a2907b GT |
711 | #if 0 |
712 | switch (m_data->get_ModifiedStatus()) | |
713 | { | |
714 | case wxDbTable::UpdatePending: | |
715 | result = m_data->Update(); | |
716 | break; | |
717 | case wxDbTable::InsertPending: | |
718 | result = (m_data->Insert() == SQL_SUCCESS); | |
719 | break; | |
720 | default: | |
721 | //Nothing | |
722 | break; | |
723 | } | |
724 | #else | |
725 | wxLogDebug(wxT("WARNING : Row writeback not implemented ")); | |
726 | #endif | |
727 | return result; | |
728 | } | |
729 | ||
730 | #include <wx/arrimpl.cpp> | |
731 | ||
732 | WX_DEFINE_OBJARRAY(keyarray); | |
733 | ||
734 | #endif // #if wxUSE_NEW_GRID | |
735 | #endif // #if wxUSE_ODBC | |
736 |