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>
17 #include <wx/dbtable.h>
19 enum DialogModes
{mView
,mCreate
,mEdit
,mSearch
};
21 // ID for the menu quit command
22 #define FILE_CREATE 100
23 #define FILE_RECREATE_TABLE 110
24 #define FILE_RECREATE_INDEXES 120
26 #define EDIT_PARAMETERS 200
27 #define ABOUT_DEMO 300
29 // this seems to be missing, Robert Roebling (?)
31 #if defined(__WXMAC__)
32 #define MAX_PATH 260 /* max. length of full pathname */
34 #define MAX_PATH 256 /* max. length of full pathname */
38 // Name of the table to be created/opened
39 const wxChar CONTACT_TABLE_NAME
[] = "contacts";
42 #define wxODBC_BLOB_EXPERIMENT 0
44 // Number of columns in the CONTACT table
45 #if wxODBC_BLOB_EXPERIMENT > 0
46 const int CONTACT_NO_COLS
= 13; // 0-12
48 const int CONTACT_NO_COLS
= 12; // 0-11
51 const wxChar PARAM_FILENAME
[] = "dbtest.cfg";
54 enum Language
{langENGLISH
, langFRENCH
, langGERMAN
, langSPANISH
, langOTHER
};
56 // Forward class declarations
61 // This class contains the actual data members that are used for transferring
62 // data back and forth from the database to the program.
64 // NOTE: The object described in this class is just for example purposes, and has no
65 // real meaning other than to show each type of field being used by the database
67 class CstructContact
: public wxObject
70 wxChar Name
[50+1]; // Contact's name
75 wxChar PostalCode
[15+1];
77 TIMESTAMP_STRUCT JoinDate
; // Date on which this person joined the wxWindows project
78 Language NativeLanguage
; // Enumerated type indicating person's native language
79 #if wxODBC_BLOB_EXPERIMENT > 0
80 wxChar Picture
[50000];
82 bool IsDeveloper
; // Is this person a developer for wxWindows, or just a subscriber
83 UCHAR Contributions
; // Something to show off an integer field
84 ULONG LinesOfCode
; // Something to show off a 'long' field
89 // The Ccontact class derives from wxDbTable, so we have access to all
90 // of the database table functions and the local memory variables that
91 // the database classes will store the data into (and read the data from)
92 // all combined in this one class.
94 class Ccontact
: public wxDbTable
, public CstructContact
97 // Used to keep track of whether this class had a wxDb instance
98 // passed in to it or not. If an existing wxDb instance was not
99 // passed in at Ccontact creation time, then when the Ccontact
100 // instance is deleted, the connection will be freed as Ccontact
101 // created its own connection when it was created.
104 // Calls wxDbTable::SetColDefs() once for each column that is
105 // to be associated with some member variable for use with
106 // this database object.
110 // Used in places where we need to construct a WHERE clause to
111 // be passed to the SetWhereClause() function. From example,
112 // where building the WHERE clause requires using ::Printf()
113 // to build the string.
116 // WHERE string returned from the query dialog
117 wxString qryWhereStr
;
119 Ccontact(wxDb
*pwxDb
=NULL
);
124 // Contains all the index definitions and calls to wxDbTable::CreateIndex()
125 // required to create all the indexes we wish to define for this table.
126 bool CreateIndexes(void);
128 // Since we do not wish to have duplicate code blocks all over our program
129 // for a common query/fetch that we will need to do in many places, we
130 // include this member function that does it all for us in one place.
131 bool FetchByName(const wxString
&name
);
133 }; // Ccontact class definition
136 typedef struct Cparameters
138 wxChar ODBCSource
[SQL_MAX_DSN_LENGTH
+1];
139 wxChar UserName
[SQL_MAX_USER_NAME_LEN
+1];
140 wxChar Password
[SQL_MAX_AUTHSTR_LEN
+1];
141 wxChar DirPath
[MAX_PATH
+1];
145 // Define a new frame type
146 class DatabaseDemoFrame
: public wxFrame
149 CeditorDlg
*pEditorDlg
;
150 CparameterDlg
*pParamDlg
;
153 DatabaseDemoFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& sz
);
155 void OnCloseWindow(wxCloseEvent
& event
);
156 void OnCreate(wxCommandEvent
& event
);
157 void OnRecreateTable(wxCommandEvent
& event
);
158 void OnRecreateIndexes(wxCommandEvent
& event
);
159 void OnExit(wxCommandEvent
& event
);
160 void OnEditParameters(wxCommandEvent
& event
);
161 void OnAbout(wxCommandEvent
& event
);
163 void BuildEditorDialog();
164 void BuildParameterDialog(wxWindow
*parent
);
166 DECLARE_EVENT_TABLE()
167 }; // DatabaseDemoFrame
170 // Define a new application type
171 class DatabaseDemoApp
: public wxApp
174 // These are the parameters that are stored in the "PARAM_FILENAME" file
175 // that are read in at startup of the program that indicate the connection
176 // parameters to be used for connecting to the ODBC data source.
179 // Pointer to the main frame used by the App
180 DatabaseDemoFrame
*DemoFrame
;
182 // Pointer to the main database connection used in the program. This
183 // pointer would normally be used for doing things as database lookups
184 // for user login names and passwords, getting workstation settings, etc.
186 // ---> IMPORTANT <---
188 // For each database object created which uses this wxDb pointer
189 // connection to the database, when a CommitTrans() or RollBackTrans()
190 // will commit or rollback EVERY object which uses this wxDb pointer.
192 // To allow each table object (those derived from wxDbTable) to be
193 // individually committed or rolled back, you MUST use a different
194 // instance of wxDb in the constructor of the table. Doing so creates
195 // more overhead, and will use more database connections (some DBs have
196 // connection limits...), so use connections sparringly.
198 // It is recommended that one "main" database connection be created for
199 // the entire program to use for READ-ONLY database accesses, but for each
200 // table object which will do a CommitTrans() or RollbackTrans() that a
201 // new wxDb object be created and used for it.
204 // Contains the ODBC connection information used by
205 // all database connections
206 wxDbConnectInf
*DbConnectInf
;
210 // Read/Write ODBC connection parameters to the "PARAM_FILENAME" file
211 bool ReadParamFile(Cparameters
¶ms
);
212 bool WriteParamFile(Cparameters
¶ms
);
214 void CreateDataTable(bool recreate
);
215 }; // DatabaseDemoApp
218 DECLARE_APP(DatabaseDemoApp
)
221 // *************************** CeditorDlg ***************************
223 class CeditorDlg
: public wxPanel
226 // Used to indicate whether all of the widget pointers (defined
227 // below) have been initialized to point to the memory for
228 // the named widget. Used as a safeguard from using the widget
229 // before it has been initialized.
232 // Used when the EDIT button has been pressed to maintain the
233 // original name that was displayed in the editor before the
234 // EDIT button was pressed, so that if CANCEL is pressed, a
235 // FetchByName() can be done to retrieve the original data
236 // to repopulate the dialog.
239 // Pointers to all widgets on the dialog
240 wxButton
*pCreateBtn
, *pEditBtn
, *pDeleteBtn
, *pCopyBtn
, *pSaveBtn
, *pCancelBtn
;
241 wxButton
*pPrevBtn
, *pNextBtn
, *pQueryBtn
, *pResetBtn
, *pDoneBtn
, *pHelpBtn
;
242 wxButton
*pNameListBtn
;
243 wxTextCtrl
*pNameTxt
, *pAddress1Txt
, *pAddress2Txt
,*pCityTxt
, *pStateTxt
, *pCountryTxt
,*pPostalCodeTxt
;
244 wxStaticText
*pNameMsg
, *pAddress1Msg
, *pAddress2Msg
,*pCityMsg
, *pStateMsg
, *pCountryMsg
,*pPostalCodeMsg
;
245 wxTextCtrl
*pJoinDateTxt
,*pContribTxt
, *pLinesTxt
;
246 wxStaticText
*pJoinDateMsg
,*pContribMsg
, *pLinesMsg
;
247 wxRadioBox
*pDeveloperRadio
;
248 wxChoice
*pNativeLangChoice
;
249 wxStaticText
*pNativeLangMsg
;
252 // Indicates if the editor dialog has been initialized yet (used to
253 // help trap if the Initialize() function failed to load all required
257 enum DialogModes mode
;
259 // Pointer to the wxDbTable instance that is used to manipulate
260 // the data in memory and in the database
263 CeditorDlg(wxWindow
*parent
);
265 void OnCloseWindow(wxCloseEvent
& event
);
266 void OnButton( wxCommandEvent
&event
);
267 void OnCommand(wxWindow
& win
, wxCommandEvent
& event
);
268 void OnActivate(bool) {}; // necessary for hot keys
272 // Sets wxStaticText fields to be editable or not depending
273 // on the current value of 'mode'
274 void FieldsEditable();
276 // Sets the editor mode, determining what state widgets
277 // on the dialog are to be in based on the operation
279 void SetMode(enum DialogModes m
);
281 // Update/Retrieve data from the widgets on the dialog
285 // Inserts/updates the database with the current data
286 // retrieved from the editor dialog
289 // Database functions for changing the data that is to
290 // be displayed on the dialog. GetNextRec()/GetPrevRec()
291 // provide database independent methods that do not require
292 // backward scrolling cursors to obtain the record that
293 // is prior to the current record in the search sequence.
296 bool GetRec(const wxString
&whereStr
);
298 DECLARE_EVENT_TABLE()
301 #define EDITOR_DIALOG 199
303 // Editor dialog control ids
304 #define EDITOR_DIALOG_FN_GROUP 200
305 #define EDITOR_DIALOG_SEARCH_GROUP 201
306 #define EDITOR_DIALOG_CREATE 202
307 #define EDITOR_DIALOG_EDIT 203
308 #define EDITOR_DIALOG_DELETE 204
309 #define EDITOR_DIALOG_COPY 205
310 #define EDITOR_DIALOG_SAVE 206
311 #define EDITOR_DIALOG_CANCEL 207
312 #define EDITOR_DIALOG_PREV 208
313 #define EDITOR_DIALOG_NEXT 209
314 #define EDITOR_DIALOG_QUERY 211
315 #define EDITOR_DIALOG_RESET 212
316 #define EDITOR_DIALOG_NAME_MSG 213
317 #define EDITOR_DIALOG_NAME_TEXT 214
318 #define EDITOR_DIALOG_LOOKUP 215
319 #define EDITOR_DIALOG_ADDRESS1_MSG 216
320 #define EDITOR_DIALOG_ADDRESS1_TEXT 217
321 #define EDITOR_DIALOG_ADDRESS2_MSG 218
322 #define EDITOR_DIALOG_ADDRESS2_TEXT 219
323 #define EDITOR_DIALOG_CITY_MSG 220
324 #define EDITOR_DIALOG_CITY_TEXT 221
325 #define EDITOR_DIALOG_COUNTRY_MSG 222
326 #define EDITOR_DIALOG_COUNTRY_TEXT 223
327 #define EDITOR_DIALOG_POSTAL_MSG 224
328 #define EDITOR_DIALOG_POSTAL_TEXT 225
329 #define EDITOR_DIALOG_LANG_MSG 226
330 #define EDITOR_DIALOG_LANG_CHOICE 227
331 #define EDITOR_DIALOG_DATE_MSG 228
332 #define EDITOR_DIALOG_DATE_TEXT 229
333 #define EDITOR_DIALOG_CONTRIB_MSG 230
334 #define EDITOR_DIALOG_CONTRIB_TEXT 231
335 #define EDITOR_DIALOG_LINES_MSG 232
336 #define EDITOR_DIALOG_LINES_TEXT 233
337 #define EDITOR_DIALOG_STATE_MSG 234
338 #define EDITOR_DIALOG_STATE_TEXT 235
339 #define EDITOR_DIALOG_DEVELOPER 236
340 #define EDITOR_DIALOG_JOIN_MSG 237
341 #define EDITOR_DIALOG_JOIN_TEXT 238
343 // *************************** CparameterDlg ***************************
345 class CparameterDlg
: public wxDialog
348 // Used to indicate whether all of the widget pointers (defined
349 // below) have been initialized to point to the memory for
350 // the named widget. Used as a safeguard from using the widget
351 // before it has been initialized.
354 enum DialogModes mode
;
356 // Have the parameters been saved yet, or do they
357 // need to be saved to update the params on disk
361 Cparameters savedParamSettings
;
363 // Pointers to all widgets on the dialog
364 wxStaticText
*pParamODBCSourceMsg
;
365 wxListBox
*pParamODBCSourceList
;
366 wxStaticText
*pParamUserNameMsg
, *pParamPasswordMsg
, *pParamDirPathMsg
;
367 wxTextCtrl
*pParamUserNameTxt
, *pParamPasswordTxt
, *pParamDirPathTxt
;
368 wxButton
*pParamSaveBtn
, *pParamCancelBtn
;
371 CparameterDlg(wxWindow
*parent
);
373 void OnCloseWindow(wxCloseEvent
& event
);
374 void OnButton( wxCommandEvent
&event
);
375 void OnCommand(wxWindow
& win
, wxCommandEvent
& event
);
376 void OnActivate(bool) {}; // necessary for hot keys
378 // Update/Retrieve data from the widgets on the dialog
382 // Stores the defined parameter for connecting to the selected ODBC
383 // data source to the config file name in "PARAM_FILENAME"
386 // Populates the 'pParamODBCSourceList' listbox with the names of all
387 // ODBC datasource defined for use at the current workstation
388 void FillDataSourceList();
390 DECLARE_EVENT_TABLE()
393 #define PARAMETER_DIALOG 400
395 // Parameter dialog control ids
396 #define PARAMETER_DIALOG_SOURCE_MSG 401
397 #define PARAMETER_DIALOG_SOURCE_LISTBOX 402
398 #define PARAMETER_DIALOG_NAME_MSG 403
399 #define PARAMETER_DIALOG_NAME_TEXT 404
400 #define PARAMETER_DIALOG_PASSWORD_MSG 405
401 #define PARAMETER_DIALOG_PASSWORD_TEXT 406
402 #define PARAMETER_DIALOG_DIRPATH_MSG 407
403 #define PARAMETER_DIALOG_DIRPATH_TEXT 408
404 #define PARAMETER_DIALOG_SAVE 409
405 #define PARAMETER_DIALOG_CANCEL 410
407 // *************************** CqueryDlg ***************************
426 wxChar
* const langQRY_EQ
= "column = column | value";
427 wxChar
* const langQRY_LT
= "column < column | value";
428 wxChar
* const langQRY_GT
= "column > column | value";
429 wxChar
* const langQRY_LE
= "column <= column | value";
430 wxChar
* const langQRY_GE
= "column >= column | value";
431 wxChar
* const langQRY_BEGINS
= "columns that BEGIN with the string entered";
432 wxChar
* const langQRY_CONTAINS
= "columns that CONTAIN the string entered";
433 wxChar
* const langQRY_LIKE
= "% matches 0 or more of any char; _ matches 1 char";
434 wxChar
* const langQRY_BETWEEN
= "column BETWEEN value AND value";
437 class CqueryDlg
: public wxDialog
440 wxDbColInf
*colInf
; // Column inf. returned by db->GetColumns()
441 wxDbTable
*dbTable
; // generic wxDbTable object for attaching to the table to query
442 wxChar
*masterTableName
; // Name of the table that 'dbTable' will be associated with
443 wxString pWhere
; // A pointer to the storage for the resulting where clause
447 // Used to indicate whether all of the widget pointers (defined
448 // below) have been initialized to point to the memory for
449 // the named widget. Used as a safeguard from using the widget
450 // before it has been initialized.
454 wxStaticText
*pQueryCol1Msg
;
455 wxChoice
*pQueryCol1Choice
;
456 wxStaticText
*pQueryNotMsg
;
457 wxCheckBox
*pQueryNotCheck
;
458 wxStaticText
*pQueryOperatorMsg
;
459 wxChoice
*pQueryOperatorChoice
;
460 wxStaticText
*pQueryCol2Msg
;
461 wxChoice
*pQueryCol2Choice
;
462 wxStaticText
*pQueryValue1Msg
;
463 wxTextCtrl
*pQueryValue1Txt
;
464 wxStaticText
*pQueryValue2Msg
;
465 wxTextCtrl
*pQueryValue2Txt
;
466 wxStaticText
*pQuerySqlWhereMsg
;
467 wxTextCtrl
*pQuerySqlWhereMtxt
;
468 wxButton
*pQueryAddBtn
;
469 wxButton
*pQueryAndBtn
;
470 wxButton
*pQueryOrBtn
;
471 wxButton
*pQueryLParenBtn
;
472 wxButton
*pQueryRParenBtn
;
473 wxButton
*pQueryDoneBtn
;
474 wxButton
*pQueryClearBtn
;
475 wxButton
*pQueryCountBtn
;
476 wxButton
*pQueryHelpBtn
;
477 wxStaticBox
*pQueryHintGrp
;
478 wxStaticText
*pQueryHintMsg
;
480 wxTextCtrl
*pFocusTxt
;
482 CqueryDlg(wxWindow
*parent
, wxDb
*pDb
, wxChar
*tblName
[], const wxString
&pWhereArg
);
485 void OnButton( wxCommandEvent
&event
);
486 void OnCommand(wxWindow
& win
, wxCommandEvent
& event
);
487 void OnCloseWindow(wxCloseEvent
& event
);
488 void OnActivate(bool) {}; // necessary for hot keys
490 void AppendToWhere(wxChar
*s
);
491 void ProcessAddBtn();
492 void ProcessCountBtn();
493 bool ValidateWhereClause();
495 DECLARE_EVENT_TABLE()
498 #define QUERY_DIALOG 300
500 // Parameter dialog control ids
501 #define QUERY_DIALOG_COL_MSG 301
502 #define QUERY_DIALOG_COL_CHOICE 302
503 #define QUERY_DIALOG_NOT_MSG 303
504 #define QUERY_DIALOG_NOT_CHECKBOX 304
505 #define QUERY_DIALOG_OP_MSG 305
506 #define QUERY_DIALOG_OP_CHOICE 306
507 #define QUERY_DIALOG_COL2_MSG 307
508 #define QUERY_DIALOG_COL2_CHOICE 308
509 #define QUERY_DIALOG_WHERE_MSG 309
510 #define QUERY_DIALOG_WHERE_TEXT 310
511 #define QUERY_DIALOG_ADD 311
512 #define QUERY_DIALOG_AND 312
513 #define QUERY_DIALOG_OR 313
514 #define QUERY_DIALOG_LPAREN 314
515 #define QUERY_DIALOG_RPAREN 315
516 #define QUERY_DIALOG_DONE 316
517 #define QUERY_DIALOG_CLEAR 317
518 #define QUERY_DIALOG_COUNT 318
519 #define QUERY_DIALOG_VALUE1_MSG 319
520 #define QUERY_DIALOG_VALUE1_TEXT 320
521 #define QUERY_DIALOG_VALUE2_MSG 321
522 #define QUERY_DIALOG_VALUE2_TEXT 322
523 #define QUERY_DIALOG_HINT_GROUP 323
524 #define QUERY_DIALOG_HINT_MSG 324