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 HELP_ABOUT 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
);
216 // Pointer to the wxDbTable instance that is used to manipulate
217 // the data in memory and in the database
220 }; // DatabaseDemoApp
223 DECLARE_APP(DatabaseDemoApp
)
226 // *************************** CeditorDlg ***************************
228 class CeditorDlg
: public wxPanel
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.
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.
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
;
257 // Indicates if the editor dialog has been initialized yet (used to
258 // help trap if the Initialize() function failed to load all required
262 enum DialogModes mode
;
264 CeditorDlg(wxWindow
*parent
);
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
273 // Sets wxStaticText fields to be editable or not depending
274 // on the current value of 'mode'
275 void FieldsEditable();
277 // Sets the editor mode, determining what state widgets
278 // on the dialog are to be in based on the operation
280 void SetMode(enum DialogModes m
);
282 // Update/Retrieve data from the widgets on the dialog
286 // Inserts/updates the database with the current data
287 // retrieved from the editor dialog
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.
297 bool GetRec(const wxString
&whereStr
);
299 DECLARE_EVENT_TABLE()
302 #define EDITOR_DIALOG 199
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
344 // *************************** CparameterDlg ***************************
346 class CparameterDlg
: public wxDialog
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.
355 enum DialogModes mode
;
357 // Have the parameters been saved yet, or do they
358 // need to be saved to update the params on disk
362 Cparameters savedParamSettings
;
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
;
372 CparameterDlg(wxWindow
*parent
);
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
379 // Update/Retrieve data from the widgets on the dialog
383 // Stores the defined parameter for connecting to the selected ODBC
384 // data source to the config file name in "PARAM_FILENAME"
387 // Populates the 'pParamODBCSourceList' listbox with the names of all
388 // ODBC datasource defined for use at the current workstation
389 void FillDataSourceList();
391 DECLARE_EVENT_TABLE()
394 #define PARAMETER_DIALOG 400
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
408 // *************************** CqueryDlg ***************************
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";
438 class CqueryDlg
: public wxDialog
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
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.
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
;
481 wxTextCtrl
*pFocusTxt
;
483 CqueryDlg(wxWindow
*parent
, wxDb
*pDb
, wxChar
*tblName
[], const wxString
&pWhereArg
);
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
491 void AppendToWhere(wxChar
*s
);
492 void ProcessAddBtn();
493 void ProcessCountBtn();
494 bool ValidateWhereClause();
496 DECLARE_EVENT_TABLE()
499 #define QUERY_DIALOG 300
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