swift
datastore.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_MISC_DB_DATASTORE_H
7 #define SWIFT_MISC_DB_DATASTORE_H
8 
9 #include <QJsonObject>
10 #include <QJsonValue>
11 #include <QObject>
12 #include <QString>
13 #include <QVersionNumber>
14 
15 #include "config/buildconfig.h"
16 #include "misc/propertyindex.h"
17 #include "misc/swiftmiscexport.h"
18 #include "misc/timestampbased.h"
19 
20 namespace swift::misc
21 {
22  class CIcon;
23  namespace db
24  {
27  {
28  Undefined = 0,
29  Valid = 1 << 0,
30  Invalid = 1 << 1,
31  All = Valid | Invalid
32  };
33 
35  Q_DECLARE_FLAGS(DbKeyStateFilter, DbKeyState)
36 
37 
41  {
42  // since we use different keys all the compares, set, get are in the derived class
43  // in general we can say, it was a bad decision to use different key types
44  // IndexDbIntegerKey = CPropertyIndexRef::GlobalIndexIDatastore for future usage
45 
46  public:
48  const QString &getVersion() const { return m_version; }
49 
51  QVersionNumber getQVersion() const;
52 
54  bool hasVersion() const { return !m_version.isEmpty(); }
55 
57  void setVersion(const QString &version) { m_version = version; }
58 
59  protected:
61  void setTimestampVersionFromDatabaseJson(const QJsonObject &json, const QString &prefix = QString());
62 
63  QString m_version;
64  };
65 
70  {
71  public:
74  {
75  IndexDbIntegerKey = CPropertyIndexRef::GlobalIndexIDatastoreInteger,
76  IndexDbKeyAsString,
77  IndexIsLoadedFromDb,
78  IndexDatabaseIcon,
79  IndexVersion,
80  IndexEndMarker
81  };
82 
84  int getDbKey() const { return m_dbKey; }
85 
87  QString getDbKeyAsString() const;
88 
90  QJsonValue getDbKeyAsJsonValue() const;
91 
93  QString getDbKeyAsStringInParentheses(const QString &prefix = {}) const;
94 
96  void setDbKey(int key) { m_dbKey = key; }
97 
99  void setDbKey(const QString &key);
100 
102  bool hasValidDbKey() const { return m_dbKey >= 0; }
103 
105  bool isDbEqual(const IDatastoreObjectWithIntegerKey &other) const
106  {
107  return other.isLoadedFromDb() && this->isLoadedFromDb() && other.getDbKey() == this->getDbKey();
108  }
109 
112  bool isLoadedFromDb() const;
113 
115  bool matchesDbKeyState(db::DbKeyStateFilter filter) const;
116 
118  const CIcon &toDatabaseIcon() const;
119 
121  static int invalidDbKey() { return -1; }
122 
124  static int stringToDbKey(const QString &candidate);
125 
127  static const CPropertyIndex &keyIndex()
128  {
129  static const CPropertyIndex k(IndexDbIntegerKey);
130  return k;
131  }
132 
133  protected:
136 
138  IDatastoreObjectWithIntegerKey(int key) : m_dbKey(key) {}
139 
141  void setKeyVersionTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix = QString());
142 
144  static bool existsKey(const QJsonObject &json, const QString &prefix = QString());
145 
147  QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const;
148 
150  void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant);
151 
153  int comparePropertyByIndex(CPropertyIndexRef index,
154  const IDatastoreObjectWithIntegerKey &compareValue) const;
155 
157  static bool canHandleIndex(swift::misc::CPropertyIndexRef index);
158 
159  int m_dbKey = -1;
160  };
161 
166  {
167  public:
170  {
171  IndexDbStringKey = CPropertyIndexRef::GlobalIndexIDatastoreString,
172  IndexDbKeyAsString,
173  IndexIsLoadedFromDb,
174  IndexDatabaseIcon,
175  IndexVersion,
176  IndexEndMarker
177  };
178 
180  const QString &getDbKey() const { return m_dbKey; }
181 
183  QString getDbKeyAsString() const { return getDbKey(); }
184 
186  QJsonValue getDbKeyAsJsonValue() const;
187 
189  QString getDbKeyAsStringInParentheses(const QString &prefix = {}) const;
190 
192  void setDbKey(const QString &key) { m_dbKey = key.trimmed().toUpper(); }
193 
195  bool hasValidDbKey() const { return !m_dbKey.isEmpty(); }
196 
198  bool isDbEqual(const IDatastoreObjectWithStringKey &other) const
199  {
200  return other.isLoadedFromDb() && this->isLoadedFromDb() && other.getDbKey() == this->getDbKey();
201  }
202 
204  bool isLoadedFromDb() const { return m_loadedFromDb; }
205 
207  void setLoadedFromDb(bool loaded) { m_loadedFromDb = loaded; }
208 
210  bool matchesDbKeyState(db::DbKeyStateFilter filter) const;
211 
213  const CIcon &toDatabaseIcon() const;
214 
216  static QString invalidDbKey() { return {}; }
217 
219  static const CPropertyIndex &keyIndex()
220  {
221  static const CPropertyIndex k(IndexDbStringKey);
222  return k;
223  }
224 
225  protected:
228 
230  IDatastoreObjectWithStringKey(const QString &key) : m_dbKey(key) {}
231 
233  void setKeyVersionTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix = QString());
234 
236  static bool existsKey(const QJsonObject &json, const QString &prefix = QString());
237 
239  QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const;
240 
242  void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant);
243 
245  int comparePropertyByIndex(swift::misc::CPropertyIndexRef index,
246  const IDatastoreObjectWithStringKey &compareValue) const;
247 
249  static bool canHandleIndex(swift::misc::CPropertyIndexRef index);
250 
251  QString m_dbKey;
252  bool m_loadedFromDb =
253  false;
254  };
255  } // namespace db
256 } // namespace swift::misc
257 
258 Q_DECLARE_METATYPE(swift::misc::db::DbKeyState)
259 Q_DECLARE_METATYPE(swift::misc::db::DbKeyStateFilter)
260 Q_DECLARE_OPERATORS_FOR_FLAGS(swift::misc::db::DbKeyStateFilter)
261 
262 #endif // SWIFT_MISC_DB_DATASTORE_H
Value object for icons. An icon is stored in the global icon repository and identified by its index....
Definition: icon.h:39
Non-owning reference to a CPropertyIndex with a subset of its features.
Entity with timestamp.
Supposed to be used only in filter operations.
Definition: datastore.h:41
QString m_version
version info
Definition: datastore.h:63
const QString & getVersion() const
Version info.
Definition: datastore.h:48
bool hasVersion() const
Having a version?s.
Definition: datastore.h:54
void setVersion(const QString &version)
Version info.
Definition: datastore.h:57
Class from which a derived class can inherit datastore-related functions.
Definition: datastore.h:70
bool isLoadedFromDb() const
Loaded from DB.
Definition: datastore.cpp:49
void setDbKey(int key)
Set the DB key.
Definition: datastore.h:96
bool isDbEqual(const IDatastoreObjectWithIntegerKey &other) const
Same DB key and hence equal.
Definition: datastore.h:105
IDatastoreObjectWithIntegerKey(int key)
Constructor.
Definition: datastore.h:138
static const CPropertyIndex & keyIndex()
The key index.
Definition: datastore.h:127
bool hasValidDbKey() const
Has valid DB key.
Definition: datastore.h:102
Class from which a derived class can inherit datastore-related functions.
Definition: datastore.h:166
QString getDbKeyAsString() const
DB key as string.
Definition: datastore.h:183
bool isLoadedFromDb() const
Loaded from DB.
Definition: datastore.h:204
void setLoadedFromDb(bool loaded)
Mark as loaded from DB.
Definition: datastore.h:207
IDatastoreObjectWithStringKey(const QString &key)
Constructor.
Definition: datastore.h:230
bool hasValidDbKey() const
Has valid DB key.
Definition: datastore.h:195
static QString invalidDbKey()
Invalid key.
Definition: datastore.h:216
bool isDbEqual(const IDatastoreObjectWithStringKey &other) const
Same DB key and hence equal.
Definition: datastore.h:198
const QString & getDbKey() const
Get DB key.
Definition: datastore.h:180
static const CPropertyIndex & keyIndex()
The key index.
Definition: datastore.h:219
void setDbKey(const QString &key)
Set the DB key.
Definition: datastore.h:192
DbKeyState
State of key (in DB, ...?)
Definition: datastore.h:27
Free functions in swift::misc.
#define SWIFT_MISC_EXPORT
Export a class or function from the library.