]> git.saurik.com Git - wxWidgets.git/blob - samples/db/dbtest.h
More changes to use the latest wxDb/wxDbTable method enhancements
[wxWidgets.git] / samples / db / dbtest.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dbtest.h
3 // Purpose: wxWindows database demo app
4 // Author: George Tasker
5 // Modified by:
6 // Created: 1998
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Remstar International, Inc.
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma interface "dbtest.h"
14 #endif
15
16 #include <wx/string.h>
17 #include <wx/db.h>
18 #include <wx/dbtable.h>
19
20 enum DialogModes {mView,mCreate,mEdit,mSearch};
21
22 // ID for the menu quit command
23 #define FILE_CREATE 100
24 #define FILE_RECREATE_TABLE 110
25 #define FILE_RECREATE_INDEXES 120
26 #define FILE_EXIT 199
27 #define EDIT_PARAMETERS 200
28 #define ABOUT_DEMO 300
29
30 // this seems to be missing, Robert Roebling (?)
31 #ifndef MAX_PATH
32 #if defined(__WXMAC__)
33 #define MAX_PATH 260 /* max. length of full pathname */
34 #else /* _MAC */
35 #define MAX_PATH 256 /* max. length of full pathname */
36 #endif
37 #endif
38
39 // Name of the table to be created/opened
40 const wxChar CONTACT_TABLE_NAME[] = "contacts";
41
42 // Number of columns in the CONTACT table
43 const int CONTACT_NO_COLS = 12; // 0-11
44
45 const wxChar PARAM_FILENAME[] = "dbtest.cfg";
46
47
48 enum Language {langENGLISH, langFRENCH, langGERMAN, langSPANISH, langOTHER};
49
50 // Forward class declarations
51 class CeditorDlg;
52 class CparameterDlg;
53
54 //
55 // This class contains the actual data members that are used for transferring
56 // data back and forth from the database to the program.
57 //
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
60 //
61 class CstructContact : public wxObject
62 {
63 public:
64 wxChar Name[50+1]; // Contact's name
65 wxChar Addr1[50+1];
66 wxChar Addr2[50+1];
67 wxChar City[25+1];
68 wxChar State[25+1];
69 wxChar PostalCode[15+1];
70 wxChar Country[20+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
76 }; // CstructContact
77
78
79 //
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.
84 //
85 class Ccontact : public wxDbTable, public CstructContact
86 {
87 private:
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.
93 bool freeDbConn;
94
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.
98 void SetupColumns();
99
100 public:
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.
105 wxString whereStr;
106
107 // WHERE string returned from the query dialog
108 wxString qryWhereStr;
109
110 Ccontact(wxDb *pwxDb=NULL);
111 ~Ccontact();
112
113 void Initialize();
114
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);
118
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);
123
124 }; // Ccontact class definition
125
126
127 typedef struct Cparameters
128 {
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];
133 } Cparameters;
134
135
136 // Define a new frame type
137 class DatabaseDemoFrame: public wxFrame
138 {
139 private:
140 CeditorDlg *pEditorDlg;
141 CparameterDlg *pParamDlg;
142
143 public:
144 DatabaseDemoFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& sz);
145
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);
153
154 void BuildEditorDialog();
155 void BuildParameterDialog(wxWindow *parent);
156
157 DECLARE_EVENT_TABLE()
158 }; // DatabaseDemoFrame
159
160
161 // Define a new application type
162 class DatabaseDemoApp: public wxApp
163 {
164 public:
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.
168 Cparameters params;
169
170 // Pointer to the main frame used by the App
171 DatabaseDemoFrame *DemoFrame;
172
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.
176 //
177 // ---> IMPORTANT <---
178 //
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.
182 //
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.
188 //
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.
193 wxDb *READONLY_DB;
194
195 // Contains the ODBC connection information used by
196 // all database connections
197 wxDbConnectInf *DbConnectInf;
198
199 bool OnInit();
200
201 // Read/Write ODBC connection parameters to the "PARAM_FILENAME" file
202 bool ReadParamFile(Cparameters &params);
203 bool WriteParamFile(Cparameters &params);
204
205 void CreateDataTable(bool recreate);
206 }; // DatabaseDemoApp
207
208
209 DECLARE_APP(DatabaseDemoApp)
210
211
212 // *************************** CeditorDlg ***************************
213
214 class CeditorDlg : public wxPanel
215 {
216 private:
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.
221 bool widgetPtrsSet;
222
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.
228 wxString saveName;
229
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;
241
242 public:
243 // Indicates if the editor dialog has been initialized yet (used to
244 // help trap if the Initialize() function failed to load all required
245 // resources or not.
246 bool initialized;
247
248 enum DialogModes mode;
249
250 // Pointer to the wxDbTable instance that is used to manipulate
251 // the data in memory and in the database
252 Ccontact *Contact;
253
254 CeditorDlg(wxWindow *parent);
255
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
260
261 bool Initialize();
262
263 // Sets wxStaticText fields to be editable or not depending
264 // on the current value of 'mode'
265 void FieldsEditable();
266
267 // Sets the editor mode, determining what state widgets
268 // on the dialog are to be in based on the operation
269 // being performed.
270 void SetMode(enum DialogModes m);
271
272 // Update/Retrieve data from the widgets on the dialog
273 bool PutData();
274 bool GetData();
275
276 // Inserts/updates the database with the current data
277 // retrieved from the editor dialog
278 bool Save();
279
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.
285 bool GetNextRec();
286 bool GetPrevRec();
287 bool GetRec(const wxString &whereStr);
288
289 DECLARE_EVENT_TABLE()
290 }; // CeditorDlg
291
292 #define EDITOR_DIALOG 199
293
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
333
334 // *************************** CparameterDlg ***************************
335
336 class CparameterDlg : public wxDialog
337 {
338 private:
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.
343 bool widgetPtrsSet;
344
345 enum DialogModes mode;
346
347 // Have the parameters been saved yet, or do they
348 // need to be saved to update the params on disk
349 bool saved;
350
351 // Original params
352 Cparameters savedParamSettings;
353
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;
360
361 public:
362 CparameterDlg(wxWindow *parent);
363
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
368
369 // Update/Retrieve data from the widgets on the dialog
370 bool PutData();
371 bool GetData();
372
373 // Stores the defined parameter for connecting to the selected ODBC
374 // data source to the config file name in "PARAM_FILENAME"
375 bool Save();
376
377 // Populates the 'pParamODBCSourceList' listbox with the names of all
378 // ODBC datasource defined for use at the current workstation
379 void FillDataSourceList();
380
381 DECLARE_EVENT_TABLE()
382 }; // CparameterDlg
383
384 #define PARAMETER_DIALOG 400
385
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
397
398 // *************************** CqueryDlg ***************************
399
400
401 // QUERY DIALOG
402 enum qryOp
403 {
404 qryOpEQ,
405 qryOpLT,
406 qryOpGT,
407 qryOpLE,
408 qryOpGE,
409 qryOpBEGINS,
410 qryOpCONTAINS,
411 qryOpLIKE,
412 qryOpBETWEEN
413 };
414
415
416 // Query strings
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";
426
427
428 class CqueryDlg : public wxDialog
429 {
430 private:
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
435 wxDb *pDB;
436
437 public:
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.
442 bool widgetPtrsSet;
443
444 // Widget pointers
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;
470
471 wxTextCtrl *pFocusTxt;
472
473 CqueryDlg(wxWindow *parent, wxDb *pDb, wxChar *tblName[], const wxString &pWhereArg);
474 ~CqueryDlg();
475
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
480
481 void AppendToWhere(wxChar *s);
482 void ProcessAddBtn();
483 void ProcessCountBtn();
484 bool ValidateWhereClause();
485
486 DECLARE_EVENT_TABLE()
487 }; // CqueryDlg
488
489 #define QUERY_DIALOG 300
490
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
516