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.
44 #pragma implementation "listdb.h"
47 #include "wx/wxprec.h"
57 #include "wx/dbtable.h"
59 extern wxDbList WXDLLEXPORT
*PtrBegDbList
; /* from db.cpp, used in getting back error results from db connections */
64 // Used for passing the selected listbox selection back to the calling
65 // routine. This variable must be declared as 'extern' in the calling
67 wxChar ListDB_Selection
[LOOKUP_COL_LEN
+1];
69 // If the listbox contains two columns of data, the second column is
70 // returned in this variable.
71 wxChar ListDB_Selection2
[LOOKUP_COL_LEN
+1];
74 const int LISTDB_NO_SPACES_BETWEEN_COLS
= 3;
77 extern wxApp
*DatabaseDemoApp
;
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.
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
89 * NOTE: The value returned by this function is for temporary use only and
90 * should be copied for long term use
92 const wxChar
*GetExtendedDBErrorMsg2(wxDb
*pDb
, wxChar
*ErrFile
, int ErrLine
)
99 if (ErrFile
|| ErrLine
)
101 msg
+= wxT("File: ");
103 msg
+= wxT(" Line: ");
104 tStr
.Printf(wxT("%d"),ErrLine
);
109 msg
.Append (wxT("\nODBC errors:\n"));
112 // Display errors for this connection
114 for (i
= 0; i
< DB_MAX_ERROR_HISTORY
; i
++)
116 if (pDb
->errorList
[i
])
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(""));
129 } // GetExtendedDBErrorMsg
132 // Clookup constructor
133 Clookup::Clookup(wxChar
*tblName
, wxChar
*colName
, wxDb
*pDb
, const wxString
&defDir
)
134 : wxDbTable(pDb
, tblName
, 1, wxT(""), !wxDB_QUERY_ONLY
,
138 SetColDefs (0, colName
, DB_DATA_TYPE_VARCHAR
, lookupCol
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
143 // Clookup2 constructor
144 Clookup2::Clookup2(wxChar
*tblName
, wxChar
*colName1
, wxChar
*colName2
,
145 wxDb
*pDb
, const wxString
&defDir
)
146 : wxDbTable(pDb
, tblName
, (1 + (wxStrlen(colName2
) > 0)), wxT(""),
147 !wxDB_QUERY_ONLY
, defDir
)
156 SetColDefs (i
, colName1
, DB_DATA_TYPE_VARCHAR
, lookupCol1
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
158 if (wxStrlen(colName2
) > 0)
159 SetColDefs (++i
, colName2
, DB_DATA_TYPE_VARCHAR
, lookupCol2
, SQL_C_CHAR
, LOOKUP_COL_LEN
+1, FALSE
, FALSE
);
164 BEGIN_EVENT_TABLE(ClookUpDlg
, wxDialog
)
165 EVT_BUTTON(LOOKUP_DIALOG_OK
, ClookUpDlg::OnButton
)
166 EVT_BUTTON(LOOKUP_DIALOG_CANCEL
, ClookUpDlg::OnButton
)
167 EVT_CLOSE(ClookUpDlg::OnClose
)
171 // This is a generic lookup constructor that will work with any table and any column
172 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, wxChar
*windowTitle
, wxChar
*tableName
,
173 wxChar
*colName
, wxChar
*where
, wxChar
*orderBy
,
174 wxDb
*pDb
, const wxString
&defDir
)
175 : wxDialog (parent
, LOOKUP_DIALOG
, wxT("Select..."), wxPoint(-1, -1), wxSize(400, 290))
179 wxStrcpy(ListDB_Selection
,wxT(""));
180 widgetPtrsSet
= FALSE
;
186 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint( 5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, wxT("LookUpSelectList"));
187 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, wxT("&Ok"), wxPoint(113, 222), wxSize( 70, 35), 0, wxDefaultValidator
, wxT("LookUpOkBtn"));
188 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, wxT("C&ancel"), wxPoint(212, 222), wxSize( 70, 35), 0, wxDefaultValidator
, wxT("LookUpCancelBtn"));
190 widgetPtrsSet
= TRUE
;
192 // Query the lookup table and display the result set
193 lookup
= new Clookup(tableName
, colName
, pDb
, defDir
);
196 wxMessageBox(wxT("Error allocating memory for 'Clookup'object."),wxT("Error..."));
204 tStr
.Printf(wxT("Unable to open the table '%s'."),tableName
);
205 wxMessageBox(tStr
,wxT("ODBC Error..."));
210 lookup
->SetOrderByClause(orderBy
);
211 lookup
->SetWhereClause(where
);
212 if (!lookup
->Query())
214 wxMessageBox(wxT("ODBC error during Query()"),wxT("ODBC Error..."));
219 // Fill in the list box from the query result set
220 while (lookup
->GetNext())
221 pLookUpSelectList
->Append(lookup
->lookupCol
);
223 // Highlight the first list item
224 pLookUpSelectList
->SetSelection(0);
226 // Make the OK activate by pressing Enter
227 if (pLookUpSelectList
->GetCount())
228 pLookUpOkBtn
->SetDefault();
231 pLookUpCancelBtn
->SetDefault();
232 pLookUpOkBtn
->Enable(FALSE
);
235 // Display the dialog window
236 SetTitle(windowTitle
);
241 } // Generic lookup constructor
245 // This is a generic lookup constructor that will work with any table and any column.
246 // It extends the capabilites of the lookup dialog in the following ways:
248 // 1) 2 columns rather than one
249 // 2) The ability to select DISTINCT column values
251 // Only set distinctValues equal to TRUE if necessary. In many cases, the constraints
252 // of the index(es) will enforce this uniqueness. Selecting DISTINCT does require
253 // overhead by the database to ensure that all values returned are distinct. Therefore,
254 // use this ONLY when you need it.
256 // For complicated queries, you can pass in the sql select statement. This would be
257 // necessary if joins are involved since by default both columns must come from the
260 // If you do query by sql statement, you must pass in the maximum lenght of column1,
261 // since it cannot be derived when you query using your own sql statement.
263 // The optional database connection can be used if you'd like the lookup class
264 // to use a database pointer other than wxGetApp().READONLY_DB. This is necessary if
265 // records are being saved, but not committed to the db, yet should be included
266 // in the lookup window.
268 ClookUpDlg::ClookUpDlg(wxWindow
*parent
, wxChar
*windowTitle
, wxChar
*tableName
,
269 wxChar
*dispCol1
, wxChar
*dispCol2
, wxChar
*where
, wxChar
*orderBy
,
270 wxDb
*pDb
, const wxString
&defDir
, bool distinctValues
,
271 wxChar
*selectStmt
, int maxLenCol1
, bool allowOk
)
272 : wxDialog (parent
, LOOKUP_DIALOG
, wxT("Select..."), wxPoint(-1, -1), wxSize(400, 290))
276 wxStrcpy(ListDB_Selection
,wxT(""));
277 wxStrcpy(ListDB_Selection2
,wxT(""));
278 widgetPtrsSet
= FALSE
;
281 noDisplayCols
= (wxStrlen(dispCol2
) ? 2 : 1);
284 wxFont
fixedFont(12,wxMODERN
,wxNORMAL
,wxNORMAL
);
286 // this is done with fixed font so that the second column (if any) will be left
287 // justified in the second column
288 pLookUpSelectList
= new wxListBox(this, LOOKUP_DIALOG_SELECT
, wxPoint(5, 15), wxSize(384, 195), 0, 0, wxLB_SINGLE
|wxLB_ALWAYS_SB
, wxDefaultValidator
, wxT("LookUpSelectList"));
290 pLookUpSelectList
->SetFont(fixedFont
);
292 pLookUpOkBtn
= new wxButton(this, LOOKUP_DIALOG_OK
, wxT("&Ok"), wxPoint(113, 222), wxSize(70, 35), 0, wxDefaultValidator
, wxT("LookUpOkBtn"));
293 pLookUpCancelBtn
= new wxButton(this, LOOKUP_DIALOG_CANCEL
, wxT("C&ancel"), wxPoint(212, 222), wxSize(70, 35), 0, wxDefaultValidator
, wxT("LookUpCancelBtn"));
295 widgetPtrsSet
= TRUE
;
297 // Query the lookup table and display the result set
298 lookup2
= new Clookup2(tableName
, dispCol1
, dispCol2
, pDb
, defDir
);
301 wxMessageBox(wxT("Error allocating memory for 'Clookup2' object."),wxT("Error..."));
306 if (!lookup2
->Open())
309 tStr
.Printf(wxT("Unable to open the table '%s'."),tableName
);
310 tStr
+= GetExtendedDBErrorMsg2(pDb
,__TFILE__
,__LINE__
);
311 wxMessageBox(tStr
,wxT("ODBC Error..."));
316 // If displaying 2 columns, determine the maximum length of column1
319 maxColLen
= col1Len
= maxLenCol1
; // user passed in max col length for column 1
322 maxColLen
= LOOKUP_COL_LEN
;
323 if (wxStrlen(dispCol2
))
325 wxString q
= wxT("SELECT MAX({fn LENGTH(");
327 q
+= wxT(")}), NULL");
335 if (!lookup2
->QueryBySqlStmt(q
))
337 wxMessageBox(wxT("ODBC error during QueryBySqlStmt()"),wxT("ODBC Error..."));
341 if (lookup2
->GetNext())
342 maxColLen
= col1Len
= wxAtoi(lookup2
->lookupCol1
);
344 wxMessageBox(wxT("ODBC error during GetNext()"),wxT("ODBC Error..."));
348 // Query the actual record set
349 if (selectStmt
&& wxStrlen(selectStmt
)) // Query by sql stmt passed in
351 if (!lookup2
->QueryBySqlStmt(selectStmt
))
353 wxMessageBox(wxT("ODBC error during QueryBySqlStmt()"),wxT("ODBC Error..."));
358 else // Query using where and order by clauses
360 lookup2
->SetOrderByClause(orderBy
);
361 lookup2
->SetWhereClause(where
);
362 if (!lookup2
->Query(FALSE
, distinctValues
))
364 wxMessageBox(wxT("ODBC error during Query()"),wxT("ODBC Error..."));
370 // Fill in the list box from the query result set
372 while (lookup2
->GetNext())
374 s
= lookup2
->lookupCol1
;
375 if (wxStrlen(dispCol2
)) // Append the optional column 2
377 s
.Append(wxT(' '), (maxColLen
+ LISTDB_NO_SPACES_BETWEEN_COLS
- wxStrlen(lookup2
->lookupCol1
)));
378 s
.Append(lookup2
->lookupCol2
);
380 pLookUpSelectList
->Append(s
);
383 // Highlight the first list item
384 pLookUpSelectList
->SetSelection(0);
386 // Make the OK activate by pressing Enter
387 if (pLookUpSelectList
->GetCount())
388 pLookUpOkBtn
->SetDefault();
391 pLookUpCancelBtn
->SetDefault();
392 pLookUpOkBtn
->Enable(FALSE
);
395 pLookUpOkBtn
->Enable(allowOk
);
397 // Display the dialog window
398 SetTitle(windowTitle
);
403 } // Generic lookup constructor 2
406 void ClookUpDlg::OnClose(wxCloseEvent
& event
)
408 widgetPtrsSet
= FALSE
;
409 GetParent()->Enable(TRUE
);
418 while (wxIsBusy()) wxEndBusyCursor();
423 } // ClookUpDlg::OnClose
426 void ClookUpDlg::OnButton( wxCommandEvent
&event
)
428 wxWindow
*win
= (wxWindow
*) event
.GetEventObject();
429 OnCommand( *win
, event
);
433 void ClookUpDlg::OnCommand(wxWindow
& win
, wxCommandEvent
& WXUNUSED(event
))
435 wxString widgetName
= win
.GetName();
440 if (widgetName
== pLookUpOkBtn
->GetName())
442 if (pLookUpSelectList
->GetSelection() != -1)
444 if (noDisplayCols
== 1)
445 wxStrcpy (ListDB_Selection
, pLookUpSelectList
->GetStringSelection());
446 else // 2 display columns
448 wxString s
= pLookUpSelectList
->GetStringSelection();
450 s
= s
.SubString(0, col1Len
-1);
452 wxStrcpy(ListDB_Selection
, s
);
454 s
= pLookUpSelectList
->GetStringSelection();
455 s
= s
.Mid(col1Len
+ LISTDB_NO_SPACES_BETWEEN_COLS
);
457 wxStrcpy(ListDB_Selection2
, s
);
462 wxStrcpy(ListDB_Selection
,wxT(""));
463 wxStrcpy(ListDB_Selection2
,wxT(""));
469 if (widgetName
== pLookUpCancelBtn
->GetName())
471 wxStrcpy (ListDB_Selection
,wxT(""));
472 wxStrcpy (ListDB_Selection2
,wxT(""));
477 }; // ClookUpDlg::OnCommand
479 // *********************************** listdb.cpp **********************************