]> git.saurik.com Git - wxWidgets.git/blame - samples/db/listdb.cpp
Incomplete wxUSE_* warning fix.
[wxWidgets.git] / samples / db / listdb.cpp
CommitLineData
108106cf
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: listdb.cpp
3// Purpose: Data table lookup listbox code
4// Author: George Tasker/Doug Card
5// Modified by:
6// Created: 1996
7// RCS-ID: $Id$
8// Copyright: (c) 1996 Remstar International, Inc.
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12/*
13// SYNOPSIS START
14
15 Member functions for the classes defined in LISTDB.H
16
e70e8f4c 17 This class is used to present a generic ListBox lookup window for
925e9792 18 use with any of the object creation/selection choice widgets. This
e70e8f4c
GT
19 dialog window will present a (possibly) scrolling list of values
20 that come from a data table source. Based on the object type passed
21 in the constructor, a ListBox is built to present the user with a
22 single selection listbox.
23
24 The string selected from the list box is stored in the Global variable
94613352 25 "ListDB_Selection", and will remain set until another interation of this
e70e8f4c
GT
26 routine is called.
27
28 For each object (database) type that is to be used, an overridden
29 constructor should be written to appropriately link to the proper
30 data table/object for building the list.
925e9792 31
108106cf 32 The data table record access is all handled through the routines
f6bcfd97 33 in this module, interfacing with the methods defined in wxDbTable.
108106cf 34
e70e8f4c
GT
35 All objects which use data table access must be initialized and
36 have opened the table prior to passing them in the dialog
37 constructor, and the 'where' query should already have been set
38 and performed before creating this dialog instance.
925e9792 39
108106cf
JS
40// SYNOPSIS STOP
41*/
42
43#ifdef __GNUG__
44#pragma implementation "listdb.h"
45#endif
46
47#include "wx/wxprec.h"
48
49#ifdef __BORLANDC__
50#pragma hdrstop
51#endif //__BORLANDC__
52
53#ifndef WX_PRECOMP
fb86524f 54#include "wx/wx.h"
108106cf
JS
55#endif //WX_PRECOMP
56
fb86524f 57#include "wx/dbtable.h"
108106cf 58
3f755e2d 59extern wxDbList WXDLLEXPORT *PtrBegDbList; /* from db.cpp, used in getting back error results from db connections */
65d7ddc4 60
108106cf 61#include "listdb.h"
049977d0 62//#include "dbtest.h"
108106cf
JS
63
64// Used for passing the selected listbox selection back to the calling
65// routine. This variable must be declared as 'extern' in the calling
66// source module
94613352 67wxChar ListDB_Selection[LOOKUP_COL_LEN+1];
108106cf
JS
68
69// If the listbox contains two columns of data, the second column is
70// returned in this variable.
94613352 71wxChar ListDB_Selection2[LOOKUP_COL_LEN+1];
108106cf
JS
72
73// Constants
5b077d48 74const int LISTDB_NO_SPACES_BETWEEN_COLS = 3;
108106cf
JS
75
76
049977d0
GT
77extern wxApp *DatabaseDemoApp;
78
65d7ddc4
GT
79
80/*
81 * This function will return the exact string(s) from the database engine
82 * indicating all error conditions which have just occured during the
83 * last call to the database engine.
84 *
85 * This demo uses the returned string by displaying it in a wxMessageBox. The
86 * formatting therefore is not the greatest, but this is just a demo, not a
87 * finished product. :-) gt
88 *
89 * NOTE: The value returned by this function is for temporary use only and
90 * should be copied for long term use
91 */
e689d52a 92wxString GetExtendedDBErrorMsg2(wxDb *pDb, wxChar *ErrFile, int ErrLine)
65d7ddc4 93{
e70e8f4c 94 static wxString msg;
3fe813a9 95 msg = wxT("");
e70e8f4c
GT
96
97 wxString tStr;
98
99 if (ErrFile || ErrLine)
100 {
94613352 101 msg += wxT("File: ");
e70e8f4c 102 msg += ErrFile;
94613352
GT
103 msg += wxT(" Line: ");
104 tStr.Printf(wxT("%d"),ErrLine);
3fe813a9 105 msg += tStr.c_str();
94613352 106 msg += wxT("\n");
e70e8f4c
GT
107 }
108
94613352
GT
109 msg.Append (wxT("\nODBC errors:\n"));
110 msg += wxT("\n");
925e9792 111
3fe813a9
GT
112 // Display errors for this connection
113 int i;
114 for (i = 0; i < DB_MAX_ERROR_HISTORY; i++)
e70e8f4c 115 {
3fe813a9 116 if (pDb->errorList[i])
e70e8f4c 117 {
3fe813a9
GT
118 msg.Append(pDb->errorList[i]);
119 if (wxStrcmp(pDb->errorList[i],wxT("")) != 0)
120 msg.Append(wxT("\n"));
121 // Clear the errmsg buffer so the next error will not
122 // end up showing the previous error that have occurred
123 wxStrcpy(pDb->errorList[i],wxT(""));
e70e8f4c
GT
124 }
125 }
94613352 126 msg += wxT("\n");
e70e8f4c 127
e689d52a 128 return msg;
65d7ddc4
GT
129} // GetExtendedDBErrorMsg
130
131
108106cf 132// Clookup constructor
da1e87c4 133Clookup::Clookup(wxString tblName, wxString colName, wxDb *pDb, const wxString &defDir)
e689d52a 134 : wxDbTable(pDb, tblName, 1, (const wxString &)wxEmptyString, !wxDB_QUERY_ONLY,
049977d0 135 defDir)
108106cf
JS
136{
137
e689d52a 138 SetColDefs (0, colName, DB_DATA_TYPE_VARCHAR, lookupCol, SQL_C_WXCHAR, LOOKUP_COL_LEN+1, false, false);
108106cf
JS
139
140} // Clookup()
141
142
143// Clookup2 constructor
da1e87c4 144Clookup2::Clookup2(wxString tblName, wxString colName1, wxString colName2,
049977d0 145 wxDb *pDb, const wxString &defDir)
e689d52a 146 : wxDbTable(pDb, tblName, (UWORD)(1 + (wxStrlen(colName2) > 0)), (const wxString &)wxEmptyString,
049977d0 147 !wxDB_QUERY_ONLY, defDir)
108106cf 148{
049977d0
GT
149 wxASSERT(pDb);
150 wxASSERT(tblName);
151 wxASSERT(colName1);
152 wxASSERT(colName2);
153
e70e8f4c 154 int i = 0;
108106cf 155
e689d52a 156 SetColDefs ((UWORD)i, colName1, DB_DATA_TYPE_VARCHAR, lookupCol1, SQL_C_WXCHAR, LOOKUP_COL_LEN+1, false, false);
108106cf 157
f6bcfd97 158 if (wxStrlen(colName2) > 0)
e689d52a 159 SetColDefs ((UWORD)(++i), colName2, DB_DATA_TYPE_VARCHAR, lookupCol2, SQL_C_WXCHAR, LOOKUP_COL_LEN+1, false, false);
108106cf
JS
160
161} // Clookup2()
162
163
65d7ddc4 164BEGIN_EVENT_TABLE(ClookUpDlg, wxDialog)
65d7ddc4
GT
165 EVT_BUTTON(LOOKUP_DIALOG_OK, ClookUpDlg::OnButton)
166 EVT_BUTTON(LOOKUP_DIALOG_CANCEL, ClookUpDlg::OnButton)
167 EVT_CLOSE(ClookUpDlg::OnClose)
e689d52a 168 EVT_LISTBOX_DCLICK(LOOKUP_DIALOG_SELECT, ClookUpDlg::OnDClick)
65d7ddc4
GT
169END_EVENT_TABLE()
170
049977d0 171
108106cf 172// This is a generic lookup constructor that will work with any table and any column
e689d52a
GT
173ClookUpDlg::ClookUpDlg(wxWindow *parent, const wxString &windowTitle, const wxString &tableName,
174 const wxString &colName, const wxString &where, const wxString &orderBy,
049977d0 175 wxDb *pDb, const wxString &defDir)
6d841efd 176 : wxDialog (parent, LOOKUP_DIALOG, wxT("Select..."), wxDefaultPosition, wxSize(400, 290))
108106cf 177{
e70e8f4c 178 wxBeginBusyCursor();
925e9792 179
94613352 180 wxStrcpy(ListDB_Selection,wxT(""));
6d841efd 181 widgetPtrsSet = false;
e70e8f4c
GT
182 lookup = 0;
183 lookup2 = 0;
184 noDisplayCols = 1;
185 col1Len = 0;
186
e689d52a
GT
187 pLookUpSelectList = new wxListBox(this, LOOKUP_DIALOG_SELECT, wxPoint( 5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE|wxLB_ALWAYS_SB, wxDefaultValidator, wxT("LookUpSelectList"));
188 pLookUpOkBtn = new wxButton(this, LOOKUP_DIALOG_OK, wxT("&Ok"), wxPoint(113, 222), wxSize( 70, 35), 0, wxDefaultValidator, wxT("LookUpOkBtn"));
189 pLookUpCancelBtn = new wxButton(this, LOOKUP_DIALOG_CANCEL, wxT("C&ancel"), wxPoint(212, 222), wxSize( 70, 35), 0, wxDefaultValidator, wxT("LookUpCancelBtn"));
e70e8f4c 190
6d841efd 191 widgetPtrsSet = true;
e70e8f4c
GT
192
193 // Query the lookup table and display the result set
2f6c54eb 194 lookup = new Clookup(tableName, colName, pDb, defDir);
3fe813a9 195 if (!lookup)
e70e8f4c 196 {
e689d52a 197 wxMessageBox(wxT("Error allocating memory for 'Clookup' object."),wxT("Error..."));
e70e8f4c
GT
198 Close();
199 return;
200 }
201
202 if (!lookup->Open())
203 {
204 wxString tStr;
f0fb3b3b 205 tStr.Printf(wxT("Unable to open the table '%s'."), tableName.c_str());
ee377e7f
GT
206 wxMessageBox(wxDbLogExtendedErrorMsg(tStr.c_str(),lookup->GetDb(),__TFILE__,__LINE__),
207 wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
208
e70e8f4c
GT
209 Close();
210 return;
211 }
212
f6bcfd97
BP
213 lookup->SetOrderByClause(orderBy);
214 lookup->SetWhereClause(where);
e70e8f4c
GT
215 if (!lookup->Query())
216 {
ee377e7f
GT
217 wxString tStr;
218 tStr = wxT("ODBC error during Query()\n\n");
219 wxMessageBox(wxDbLogExtendedErrorMsg(tStr.c_str(),lookup->GetDb(),__TFILE__,__LINE__),
220 wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
221
e70e8f4c
GT
222 Close();
223 return;
224 }
225
226 // Fill in the list box from the query result set
227 while (lookup->GetNext())
228 pLookUpSelectList->Append(lookup->lookupCol);
229
e70e8f4c 230 // Make the OK activate by pressing Enter
2f8f7c77 231 if (pLookUpSelectList->GetCount())
ae883e9a
GT
232 {
233 pLookUpSelectList->SetSelection(0);
e70e8f4c 234 pLookUpOkBtn->SetDefault();
ae883e9a 235 }
e70e8f4c
GT
236 else
237 {
238 pLookUpCancelBtn->SetDefault();
6d841efd 239 pLookUpOkBtn->Enable(false);
e70e8f4c
GT
240 }
241
242 // Display the dialog window
243 SetTitle(windowTitle);
244 Centre(wxBOTH);
245 wxEndBusyCursor();
246 ShowModal();
108106cf
JS
247
248} // Generic lookup constructor
249
250
251//
252// This is a generic lookup constructor that will work with any table and any column.
253// It extends the capabilites of the lookup dialog in the following ways:
254//
e70e8f4c 255// 1) 2 columns rather than one
108106cf
JS
256// 2) The ability to select DISTINCT column values
257//
6d841efd 258// Only set distinctValues equal to true if necessary. In many cases, the constraints
108106cf
JS
259// of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
260// overhead by the database to ensure that all values returned are distinct. Therefore,
261// use this ONLY when you need it.
262//
263// For complicated queries, you can pass in the sql select statement. This would be
264// necessary if joins are involved since by default both columns must come from the
265// same table.
266//
a0d9c6cb 267// If you do query by sql statement, you must pass in the maximum length of column1,
108106cf
JS
268// since it cannot be derived when you query using your own sql statement.
269//
270// The optional database connection can be used if you'd like the lookup class
049977d0 271// to use a database pointer other than wxGetApp().READONLY_DB. This is necessary if
108106cf
JS
272// records are being saved, but not committed to the db, yet should be included
273// in the lookup window.
274//
e689d52a 275ClookUpDlg::ClookUpDlg(wxWindow *parent, const wxString &windowTitle, const wxString &tableName,
f0fb3b3b 276 const wxString &dispCol1, const wxString &dispCol2,
e689d52a 277 const wxString &where, const wxString &orderBy,
925e9792 278 wxDb *pDb, const wxString &defDir, bool distinctValues,
e689d52a 279 const wxString &selectStmt, int maxLenCol1, bool allowOk)
6d841efd 280 : wxDialog (parent, LOOKUP_DIALOG, wxT("Select..."), wxDefaultPosition, wxSize(400, 290))
108106cf 281{
e70e8f4c 282 wxBeginBusyCursor();
925e9792 283
94613352
GT
284 wxStrcpy(ListDB_Selection,wxT(""));
285 wxStrcpy(ListDB_Selection2,wxT(""));
6d841efd 286 widgetPtrsSet = false;
e70e8f4c
GT
287 lookup = 0;
288 lookup2 = 0;
e689d52a
GT
289
290 noDisplayCols = (dispCol2.Length() == 0 ? 1 : 2);
e70e8f4c
GT
291 col1Len = 0;
292
e689d52a 293 wxFont fixedFont(12, wxMODERN, wxNORMAL, wxNORMAL);
e70e8f4c
GT
294
295 // this is done with fixed font so that the second column (if any) will be left
296 // justified in the second column
94613352 297 pLookUpSelectList = new wxListBox(this, LOOKUP_DIALOG_SELECT, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE|wxLB_ALWAYS_SB, wxDefaultValidator, wxT("LookUpSelectList"));
1fc5dd6f
JS
298
299 pLookUpSelectList->SetFont(fixedFont);
300
94613352
GT
301 pLookUpOkBtn = new wxButton(this, LOOKUP_DIALOG_OK, wxT("&Ok"), wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator, wxT("LookUpOkBtn"));
302 pLookUpCancelBtn = new wxButton(this, LOOKUP_DIALOG_CANCEL, wxT("C&ancel"), wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator, wxT("LookUpCancelBtn"));
e70e8f4c 303
6d841efd 304 widgetPtrsSet = true;
e70e8f4c
GT
305
306 // Query the lookup table and display the result set
2f6c54eb 307 lookup2 = new Clookup2(tableName, dispCol1, dispCol2, pDb, defDir);
3fe813a9 308 if (!lookup2)
e70e8f4c 309 {
94613352 310 wxMessageBox(wxT("Error allocating memory for 'Clookup2' object."),wxT("Error..."));
e70e8f4c
GT
311 Close();
312 return;
313 }
314
315 if (!lookup2->Open())
316 {
317 wxString tStr;
f0fb3b3b 318 tStr.Printf(wxT("Unable to open the table '%s'."),tableName.c_str());
74de91cc 319 tStr += GetExtendedDBErrorMsg2(pDb,__TFILE__,__LINE__);
94613352 320 wxMessageBox(tStr,wxT("ODBC Error..."));
e70e8f4c
GT
321 Close();
322 return;
323 }
324
325 // If displaying 2 columns, determine the maximum length of column1
326 int maxColLen;
ee377e7f 327 if (maxLenCol1 > 0)
e70e8f4c
GT
328 maxColLen = col1Len = maxLenCol1; // user passed in max col length for column 1
329 else
330 {
ee377e7f
GT
331 // NOTE: Some databases (Firebird/Interbase) cannot handle the "fn" and "MAX()" functions
332
e70e8f4c 333 maxColLen = LOOKUP_COL_LEN;
f6bcfd97 334 if (wxStrlen(dispCol2))
e70e8f4c 335 {
94613352 336 wxString q = wxT("SELECT MAX({fn LENGTH(");
e70e8f4c 337 q += dispCol1;
94613352
GT
338 q += wxT(")}), NULL");
339 q += wxT(" FROM ");
e70e8f4c 340 q += tableName;
f6bcfd97 341 if (wxStrlen(where))
e70e8f4c 342 {
94613352 343 q += wxT(" WHERE ");
e70e8f4c
GT
344 q += where;
345 }
94613352 346 if (!lookup2->QueryBySqlStmt(q))
e70e8f4c 347 {
ee377e7f
GT
348 wxString tStr;
349 tStr = wxT("ODBC error during QueryBySqlStmt()\n\n");
350 wxMessageBox(wxDbLogExtendedErrorMsg(tStr.c_str(),lookup2->GetDb(),__TFILE__,__LINE__),
351 wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
352
e70e8f4c
GT
353 Close();
354 return;
355 }
356 if (lookup2->GetNext())
74de91cc 357 maxColLen = col1Len = wxAtoi(lookup2->lookupCol1);
e70e8f4c 358 else
ee377e7f
GT
359 {
360 wxString tStr;
361 tStr = wxT("ODBC error during GetNext()\n\n");
362 wxMessageBox(wxDbLogExtendedErrorMsg(tStr.c_str(),lookup2->GetDb(),__TFILE__,__LINE__),
363 wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
364 }
e70e8f4c
GT
365 }
366 }
367
368 // Query the actual record set
f6bcfd97 369 if (selectStmt && wxStrlen(selectStmt)) // Query by sql stmt passed in
e70e8f4c
GT
370 {
371 if (!lookup2->QueryBySqlStmt(selectStmt))
372 {
ee377e7f
GT
373 wxString tStr;
374 tStr = wxT("ODBC error during QueryBySqlStmt()\n\n");
375 wxMessageBox(wxDbLogExtendedErrorMsg(tStr.c_str(),lookup2->GetDb(),__TFILE__,__LINE__),
376 wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
377
e70e8f4c
GT
378 Close();
379 return;
380 }
381 }
382 else // Query using where and order by clauses
383 {
f6bcfd97
BP
384 lookup2->SetOrderByClause(orderBy);
385 lookup2->SetWhereClause(where);
6d841efd 386 if (!lookup2->Query(false, distinctValues))
e70e8f4c 387 {
ee377e7f
GT
388 wxString tStr;
389 tStr = wxT("ODBC error during Query()\n\n");
390 wxMessageBox(wxDbLogExtendedErrorMsg(tStr.c_str(),lookup2->GetDb(),__TFILE__,__LINE__),
391 wxT("ODBC Error..."),wxOK | wxICON_EXCLAMATION);
392
e70e8f4c
GT
393 Close();
394 return;
395 }
396 }
397
398 // Fill in the list box from the query result set
399 wxString s;
400 while (lookup2->GetNext())
401 {
402 s = lookup2->lookupCol1;
f6bcfd97 403 if (wxStrlen(dispCol2)) // Append the optional column 2
e70e8f4c 404 {
94613352 405 s.Append(wxT(' '), (maxColLen + LISTDB_NO_SPACES_BETWEEN_COLS - wxStrlen(lookup2->lookupCol1)));
e70e8f4c
GT
406 s.Append(lookup2->lookupCol2);
407 }
408 pLookUpSelectList->Append(s);
409 }
410
e70e8f4c 411 // Make the OK activate by pressing Enter
2f8f7c77 412 if (pLookUpSelectList->GetCount())
ae883e9a
GT
413 {
414 pLookUpSelectList->SetSelection(0);
e70e8f4c 415 pLookUpOkBtn->SetDefault();
ae883e9a 416 }
e70e8f4c
GT
417 else
418 {
419 pLookUpCancelBtn->SetDefault();
6d841efd 420 pLookUpOkBtn->Enable(false);
e70e8f4c
GT
421 }
422
423 pLookUpOkBtn->Enable(allowOk);
424
425 // Display the dialog window
426 SetTitle(windowTitle);
427 Centre(wxBOTH);
428 wxEndBusyCursor();
429 ShowModal();
108106cf
JS
430
431} // Generic lookup constructor 2
432
433
65d7ddc4 434void ClookUpDlg::OnClose(wxCloseEvent& event)
108106cf 435{
6d841efd
WS
436 widgetPtrsSet = false;
437 GetParent()->Enable(true);
108106cf 438
e70e8f4c
GT
439 if (lookup)
440 delete lookup;
441 if (lookup2)
442 delete lookup2;
108106cf 443
3ca6a5f0
BP
444 SetReturnCode(1);
445
e70e8f4c 446 while (wxIsBusy()) wxEndBusyCursor();
3ca6a5f0 447 event.Skip();
65d7ddc4 448
6d841efd 449// return true;
108106cf
JS
450
451} // ClookUpDlg::OnClose
452
453
e689d52a
GT
454void ClookUpDlg::OnDClick( wxCommandEvent &event )
455{
456 wxWindow *win = (wxWindow*) event.GetEventObject();
457 OnCommand( *win, event );
458}
459
460
65d7ddc4
GT
461void ClookUpDlg::OnButton( wxCommandEvent &event )
462{
463 wxWindow *win = (wxWindow*) event.GetEventObject();
464 OnCommand( *win, event );
465}
466
467
74de91cc 468void ClookUpDlg::OnCommand(wxWindow& win, wxCommandEvent& WXUNUSED(event))
108106cf 469{
e70e8f4c
GT
470 wxString widgetName = win.GetName();
471
472 if (widgetPtrsSet)
473 {
e689d52a
GT
474 bool doubleclick = false;
475 // Double click
476 if (widgetName == pLookUpSelectList->GetName())
477 {
478 doubleclick = true;
479 } // Double click
480
e70e8f4c 481 // OK Button
e689d52a 482 if (widgetName == pLookUpOkBtn->GetName() || doubleclick)
e70e8f4c
GT
483 {
484 if (pLookUpSelectList->GetSelection() != -1)
485 {
486 if (noDisplayCols == 1)
f6bcfd97 487 wxStrcpy (ListDB_Selection, pLookUpSelectList->GetStringSelection());
e70e8f4c
GT
488 else // 2 display columns
489 {
490 wxString s = pLookUpSelectList->GetStringSelection();
491 // Column 1
492 s = s.SubString(0, col1Len-1);
493 s = s.Strip();
f6bcfd97 494 wxStrcpy(ListDB_Selection, s);
e70e8f4c
GT
495 // Column 2
496 s = pLookUpSelectList->GetStringSelection();
497 s = s.Mid(col1Len + LISTDB_NO_SPACES_BETWEEN_COLS);
498 s = s.Strip();
f6bcfd97 499 wxStrcpy(ListDB_Selection2, s);
e70e8f4c
GT
500 }
501 }
502 else
503 {
94613352
GT
504 wxStrcpy(ListDB_Selection,wxT(""));
505 wxStrcpy(ListDB_Selection2,wxT(""));
e70e8f4c
GT
506 }
507 Close();
508 } // OK Button
509
510 // Cancel Button
511 if (widgetName == pLookUpCancelBtn->GetName())
512 {
94613352
GT
513 wxStrcpy (ListDB_Selection,wxT(""));
514 wxStrcpy (ListDB_Selection2,wxT(""));
e70e8f4c
GT
515 Close();
516 } // Cancel Button
e689d52a 517
e70e8f4c 518 }
108106cf
JS
519
520}; // ClookUpDlg::OnCommand
521
522// *********************************** listdb.cpp **********************************