swift
Loading...
Searching...
No Matches
datastore.cpp
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
4#include "misc/db/datastore.h"
5
6#include <QDateTime>
7#include <QStringBuilder>
8#include <QtGlobal>
9
12#include "misc/icon.h"
13
14namespace swift::misc::db
15{
17
19 {
20 // we check 2 formats, the DB format and the backend object format
21 QString timestampString(json.value(prefix % u"lastupdated").toString());
22 if (timestampString.isEmpty())
23 {
24 timestampString = json.value(prefix % u"tsLastUpdated").toString();
25 }
26 const QDateTime ts(CDatastoreUtility::parseTimestamp(timestampString));
27 this->setUtcTimestamp(ts);
28
29 // version
30 this->setVersion(json.value(prefix % u"version").toString());
31 }
32
34 {
35 if (m_dbKey < 0)
36 {
37 return {};
38 }
40 }
41
43 {
44 if (m_dbKey < 0)
45 {
46 return {};
47 }
48 return prefix % u'(' % QString::number(m_dbKey) % u')';
49 }
50
52 {
53 bool ok {};
54 const int k = key.toInt(&ok);
55 m_dbKey = ok ? k : -1;
56 }
57
59
60 bool IDatastoreObjectWithIntegerKey::matchesDbKeyState(db::DbKeyStateFilter filter) const
61 {
62 if (filter == All)
63 {
64 return true;
65 }
66 const bool validKey = this->hasValidDbKey();
67 return (validKey && filter.testFlag(Valid)) || (!validKey && filter.testFlag(Invalid));
68 }
69
71 {
72 static const CIcon empty;
73 if (this->hasValidDbKey())
74 {
75 return CIcon::iconByIndex(CIcons::StandardIconDatabaseKey16);
76 }
77 return empty;
78 }
79
81 {
82 if (candidate.isEmpty())
83 {
84 return invalidDbKey();
85 }
86 bool ok {};
87 int k = candidate.toInt(&ok);
88 return ok ? k : invalidDbKey();
89 }
90
92 {
93 if (this->hasValidDbKey())
94 {
95 return { m_dbKey };
96 }
97 return {};
98 }
99
101 const QString &prefix)
102 {
103 // this function is performance sensitive, as it is called for all DB data
104 const int dbKey = json.value(prefix % u"id").toInt(-1);
105 this->setDbKey(dbKey);
107 }
108
110 {
111 const QJsonValue jv(json.value(prefix % u"id"));
112 return !(jv.isNull() || jv.isUndefined());
113 }
114
116 {
118 {
120 }
121 const auto i = index.frontCasted<ColumnIndex>();
122 switch (i)
123 {
124 case IndexDbIntegerKey: return QVariant::fromValue(m_dbKey);
125 case IndexDbKeyAsString: return QVariant::fromValue(this->getDbKeyAsString());
126 case IndexIsLoadedFromDb: return QVariant::fromValue(this->hasValidDbKey());
127 case IndexDatabaseIcon: return QVariant::fromValue(this->toDatabaseIcon());
128 case IndexVersion: return QVariant::fromValue(this->getVersion());
129 default: break;
130 }
131 return {};
132 }
133
135 {
137 {
139 return;
140 }
141 const auto i = index.frontCasted<ColumnIndex>();
142 switch (i)
143 {
144 case IndexDbIntegerKey: m_dbKey = variant.toInt(); break;
145 case IndexDbKeyAsString: m_dbKey = stringToDbKey(variant.toString()); break;
146 case IndexVersion: this->setVersion(variant.toString()); break;
147 default: break;
148 }
149 }
150
152 const IDatastoreObjectWithIntegerKey &compareValue) const
153 {
155 {
156 return ITimestampBased::comparePropertyByIndex(index, compareValue);
157 }
158 const auto i = index.frontCasted<ColumnIndex>();
159 switch (i)
160 {
161 case IndexDbKeyAsString: // fall thru
162 case IndexDbIntegerKey: return Compare::compare(m_dbKey, compareValue.getDbKey());
163 case IndexDatabaseIcon: return Compare::compare(this->hasValidDbKey(), compareValue.hasValidDbKey());
164 case IndexVersion: return this->getVersion().compare(compareValue.getVersion());
165 default: break;
166 }
167 Q_ASSERT_X(false, Q_FUNC_INFO, "Compare failed");
168 return 0;
169 }
170
172 {
174 {
175 return true;
176 }
177 const int i = index.frontCasted<int>();
178 return (i >= static_cast<int>(IndexDbIntegerKey)) && (i < static_cast<int>(IndexEndMarker));
179 }
180
182 {
183 if (this->hasValidDbKey())
184 {
185 return { m_dbKey };
186 }
187 static const QJsonValue null;
188 return null;
189 }
190
192 {
193 if (m_dbKey.isEmpty())
194 {
195 return {};
196 }
197 return prefix % '(' % m_dbKey % ')';
198 }
199
200 bool IDatastoreObjectWithStringKey::matchesDbKeyState(db::DbKeyStateFilter filter) const
201 {
202 if (filter == All)
203 {
204 return true;
205 }
206 return this->hasValidDbKey() && filter.testFlag(Valid);
207 }
208
210 {
211 static const CIcon empty;
212 if (this->hasValidDbKey())
213 {
214 return CIcon::iconByIndex(CIcons::StandardIconDatabaseKey16);
215 }
216 return empty;
217 }
218
220 const QString &prefix)
221 {
222 QString dbKey = json.value(prefix % u"id").toString();
223 this->setDbKey(dbKey);
225 }
226
228 {
229 const QJsonValue jv(json.value(prefix % u"id"));
230 return !(jv.isNull() || jv.isUndefined());
231 }
232
234 {
236 {
238 }
239 const auto i = index.frontCasted<ColumnIndex>();
240 switch (i)
241 {
242 case IndexDbKeyAsString: // fall thru
243 case IndexDbStringKey: return QVariant::fromValue(m_dbKey);
244 case IndexDatabaseIcon: return QVariant::fromValue(this->toDatabaseIcon());
245 case IndexIsLoadedFromDb: return QVariant::fromValue(m_loadedFromDb);
246 case IndexVersion: return QVariant::fromValue(this->getVersion());
247 default: break;
248 }
249 return {};
250 }
251
253 {
255 {
257 return;
258 }
259 const auto i = index.frontCasted<ColumnIndex>();
260 switch (i)
261 {
262 case IndexDbStringKey:
263 case IndexDbKeyAsString: m_dbKey = variant.value<QString>(); break;
264 case IndexIsLoadedFromDb: m_loadedFromDb = variant.toBool(); break;
265 case IndexVersion: this->setVersion(variant.toString()); break;
266 default: break;
267 }
268 }
269
271 const IDatastoreObjectWithStringKey &compareValue) const
272 {
274 {
275 return ITimestampBased::comparePropertyByIndex(index, compareValue);
276 }
277 const auto i = index.frontCasted<ColumnIndex>();
278 switch (i)
279 {
280 case IndexDbKeyAsString: // fall thru
281 case IndexDbStringKey: return m_dbKey.compare(compareValue.getDbKey());
282 case IndexDatabaseIcon: return Compare::compare(this->hasValidDbKey(), compareValue.hasValidDbKey());
283 case IndexVersion: return this->getVersion().compare(compareValue.getVersion());
284 default: break;
285 }
286 Q_ASSERT_X(false, Q_FUNC_INFO, "Compare failed");
287 return 0;
288 }
289
291 {
292 if (index.isEmpty())
293 {
294 return false;
295 }
297 {
298 return true;
299 }
300 const int i = index.frontCasted<int>();
301 return (i >= static_cast<int>(IndexDbStringKey)) && (i < static_cast<int>(IndexEndMarker));
302 }
303} // namespace swift::misc::db
Value object for icons. An icon is stored in the global icon repository and identified by its index....
Definition icon.h:39
static const CIcon & iconByIndex(CIcons::IconIndex index)
Icon for given index.
Definition icon.cpp:63
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
int comparePropertyByIndex(CPropertyIndexRef index, const ITimestampBased &compareValue) const
Compare for index.
static bool canHandleIndex(CPropertyIndexRef index)
Can given index be handled.
void setUtcTimestamp(const QDateTime &timestamp)
Set timestamp.
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
static QDateTime parseTimestamp(const QString &timestamp)
Parse a timestamp object.
const QString & getVersion() const
Version info.
Definition datastore.h:48
QVersionNumber getQVersion() const
Version as QVersion.
Definition datastore.cpp:16
void setTimestampVersionFromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
Set versionn and timestamp values.
Definition datastore.cpp:18
void setVersion(const QString &version)
Version info.
Definition datastore.h:57
static int stringToDbKey(const QString &candidate)
Convert string to DB key.
Definition datastore.cpp:80
const CIcon & toDatabaseIcon() const
Database icon if this has valid key, otherwise empty.
Definition datastore.cpp:70
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
QJsonValue getDbKeyAsJsonValue() const
Key as JSON value, or null.
Definition datastore.cpp:91
QString getDbKeyAsString() const
DB key as string.
Definition datastore.cpp:33
bool isLoadedFromDb() const
Loaded from DB.
Definition datastore.cpp:58
static bool existsKey(const QJsonObject &json, const QString &prefix=QString())
Is a key available?
static bool canHandleIndex(swift::misc::CPropertyIndexRef index)
Can given index be handled?
void setDbKey(int key)
Set the DB key.
Definition datastore.h:96
QString getDbKeyAsStringInParentheses(const QString &prefix={}) const
Db key in parentheses, e.g. "(3)".
Definition datastore.cpp:42
int comparePropertyByIndex(CPropertyIndexRef index, const IDatastoreObjectWithIntegerKey &compareValue) const
Compare for index.
void setKeyVersionTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
Set key and timestamp values.
bool matchesDbKeyState(db::DbKeyStateFilter filter) const
Matches filter?
Definition datastore.cpp:60
bool hasValidDbKey() const
Has valid DB key.
Definition datastore.h:102
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
bool m_loadedFromDb
as we have no artificial key, it can happen key is set, but not loaded from DB
Definition datastore.h:252
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
static bool existsKey(const QJsonObject &json, const QString &prefix=QString())
Is a key available?
bool hasValidDbKey() const
Has valid DB key.
Definition datastore.h:195
const CIcon & toDatabaseIcon() const
Database icon if this has valid key, otherwise empty.
static bool canHandleIndex(swift::misc::CPropertyIndexRef index)
Can given index be handled.
const QString & getDbKey() const
Get DB key.
Definition datastore.h:180
QJsonValue getDbKeyAsJsonValue() const
Key as JSON value, or null.
QString getDbKeyAsStringInParentheses(const QString &prefix={}) const
Db key in parentheses, e.g. "(3)".
void setKeyVersionTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix=QString())
Set key and timestamp values.
int comparePropertyByIndex(swift::misc::CPropertyIndexRef index, const IDatastoreObjectWithStringKey &compareValue) const
Compare for index.
bool matchesDbKeyState(db::DbKeyStateFilter filter) const
Matches filter?
void setDbKey(const QString &key)
Set the DB key.
Definition datastore.h:192
int compare(T a, T b)
Compare arithmetic values.
QJsonValue value(QLatin1StringView key) const const
bool isNull() const const
bool isUndefined() const const
int toInt(int defaultValue) const const
QString toString() const const
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
bool isEmpty() const const
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) const const
QVariant fromValue(T &&value)
bool toBool() const const
int toInt(bool *ok) const const
QString toString() const const
T value() const &const
QVersionNumber fromString(QAnyStringView string, qsizetype *suffixIndex)