void CSelectDB::OnSelect() { OPENFILENAME FileName; TCHAR szFileName[400]; TCHAR szDir[400]; memset(&FileName, 0, sizeof(FileName)); memset(szFileName, 0, sizeof(szFileName)); memset(&szDir, 0, sizeof(szDir)); FileName.lStructSize = sizeof(FileName); FileName.lpstrTitle = _T("Open Database"); FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST; FileName.nMaxFile = 400; FileName.lpstrFile = szFileName; FileName.lpstrInitialDir = szDir; FileName.lpstrFilter = _T("Database Files (.MDB)\0*.mdb"); FileName.lpstrDefExt = _T("mdb"); if(GetOpenFileName(&FileName) == 0) return; CString csPath(FileName.lpstrFile); if(ValidDB(csPath) == FALSE) { MessageBox(_T("Invalid Database"), _T("Ditto"), MB_OK); m_ePath.SetFocus(); } else m_ePath.SetWindowText(csPath); }
void CSelectDB::OnUseDefault() { CGetSetOptions::SetDBPath(""); CString csPath = CGetSetOptions::GetDBPath(); if(ValidDB(csPath) == FALSE) DeleteFile(csPath); if(CheckDBExists(CGetSetOptions::GetDBPath())) EndDialog(IDOK); }
BOOL COpenAccessdatabase::ValidDB(CString csPath, BOOL bUpgrade) { BOOL bResult = TRUE; BOOL bUpgraded = FALSE; try { CDaoDatabase db; try { db.Open(csPath); } catch(CDaoException* e) { TCHAR szErrorMessage[512]; UINT nHelpContext; if(e->GetErrorMessage(szErrorMessage, 512, &nHelpContext)) { if(STRCMP(szErrorMessage, _T("Unable to initialize DAO/Jet db engine.")) == 0) { e->Delete(); return ERROR_OPENING_DATABASE; } } e->ReportError(); e->Delete(); return FALSE; } CDaoTableDef table(&db); CDaoFieldInfo info; table.Open(_T("Main")); table.GetFieldInfo(_T("lID"), info); table.GetFieldInfo(_T("lDate"), info); ON_FIELD_ABSENT(_T("mText"), Upgrade_mText(db)); // +mText, -strText, -strType table.GetFieldInfo(_T("lShortCut"), info); table.GetFieldInfo(_T("lDontAutoDelete"), info); table.GetFieldInfo(_T("lTotalCopySize"), info); ON_FIELD_ABSENT(_T("bIsGroup"), Upgrade_Groups(db)); table.GetFieldInfo(_T("lParentID"), info); // part of Upgrade_Groups table.GetFieldInfo(_T("dOrder"), info); // part of Upgrade_Groups ON_FIELD_ABSENT(_T("lDataID"), Upgrade_ShareData(db)); // +lDataID, -lParentID table.Close(); table.Open(_T("Data")); table.GetFieldInfo(_T("lID"), info); table.GetFieldInfo(_T("lDataID"), info); // part of Upgrade_ShareData() table.GetFieldInfo(_T("strClipBoardFormat"), info); table.GetFieldInfo(_T("ooData"), info); table.Close(); table.Open(_T("Types")); table.GetFieldInfo(_T("ID"), info); table.GetFieldInfo(_T("TypeText"), info); table.Close(); } catch(CDaoException* e) { ASSERT(FALSE); e->Delete(); return FALSE; } // if we upgraded, perform full validation again without upgrading if( bUpgraded ) return ValidDB(csPath, FALSE); return bResult; }
BOOL CheckDBExists(CString csDBPath) { //If this is the first time running this version then convert the old database to the new db if(csDBPath.IsEmpty() && g_Opt.m_bU3 == false) { csDBPath = GetDefaultDBName(); if(FileExists(csDBPath) == FALSE && CGetSetOptions::GetIsPortableDitto() == FALSE) { CString csOldDB = CGetSetOptions::GetDBPathOld(); if(csOldDB.IsEmpty()) { csOldDB = GetOLDDefaultDBName(); } if(FileExists(csOldDB)) { //create the new sqlite db CreateDB(csDBPath); CAccessToSqlite Convert; Convert.ConvertDatabase(csDBPath, csOldDB); } } } BOOL bRet = FALSE; if(FileExists(csDBPath) == FALSE) { csDBPath = GetDefaultDBName(); nsPath::CPath FullPath(csDBPath); CString csPath = FullPath.GetPath().GetStr(); if(csPath.IsEmpty() == false && FileExists(csDBPath) == FALSE) { CreateDirectory(csPath, NULL); } // -- create a new one bRet = CreateDB(csDBPath); } else { if(ValidDB(csDBPath) == FALSE) { //Db existed but was bad CString csMarkAsBad; csMarkAsBad = csDBPath; csMarkAsBad.Replace(_T("."), _T("_BAD.")); CString csPath = GetDefaultDBName(); CString cs; cs.Format(_T("%s \"%s\",\n") _T("%s \"%s\",\n") _T("%s,\n") _T("\"%s\""), theApp.m_Language.GetString("Database_Format", "Unrecognized Database Format"), csDBPath, theApp.m_Language.GetString("File_Renamed", "the file will be renamed"), csMarkAsBad, theApp.m_Language.GetString("New_Database", "and a new database will be created"), csPath); AfxMessageBox(cs); CFile::Rename(csDBPath, csMarkAsBad); csDBPath = csPath; bRet = CreateDB(csDBPath); } else { bRet = TRUE; } } if(bRet) { bRet = OpenDatabase(csDBPath); } return bRet; }