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