1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Data table lookup listbox code
4 // Author: George Tasker/Doug Card
8 // Copyright: (c) 1996 Remstar International, Inc.
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
15 Member functions for the classes defined in LISTDB.H
17 This class is used to present a generic ListBox lookup window for
18 use with any of the object creation/selection choice widgets. This
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.
24 The string selected from the list box is stored in the Global variable
25 "ListDB_Selection", and will remain set until another interation of this
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.
32 The data table record access is all handled through the routines
33 in this module, interfacing with the methods defined in wxDbTable.
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.
43 #include "wx/wxprec.h"
53 #include "wx/dbtable.h"
55 extern wxDbList WXDLLEXPORT
*PtrBegDbList
; /* from db.cpp, used in getting back error results from db connections */
60 // Used for passing the selected listbox selection back to the calling
61 // routine. This variable must be declared as 'extern' in the calling
63 wxChar ListDB_Selection
[LOOKUP_COL_LEN
+1];
65 // If the listbox contains two columns of data, the second column is
66 // returned in this variable.
67 wxChar ListDB_Selection2
[LOOKUP_COL_LEN
+1];
70 const int LISTDB_NO_SPACES_BETWEEN_COLS
= 3;
73 extern wxApp
*DatabaseDemoApp
;
77 * This function will return the exact string(s) from the database engine
78 * indicating all error conditions which have just occurred during the
79 * last call to the database engine.
81 * This demo uses the returned string by displaying it in a wxMessageBox. The
82 * formatting therefore is not the greatest, but this is just a demo, not a
83 * finished product. :-) gt
85 * NOTE: The value returned by this function is for temporary use only and
86 * should be copied for long term use
88 wxString
GetExtendedDBErrorMsg2(wxDb
*pDb
, wxChar
*ErrFile
, int ErrLine
)
95 if (ErrFile
|| ErrLine
)
99 msg
+= wxT(" Line: ");
100 tStr
.Printf(wxT("%d"),ErrLine
);
105 msg
.Append (wxT("\nODBC errors:\n"));
108 // Display errors for this connection
110 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
112 if (pDb
->errorList
[i
])
114 msg
.Append(pDb
->errorList
[i
]);
115 if (wxStrcmp(pDb
->errorList
[i
],wxT("")) != 0)
116 msg
.Append(wxT("\n"));
117 // Clear the errmsg buffer so the next error will not
118 // end up showing the previous error that have occurred
119 wxStrcpy(pDb
->errorList
[i
],wxT(""));
125 } // GetExtendedDBErrorMsg
128 // Clookup constructor
129 Clookup::Clookup(wxString tblName
, wxString colName
, wxDb
*pDb
, const wxString
&defDir
)
130 : wxDbTable(pDb
, tblName
, 1, (const wxString
&)wxEmptyString
, !wxDB_QUERY_ONLY
,
134 SetColDefs (0, colName
, DB_DATA_TYPE_VARCHAR
, lookupCol
, SQL_C_WXCHAR
, LOOKUP_COL_LEN
+1, false, false);
139 // Clookup2 constructor
140 Clookup2::Clookup2(wxString tblName
, wxString colName1
, wxString colName2
,
141 wxDb
*pDb
, const wxString
&defDir
)
142 : wxDbTable(pDb
, tblName
, (UWORD
)(1 + (wxStrlen(colName2
) > 0)), (const wxString
&)wxEmptyString
,
143 !wxDB_QUERY_ONLY
, defDir
)
152 SetColDefs ((UWORD
)i
, colName1
, DB_DATA_TYPE_VARCHAR
, lookupCol1
, SQL_C_WXCHAR
, LOOKUP_COL_LEN
+1, false, false);
154 if (wxStrlen(colName2
) > 0)
155 SetColDefs ((UWORD
)(++i
), colName2
, DB_DATA_TYPE_VARCHAR
, lookupCol2
, SQL_C_WXCHAR
, LOOKUP_COL_LEN
+1, false, false);
160 BEGIN_EVENT_TABLE(ClookUpDlg
, wxDialog
)
161 EVT_BUTTON(LOOKUP_DIALOG_OK
, ClookUpDlg::OnButton
)
162 EVT_BUTTON(LOOKUP_DIALOG_CANCEL
, ClookUpDlg::OnButton
)
163 EVT_CLOSE(ClookUpDlg::OnClose
)
164 EVT_LISTBOX_DCLICK(LOOKUP_DIALOG_SELECT
, ClookUpDlg::OnDClick
)
168 // This is a generic lookup constructor that will work with any table and any column
169 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, const wxString
&windowTitle
, const wxString
&tableName
,
170 const wxString
&colName
, const wxString
&where
, const wxString
&orderBy
,
171 wxDb
*pDb
, const wxString
&defDir
)
172 : wxDialog (parent
, LOOKUP_DIALOG
, wxT("Select..."), wxDefaultPosition
, wxSize(400, 290))
176 wxStrcpy(ListDB_Selection
,wxT(""));
177 widgetPtrsSet
= false;
183 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint( 5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, wxT("LookUpSelectList"));
184 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, wxT("&Ok"), wxPoint(113, 222), wxSize( 70, 35), 0, wxDefaultValidator
, wxT("LookUpOkBtn"));
185 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, wxT("C&ancel"), wxPoint(212, 222), wxSize( 70, 35), 0, wxDefaultValidator
, wxT("LookUpCancelBtn"));
187 widgetPtrsSet
= true;
189 // Query the lookup table and display the result set
190 lookup
= new Clookup(tableName
, colName
, pDb
, defDir
);
193 wxMessageBox(wxT("Error allocating memory for 'Clookup' object."),wxT("Error..."));
201 tStr
.Printf(wxT("Unable to open the table '%s'."), tableName
.c_str());
202 wxMessageBox(wxDbLogExtendedErrorMsg(tStr
.c_str(),lookup
->GetDb(),__TFILE__
,__LINE__
),
203 wxT("ODBC Error..."),wxOK
| wxICON_EXCLAMATION
);
209 lookup
->SetOrderByClause(orderBy
);
210 lookup
->SetWhereClause(where
);
211 if (!lookup
->Query())
214 tStr
= wxT("ODBC error during Query()\n\n");
215 wxMessageBox(wxDbLogExtendedErrorMsg(tStr
.c_str(),lookup
->GetDb(),__TFILE__
,__LINE__
),
216 wxT("ODBC Error..."),wxOK
| wxICON_EXCLAMATION
);
222 // Fill in the list box from the query result set
223 while (lookup
->GetNext())
224 pLookUpSelectList
->Append(lookup
->lookupCol
);
226 // Make the OK activate by pressing Enter
227 if (pLookUpSelectList
->GetCount())
229 pLookUpSelectList
->SetSelection(0);
230 pLookUpOkBtn
->SetDefault();
234 pLookUpCancelBtn
->SetDefault();
235 pLookUpOkBtn
->Enable(false);
238 // Display the dialog window
239 SetTitle(windowTitle
);
244 } // Generic lookup constructor
248 // This is a generic lookup constructor that will work with any table and any column.
249 // It extends the capabilites of the lookup dialog in the following ways:
251 // 1) 2 columns rather than one
252 // 2) The ability to select DISTINCT column values
254 // Only set distinctValues equal to true if necessary. In many cases, the constraints
255 // of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
256 // overhead by the database to ensure that all values returned are distinct. Therefore,
257 // use this ONLY when you need it.
259 // For complicated queries, you can pass in the sql select statement. This would be
260 // necessary if joins are involved since by default both columns must come from the
263 // If you do query by sql statement, you must pass in the maximum length of column1,
264 // since it cannot be derived when you query using your own sql statement.
266 // The optional database connection can be used if you'd like the lookup class
267 // to use a database pointer other than wxGetApp().READONLY_DB. This is necessary if
268 // records are being saved, but not committed to the db, yet should be included
269 // in the lookup window.
271 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, const wxString
&windowTitle
, const wxString
&tableName
,
272 const wxString
&dispCol1
, const wxString
&dispCol2
,
273 const wxString
&where
, const wxString
&orderBy
,
274 wxDb
*pDb
, const wxString
&defDir
, bool distinctValues
,
275 const wxString
&selectStmt
, int maxLenCol1
, bool allowOk
)
276 : wxDialog (parent
, LOOKUP_DIALOG
, wxT("Select..."), wxDefaultPosition
, wxSize(400, 290))
280 wxStrcpy(ListDB_Selection
,wxT(""));
281 wxStrcpy(ListDB_Selection2
,wxT(""));
282 widgetPtrsSet
= false;
286 noDisplayCols
= (dispCol2
.Length() == 0 ? 1 : 2);
289 wxFont
fixedFont(12, wxMODERN
, wxNORMAL
, wxNORMAL
);
291 // this is done with fixed font so that the second column (if any) will be left
292 // justified in the second column
293 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, wxT("LookUpSelectList"));
295 pLookUpSelectList
->SetFont(fixedFont
);
297 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, wxT("&Ok"), wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator
, wxT("LookUpOkBtn"));
298 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, wxT("C&ancel"), wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator
, wxT("LookUpCancelBtn"));
300 widgetPtrsSet
= true;
302 // Query the lookup table and display the result set
303 lookup2
= new Clookup2(tableName
, dispCol1
, dispCol2
, pDb
, defDir
);
306 wxMessageBox(wxT("Error allocating memory for 'Clookup2' object."),wxT("Error..."));
311 if (!lookup2
->Open())
314 tStr
.Printf(wxT("Unable to open the table '%s'."),tableName
.c_str());
315 tStr
+= GetExtendedDBErrorMsg2(pDb
,__TFILE__
,__LINE__
);
316 wxMessageBox(tStr
,wxT("ODBC Error..."));
321 // If displaying 2 columns, determine the maximum length of column1
324 maxColLen
= col1Len
= maxLenCol1
; // user passed in max col length for column 1
327 // NOTE: Some databases (Firebird/Interbase) cannot handle the "fn" and "MAX()" functions
329 maxColLen
= LOOKUP_COL_LEN
;
330 if (wxStrlen(dispCol2
))
332 wxString q
= wxT("SELECT MAX({fn LENGTH(");
334 q
+= wxT(")}), NULL");
342 if (!lookup2
->QueryBySqlStmt(q
))
345 tStr
= wxT("ODBC error during QueryBySqlStmt()\n\n");
346 wxMessageBox(wxDbLogExtendedErrorMsg(tStr
.c_str(),lookup2
->GetDb(),__TFILE__
,__LINE__
),
347 wxT("ODBC Error..."),wxOK
| wxICON_EXCLAMATION
);
352 if (lookup2
->GetNext())
353 maxColLen
= col1Len
= wxAtoi(lookup2
->lookupCol1
);
357 tStr
= wxT("ODBC error during GetNext()\n\n");
358 wxMessageBox(wxDbLogExtendedErrorMsg(tStr
.c_str(),lookup2
->GetDb(),__TFILE__
,__LINE__
),
359 wxT("ODBC Error..."),wxOK
| wxICON_EXCLAMATION
);
364 // Query the actual record set
365 if (selectStmt
&& wxStrlen(selectStmt
)) // Query by sql stmt passed in
367 if (!lookup2
->QueryBySqlStmt(selectStmt
))
370 tStr
= wxT("ODBC error during QueryBySqlStmt()\n\n");
371 wxMessageBox(wxDbLogExtendedErrorMsg(tStr
.c_str(),lookup2
->GetDb(),__TFILE__
,__LINE__
),
372 wxT("ODBC Error..."),wxOK
| wxICON_EXCLAMATION
);
378 else // Query using where and order by clauses
380 lookup2
->SetOrderByClause(orderBy
);
381 lookup2
->SetWhereClause(where
);
382 if (!lookup2
->Query(false, distinctValues
))
385 tStr
= wxT("ODBC error during Query()\n\n");
386 wxMessageBox(wxDbLogExtendedErrorMsg(tStr
.c_str(),lookup2
->GetDb(),__TFILE__
,__LINE__
),
387 wxT("ODBC Error..."),wxOK
| wxICON_EXCLAMATION
);
394 // Fill in the list box from the query result set
396 while (lookup2
->GetNext())
398 s
= lookup2
->lookupCol1
;
399 if (wxStrlen(dispCol2
)) // Append the optional column 2
401 s
.Append(wxT(' '), (maxColLen
+ LISTDB_NO_SPACES_BETWEEN_COLS
- wxStrlen(lookup2
->lookupCol1
)));
402 s
.Append(lookup2
->lookupCol2
);
404 pLookUpSelectList
->Append(s
);
407 // Make the OK activate by pressing Enter
408 if (pLookUpSelectList
->GetCount())
410 pLookUpSelectList
->SetSelection(0);
411 pLookUpOkBtn
->SetDefault();
415 pLookUpCancelBtn
->SetDefault();
416 pLookUpOkBtn
->Enable(false);
419 pLookUpOkBtn
->Enable(allowOk
);
421 // Display the dialog window
422 SetTitle(windowTitle
);
427 } // Generic lookup constructor 2
430 void ClookUpDlg::OnClose(wxCloseEvent
& event
)
432 widgetPtrsSet
= false;
433 GetParent()->Enable(true);
442 while (wxIsBusy()) wxEndBusyCursor();
447 } // ClookUpDlg::OnClose
450 void ClookUpDlg::OnDClick( wxCommandEvent
&event
)
452 wxWindow
*win
= (wxWindow
*) event
.GetEventObject();
453 OnCommand( *win
, event
);
457 void ClookUpDlg::OnButton( wxCommandEvent
&event
)
459 wxWindow
*win
= (wxWindow
*) event
.GetEventObject();
460 OnCommand( *win
, event
);
464 void ClookUpDlg::OnCommand(wxWindow
& win
, wxCommandEvent
& WXUNUSED(event
))
466 wxString widgetName
= win
.GetName();
470 bool doubleclick
= false;
472 if (widgetName
== pLookUpSelectList
->GetName())
478 if (widgetName
== pLookUpOkBtn
->GetName() || doubleclick
)
480 if (pLookUpSelectList
->GetSelection() != -1)
482 if (noDisplayCols
== 1)
483 wxStrcpy (ListDB_Selection
, pLookUpSelectList
->GetStringSelection());
484 else // 2 display columns
486 wxString s
= pLookUpSelectList
->GetStringSelection();
488 s
= s
.SubString(0, col1Len
-1);
490 wxStrcpy(ListDB_Selection
, s
);
492 s
= pLookUpSelectList
->GetStringSelection();
493 s
= s
.Mid(col1Len
+ LISTDB_NO_SPACES_BETWEEN_COLS
);
495 wxStrcpy(ListDB_Selection2
, s
);
500 wxStrcpy(ListDB_Selection
,wxT(""));
501 wxStrcpy(ListDB_Selection2
,wxT(""));
507 if (widgetName
== pLookUpCancelBtn
->GetName())
509 wxStrcpy (ListDB_Selection
,wxT(""));
510 wxStrcpy (ListDB_Selection2
,wxT(""));
516 } // ClookUpDlg::OnCommand
518 // *********************************** listdb.cpp **********************************