From 80ccc1282cb1c756f3dcf571b2015fe068187627 Mon Sep 17 00:00:00 2001 From: George Tasker Date: Tue, 1 Aug 2000 18:47:04 +0000 Subject: [PATCH] Subqueries for MySQL are now not performed, as MySQL does not support subqueries Compile time warnings with GCC eliminated. Call to wxDbCloseConnections() was missing from the frame ::OnCloseWindow() function, so that the connections to the database were not properly CLOSEd, they were only marked as FREE in the DB connection cache. Several crash/segfault problems have been corrected New functions added under the FILE menu for RECREATE TABLE and RECREATE INDEXES git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@7911 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/db/dbtest.cpp | 448 +++++++++++++++++++++++++----------------- samples/db/dbtest.h | 14 +- 2 files changed, 282 insertions(+), 180 deletions(-) diff --git a/samples/db/dbtest.cpp b/samples/db/dbtest.cpp index 1b5522ec68..b2224c066d 100644 --- a/samples/db/dbtest.cpp +++ b/samples/db/dbtest.cpp @@ -91,9 +91,10 @@ wxDb *READONLY_DB; * NOTE: The value returned by this function is for temporary use only and * should be copied for long term use */ -char *GetExtendedDBErrorMsg(char *ErrFile, int ErrLine) +const char *GetExtendedDBErrorMsg(char *ErrFile, int ErrLine) { static wxString msg; + msg = ""; wxString tStr; @@ -127,12 +128,15 @@ char *GetExtendedDBErrorMsg(char *ErrFile, int ErrLine) msg.Append(pDbList->PtrDb->errorList[i]); if (wxStrcmp(pDbList->PtrDb->errorList[i],"") != 0) msg.Append("\n"); + // Clear the errmsg buffer so the next error will not + // end up showing the previous error that have occurred + wxStrcpy(pDbList->PtrDb->errorList[i],""); } } } msg += "\n"; - return (char*) (const char*) msg; + return /*(char*) (const char*) msg*/msg.c_str(); } // GetExtendedDBErrorMsg @@ -146,7 +150,9 @@ bool DatabaseDemoApp::OnInit() // Make a menubar wxMenu *file_menu = new wxMenu; - file_menu->Append(FILE_CREATE, "&Create contact table"); + file_menu->Append(FILE_CREATE, "&Create CONTACT table"); + file_menu->Append(FILE_RECREATE_TABLE, "&Recreate CONTACT table"); + file_menu->Append(FILE_RECREATE_INDEXES, "&Recreate CONTACT indexes"); file_menu->Append(FILE_EXIT, "E&xit"); wxMenu *edit_menu = new wxMenu; @@ -173,6 +179,9 @@ bool DatabaseDemoApp::OnInit() params.Password[0] = 0; params.DirPath[0] = 0; + // Show the frame + DemoFrame->Show(TRUE); + FILE *paramFile; if ((paramFile = fopen(paramFilename, "r")) == NULL) { @@ -225,13 +234,16 @@ bool DatabaseDemoApp::OnInit() DemoFrame->BuildEditorDialog(); // Show the frame - DemoFrame->Show(TRUE); + DemoFrame->Refresh(); +// DemoFrame->Show(TRUE); return TRUE; } // DatabaseDemoApp::OnInit() BEGIN_EVENT_TABLE(DatabaseDemoFrame, wxFrame) EVT_MENU(FILE_CREATE, DatabaseDemoFrame::OnCreate) + EVT_MENU(FILE_RECREATE_TABLE, DatabaseDemoFrame::OnRecreateTable) + EVT_MENU(FILE_RECREATE_INDEXES, DatabaseDemoFrame::OnRecreateIndexes) EVT_MENU(FILE_EXIT, DatabaseDemoFrame::OnExit) EVT_MENU(EDIT_PARAMETERS, DatabaseDemoFrame::OnEditParameters) EVT_MENU(ABOUT_DEMO, DatabaseDemoFrame::OnAbout) @@ -244,11 +256,44 @@ DatabaseDemoFrame::DatabaseDemoFrame(wxFrame *frame, const wxString& title, wxFrame(frame, -1, title, pos, size) { // Put any code in necessary for initializing the main frame here + pEditorDlg = NULL; + pParamDlg = NULL; } void DatabaseDemoFrame::OnCreate(wxCommandEvent& event) { - CreateDataTable(); + CreateDataTable(FALSE); +} + +void DatabaseDemoFrame::OnRecreateTable(wxCommandEvent& event) +{ + CreateDataTable(TRUE); +} + +void DatabaseDemoFrame::OnRecreateIndexes(wxCommandEvent& event) +{ + // Using a new connection to the database so as not to disturb + // the current cursors on the table in use in the editor dialog + Ccontact *Contact = new Ccontact(); + + if (!Contact) + { + wxEndBusyCursor(); + wxMessageBox("Error allocating memory for 'Ccontact'object.\n\nTable could not be opened.","Error...",wxOK | wxICON_EXCLAMATION); + return; + } + + if (!Contact->CreateIndexes()) + { + wxEndBusyCursor(); + wxString tStr; + tStr = "Error creating CONTACTS indexes.\nNew indexes will be unavailable.\n\n"; + tStr += GetExtendedDBErrorMsg(__FILE__,__LINE__); + wxMessageBox(tStr,"ODBC Error...",wxOK | wxICON_EXCLAMATION); + } + + delete Contact; + Contact = NULL; } void DatabaseDemoFrame::OnExit(wxCommandEvent& event) @@ -275,14 +320,21 @@ void DatabaseDemoFrame::OnCloseWindow(wxCloseEvent& event) // to close the program here that is not done elsewhere // Clean up time - if (pEditorDlg->Close()) + if (pEditorDlg && pEditorDlg->Close()) pEditorDlg = NULL; else { - event.Veto(); - return; + if (pEditorDlg) + { + event.Veto(); + return; + } } + // This function will close all the connections to the database that have been + // previously cached. + wxDbCloseConnections(); + // Cleans up the environment space allocated for the SQL/ODBC connection handle SQLFreeEnv(DbConnectInf.Henv); @@ -291,9 +343,11 @@ void DatabaseDemoFrame::OnCloseWindow(wxCloseEvent& event) } // DatabaseDemoFrame::OnCloseWindow() -void DatabaseDemoFrame::CreateDataTable() +void DatabaseDemoFrame::CreateDataTable(bool recreate) { - bool Ok = (wxMessageBox("Any data currently residing in the table will be erased.\n\nAre you sure?","Confirm",wxYES_NO|wxICON_QUESTION) == wxYES); + bool Ok = TRUE; + if (recreate) + Ok = (wxMessageBox("Any data currently residing in the table will be erased.\n\nAre you sure?","Confirm",wxYES_NO|wxICON_QUESTION) == wxYES); if (!Ok) return; @@ -310,7 +364,7 @@ void DatabaseDemoFrame::CreateDataTable() return; } - if (!Contact->CreateTable(FALSE)) + if (!Contact->CreateTable(recreate)) { wxEndBusyCursor(); wxString tStr; @@ -335,6 +389,7 @@ void DatabaseDemoFrame::CreateDataTable() wxEndBusyCursor(); delete Contact; + Contact = NULL; if (success) wxMessageBox("Table and index(es) were successfully created.","Notice...",wxOK | wxICON_INFORMATION); @@ -343,9 +398,23 @@ void DatabaseDemoFrame::CreateDataTable() void DatabaseDemoFrame::BuildEditorDialog() { + pEditorDlg = NULL; pEditorDlg = new CeditorDlg(this); - if (!pEditorDlg) + if (pEditorDlg) + pEditorDlg->Initialize(); + else + { wxMessageBox("Unable to create the editor dialog for some reason","Error...",wxOK | wxICON_EXCLAMATION); + DemoFrame->Close(); + } + + if (!pEditorDlg->initialized) + { + pEditorDlg->Close(); + pEditorDlg = NULL; + wxMessageBox("Unable to initialize the editor dialog for some reason","Error...",wxOK | wxICON_EXCLAMATION); + DemoFrame->Close(TRUE); + } } // DatabaseDemoFrame::BuildEditorDialog() @@ -509,168 +578,18 @@ BEGIN_EVENT_TABLE(CeditorDlg, wxPanel) EVT_CLOSE(CeditorDlg::OnCloseWindow) END_EVENT_TABLE() -CeditorDlg::CeditorDlg(wxWindow *parent) : wxPanel (parent, 1, 1, 460, 455) +CeditorDlg::CeditorDlg(wxWindow *parent) : wxPanel (parent, 0, 0, 537, 480) { // Since the ::OnCommand() function is overridden, this prevents the widget // detection in ::OnCommand() until all widgets have been initialized to prevent // uninitialized pointers from crashing the program widgetPtrsSet = FALSE; - // Create the data structure and a new database connection. - // (As there is not a pDb being passed in the constructor, a new database - // connection is created) - Contact = new Ccontact(); + initialized = FALSE; - if (!Contact) - { - wxMessageBox("Unable to instantiate an instance of Ccontact","Error...",wxOK | wxICON_EXCLAMATION); - return; - } + Contact = NULL; - // Check if the table exists or not. If it doesn't, ask the user if they want to - // create the table. Continue trying to create the table until it exists, or user aborts - while (!Contact->GetDb()->TableExists((char *)CONTACT_TABLE_NAME,DbConnectInf.Uid,DbConnectInf.defaultDir)) - { - wxString tStr; - tStr.Printf("Unable to open the table '%s'.\n\nTable may need to be created...?\n\n",CONTACT_TABLE_NAME); - tStr += GetExtendedDBErrorMsg(__FILE__,__LINE__); - wxMessageBox(tStr,"ODBC Error...",wxOK | wxICON_EXCLAMATION); - - bool createTable = (wxMessageBox("Do you wish to try to create/clear the CONTACTS table?","Confirm",wxYES_NO|wxICON_QUESTION) == wxYES); - - if (!createTable) - { - delete Contact; - Close(); - DemoFrame->Close(); - return; - } - else - DemoFrame->CreateDataTable(); - } - - // Tables must be "opened" before anything other than creating/deleting table can be done - if (!Contact->Open()) - { - // Table does exist, there was some problem opening it. Currently this should - // never fail, except in the case of the table not exisiting. Open() basically - // only sets up variable/pointer values, other than checking for table existence. - if (Contact->GetDb()->TableExists((char *)CONTACT_TABLE_NAME)) - { - wxString tStr; - tStr.Printf("Unable to open the table '%s'.\n\n",CONTACT_TABLE_NAME); - tStr += GetExtendedDBErrorMsg(__FILE__,__LINE__); - wxMessageBox(tStr,"ODBC Error...",wxOK | wxICON_EXCLAMATION); - delete Contact; - Close(); - DemoFrame->Close(); - return; - } - } - - // Build the dialog - - (void)new wxStaticBox(this, EDITOR_DIALOG_FN_GROUP, "", wxPoint(15, 1), wxSize(497, 69), 0, "FunctionGrp"); - (void)new wxStaticBox(this, EDITOR_DIALOG_SEARCH_GROUP, "", wxPoint(417, 1), wxSize(95, 242), 0, "SearchGrp"); - - pCreateBtn = new wxButton(this, EDITOR_DIALOG_CREATE, "&Create", wxPoint( 25, 21), wxSize( 70, 35), 0, wxDefaultValidator, "CreateBtn"); - pEditBtn = new wxButton(this, EDITOR_DIALOG_EDIT, "&Edit", wxPoint(102, 21), wxSize( 70, 35), 0, wxDefaultValidator, "EditBtn"); - pDeleteBtn = new wxButton(this, EDITOR_DIALOG_DELETE, "&Delete", wxPoint(179, 21), wxSize( 70, 35), 0, wxDefaultValidator, "DeleteBtn"); - pCopyBtn = new wxButton(this, EDITOR_DIALOG_COPY, "Cop&y", wxPoint(256, 21), wxSize( 70, 35), 0, wxDefaultValidator, "CopyBtn"); - pSaveBtn = new wxButton(this, EDITOR_DIALOG_SAVE, "&Save", wxPoint(333, 21), wxSize( 70, 35), 0, wxDefaultValidator, "SaveBtn"); - pCancelBtn = new wxButton(this, EDITOR_DIALOG_CANCEL, "C&ancel", wxPoint(430, 21), wxSize( 70, 35), 0, wxDefaultValidator, "CancelBtn"); - pPrevBtn = new wxButton(this, EDITOR_DIALOG_PREV, "<< &Prev", wxPoint(430, 81), wxSize( 70, 35), 0, wxDefaultValidator, "PrevBtn"); - pNextBtn = new wxButton(this, EDITOR_DIALOG_NEXT, "&Next >>", wxPoint(430, 121), wxSize( 70, 35), 0, wxDefaultValidator, "NextBtn"); - pQueryBtn = new wxButton(this, EDITOR_DIALOG_QUERY, "&Query", wxPoint(430, 161), wxSize( 70, 35), 0, wxDefaultValidator, "QueryBtn"); - pResetBtn = new wxButton(this, EDITOR_DIALOG_RESET, "&Reset", wxPoint(430, 200), wxSize( 70, 35), 0, wxDefaultValidator, "ResetBtn"); - pNameMsg = new wxStaticText(this, EDITOR_DIALOG_NAME_MSG, "Name:", wxPoint( 17, 80), wxSize( -1, -1), 0, "NameMsg"); - pNameTxt = new wxTextCtrl(this, EDITOR_DIALOG_NAME_TEXT, "", wxPoint( 17, 97), wxSize(308, 25), 0, wxDefaultValidator, "NameTxt"); - pNameListBtn = new wxButton(this, EDITOR_DIALOG_LOOKUP, "&Lookup", wxPoint(333, 97), wxSize( 70, 24), 0, wxDefaultValidator, "LookupBtn"); - pAddress1Msg = new wxStaticText(this, EDITOR_DIALOG_ADDRESS1_MSG, "Address:", wxPoint( 17, 130), wxSize( -1, -1), 0, "Address1Msg"); - pAddress1Txt = new wxTextCtrl(this, EDITOR_DIALOG_ADDRESS2_TEXT, "", wxPoint( 17, 147), wxSize(308, 25), 0, wxDefaultValidator, "Address1Txt"); - pAddress2Msg = new wxStaticText(this, EDITOR_DIALOG_ADDRESS2_MSG, "Address:", wxPoint( 17, 180), wxSize( -1, -1), 0, "Address2Msg"); - pAddress2Txt = new wxTextCtrl(this, EDITOR_DIALOG_ADDRESS2_TEXT, "", wxPoint( 17, 197), wxSize(308, 25), 0, wxDefaultValidator, "Address2Txt"); - pCityMsg = new wxStaticText(this, EDITOR_DIALOG_CITY_MSG, "City:", wxPoint( 17, 230), wxSize( -1, -1), 0, "CityMsg"); - pCityTxt = new wxTextCtrl(this, EDITOR_DIALOG_CITY_TEXT, "", wxPoint( 17, 247), wxSize(225, 25), 0, wxDefaultValidator, "CityTxt"); - pStateMsg = new wxStaticText(this, EDITOR_DIALOG_STATE_MSG, "State:", wxPoint(250, 230), wxSize( -1, -1), 0, "StateMsg"); - pStateTxt = new wxTextCtrl(this, EDITOR_DIALOG_STATE_TEXT, "", wxPoint(250, 247), wxSize(153, 25), 0, wxDefaultValidator, "StateTxt"); - pCountryMsg = new wxStaticText(this, EDITOR_DIALOG_COUNTRY_MSG, "Country:", wxPoint( 17, 280), wxSize( -1, -1), 0, "CountryMsg"); - pCountryTxt = new wxTextCtrl(this, EDITOR_DIALOG_COUNTRY_TEXT, "", wxPoint( 17, 297), wxSize(225, 25), 0, wxDefaultValidator, "CountryTxt"); - pPostalCodeMsg = new wxStaticText(this, EDITOR_DIALOG_POSTAL_MSG, "Postal Code:",wxPoint(250, 280), wxSize( -1, -1), 0, "PostalCodeMsg"); - pPostalCodeTxt = new wxTextCtrl(this, EDITOR_DIALOG_POSTAL_TEXT, "", wxPoint(250, 297), wxSize(153, 25), 0, wxDefaultValidator, "PostalCodeTxt"); - - wxString choice_strings[5]; - choice_strings[0] = "English"; - choice_strings[1] = "French"; - choice_strings[2] = "German"; - choice_strings[3] = "Spanish"; - choice_strings[4] = "Other"; - - pNativeLangChoice = new wxChoice(this, EDITOR_DIALOG_LANG_CHOICE, wxPoint( 17, 346), wxSize(277, -1), 5, choice_strings); - pNativeLangMsg = new wxStaticText(this, EDITOR_DIALOG_LANG_MSG, "Native language:", wxPoint( 17, 330), wxSize( -1, -1), 0, "NativeLangMsg"); - - wxString radio_strings[2]; - radio_strings[0] = "No"; - radio_strings[1] = "Yes"; - pDeveloperRadio = new wxRadioBox(this,EDITOR_DIALOG_DEVELOPER, "Developer:", wxPoint(303, 330), wxSize( -1, -1), 2, radio_strings, 2, wxHORIZONTAL); - pJoinDateMsg = new wxStaticText(this, EDITOR_DIALOG_JOIN_MSG, "Date joined:", wxPoint( 17, 380), wxSize( -1, -1), 0, "JoinDateMsg"); - pJoinDateTxt = new wxTextCtrl(this, EDITOR_DIALOG_JOIN_TEXT, "", wxPoint( 17, 397), wxSize(150, 25), 0, wxDefaultValidator, "JoinDateTxt"); - pContribMsg = new wxStaticText(this, EDITOR_DIALOG_CONTRIB_MSG,"Contributions:", wxPoint(175, 380), wxSize( -1, -1), 0, "ContribMsg"); - pContribTxt = new wxTextCtrl(this, EDITOR_DIALOG_CONTRIB_TEXT, "", wxPoint(175, 397), wxSize(120, 25), 0, wxDefaultValidator, "ContribTxt"); - pLinesMsg = new wxStaticText(this, EDITOR_DIALOG_LINES_MSG, "Lines of code:", wxPoint(303, 380), wxSize( -1, -1), 0, "LinesMsg"); - pLinesTxt = new wxTextCtrl(this, EDITOR_DIALOG_LINES_TEXT, "", wxPoint(303, 397), wxSize(100, 25), 0, wxDefaultValidator, "LinesTxt"); - - // Now that all the widgets on the panel are created, its safe to allow ::OnCommand() to - // handle all widget processing - widgetPtrsSet = TRUE; - - // Setup the orderBy and where clauses to return back a single record as the result set, - // as there will only be one record being shown on the dialog at a time, this optimizes - // network traffic by only returning a one row result - - Contact->SetOrderByClause("NAME"); // field name to sort by - - // The wxString "whereStr" is not a member of the wxDbTable object, it is a member variable - // specifically in the Ccontact class. It is used here for simpler construction of a varying - // length string, and then after the string is built, the wxDbTable member variable "where" is - // assigned the pointer to the constructed string. - // - // The constructed where clause below has a sub-query within it "SELECT MIN(NAME) FROM %s" - // to achieve a single row (in this case the first name in alphabetical order). - - if (Contact->GetDb()->Dbms() != dbmsPOSTGRES) - { - Contact->whereStr.sprintf("NAME = (SELECT MIN(NAME) FROM %s)",Contact->GetTableName()); - // NOTE: (const char*) returns a pointer which may not be valid later, so this is short term use only - Contact->SetWhereClause(Contact->whereStr.c_str()); - } - else - Contact->SetWhereClause(""); - - // Perform the Query to get the result set. - // NOTE: If there are no rows returned, that is a valid result, so Query() would return TRUE. - // Only if there is a database error will Query() come back as FALSE - if (!Contact->Query()) - { - wxString tStr; - tStr = "ODBC error during Query()\n\n"; - tStr += GetExtendedDBErrorMsg(__FILE__,__LINE__); - wxMessageBox(tStr,"ODBC Error...",wxOK | wxICON_EXCLAMATION); - GetParent()->Close(); - return; - } - - // Since Query succeeded, now get the row that was returned - if (!Contact->GetNext()) - // If the GetNext() failed at this point, then there are no rows to retrieve, - // so clear the values in the members of "Contact" so that PutData() blanks the - // widgets on the dialog - Contact->Initialize(); - - SetMode(mView); - PutData(); - - Show(TRUE); + Show(FALSE); } // CeditorDlg constructor @@ -680,7 +599,10 @@ void CeditorDlg::OnCloseWindow(wxCloseEvent& event) if ((mode != mCreate) && (mode != mEdit)) { if (Contact) + { delete Contact; + Contact = NULL; + } this->Destroy(); } else @@ -805,7 +727,7 @@ void CeditorDlg::OnCommand(wxWindow& win, wxCommandEvent& event) } // Previous record not available, retrieve first record in table - if (Contact->GetDb()->Dbms() != dbmsPOSTGRES) + if (Contact->GetDb()->Dbms() != dbmsPOSTGRES && Contact->GetDb()->Dbms() != dbmsMY_SQL) { Contact->whereStr = "NAME = (SELECT MIN(NAME) FROM "; Contact->whereStr += Contact->GetTableName(); @@ -867,7 +789,7 @@ void CeditorDlg::OnCommand(wxWindow& win, wxCommandEvent& event) Contact->whereStr = ""; Contact->SetOrderByClause("NAME"); - if (Contact->GetDb()->Dbms() != dbmsPOSTGRES) + if (Contact->GetDb()->Dbms() != dbmsPOSTGRES && Contact->GetDb()->Dbms() != dbmsMY_SQL) { Contact->whereStr = "NAME = (SELECT MIN(NAME) FROM "; Contact->whereStr += CONTACT_TABLE_NAME; @@ -911,7 +833,7 @@ void CeditorDlg::OnCommand(wxWindow& win, wxCommandEvent& event) Contact->qryWhereStr = ""; Contact->SetOrderByClause("NAME"); - if (Contact->GetDb()->Dbms() != dbmsPOSTGRES) + if (Contact->GetDb()->Dbms() != dbmsPOSTGRES && Contact->GetDb()->Dbms() != dbmsMY_SQL) { Contact->whereStr = "NAME = (SELECT MIN(NAME) FROM "; Contact->whereStr += CONTACT_TABLE_NAME; @@ -961,6 +883,180 @@ void CeditorDlg::OnCommand(wxWindow& win, wxCommandEvent& event) } // CeditorDlg::OnCommand() +bool CeditorDlg::Initialize() +{ + // Create the data structure and a new database connection. + // (As there is not a pDb being passed in the constructor, a new database + // connection is created) + Contact = new Ccontact(); + + if (!Contact) + { + wxMessageBox("Unable to instantiate an instance of Ccontact","Error...",wxOK | wxICON_EXCLAMATION); + return FALSE; + } + + // Check if the table exists or not. If it doesn't, ask the user if they want to + // create the table. Continue trying to create the table until it exists, or user aborts + while (!Contact->GetDb()->TableExists((char *)CONTACT_TABLE_NAME,DbConnectInf.Uid,DbConnectInf.defaultDir)) + { + wxString tStr; + tStr.Printf("Unable to open the table '%s'.\n\nTable may need to be created...?\n\n",CONTACT_TABLE_NAME); + tStr += GetExtendedDBErrorMsg(__FILE__,__LINE__); + wxMessageBox(tStr,"ODBC Error...",wxOK | wxICON_EXCLAMATION); + + bool createTable = (wxMessageBox("Do you wish to try to create/clear the CONTACTS table?","Confirm",wxYES_NO|wxICON_QUESTION) == wxYES); + + if (!createTable) + { +// Close(); + return FALSE; + } + else + DemoFrame->CreateDataTable(TRUE); + } + + // Tables must be "opened" before anything other than creating/deleting table can be done + if (!Contact->Open()) + { + // Table does exist, or there was some problem opening it. Currently this should + // never fail, except in the case of the table not exisiting or the current + // user has insufficent privileges to access the table +#if 0 + +// This code is experimenting with a new function that will hopefully be available +// in the 2.4 release. This check will determine whether the open failing was due +// to the table not existing, or the users privileges being insufficient to +// open the table. + if (!Contact->GetDb()->TablePrivileges(CONTACT_TABLE_NAME,"SELECT",Contact->GetDb()->GetUsername(),DbConnectInf.defaultDir)) + { + wxString tStr; + tStr.Printf("Unable to open the table '%s'.\n\n",CONTACT_TABLE_NAME); + tStr += GetExtendedDBErrorMsg(__FILE__,__LINE__); + wxMessageBox(tStr,"ODBC Error...",wxOK | wxICON_EXCLAMATION); + } + else +#endif + if (Contact->GetDb()->TableExists(CONTACT_TABLE_NAME,Contact->GetDb()->GetUsername(),DbConnectInf.defaultDir)) + { + wxString tStr; + tStr.Printf("Unable to open the table '%s'.\n\n",CONTACT_TABLE_NAME); + tStr += GetExtendedDBErrorMsg(__FILE__,__LINE__); + wxMessageBox(tStr,"ODBC Error...",wxOK | wxICON_EXCLAMATION); + } + + return FALSE; + } + + // Build the dialog + + (void)new wxStaticBox(this, EDITOR_DIALOG_FN_GROUP, "", wxPoint(15, 1), wxSize(497, 69), 0, "FunctionGrp"); + (void)new wxStaticBox(this, EDITOR_DIALOG_SEARCH_GROUP, "", wxPoint(417, 1), wxSize(95, 242), 0, "SearchGrp"); + + pCreateBtn = new wxButton(this, EDITOR_DIALOG_CREATE, "&Create", wxPoint( 25, 21), wxSize( 70, 35), 0, wxDefaultValidator, "CreateBtn"); + pEditBtn = new wxButton(this, EDITOR_DIALOG_EDIT, "&Edit", wxPoint(102, 21), wxSize( 70, 35), 0, wxDefaultValidator, "EditBtn"); + pDeleteBtn = new wxButton(this, EDITOR_DIALOG_DELETE, "&Delete", wxPoint(179, 21), wxSize( 70, 35), 0, wxDefaultValidator, "DeleteBtn"); + pCopyBtn = new wxButton(this, EDITOR_DIALOG_COPY, "Cop&y", wxPoint(256, 21), wxSize( 70, 35), 0, wxDefaultValidator, "CopyBtn"); + pSaveBtn = new wxButton(this, EDITOR_DIALOG_SAVE, "&Save", wxPoint(333, 21), wxSize( 70, 35), 0, wxDefaultValidator, "SaveBtn"); + pCancelBtn = new wxButton(this, EDITOR_DIALOG_CANCEL, "C&ancel", wxPoint(430, 21), wxSize( 70, 35), 0, wxDefaultValidator, "CancelBtn"); + pPrevBtn = new wxButton(this, EDITOR_DIALOG_PREV, "<< &Prev", wxPoint(430, 81), wxSize( 70, 35), 0, wxDefaultValidator, "PrevBtn"); + pNextBtn = new wxButton(this, EDITOR_DIALOG_NEXT, "&Next >>", wxPoint(430, 121), wxSize( 70, 35), 0, wxDefaultValidator, "NextBtn"); + pQueryBtn = new wxButton(this, EDITOR_DIALOG_QUERY, "&Query", wxPoint(430, 161), wxSize( 70, 35), 0, wxDefaultValidator, "QueryBtn"); + pResetBtn = new wxButton(this, EDITOR_DIALOG_RESET, "&Reset", wxPoint(430, 200), wxSize( 70, 35), 0, wxDefaultValidator, "ResetBtn"); + pNameMsg = new wxStaticText(this, EDITOR_DIALOG_NAME_MSG, "Name:", wxPoint( 17, 80), wxSize( -1, -1), 0, "NameMsg"); + pNameTxt = new wxTextCtrl(this, EDITOR_DIALOG_NAME_TEXT, "", wxPoint( 17, 97), wxSize(308, 25), 0, wxDefaultValidator, "NameTxt"); + pNameListBtn = new wxButton(this, EDITOR_DIALOG_LOOKUP, "&Lookup", wxPoint(333, 97), wxSize( 70, 24), 0, wxDefaultValidator, "LookupBtn"); + pAddress1Msg = new wxStaticText(this, EDITOR_DIALOG_ADDRESS1_MSG, "Address:", wxPoint( 17, 130), wxSize( -1, -1), 0, "Address1Msg"); + pAddress1Txt = new wxTextCtrl(this, EDITOR_DIALOG_ADDRESS2_TEXT, "", wxPoint( 17, 147), wxSize(308, 25), 0, wxDefaultValidator, "Address1Txt"); + pAddress2Msg = new wxStaticText(this, EDITOR_DIALOG_ADDRESS2_MSG, "Address:", wxPoint( 17, 180), wxSize( -1, -1), 0, "Address2Msg"); + pAddress2Txt = new wxTextCtrl(this, EDITOR_DIALOG_ADDRESS2_TEXT, "", wxPoint( 17, 197), wxSize(308, 25), 0, wxDefaultValidator, "Address2Txt"); + pCityMsg = new wxStaticText(this, EDITOR_DIALOG_CITY_MSG, "City:", wxPoint( 17, 230), wxSize( -1, -1), 0, "CityMsg"); + pCityTxt = new wxTextCtrl(this, EDITOR_DIALOG_CITY_TEXT, "", wxPoint( 17, 247), wxSize(225, 25), 0, wxDefaultValidator, "CityTxt"); + pStateMsg = new wxStaticText(this, EDITOR_DIALOG_STATE_MSG, "State:", wxPoint(250, 230), wxSize( -1, -1), 0, "StateMsg"); + pStateTxt = new wxTextCtrl(this, EDITOR_DIALOG_STATE_TEXT, "", wxPoint(250, 247), wxSize(153, 25), 0, wxDefaultValidator, "StateTxt"); + pCountryMsg = new wxStaticText(this, EDITOR_DIALOG_COUNTRY_MSG, "Country:", wxPoint( 17, 280), wxSize( -1, -1), 0, "CountryMsg"); + pCountryTxt = new wxTextCtrl(this, EDITOR_DIALOG_COUNTRY_TEXT, "", wxPoint( 17, 297), wxSize(225, 25), 0, wxDefaultValidator, "CountryTxt"); + pPostalCodeMsg = new wxStaticText(this, EDITOR_DIALOG_POSTAL_MSG, "Postal Code:",wxPoint(250, 280), wxSize( -1, -1), 0, "PostalCodeMsg"); + pPostalCodeTxt = new wxTextCtrl(this, EDITOR_DIALOG_POSTAL_TEXT, "", wxPoint(250, 297), wxSize(153, 25), 0, wxDefaultValidator, "PostalCodeTxt"); + + wxString choice_strings[5]; + choice_strings[0] = "English"; + choice_strings[1] = "French"; + choice_strings[2] = "German"; + choice_strings[3] = "Spanish"; + choice_strings[4] = "Other"; + + pNativeLangChoice = new wxChoice(this, EDITOR_DIALOG_LANG_CHOICE, wxPoint( 17, 346), wxSize(277, -1), 5, choice_strings); + pNativeLangMsg = new wxStaticText(this, EDITOR_DIALOG_LANG_MSG, "Native language:", wxPoint( 17, 330), wxSize( -1, -1), 0, "NativeLangMsg"); + + wxString radio_strings[2]; + radio_strings[0] = "No"; + radio_strings[1] = "Yes"; + pDeveloperRadio = new wxRadioBox(this,EDITOR_DIALOG_DEVELOPER, "Developer:", wxPoint(303, 330), wxSize( -1, -1), 2, radio_strings, 2, wxHORIZONTAL); + pJoinDateMsg = new wxStaticText(this, EDITOR_DIALOG_JOIN_MSG, "Date joined:", wxPoint( 17, 380), wxSize( -1, -1), 0, "JoinDateMsg"); + pJoinDateTxt = new wxTextCtrl(this, EDITOR_DIALOG_JOIN_TEXT, "", wxPoint( 17, 397), wxSize(150, 25), 0, wxDefaultValidator, "JoinDateTxt"); + pContribMsg = new wxStaticText(this, EDITOR_DIALOG_CONTRIB_MSG,"Contributions:", wxPoint(175, 380), wxSize( -1, -1), 0, "ContribMsg"); + pContribTxt = new wxTextCtrl(this, EDITOR_DIALOG_CONTRIB_TEXT, "", wxPoint(175, 397), wxSize(120, 25), 0, wxDefaultValidator, "ContribTxt"); + pLinesMsg = new wxStaticText(this, EDITOR_DIALOG_LINES_MSG, "Lines of code:", wxPoint(303, 380), wxSize( -1, -1), 0, "LinesMsg"); + pLinesTxt = new wxTextCtrl(this, EDITOR_DIALOG_LINES_TEXT, "", wxPoint(303, 397), wxSize(100, 25), 0, wxDefaultValidator, "LinesTxt"); + + // Now that all the widgets on the panel are created, its safe to allow ::OnCommand() to + // handle all widget processing + widgetPtrsSet = TRUE; + + // Setup the orderBy and where clauses to return back a single record as the result set, + // as there will only be one record being shown on the dialog at a time, this optimizes + // network traffic by only returning a one row result + + Contact->SetOrderByClause("NAME"); // field name to sort by + + // The wxString "whereStr" is not a member of the wxDbTable object, it is a member variable + // specifically in the Ccontact class. It is used here for simpler construction of a varying + // length string, and then after the string is built, the wxDbTable member variable "where" is + // assigned the pointer to the constructed string. + // + // The constructed where clause below has a sub-query within it "SELECT MIN(NAME) FROM %s" + // to achieve a single row (in this case the first name in alphabetical order). + + if (Contact->GetDb()->Dbms() != dbmsPOSTGRES && Contact->GetDb()->Dbms() != dbmsMY_SQL) + { + Contact->whereStr.sprintf("NAME = (SELECT MIN(NAME) FROM %s)",Contact->GetTableName()); + // NOTE: (const char*) returns a pointer which may not be valid later, so this is short term use only + Contact->SetWhereClause(Contact->whereStr.c_str()); + } + else + Contact->SetWhereClause(""); + + // Perform the Query to get the result set. + // NOTE: If there are no rows returned, that is a valid result, so Query() would return TRUE. + // Only if there is a database error will Query() come back as FALSE + if (!Contact->Query()) + { + wxString tStr; + tStr = "ODBC error during Query()\n\n"; + tStr += GetExtendedDBErrorMsg(__FILE__,__LINE__); + wxMessageBox(tStr,"ODBC Error...",wxOK | wxICON_EXCLAMATION); +// GetParent()->Close(); + return FALSE; + } + + // Since Query succeeded, now get the row that was returned + if (!Contact->GetNext()) + // If the GetNext() failed at this point, then there are no rows to retrieve, + // so clear the values in the members of "Contact" so that PutData() blanks the + // widgets on the dialog + Contact->Initialize(); + + SetMode(mView); + PutData(); + + Show(TRUE); + + initialized = TRUE; + return TRUE; +} // CeditorDlg::Initialize() + + void CeditorDlg::FieldsEditable() { pNameTxt->Enable((mode == mCreate) || (mode == mEdit)); @@ -1235,7 +1331,7 @@ bool CeditorDlg::GetNextRec() { wxString w; - if (Contact->GetDb()->Dbms() != dbmsPOSTGRES) + if (Contact->GetDb()->Dbms() != dbmsPOSTGRES && Contact->GetDb()->Dbms() != dbmsMY_SQL) { w = "NAME = (SELECT MIN(NAME) FROM "; w += Contact->GetTableName(); @@ -1270,7 +1366,7 @@ bool CeditorDlg::GetPrevRec() { wxString w; - if (Contact->GetDb()->Dbms() != dbmsPOSTGRES) + if (Contact->GetDb()->Dbms() != dbmsPOSTGRES && Contact->GetDb()->Dbms() != dbmsMY_SQL) { w = "NAME = (SELECT MAX(NAME) FROM "; w += Contact->GetTableName(); @@ -1385,11 +1481,11 @@ void CparameterDlg::OnCloseWindow(wxCloseEvent& event) wxGetApp().params = savedParamSettings; } - if (GetParent() != NULL) - GetParent()->SetFocus(); +// if (GetParent() != NULL) +// GetParent()->SetFocus(); this->Destroy(); -} // Cparameter::OnCloseWindow() +} // CparameterDlg::OnCloseWindow() void CparameterDlg::OnButton( wxCommandEvent &event ) @@ -1566,7 +1662,7 @@ CqueryDlg::CqueryDlg(wxWindow *parent, wxDb *pDb, char *tblName[], char *pWhereA // Initialize the WHERE clause from the string passed in pWhere = pWhereArg; // Save a pointer to the output buffer - if (wxStrlen(pWhere) > DB_MAX_WHERE_CLAUSE_LEN) // Check the length of the buffer passed in + if (wxStrlen(pWhere) > (unsigned int)DB_MAX_WHERE_CLAUSE_LEN) // Check the length of the buffer passed in { wxString s; s.Printf("Maximum where clause length exceeded.\nLength must be less than %d", DB_MAX_WHERE_CLAUSE_LEN+1); @@ -1848,7 +1944,7 @@ void CqueryDlg::OnCommand(wxWindow& win, wxCommandEvent& event) if (widgetName == pQueryDoneBtn->GetName()) { // Be sure the where clause will not overflow the output buffer - if (wxStrlen(pQuerySqlWhereMtxt->GetValue()) > DB_MAX_WHERE_CLAUSE_LEN) + if (wxStrlen(pQuerySqlWhereMtxt->GetValue()) > (unsigned int)DB_MAX_WHERE_CLAUSE_LEN) { wxString s; s.Printf("Maximum where clause length exceeded.\nLength must be less than %d", DB_MAX_WHERE_CLAUSE_LEN+1); diff --git a/samples/db/dbtest.h b/samples/db/dbtest.h index 375dd149e3..ecfd357a04 100644 --- a/samples/db/dbtest.h +++ b/samples/db/dbtest.h @@ -20,9 +20,11 @@ enum DialogModes {mView,mCreate,mEdit,mSearch}; // ID for the menu quit command -#define FILE_CREATE 100 -#define FILE_EXIT 199 -#define EDIT_PARAMETERS 200 +#define FILE_CREATE 100 +#define FILE_RECREATE_TABLE 110 +#define FILE_RECREATE_INDEXES 120 +#define FILE_EXIT 199 +#define EDIT_PARAMETERS 200 #define ABOUT_DEMO 300 // this seems to be missing, Robert Roebling (?) @@ -129,11 +131,13 @@ class DatabaseDemoFrame: public wxFrame void OnCloseWindow(wxCloseEvent& event); void OnCreate(wxCommandEvent& event); + void OnRecreateTable(wxCommandEvent& event); + void OnRecreateIndexes(wxCommandEvent& event); void OnExit(wxCommandEvent& event); void OnEditParameters(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); - void CreateDataTable(); + void CreateDataTable(bool recreate); void BuildEditorDialog(); void BuildParameterDialog(wxWindow *parent); @@ -163,6 +167,7 @@ class CeditorDlg : public wxPanel wxStaticText *pNativeLangMsg; public: + bool initialized; enum DialogModes mode; Ccontact *Contact; // this is the table object that will be being manipulated @@ -173,6 +178,7 @@ class CeditorDlg : public wxPanel void OnCommand(wxWindow& win, wxCommandEvent& event); void OnActivate(bool) {}; // necessary for hot keys + bool Initialize(); void FieldsEditable(); void SetMode(enum DialogModes m); bool PutData();