DatabaseManager.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <cstdint>
  5. #include <optional>
  6. #include <sqlite3.h>
  7. // データベースに保存される画像情報を保持する構造体
  8. struct ImageData {
  9. int64_t id; // DBのプライマリキー
  10. std::string path; // 画像のファイルパス
  11. uint64_t dhash; // 計算済みのdHash値
  12. uint64_t phash; // 計算済みのpHash値
  13. int64_t timestamp; // ファイルの最終更新時刻(再計算判定用)
  14. int64_t file_size; // ファイルサイズ(再計算判定用)
  15. bool is_searched = false; // 検索完了ステータス(GUI等用)
  16. };
  17. // SQLiteを用いた画像メタデータとハッシュのDB管理クラス
  18. class DatabaseManager {
  19. public:
  20. DatabaseManager(const std::string& dbPath);
  21. ~DatabaseManager();
  22. // データベースとの接続を開く
  23. bool open();
  24. // データベースを閉じる
  25. void close();
  26. // 画像データをDBに追加または更新する
  27. bool addImage(const ImageData& data);
  28. // DB内の全画像情報を取得する
  29. std::vector<ImageData> getAllImages();
  30. // 指定されたパスの画像をDBから削除する
  31. bool removeImage(const std::string& path);
  32. // 実体ファイルが削除済みの古いエントリをDBから消去する
  33. void cleanupStaleEntries();
  34. // トランザクション処理(大量追加時の高速化のため)
  35. void beginTransaction();
  36. void commitTransaction();
  37. void rollbackTransaction();
  38. // 単一パスの画像情報をDBから検索して返す
  39. std::optional<ImageData> getImageByPath(const std::string& path);
  40. // 指定ディレクトリ配下の全画像の検索完了状態を一括更新する
  41. bool setDirectorySearchedStatus(const std::string& dirPath, bool isSearched);
  42. private:
  43. std::string m_dbPath;
  44. sqlite3* m_db = nullptr;
  45. bool initSchema();
  46. };