From 0afe9ca6bf53da111e445d2aea7cfc94e7e3c601 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 9 Aug 2020 17:12:14 +0200 Subject: db backend: mostly feature-complete --- sqlitebackend.h | 65 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 25 deletions(-) (limited to 'sqlitebackend.h') diff --git a/sqlitebackend.h b/sqlitebackend.h index 4dfd2f9..52faedc 100644 --- a/sqlitebackend.h +++ b/sqlitebackend.h @@ -16,86 +16,101 @@ enum TagChange { class Tag { public: + Tag() : valid(false) {} Tag(long long int id, QString name, qreal anchor_x, qreal anchor_y, QByteArray metadata); - Tag(); Tag(long long int id, const Tag &other); + bool isValid() { return valid; } + long long int id; QString name; QPointF anchor; QVariantMap metadata; + +private: + bool valid; }; class SQLiteSaveFile : public QObject { Q_OBJECT public: - explicit SQLiteSaveFile(QObject *parent = nullptr, QString filename = ":memory:"); - bool connect(); - bool isOpen() { return m_isOpen; } + explicit SQLiteSaveFile(QObject *parent = nullptr); QList getAllTags(); - QByteArray getImage(); + const QByteArray &getImage() const { return m_image; }; bool updateTag(Tag tag); bool deleteTag(Tag tag); bool createTag(Tag tag); + bool isMemory() { return m_memory; } /* backend db points to temporary memory db */ + bool isDirty() { return m_dirty; } /* backend db was changed since opening */ + bool isOpen() { return m_open; } /* backend db is open */ + bool setMeta(const QString &key, const QVariant &value); bool setMeta(std::initializer_list> metas); - QVariant getMeta(const QString &key); + const QVariant getMeta(const QString &key) const; - QString errorString() { return lastErrorString; } + const QString &errorString() const { return m_lastErrorString; } enum Error { NoError = 0, + FileNotFoundError, SQLiteError, ImageOpenError, - ImageReadError + ImageReadError, + MaxError }; - inline const static QString errorNames[] = { + inline const static QString errorNames[MaxError] = { [NoError] = "No Error", + [FileNotFoundError] = "File not found", [SQLiteError] = "Database Error", [ImageOpenError] = "Error Opening Image", [ImageReadError] = "Error Reading Image" }; Error error(); - void resetError() { lastError = NoError; lastErrorString = QString(); } + void resetError() const { m_lastError = NoError; m_lastErrorString = QString(); } public slots: /** Save this project file under a new name. This changes the backend database this project file object points to, and copies all data. * Callers can continue to use the same project file object afterwards. */ bool saveAs(const QString &filename); + bool open(const QString &filename); bool reloadImageFromDisk(); - bool loadImageFromDisk(const QString &filename); + bool loadImageFromDisk(const QString &m_filename); bool clearNew(); signals: void tagChange(TagChange change, const Tag &tag); void fileReload(); - void fileIOError(Error e, QString errorName, QString description); + void fileIOError(Error e, QString errorName, QString description) const; private: + bool connect(); bool initDb(bool setCreationDate=true); bool runSql(QString query, std::initializer_list bindings={}); bool setMetaLocked(const QString &key, const QVariant &value); bool setMetaLocked(std::initializer_list> metas); - QVariant getMetaLocked(const QString &key); - - void setError(Error e, QString desc) { lastError = e; lastErrorString = desc; fileIOError(e, errorNames[e], desc); } - bool setDatabaseError(const QSqlQuery &q); - bool setDatabaseError(const QSqlDatabase &db); - - Error lastError; - QString lastErrorString; - QSqlDatabase db; - QMutex dbMut; - QString filename; - QByteArray imageData; - bool m_isOpen; + const QVariant getMetaLocked(const QString &key) const; + + void setError(Error e, QString desc) const { m_lastError = e; m_lastErrorString = desc; fileIOError(e, errorNames[e], desc); } + bool setDatabaseError(const QSqlQuery &q) const; + bool setDatabaseError(const QSqlDatabase &m_db) const; + + mutable Error m_lastError; + mutable QString m_lastErrorString; + QSqlDatabase m_db; + mutable QMutex m_dbMut; + QString m_filename; + QByteArray m_image; + + bool m_open; + bool m_dirty; + bool m_memory; }; -- cgit