c749d642cf
- Add title/description search with Ctrl+F and a result dialog\n- Support daily/weekly/monthly recurring blocks with conflict skipping\n- Add CPack NSIS packaging, MinGW release preset, and deploy script\n- Add window/view/list/dialog fade animations\n- Document multi-device conflict behavior and update validation screenshots
92 lines
3.3 KiB
C++
92 lines
3.3 KiB
C++
#pragma once
|
|
|
|
#include <QSqlDatabase>
|
|
#include <QString>
|
|
#include <QVector>
|
|
|
|
#include <atomic>
|
|
#include <optional>
|
|
|
|
#include "core/EnergyProfile.h"
|
|
#include "core/ProposedDiff.h"
|
|
#include "core/Recurrence.h"
|
|
#include "core/SyncTypes.h"
|
|
#include "core/ScheduleItem.h"
|
|
|
|
/*
|
|
* 类名: SqliteStore
|
|
* 功能: SQLite 持久化与迁移;只暴露领域对象,不向上层泄漏 SQL 细节
|
|
* 线程安全: 否;一个实例只允许在其创建线程使用(后续如需跨线程将按线程复用连接)
|
|
*/
|
|
class SqliteStore
|
|
{
|
|
public:
|
|
explicit SqliteStore(QString databasePath);
|
|
~SqliteStore();
|
|
|
|
SqliteStore(const SqliteStore&) = delete;
|
|
SqliteStore& operator=(const SqliteStore&) = delete;
|
|
|
|
bool open(QString* error = nullptr);
|
|
void close();
|
|
bool isOpen() const;
|
|
QString databasePath() const;
|
|
QString lastError() const;
|
|
int schemaVersion() const;
|
|
|
|
bool beginTransaction();
|
|
bool commit();
|
|
void rollback();
|
|
|
|
bool insertTask(const Task& task);
|
|
bool updateTask(const Task& task);
|
|
std::optional<Task> taskById(const QString& id);
|
|
bool deleteTask(const QString& id);
|
|
bool hasBlocksForTask(const QString& taskId);
|
|
bool insertRecurrence(const Recurrence& recurrence);
|
|
std::optional<Recurrence> recurrenceForTask(const QString& taskId) const;
|
|
|
|
bool insertTimeBlock(const TimeBlock& block);
|
|
bool updateTimeBlock(const TimeBlock& block);
|
|
std::optional<TimeBlock> timeBlockById(const QString& id);
|
|
bool deleteTimeBlock(const QString& id);
|
|
|
|
bool insertProposedDiff(const ProposedDiff& diff);
|
|
std::optional<ProposedDiff> proposedDiffById(const QString& id);
|
|
bool updateProposedDiffState(const QString& id, const QString& state, const QString& resolvedAt);
|
|
|
|
// 返回与 [fromUtc, toUtc) 相交的所有块,按开始时间升序。
|
|
QVector<ScheduleItem> blocksInRange(const QDateTime& fromUtc, const QDateTime& toUtc) const;
|
|
// 已结束但仍未完成、且结束时间在最近 windowDays 内的块。
|
|
QVector<ScheduleItem> overdueBefore(const QDateTime& nowUtc, int windowDays = 30) const;
|
|
QVector<ScheduleItem> search(const QString& query, int limit = 100) const;
|
|
|
|
QVector<SyncOutboxItem> outboxBatch(int limit, QString* error = nullptr) const;
|
|
int outboxCount(QString* error = nullptr) const;
|
|
bool clearOutboxThrough(qint64 id, QString* error = nullptr);
|
|
bool setRemoteApplySuppressed(bool enabled, QString* error = nullptr);
|
|
bool applyRemoteChange(const RemoteChange& change, QString* error = nullptr);
|
|
qint64 syncCursor(const QString& deviceId, QString* error = nullptr) const;
|
|
bool setSyncCursor(const QString& deviceId, qint64 seq, QString* error = nullptr);
|
|
|
|
QVector<EnergyLevel> energyLevels() const;
|
|
bool replaceEnergyLevels(const QVector<EnergyLevel>& levels);
|
|
QVector<EnergySample> completedEnergySamples() const;
|
|
|
|
private:
|
|
bool openDatabase(QString* error);
|
|
bool configureConnection(QString* error);
|
|
bool migrate(QString* error);
|
|
bool execScriptStatement(const QString& sql);
|
|
bool setError(const QString& message) const;
|
|
bool isOpenInternal() const;
|
|
|
|
static QString makeConnectionName();
|
|
|
|
QString m_databasePath;
|
|
QString m_connectionName;
|
|
QSqlDatabase m_database;
|
|
int m_schemaVersion = 0;
|
|
mutable QString m_lastError;
|
|
};
|