! @pre *CurConn() does not exist @post *CurConn() exists or return value is false */
| 485 | @post *CurConn() exists or return value is false |
| 486 | */ |
| 487 | bool ProjectFileIO::OpenConnection(FilePath fileName /* = {} */) |
| 488 | { |
| 489 | auto &curConn = CurrConn(); |
| 490 | wxASSERT(!curConn); |
| 491 | bool isTemp = false; |
| 492 | |
| 493 | if (fileName.empty()) |
| 494 | { |
| 495 | fileName = GetFileName(); |
| 496 | if (fileName.empty()) |
| 497 | { |
| 498 | fileName = TempDirectory::UnsavedProjectFileName(); |
| 499 | isTemp = true; |
| 500 | } |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | // If this project resides in the temporary directory, then we'll mark it |
| 505 | // as temporary. |
| 506 | wxFileName temp(TempDirectory::TempDir(), wxT("")); |
| 507 | wxFileName file(fileName); |
| 508 | file.SetFullName(wxT("")); |
| 509 | if (file == temp) |
| 510 | { |
| 511 | isTemp = true; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // Pass weak_ptr to project into DBConnection constructor |
| 516 | curConn = std::make_unique<DBConnection>( |
| 517 | mProject.shared_from_this(), mpErrors, [this]{ OnCheckpointFailure(); } ); |
| 518 | auto rc = curConn->Open(fileName); |
| 519 | if (rc != SQLITE_OK) |
| 520 | { |
| 521 | // Must use SetError() here since we do not have an active DB |
| 522 | SetError( |
| 523 | XO("Failed to open database file:\n\n%s").Format(fileName), |
| 524 | {}, |
| 525 | rc |
| 526 | ); |
| 527 | curConn.reset(); |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | if (!CheckVersion()) |
| 532 | { |
| 533 | CloseConnection(); |
| 534 | curConn.reset(); |
| 535 | return false; |
| 536 | } |
| 537 | |
| 538 | mTemporary = isTemp; |
| 539 | |
| 540 | SetFileName(fileName); |
| 541 | |
| 542 | return true; |
| 543 | } |
| 544 |