]> git.saurik.com Git - wxWidgets.git/blob - samples/db/dbtest.h
02c9c6f716cf78804faefca51954c9efb20be11a
[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/dbtable.h>
18
19 enum DialogModes {mView,mCreate,mEdit,mSearch};
20
21 // ID for the menu quit command
22 #define FILE_CREATE 100
23 #define FILE_RECREATE_TABLE 110
24 #define FILE_RECREATE_INDEXES 120
25 #define FILE_EXIT 199
26 #define EDIT_PARAMETERS 200
27 #define HELP_ABOUT 300
28
29 // this seems to be missing, Robert Roebling (?)
30 #ifndef MAX_PATH
31 #if defined(__WXMAC__)
32 #define MAX_PATH 260 /* max. length of full pathname */
33 #else /* _MAC */
34 #define MAX_PATH 256 /* max. length of full pathname */
35 #endif
36 #endif
37
38 // Name of the table to be created/opened
39 const wxChar CONTACT_TABLE_NAME[] = "contacts";
40
41
42 #define wxODBC_BLOB_EXPERIMENT 0
43
44 // Number of columns in the CONTACT table
45 #if wxODBC_BLOB_EXPERIMENT > 0
46 const int CONTACT_NO_COLS = 13; // 0-12
47 #else
48 const int CONTACT_NO_COLS = 12; // 0-11
49 #endif
50
51 const wxChar PARAM_FILENAME[] = "dbtest.cfg";
52
53
54 enum Language {langENGLISH, langFRENCH, langGERMAN, langSPANISH, langOTHER};
55
56 // Forward class declarations
57 class CeditorDlg;
58 class CparameterDlg;
59
60 //
61 // This class contains the actual data members that are used for transferring
62 // data back and forth from the database to the program.
63 //
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
66 //
67 class CstructContact : public wxObject
68 {
69 public:
70 wxChar Name[50+1]; // Contact's name
71 wxChar Addr1[50+1];
72 wxChar Addr2[50+1];
73 wxChar City[25+1];
74 wxChar State[25+1];
75 wxChar PostalCode[15+1];
76 wxChar Country[20+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];
81 #endif
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
85 }; // CstructContact
86
87
88 //
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.
93 //
94 class Ccontact : public wxDbTable, public CstructContact
95 {
96 private:
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.
102 bool freeDbConn;
103
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.
107 void SetupColumns();
108
109 public:
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.
114 wxString whereStr;
115
116 // WHERE string returned from the query dialog
117 wxString qryWhereStr;
118
119 Ccontact(wxDb *pwxDb=NULL);
120 ~Ccontact();
121
122 void Initialize();
123
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);
127
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);
132
133 }; // Ccontact class definition
134
135
136 typedef struct Cparameters
137 {
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];
142 } Cparameters;
143
144
145 // Define a new frame type
146 class DatabaseDemoFrame: public wxFrame
147 {
148 private:
149 CeditorDlg *pEditorDlg;
150 CparameterDlg *pParamDlg;
151
152 public:
153 DatabaseDemoFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& sz);
154
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);
162
163 void BuildEditorDialog();
164 void BuildParameterDialog(wxWindow *parent);
165
166 DECLARE_EVENT_TABLE()
167 }; // DatabaseDemoFrame
168
169
170 // Define a new application type
171 class DatabaseDemoApp: public wxApp
172 {
173 public:
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.
177 Cparameters params;
178
179 // Pointer to the main frame used by the App
180 DatabaseDemoFrame *DemoFrame;
181
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.
185 //
186 // ---> IMPORTANT <---
187 //
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.
191 //
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.
197 //
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.
202 wxDb *READONLY_DB;
203
204 // Contains the ODBC connection information used by
205 // all database connections
206 wxDbConnectInf *DbConnectInf;
207
208 bool OnInit();
209
210 // Read/Write ODBC connection parameters to the "PARAM_FILENAME" file
211 bool ReadParamFile(Cparameters &params);
212 bool WriteParamFile(Cparameters &params);
213
214 void CreateDataTable(bool recreate);
215
216 // Pointer to the wxDbTable instance that is used to manipulate
217 // the data in memory and in the database
218 Ccontact *Contact;
219
220 }; // DatabaseDemoApp
221
222
223 DECLARE_APP(DatabaseDemoApp)
224
225
226 // *************************** CeditorDlg ***************************
227
228 class CeditorDlg : public wxPanel
229 {
230 private:
231 // Used to indicate whether all of the widget pointers (defined
232 // below) have been initialized to point to the memory for
233 // the named widget. Used as a safeguard from using the widget
234 // before it has been initialized.
235 bool widgetPtrsSet;
236
237 // Used when the EDIT button has been pressed to maintain the
238 // original name that was displayed in the editor before the
239 // EDIT button was pressed, so that if CANCEL is pressed, a
240 // FetchByName() can be done to retrieve the original data
241 // to repopulate the dialog.
242 wxString saveName;
243
244 // Pointers to all widgets on the dialog
245 wxButton *pCreateBtn, *pEditBtn, *pDeleteBtn, *pCopyBtn, *pSaveBtn, *pCancelBtn;
246 wxButton *pPrevBtn, *pNextBtn, *pQueryBtn, *pResetBtn, *pDoneBtn, *pHelpBtn;
247 wxButton *pNameListBtn;
248 wxTextCtrl *pNameTxt, *pAddress1Txt, *pAddress2Txt,*pCityTxt, *pStateTxt, *pCountryTxt,*pPostalCodeTxt;
249 wxStaticText *pNameMsg, *pAddress1Msg, *pAddress2Msg,*pCityMsg, *pStateMsg, *pCountryMsg,*pPostalCodeMsg;
250 wxTextCtrl *pJoinDateTxt,*pContribTxt, *pLinesTxt;
251 wxStaticText *pJoinDateMsg,*pContribMsg, *pLinesMsg;
252 wxRadioBox *pDeveloperRadio;
253 wxChoice *pNativeLangChoice;
254 wxStaticText *pNativeLangMsg;
255
256 public:
257 // Indicates if the editor dialog has been initialized yet (used to
258 // help trap if the Initialize() function failed to load all required
259 // resources or not.
260 bool initialized;
261
262 enum DialogModes mode;
263
264 CeditorDlg(wxWindow *parent);
265
266 void OnCloseWindow(wxCloseEvent& event);
267 void OnButton( wxCommandEvent &event );
268 void OnCommand(wxWindow& win, wxCommandEvent& event);
269 void OnActivate(bool) {}; // necessary for hot keys
270
271 bool Initialize();
272
273 // Sets wxStaticText fields to be editable or not depending
274 // on the current value of 'mode'
275 void FieldsEditable();
276
277 // Sets the editor mode, determining what state widgets
278 // on the dialog are to be in based on the operation
279 // being performed.
280 void SetMode(enum DialogModes m);
281
282 // Update/Retrieve data from the widgets on the dialog
283 bool PutData();
284 bool GetData();
285
286 // Inserts/updates the database with the current data
287 // retrieved from the editor dialog
288 bool Save();
289
290 // Database functions for changing the data that is to
291 // be displayed on the dialog. GetNextRec()/GetPrevRec()
292 // provide database independent methods that do not require
293 // backward scrolling cursors to obtain the record that
294 // is prior to the current record in the search sequence.
295 bool GetNextRec();
296 bool GetPrevRec();
297 bool GetRec(const wxString &whereStr);
298
299 DECLARE_EVENT_TABLE()
300 }; // CeditorDlg
301
302 #define EDITOR_DIALOG 199
303
304 // Editor dialog control ids
305 #define EDITOR_DIALOG_FN_GROUP 200
306 #define EDITOR_DIALOG_SEARCH_GROUP 201
307 #define EDITOR_DIALOG_CREATE 202
308 #define EDITOR_DIALOG_EDIT 203
309 #define EDITOR_DIALOG_DELETE 204
310 #define EDITOR_DIALOG_COPY 205
311 #define EDITOR_DIALOG_SAVE 206
312 #define EDITOR_DIALOG_CANCEL 207
313 #define EDITOR_DIALOG_PREV 208
314 #define EDITOR_DIALOG_NEXT 209
315 #define EDITOR_DIALOG_QUERY 211
316 #define EDITOR_DIALOG_RESET 212
317 #define EDITOR_DIALOG_NAME_MSG 213
318 #define EDITOR_DIALOG_NAME_TEXT 214
319 #define EDITOR_DIALOG_LOOKUP 215
320 #define EDITOR_DIALOG_ADDRESS1_MSG 216
321 #define EDITOR_DIALOG_ADDRESS1_TEXT 217
322 #define EDITOR_DIALOG_ADDRESS2_MSG 218
323 #define EDITOR_DIALOG_ADDRESS2_TEXT 219
324 #define EDITOR_DIALOG_CITY_MSG 220
325 #define EDITOR_DIALOG_CITY_TEXT 221
326 #define EDITOR_DIALOG_COUNTRY_MSG 222
327 #define EDITOR_DIALOG_COUNTRY_TEXT 223
328 #define EDITOR_DIALOG_POSTAL_MSG 224
329 #define EDITOR_DIALOG_POSTAL_TEXT 225
330 #define EDITOR_DIALOG_LANG_MSG 226
331 #define EDITOR_DIALOG_LANG_CHOICE 227
332 #define EDITOR_DIALOG_DATE_MSG 228
333 #define EDITOR_DIALOG_DATE_TEXT 229
334 #define EDITOR_DIALOG_CONTRIB_MSG 230
335 #define EDITOR_DIALOG_CONTRIB_TEXT 231
336 #define EDITOR_DIALOG_LINES_MSG 232
337 #define EDITOR_DIALOG_LINES_TEXT 233
338 #define EDITOR_DIALOG_STATE_MSG 234
339 #define EDITOR_DIALOG_STATE_TEXT 235
340 #define EDITOR_DIALOG_DEVELOPER 236
341 #define EDITOR_DIALOG_JOIN_MSG 237
342 #define EDITOR_DIALOG_JOIN_TEXT 238
343
344 // *************************** CparameterDlg ***************************
345
346 class CparameterDlg : public wxDialog
347 {
348 private:
349 // Used to indicate whether all of the widget pointers (defined
350 // below) have been initialized to point to the memory for
351 // the named widget. Used as a safeguard from using the widget
352 // before it has been initialized.
353 bool widgetPtrsSet;
354
355 enum DialogModes mode;
356
357 // Have the parameters been saved yet, or do they
358 // need to be saved to update the params on disk
359 bool saved;
360
361 // Original params
362 Cparameters savedParamSettings;
363
364 // Pointers to all widgets on the dialog
365 wxStaticText *pParamODBCSourceMsg;
366 wxListBox *pParamODBCSourceList;
367 wxStaticText *pParamUserNameMsg, *pParamPasswordMsg, *pParamDirPathMsg;
368 wxTextCtrl *pParamUserNameTxt, *pParamPasswordTxt, *pParamDirPathTxt;
369 wxButton *pParamSaveBtn, *pParamCancelBtn;
370
371 public:
372 CparameterDlg(wxWindow *parent);
373
374 void OnCloseWindow(wxCloseEvent& event);
375 void OnButton( wxCommandEvent &event );
376 void OnCommand(wxWindow& win, wxCommandEvent& event);
377 void OnActivate(bool) {}; // necessary for hot keys
378
379 // Update/Retrieve data from the widgets on the dialog
380 bool PutData();
381 bool GetData();
382
383 // Stores the defined parameter for connecting to the selected ODBC
384 // data source to the config file name in "PARAM_FILENAME"
385 bool Save();
386
387 // Populates the 'pParamODBCSourceList' listbox with the names of all
388 // ODBC datasource defined for use at the current workstation
389 void FillDataSourceList();
390
391 DECLARE_EVENT_TABLE()
392 }; // CparameterDlg
393
394 #define PARAMETER_DIALOG 400
395
396 // Parameter dialog control ids
397 #define PARAMETER_DIALOG_SOURCE_MSG 401
398 #define PARAMETER_DIALOG_SOURCE_LISTBOX 402
399 #define PARAMETER_DIALOG_NAME_MSG 403
400 #define PARAMETER_DIALOG_NAME_TEXT 404
401 #define PARAMETER_DIALOG_PASSWORD_MSG 405
402 #define PARAMETER_DIALOG_PASSWORD_TEXT 406
403 #define PARAMETER_DIALOG_DIRPATH_MSG 407
404 #define PARAMETER_DIALOG_DIRPATH_TEXT 408
405 #define PARAMETER_DIALOG_SAVE 409
406 #define PARAMETER_DIALOG_CANCEL 410
407
408 // *************************** CqueryDlg ***************************
409
410
411 // QUERY DIALOG
412 enum qryOp
413 {
414 qryOpEQ,
415 qryOpLT,
416 qryOpGT,
417 qryOpLE,
418 qryOpGE,
419 qryOpBEGINS,
420 qryOpCONTAINS,
421 qryOpLIKE,
422 qryOpBETWEEN
423 };
424
425
426 // Query strings
427 wxChar * const langQRY_EQ = "column = column | value";
428 wxChar * const langQRY_LT = "column < column | value";
429 wxChar * const langQRY_GT = "column > column | value";
430 wxChar * const langQRY_LE = "column <= column | value";
431 wxChar * const langQRY_GE = "column >= column | value";
432 wxChar * const langQRY_BEGINS = "columns that BEGIN with the string entered";
433 wxChar * const langQRY_CONTAINS = "columns that CONTAIN the string entered";
434 wxChar * const langQRY_LIKE = "% matches 0 or more of any char; _ matches 1 char";
435 wxChar * const langQRY_BETWEEN = "column BETWEEN value AND value";
436
437
438 class CqueryDlg : public wxDialog
439 {
440 private:
441 wxDbColInf *colInf; // Column inf. returned by db->GetColumns()
442 wxDbTable *dbTable; // generic wxDbTable object for attaching to the table to query
443 wxChar *masterTableName; // Name of the table that 'dbTable' will be associated with
444 wxString pWhere; // A pointer to the storage for the resulting where clause
445 wxDb *pDB;
446
447 public:
448 // Used to indicate whether all of the widget pointers (defined
449 // below) have been initialized to point to the memory for
450 // the named widget. Used as a safeguard from using the widget
451 // before it has been initialized.
452 bool widgetPtrsSet;
453
454 // Widget pointers
455 wxStaticText *pQueryCol1Msg;
456 wxChoice *pQueryCol1Choice;
457 wxStaticText *pQueryNotMsg;
458 wxCheckBox *pQueryNotCheck;
459 wxStaticText *pQueryOperatorMsg;
460 wxChoice *pQueryOperatorChoice;
461 wxStaticText *pQueryCol2Msg;
462 wxChoice *pQueryCol2Choice;
463 wxStaticText *pQueryValue1Msg;
464 wxTextCtrl *pQueryValue1Txt;
465 wxStaticText *pQueryValue2Msg;
466 wxTextCtrl *pQueryValue2Txt;
467 wxStaticText *pQuerySqlWhereMsg;
468 wxTextCtrl *pQuerySqlWhereMtxt;
469 wxButton *pQueryAddBtn;
470 wxButton *pQueryAndBtn;
471 wxButton *pQueryOrBtn;
472 wxButton *pQueryLParenBtn;
473 wxButton *pQueryRParenBtn;
474 wxButton *pQueryDoneBtn;
475 wxButton *pQueryClearBtn;
476 wxButton *pQueryCountBtn;
477 wxButton *pQueryHelpBtn;
478 wxStaticBox *pQueryHintGrp;
479 wxStaticText *pQueryHintMsg;
480
481 wxTextCtrl *pFocusTxt;
482
483 CqueryDlg(wxWindow *parent, wxDb *pDb, wxChar *tblName[], const wxString &pWhereArg);
484 ~CqueryDlg();
485
486 void OnButton( wxCommandEvent &event );
487 void OnCommand(wxWindow& win, wxCommandEvent& event);
488 void OnCloseWindow(wxCloseEvent& event);
489 void OnActivate(bool) {}; // necessary for hot keys
490
491 void AppendToWhere(wxChar *s);
492 void ProcessAddBtn();
493 void ProcessCountBtn();
494 bool ValidateWhereClause();
495
496 DECLARE_EVENT_TABLE()
497 }; // CqueryDlg
498
499 #define QUERY_DIALOG 300
500
501 // Parameter dialog control ids
502 #define QUERY_DIALOG_COL_MSG 301
503 #define QUERY_DIALOG_COL_CHOICE 302
504 #define QUERY_DIALOG_NOT_MSG 303
505 #define QUERY_DIALOG_NOT_CHECKBOX 304
506 #define QUERY_DIALOG_OP_MSG 305
507 #define QUERY_DIALOG_OP_CHOICE 306
508 #define QUERY_DIALOG_COL2_MSG 307
509 #define QUERY_DIALOG_COL2_CHOICE 308
510 #define QUERY_DIALOG_WHERE_MSG 309
511 #define QUERY_DIALOG_WHERE_TEXT 310
512 #define QUERY_DIALOG_ADD 311
513 #define QUERY_DIALOG_AND 312
514 #define QUERY_DIALOG_OR 313
515 #define QUERY_DIALOG_LPAREN 314
516 #define QUERY_DIALOG_RPAREN 315
517 #define QUERY_DIALOG_DONE 316
518 #define QUERY_DIALOG_CLEAR 317
519 #define QUERY_DIALOG_COUNT 318
520 #define QUERY_DIALOG_VALUE1_MSG 319
521 #define QUERY_DIALOG_VALUE1_TEXT 320
522 #define QUERY_DIALOG_VALUE2_MSG 321
523 #define QUERY_DIALOG_VALUE2_TEXT 322
524 #define QUERY_DIALOG_HINT_GROUP 323
525 #define QUERY_DIALOG_HINT_MSG 324
526