1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindows database demo app
4 // Author: George Tasker
8 // Copyright: (c) 1998 Remstar International, Inc.
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma interface "dbtest.h"
16 #include <wx/string.h>
18 #include <wx/dbtable.h>
20 enum DialogModes
{mView
,mCreate
,mEdit
,mSearch
};
22 // ID for the menu quit command
23 #define FILE_CREATE 100
24 #define FILE_RECREATE_TABLE 110
25 #define FILE_RECREATE_INDEXES 120
27 #define EDIT_PARAMETERS 200
28 #define ABOUT_DEMO 300
30 // this seems to be missing, Robert Roebling (?)
32 #if defined(__WXMAC__)
33 #define MAX_PATH 260 /* max. length of full pathname */
35 #define MAX_PATH 256 /* max. length of full pathname */
39 // Name of the table to be created/opened
40 const wxChar CONTACT_TABLE_NAME
[] = "contacts";
42 // Number of columns in the CONTACT table
43 const int CONTACT_NO_COLS
= 12; // 0-11
45 const wxChar PARAM_FILENAME
[] = "dbtest.cfg";
48 enum Language
{langENGLISH
, langFRENCH
, langGERMAN
, langSPANISH
, langOTHER
};
50 // Forward class declarations
55 // This class contains the actual data members that are used for transferring
56 // data back and forth from the database to the program.
58 // NOTE: The object described in this class is just for example purposes, and has no
59 // real meaning other than to show each type of field being used by the database
61 class CstructContact
: public wxObject
64 wxChar Name
[50+1]; // Contact's name
69 wxChar PostalCode
[15+1];
71 TIMESTAMP_STRUCT JoinDate
; // Date on which this person joined the wxWindows project
72 Language NativeLanguage
; // Enumerated type indicating person's native language
73 bool IsDeveloper
; // Is this person a developer for wxWindows, or just a subscriber
74 UCHAR Contributions
; // Something to show off an integer field
75 ULONG LinesOfCode
; // Something to show off a 'long' field
80 // The Ccontact class derives from wxDbTable, so we have access to all
81 // of the database table functions and the local memory variables that
82 // the database classes will store the data into (and read the data from)
83 // all combined in this one class.
85 class Ccontact
: public wxDbTable
, public CstructContact
88 // Used to keep track of whether this class had a wxDb instance
89 // passed in to it or not. If an existing wxDb instance was not
90 // passed in at Ccontact creation time, then when the Ccontact
91 // instance is deleted, the connection will be freed as Ccontact
92 // created its own connection when it was created.
95 // Calls wxDbTable::SetColDefs() once for each column that is
96 // to be associated with some member variable for use with
97 // this database object.
101 // Used in places where we need to construct a WHERE clause to
102 // be passed to the SetWhereClause() function. From example,
103 // where building the WHERE clause requires using ::Printf()
104 // to build the string.
107 // WHERE string returned from the query dialog
108 wxString qryWhereStr
;
110 Ccontact(wxDb
*pwxDb
=NULL
);
115 // Contains all the index definitions and calls to wxDbTable::CreateIndex()
116 // required to create all the indexes we wish to define for this table.
117 bool CreateIndexes(void);
119 // Since we do not wish to have duplicate code blocks all over our program
120 // for a common query/fetch that we will need to do in many places, we
121 // include this member function that does it all for us in one place.
122 bool FetchByName(const wxString
&name
);
124 }; // Ccontact class definition
127 typedef struct Cparameters
129 wxChar ODBCSource
[SQL_MAX_DSN_LENGTH
+1];
130 wxChar UserName
[SQL_MAX_USER_NAME_LEN
+1];
131 wxChar Password
[SQL_MAX_AUTHSTR_LEN
+1];
132 wxChar DirPath
[MAX_PATH
+1];
136 // Define a new frame type
137 class DatabaseDemoFrame
: public wxFrame
140 CeditorDlg
*pEditorDlg
;
141 CparameterDlg
*pParamDlg
;
144 DatabaseDemoFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& sz
);
146 void OnCloseWindow(wxCloseEvent
& event
);
147 void OnCreate(wxCommandEvent
& event
);
148 void OnRecreateTable(wxCommandEvent
& event
);
149 void OnRecreateIndexes(wxCommandEvent
& event
);
150 void OnExit(wxCommandEvent
& event
);
151 void OnEditParameters(wxCommandEvent
& event
);
152 void OnAbout(wxCommandEvent
& event
);
154 void BuildEditorDialog();
155 void BuildParameterDialog(wxWindow
*parent
);
157 DECLARE_EVENT_TABLE()
158 }; // DatabaseDemoFrame
161 // Define a new application type
162 class DatabaseDemoApp
: public wxApp
165 // These are the parameters that are stored in the "PARAM_FILENAME" file
166 // that are read in at startup of the program that indicate the connection
167 // parameters to be used for connecting to the ODBC data source.
170 // Pointer to the main frame used by the App
171 DatabaseDemoFrame
*DemoFrame
;
173 // Pointer to the main database connection used in the program. This
174 // pointer would normally be used for doing things as database lookups
175 // for user login names and passwords, getting workstation settings, etc.
177 // ---> IMPORTANT <---
179 // For each database object created which uses this wxDb pointer
180 // connection to the database, when a CommitTrans() or RollBackTrans()
181 // will commit or rollback EVERY object which uses this wxDb pointer.
183 // To allow each table object (those derived from wxDbTable) to be
184 // individually committed or rolled back, you MUST use a different
185 // instance of wxDb in the constructor of the table. Doing so creates
186 // more overhead, and will use more database connections (some DBs have
187 // connection limits...), so use connections sparringly.
189 // It is recommended that one "main" database connection be created for
190 // the entire program to use for READ-ONLY database accesses, but for each
191 // table object which will do a CommitTrans() or RollbackTrans() that a
192 // new wxDb object be created and used for it.
195 // Contains the ODBC connection information used by
196 // all database connections
197 wxDbConnectInf
*DbConnectInf
;
201 // Read/Write ODBC connection parameters to the "PARAM_FILENAME" file
202 bool ReadParamFile(Cparameters
¶ms
);
203 bool WriteParamFile(Cparameters
¶ms
);
205 void CreateDataTable(bool recreate
);
206 }; // DatabaseDemoApp
209 DECLARE_APP(DatabaseDemoApp
)
212 // *************************** CeditorDlg ***************************
214 class CeditorDlg
: public wxPanel
217 // Used to indicate whether all of the widget pointers (defined
218 // below) have been initialized to point to the memory for
219 // the named widget. Used as a safeguard from using the widget
220 // before it has been initialized.
223 // Used when the EDIT button has been pressed to maintain the
224 // original name that was displayed in the editor before the
225 // EDIT button was pressed, so that if CANCEL is pressed, a
226 // FetchByName() can be done to retrieve the original data
227 // to repopulate the dialog.
230 // Pointers to all widgets on the dialog
231 wxButton
*pCreateBtn
, *pEditBtn
, *pDeleteBtn
, *pCopyBtn
, *pSaveBtn
, *pCancelBtn
;
232 wxButton
*pPrevBtn
, *pNextBtn
, *pQueryBtn
, *pResetBtn
, *pDoneBtn
, *pHelpBtn
;
233 wxButton
*pNameListBtn
;
234 wxTextCtrl
*pNameTxt
, *pAddress1Txt
, *pAddress2Txt
,*pCityTxt
, *pStateTxt
, *pCountryTxt
,*pPostalCodeTxt
;
235 wxStaticText
*pNameMsg
, *pAddress1Msg
, *pAddress2Msg
,*pCityMsg
, *pStateMsg
, *pCountryMsg
,*pPostalCodeMsg
;
236 wxTextCtrl
*pJoinDateTxt
,*pContribTxt
, *pLinesTxt
;
237 wxStaticText
*pJoinDateMsg
,*pContribMsg
, *pLinesMsg
;
238 wxRadioBox
*pDeveloperRadio
;
239 wxChoice
*pNativeLangChoice
;
240 wxStaticText
*pNativeLangMsg
;
243 // Indicates if the editor dialog has been initialized yet (used to
244 // help trap if the Initialize() function failed to load all required
248 enum DialogModes mode
;
250 // Pointer to the wxDbTable instance that is used to manipulate
251 // the data in memory and in the database
254 CeditorDlg(wxWindow
*parent
);
256 void OnCloseWindow(wxCloseEvent
& event
);
257 void OnButton( wxCommandEvent
&event
);
258 void OnCommand(wxWindow
& win
, wxCommandEvent
& event
);
259 void OnActivate(bool) {}; // necessary for hot keys
263 // Sets wxStaticText fields to be editable or not depending
264 // on the current value of 'mode'
265 void FieldsEditable();
267 // Sets the editor mode, determining what state widgets
268 // on the dialog are to be in based on the operation
270 void SetMode(enum DialogModes m
);
272 // Update/Retrieve data from the widgets on the dialog
276 // Inserts/updates the database with the current data
277 // retrieved from the editor dialog
280 // Database functions for changing the data that is to
281 // be displayed on the dialog. GetNextRec()/GetPrevRec()
282 // provide database independent methods that do not require
283 // backward scrolling cursors to obtain the record that
284 // is prior to the current record in the search sequence.
287 bool GetRec(const wxString
&whereStr
);
289 DECLARE_EVENT_TABLE()
292 #define EDITOR_DIALOG 199
294 // Editor dialog control ids
295 #define EDITOR_DIALOG_FN_GROUP 200
296 #define EDITOR_DIALOG_SEARCH_GROUP 201
297 #define EDITOR_DIALOG_CREATE 202
298 #define EDITOR_DIALOG_EDIT 203
299 #define EDITOR_DIALOG_DELETE 204
300 #define EDITOR_DIALOG_COPY 205
301 #define EDITOR_DIALOG_SAVE 206
302 #define EDITOR_DIALOG_CANCEL 207
303 #define EDITOR_DIALOG_PREV 208
304 #define EDITOR_DIALOG_NEXT 209
305 #define EDITOR_DIALOG_QUERY 211
306 #define EDITOR_DIALOG_RESET 212
307 #define EDITOR_DIALOG_NAME_MSG 213
308 #define EDITOR_DIALOG_NAME_TEXT 214
309 #define EDITOR_DIALOG_LOOKUP 215
310 #define EDITOR_DIALOG_ADDRESS1_MSG 216
311 #define EDITOR_DIALOG_ADDRESS1_TEXT 217
312 #define EDITOR_DIALOG_ADDRESS2_MSG 218
313 #define EDITOR_DIALOG_ADDRESS2_TEXT 219
314 #define EDITOR_DIALOG_CITY_MSG 220
315 #define EDITOR_DIALOG_CITY_TEXT 221
316 #define EDITOR_DIALOG_COUNTRY_MSG 222
317 #define EDITOR_DIALOG_COUNTRY_TEXT 223
318 #define EDITOR_DIALOG_POSTAL_MSG 224
319 #define EDITOR_DIALOG_POSTAL_TEXT 225
320 #define EDITOR_DIALOG_LANG_MSG 226
321 #define EDITOR_DIALOG_LANG_CHOICE 227
322 #define EDITOR_DIALOG_DATE_MSG 228
323 #define EDITOR_DIALOG_DATE_TEXT 229
324 #define EDITOR_DIALOG_CONTRIB_MSG 230
325 #define EDITOR_DIALOG_CONTRIB_TEXT 231
326 #define EDITOR_DIALOG_LINES_MSG 232
327 #define EDITOR_DIALOG_LINES_TEXT 233
328 #define EDITOR_DIALOG_STATE_MSG 234
329 #define EDITOR_DIALOG_STATE_TEXT 235
330 #define EDITOR_DIALOG_DEVELOPER 236
331 #define EDITOR_DIALOG_JOIN_MSG 237
332 #define EDITOR_DIALOG_JOIN_TEXT 238
334 // *************************** CparameterDlg ***************************
336 class CparameterDlg
: public wxDialog
339 // Used to indicate whether all of the widget pointers (defined
340 // below) have been initialized to point to the memory for
341 // the named widget. Used as a safeguard from using the widget
342 // before it has been initialized.
345 enum DialogModes mode
;
347 // Have the parameters been saved yet, or do they
348 // need to be saved to update the params on disk
352 Cparameters savedParamSettings
;
354 // Pointers to all widgets on the dialog
355 wxStaticText
*pParamODBCSourceMsg
;
356 wxListBox
*pParamODBCSourceList
;
357 wxStaticText
*pParamUserNameMsg
, *pParamPasswordMsg
, *pParamDirPathMsg
;
358 wxTextCtrl
*pParamUserNameTxt
, *pParamPasswordTxt
, *pParamDirPathTxt
;
359 wxButton
*pParamSaveBtn
, *pParamCancelBtn
;
362 CparameterDlg(wxWindow
*parent
);
364 void OnCloseWindow(wxCloseEvent
& event
);
365 void OnButton( wxCommandEvent
&event
);
366 void OnCommand(wxWindow
& win
, wxCommandEvent
& event
);
367 void OnActivate(bool) {}; // necessary for hot keys
369 // Update/Retrieve data from the widgets on the dialog
373 // Stores the defined parameter for connecting to the selected ODBC
374 // data source to the config file name in "PARAM_FILENAME"
377 // Populates the 'pParamODBCSourceList' listbox with the names of all
378 // ODBC datasource defined for use at the current workstation
379 void FillDataSourceList();
381 DECLARE_EVENT_TABLE()
384 #define PARAMETER_DIALOG 400
386 // Parameter dialog control ids
387 #define PARAMETER_DIALOG_SOURCE_MSG 401
388 #define PARAMETER_DIALOG_SOURCE_LISTBOX 402
389 #define PARAMETER_DIALOG_NAME_MSG 403
390 #define PARAMETER_DIALOG_NAME_TEXT 404
391 #define PARAMETER_DIALOG_PASSWORD_MSG 405
392 #define PARAMETER_DIALOG_PASSWORD_TEXT 406
393 #define PARAMETER_DIALOG_DIRPATH_MSG 407
394 #define PARAMETER_DIALOG_DIRPATH_TEXT 408
395 #define PARAMETER_DIALOG_SAVE 409
396 #define PARAMETER_DIALOG_CANCEL 410
398 // *************************** CqueryDlg ***************************
417 wxChar
* const langQRY_EQ
= "column = column | value";
418 wxChar
* const langQRY_LT
= "column < column | value";
419 wxChar
* const langQRY_GT
= "column > column | value";
420 wxChar
* const langQRY_LE
= "column <= column | value";
421 wxChar
* const langQRY_GE
= "column >= column | value";
422 wxChar
* const langQRY_BEGINS
= "columns that BEGIN with the string entered";
423 wxChar
* const langQRY_CONTAINS
= "columns that CONTAIN the string entered";
424 wxChar
* const langQRY_LIKE
= "% matches 0 or more of any char; _ matches 1 char";
425 wxChar
* const langQRY_BETWEEN
= "column BETWEEN value AND value";
428 class CqueryDlg
: public wxDialog
431 wxDbColInf
*colInf
; // Column inf. returned by db->GetColumns()
432 wxDbTable
*dbTable
; // generic wxDbTable object for attaching to the table to query
433 wxChar
*masterTableName
; // Name of the table that 'dbTable' will be associated with
434 wxString pWhere
; // A pointer to the storage for the resulting where clause
438 // Used to indicate whether all of the widget pointers (defined
439 // below) have been initialized to point to the memory for
440 // the named widget. Used as a safeguard from using the widget
441 // before it has been initialized.
445 wxStaticText
*pQueryCol1Msg
;
446 wxChoice
*pQueryCol1Choice
;
447 wxStaticText
*pQueryNotMsg
;
448 wxCheckBox
*pQueryNotCheck
;
449 wxStaticText
*pQueryOperatorMsg
;
450 wxChoice
*pQueryOperatorChoice
;
451 wxStaticText
*pQueryCol2Msg
;
452 wxChoice
*pQueryCol2Choice
;
453 wxStaticText
*pQueryValue1Msg
;
454 wxTextCtrl
*pQueryValue1Txt
;
455 wxStaticText
*pQueryValue2Msg
;
456 wxTextCtrl
*pQueryValue2Txt
;
457 wxStaticText
*pQuerySqlWhereMsg
;
458 wxTextCtrl
*pQuerySqlWhereMtxt
;
459 wxButton
*pQueryAddBtn
;
460 wxButton
*pQueryAndBtn
;
461 wxButton
*pQueryOrBtn
;
462 wxButton
*pQueryLParenBtn
;
463 wxButton
*pQueryRParenBtn
;
464 wxButton
*pQueryDoneBtn
;
465 wxButton
*pQueryClearBtn
;
466 wxButton
*pQueryCountBtn
;
467 wxButton
*pQueryHelpBtn
;
468 wxStaticBox
*pQueryHintGrp
;
469 wxStaticText
*pQueryHintMsg
;
471 wxTextCtrl
*pFocusTxt
;
473 CqueryDlg(wxWindow
*parent
, wxDb
*pDb
, wxChar
*tblName
[], const wxString
&pWhereArg
);
476 void OnButton( wxCommandEvent
&event
);
477 void OnCommand(wxWindow
& win
, wxCommandEvent
& event
);
478 void OnCloseWindow(wxCloseEvent
& event
);
479 void OnActivate(bool) {}; // necessary for hot keys
481 void AppendToWhere(wxChar
*s
);
482 void ProcessAddBtn();
483 void ProcessCountBtn();
484 bool ValidateWhereClause();
486 DECLARE_EVENT_TABLE()
489 #define QUERY_DIALOG 300
491 // Parameter dialog control ids
492 #define QUERY_DIALOG_COL_MSG 301
493 #define QUERY_DIALOG_COL_CHOICE 302
494 #define QUERY_DIALOG_NOT_MSG 303
495 #define QUERY_DIALOG_NOT_CHECKBOX 304
496 #define QUERY_DIALOG_OP_MSG 305
497 #define QUERY_DIALOG_OP_CHOICE 306
498 #define QUERY_DIALOG_COL2_MSG 307
499 #define QUERY_DIALOG_COL2_CHOICE 308
500 #define QUERY_DIALOG_WHERE_MSG 309
501 #define QUERY_DIALOG_WHERE_TEXT 310
502 #define QUERY_DIALOG_ADD 311
503 #define QUERY_DIALOG_AND 312
504 #define QUERY_DIALOG_OR 313
505 #define QUERY_DIALOG_LPAREN 314
506 #define QUERY_DIALOG_RPAREN 315
507 #define QUERY_DIALOG_DONE 316
508 #define QUERY_DIALOG_CLEAR 317
509 #define QUERY_DIALOG_COUNT 318
510 #define QUERY_DIALOG_VALUE1_MSG 319
511 #define QUERY_DIALOG_VALUE1_TEXT 320
512 #define QUERY_DIALOG_VALUE2_MSG 321
513 #define QUERY_DIALOG_VALUE2_TEXT 322
514 #define QUERY_DIALOG_HINT_GROUP 323
515 #define QUERY_DIALOG_HINT_MSG 324