]> git.saurik.com Git - wxWidgets.git/blob - samples/db/dbtest.h
simplified (hopefully:) and explained the shared lib versioning system.
[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 ABOUT_DEMO 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 }; // DatabaseDemoApp
216
217
218 DECLARE_APP(DatabaseDemoApp)
219
220
221 // *************************** CeditorDlg ***************************
222
223 class CeditorDlg : public wxPanel
224 {
225 private:
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.
230 bool widgetPtrsSet;
231
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.
237 wxString saveName;
238
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;
250
251 public:
252 // Indicates if the editor dialog has been initialized yet (used to
253 // help trap if the Initialize() function failed to load all required
254 // resources or not.
255 bool initialized;
256
257 enum DialogModes mode;
258
259 // Pointer to the wxDbTable instance that is used to manipulate
260 // the data in memory and in the database
261 Ccontact *Contact;
262
263 CeditorDlg(wxWindow *parent);
264
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
269
270 bool Initialize();
271
272 // Sets wxStaticText fields to be editable or not depending
273 // on the current value of 'mode'
274 void FieldsEditable();
275
276 // Sets the editor mode, determining what state widgets
277 // on the dialog are to be in based on the operation
278 // being performed.
279 void SetMode(enum DialogModes m);
280
281 // Update/Retrieve data from the widgets on the dialog
282 bool PutData();
283 bool GetData();
284
285 // Inserts/updates the database with the current data
286 // retrieved from the editor dialog
287 bool Save();
288
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.
294 bool GetNextRec();
295 bool GetPrevRec();
296 bool GetRec(const wxString &whereStr);
297
298 DECLARE_EVENT_TABLE()
299 }; // CeditorDlg
300
301 #define EDITOR_DIALOG 199
302
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
342
343 // *************************** CparameterDlg ***************************
344
345 class CparameterDlg : public wxDialog
346 {
347 private:
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.
352 bool widgetPtrsSet;
353
354 enum DialogModes mode;
355
356 // Have the parameters been saved yet, or do they
357 // need to be saved to update the params on disk
358 bool saved;
359
360 // Original params
361 Cparameters savedParamSettings;
362
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;
369
370 public:
371 CparameterDlg(wxWindow *parent);
372
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
377
378 // Update/Retrieve data from the widgets on the dialog
379 bool PutData();
380 bool GetData();
381
382 // Stores the defined parameter for connecting to the selected ODBC
383 // data source to the config file name in "PARAM_FILENAME"
384 bool Save();
385
386 // Populates the 'pParamODBCSourceList' listbox with the names of all
387 // ODBC datasource defined for use at the current workstation
388 void FillDataSourceList();
389
390 DECLARE_EVENT_TABLE()
391 }; // CparameterDlg
392
393 #define PARAMETER_DIALOG 400
394
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
406
407 // *************************** CqueryDlg ***************************
408
409
410 // QUERY DIALOG
411 enum qryOp
412 {
413 qryOpEQ,
414 qryOpLT,
415 qryOpGT,
416 qryOpLE,
417 qryOpGE,
418 qryOpBEGINS,
419 qryOpCONTAINS,
420 qryOpLIKE,
421 qryOpBETWEEN
422 };
423
424
425 // Query strings
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";
435
436
437 class CqueryDlg : public wxDialog
438 {
439 private:
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
444 wxDb *pDB;
445
446 public:
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.
451 bool widgetPtrsSet;
452
453 // Widget pointers
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;
479
480 wxTextCtrl *pFocusTxt;
481
482 CqueryDlg(wxWindow *parent, wxDb *pDb, wxChar *tblName[], const wxString &pWhereArg);
483 ~CqueryDlg();
484
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
489
490 void AppendToWhere(wxChar *s);
491 void ProcessAddBtn();
492 void ProcessCountBtn();
493 bool ValidateWhereClause();
494
495 DECLARE_EVENT_TABLE()
496 }; // CqueryDlg
497
498 #define QUERY_DIALOG 300
499
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
525