// Passing NULL for the SQL environment handle causes
// the wxDbConnectInf constructor to obtain a handle
// for you.
//
// WARNING: Be certain that you do not free this handle
// directly with SQLFreeEnv(). Use either the
// method ::FreeHenv() or delete the DbConnectInf.
DbConnectInf = new wxDbConnectInf(NULL, params.ODBCSource, params.UserName,
params.Password, params.DirPath);
if (!DbConnectInf || !DbConnectInf->GetHenv())
{
wxMessageBox(wxT("Unable to define data source connection info."), wxT("DB CONNECTION ERROR..."),wxOK | wxICON_EXCLAMATION);
wxDELETE(DbConnectInf);
}
if (!ReadParamFile(params))
DemoFrame->BuildParameterDialog(NULL);
if (!wxStrlen(params.ODBCSource))
{
wxDELETE(DbConnectInf);
return(FALSE);
}
DbConnectInf->SetDsn(params.ODBCSource);
DbConnectInf->SetUserID(params.UserName);
DbConnectInf->SetPassword(params.Password);
DbConnectInf->SetDefaultDir(params.DirPath);
READONLY_DB = wxDbGetConnection(DbConnectInf);
if (READONLY_DB == 0)
{
wxMessageBox(wxT("Unable to connect to the data source.\n\nCheck the name of your data source to verify it has been correctly entered/spelled.\n\nWith some databases, the user name and password must\nbe created with full rights to the CONTACT table prior to making a connection\n(using tools provided by the database manufacturer)"), wxT("DB CONNECTION ERROR..."),wxOK | wxICON_EXCLAMATION);
DemoFrame->BuildParameterDialog(NULL);
wxDELETE(DbConnectInf);
wxMessageBox(wxT("Now exiting program.\n\nRestart program to try any new settings."),wxT("Notice..."),wxOK | wxICON_INFORMATION);
if ((paramFile = fopen(PARAM_FILENAME, wxT("r"))) == NULL)
{
wxString tStr;
tStr.Printf(wxT("Unable to open the parameter file '%s' for reading.\n\nYou must specify the data source, user name, and\npassword that will be used and save those settings."),PARAM_FILENAME);
Ok = (wxMessageBox(wxT("Any data currently residing in the table will be erased.\n\nAre you sure?"),wxT("Confirm"),wxYES_NO|wxICON_QUESTION) == wxYES);
if (!Ok)
return;
wxBeginBusyCursor();
bool success = TRUE;
Contact->GetDb()->RollbackTrans(); // Make sure the current cursor is in a known/stable state
if (!Contact->CreateTable(recreate))
{
wxEndBusyCursor();
wxString tStr;
tStr = wxT("Error creating CONTACTS table.\nTable was not created.\n\n");
wxMessageBox("Support datatypes was dumped to stdout.");
return;
} // Data types Button
if (widgetName == pDbDiagsBtn->GetName())
{
DisplayDbDiagnostics(wxGetApp().READONLY_DB);
wxMessageBox("Diagnostics info was dumped to stdout.");
return;
}
if (widgetName == pCatalogBtn->GetName())
{
if (wxGetApp().Contact->GetDb()->Catalog("","catalog.txt"))
wxMessageBox("The file 'catalog.txt' was created.");
else
wxMessageBox("Creation of the file 'catalog.txt' was failed.");
return;
}
} // 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)
wxGetApp().Contact = new Ccontact();
if (!wxGetApp().Contact)
{
wxMessageBox(wxT("Unable to instantiate an instance of Ccontact"),wxT("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 (!wxGetApp().Contact->GetDb()->TableExists((wxChar *)CONTACT_TABLE_NAME,
wxGetApp().DbConnectInf->GetUserID(),
wxGetApp().DbConnectInf->GetDefaultDir()))
{
wxString tStr;
tStr.Printf(wxT("Unable to open the table '%s'. The table may\nneed to be created.\n\nDo you wish to try to create/clear the table?\n\n"),CONTACT_TABLE_NAME);
if (!wxGetApp().Contact->GetDb()->TableExists(CONTACT_TABLE_NAME,
wxGetApp().Contact->GetDb()->GetUsername(),
wxGetApp().DbConnectInf->GetDefaultDir()))
{
wxString tStr;
tStr.Printf(wxT("Unable to open the table '%s' as the table\ndoes not appear to exist in the tablespace available\nto the currently logged in user.\n\n"),CONTACT_TABLE_NAME);
wxMessageBox(wxT("Improper date format. Please check the date\nspecified and try again.\n\nNOTE: Dates are in american format (MM/DD/YYYY)"),wxT("Notice..."),wxOK | wxICON_INFORMATION);
// Put any additional checking necessary to make certain it is alright
// to close the program here that is not done elsewhere
if (!saved)
{
bool Ok = (wxMessageBox(wxT("No changes have been saved.\n\nAre you sure you wish exit the parameter screen?"),wxT("Confirm"),wxYES_NO|wxICON_QUESTION) == wxYES);
if (!Ok)
{
event.Veto();
return;
}
wxGetApp().params = savedParamSettings;
}
if (GetParent() != NULL)
GetParent()->SetFocus();
while (wxIsBusy())
wxEndBusyCursor();
Show(FALSE);
SetReturnCode(0); // added so BoundsChecker would not report use of uninitialized variable
if (tStr.Length() > (sizeof(wxGetApp().params.ODBCSource)-1))
{
wxString errmsg;
errmsg.Printf(wxT("ODBC Data source name is longer than the data structure to hold it.\n'Cparameter.ODBCSource' must have a larger character array\nto handle a data source with this long of a name\n\nThe data source currently selected is %d characters long."),tStr.Length());
wxMessageBox(errmsg,wxT("Internal program error..."),wxOK | wxICON_EXCLAMATION);
return FALSE;
}
wxStrcpy(wxGetApp().params.ODBCSource, tStr);
}
else
return FALSE;
tStr = pParamUserNameTxt->GetValue();
if (tStr.Length() > (sizeof(wxGetApp().params.UserName)-1))
{
wxString errmsg;
errmsg.Printf(wxT("User name is longer than the data structure to hold it.\n'Cparameter.UserName' must have a larger character array\nto handle a data source with this long of a name\n\nThe user name currently specified is %d characters long."),tStr.Length());
wxMessageBox(errmsg,wxT("Internal program error..."),wxOK | wxICON_EXCLAMATION);
return FALSE;
}
wxStrcpy(wxGetApp().params.UserName, tStr);
tStr = pParamPasswordTxt->GetValue();
if (tStr.Length() > (sizeof(wxGetApp().params.Password)-1))
{
wxString errmsg;
errmsg.Printf(wxT("Password is longer than the data structure to hold it.\n'Cparameter.Password' must have a larger character array\nto handle a data source with this long of a name\n\nThe password currently specified is %d characters long."),tStr.Length());
wxMessageBox(errmsg,wxT("Internal program error..."),wxOK | wxICON_EXCLAMATION);
return FALSE;
}
wxStrcpy(wxGetApp().params.Password,tStr);
tStr = pParamDirPathTxt->GetValue();
tStr.Replace(wxT("\\"),wxT("/"));
if (tStr.Length() > (sizeof(wxGetApp().params.DirPath)-1))
{
wxString errmsg;
errmsg.Printf(wxT("DirPath is longer than the data structure to hold it.\n'Cparameter.DirPath' must have a larger character array\nto handle a data source with this long of a name\n\nThe password currently specified is %d characters long."),tStr.Length());
wxMessageBox(errmsg,wxT("Internal program error..."),wxOK | wxICON_EXCLAMATION);
return FALSE;
}
wxStrcpy(wxGetApp().params.DirPath,tStr);
return TRUE;
} // CparameterDlg::GetData()
bool CparameterDlg::Save()
{
// Copy the current params in case user cancels changing
// the params, so that we can reset them.
if (!GetData())
{
wxGetApp().params = savedParamSettings;
return FALSE;
}
wxGetApp().WriteParamFile(wxGetApp().params);
return TRUE;
} // CparameterDlg::Save()
void CparameterDlg::FillDataSourceList()
{
wxChar Dsn[SQL_MAX_DSN_LENGTH + 1];
wxChar DsDesc[255];
wxStringList strList;
while (wxDbGetDataSource(wxGetApp().DbConnectInf->GetHenv(), Dsn,
tStr.Printf(wxT("%lu of %lu records match the query criteria.\n\nEither the criteria entered produced a result set\nwith no records, or there was a syntactical error\nin the clause you entered.\n\nPress the details button to see if any database errors were reported."),whereCnt,totalCnt);