Ref T473, added version attribute for DB based value objects.

So all DB objects could have version.
This commit is contained in:
Klaus Basan
2018-12-16 21:11:57 +01:00
committed by Mat Sutcliffe
parent 9facd200f1
commit a40e37e59b
4 changed files with 74 additions and 38 deletions

View File

@@ -20,6 +20,18 @@ namespace BlackMisc
{
namespace Db
{
void IDatastoreObject::setTimestampAndVersionFromDatabaseJson(const QJsonObject &json, const QString &prefix)
{
// we check 2 formats, the DB format and the backend object format
QString timestampString(json.value(prefix % u"lastupdated").toString());
if (timestampString.isEmpty()) { timestampString = json.value(prefix % u"tsLastUpdated").toString(); }
const QDateTime ts(CDatastoreUtility::parseTimestamp(timestampString));
this->setUtcTimestamp(ts);
// version
this->setVersion(json.value(prefix % u"version").toString());
}
QString IDatastoreObjectWithIntegerKey::getDbKeyAsString() const
{
if (m_dbKey < 0) { return {}; }
@@ -35,9 +47,8 @@ namespace BlackMisc
void IDatastoreObjectWithIntegerKey::setDbKey(const QString &key)
{
bool ok;
int k = key.toInt(&ok);
if (!ok) { k = -1; }
m_dbKey = k;
const int k = key.toInt(&ok);
m_dbKey = ok ? k : -1;
}
bool IDatastoreObjectWithIntegerKey::isLoadedFromDb() const
@@ -78,12 +89,7 @@ namespace BlackMisc
// this function is performance sensitive, as it is called for all DB data
const int dbKey = json.value(prefix % u"id").toInt(-1);
this->setDbKey(dbKey);
// we check 2 formats, the DB format and the backend object format
QString timestampString(json.value(prefix % u"lastupdated").toString());
if (timestampString.isEmpty()) { timestampString = json.value(prefix % u"tsLastUpdated").toString(); }
const QDateTime ts(CDatastoreUtility::parseTimestamp(timestampString));
this->setUtcTimestamp(ts);
IDatastoreObject::setTimestampAndVersionFromDatabaseJson(json, prefix);
}
bool IDatastoreObjectWithIntegerKey::existsKey(const QJsonObject &json, const QString &prefix)
@@ -98,10 +104,11 @@ namespace BlackMisc
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexDbIntegerKey: return CVariant::from(m_dbKey);
case IndexDbKeyAsString: return CVariant::from(this->getDbKeyAsString());
case IndexDbIntegerKey: return CVariant::from(m_dbKey);
case IndexDbKeyAsString: return CVariant::from(this->getDbKeyAsString());
case IndexIsLoadedFromDb: return CVariant::from(this->hasValidDbKey());
case IndexDatabaseIcon: return CVariant::from(this->toDatabaseIcon());
case IndexDatabaseIcon: return CVariant::from(this->toDatabaseIcon());
case IndexVersion: return CVariant::from(this->getVersion());
default: break;
}
return CVariant();
@@ -113,8 +120,9 @@ namespace BlackMisc
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexDbIntegerKey: m_dbKey = variant.toInt(); break;
case IndexDbIntegerKey: m_dbKey = variant.toInt(); break;
case IndexDbKeyAsString: m_dbKey = stringToDbKey(variant.toQString()); break;
case IndexVersion: this->setVersion(variant.toQString()); break;
default: break;
}
}
@@ -128,6 +136,7 @@ namespace BlackMisc
case IndexDbKeyAsString: // fall thru
case IndexDbIntegerKey: return Compare::compare(m_dbKey, compareValue.getDbKey());
case IndexDatabaseIcon: return Compare::compare(this->hasValidDbKey(), compareValue.hasValidDbKey());
case IndexVersion: return this->getVersion().compare(compareValue.getVersion());
default: break;
}
Q_ASSERT_X(false, Q_FUNC_INFO, "Compare failed");
@@ -138,7 +147,7 @@ namespace BlackMisc
{
if (ITimestampBased::canHandleIndex(index)) { return true; }
const int i = index.frontCasted<int>();
return (i >= static_cast<int>(IndexDbIntegerKey)) && (i <= static_cast<int>(IndexDatabaseIcon));
return (i >= static_cast<int>(IndexDbIntegerKey)) && (i < static_cast<int>(IndexEndMarker));
}
QJsonValue IDatastoreObjectWithStringKey::getDbKeyAsJsonValue() const
@@ -170,9 +179,8 @@ namespace BlackMisc
void IDatastoreObjectWithStringKey::setKeyAndTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix)
{
QString dbKey = json.value(prefix % u"id").toString();
QDateTime ts(CDatastoreUtility::parseTimestamp(json.value(prefix % u"lastupdated").toString()));
this->setDbKey(dbKey);
this->setUtcTimestamp(ts);
IDatastoreObject::setTimestampAndVersionFromDatabaseJson(json, prefix);
}
bool IDatastoreObjectWithStringKey::existsKey(const QJsonObject &json, const QString &prefix)
@@ -188,11 +196,11 @@ namespace BlackMisc
switch (i)
{
case IndexDbKeyAsString: // fall thru
case IndexDbStringKey: return CVariant::from(m_dbKey);
case IndexDatabaseIcon: return CVariant::from(this->toDatabaseIcon());
case IndexDbStringKey: return CVariant::from(m_dbKey);
case IndexDatabaseIcon: return CVariant::from(this->toDatabaseIcon());
case IndexIsLoadedFromDb: return CVariant::from(m_loadedFromDb);
default:
break;
case IndexVersion: return CVariant::from(this->getVersion());
default: break;
}
return CVariant();
}
@@ -207,9 +215,8 @@ namespace BlackMisc
case IndexDbKeyAsString:
m_dbKey = variant.value<QString>();
break;
case IndexIsLoadedFromDb:
m_loadedFromDb = variant.toBool();
break;
case IndexIsLoadedFromDb: m_loadedFromDb = variant.toBool(); break;
case IndexVersion: this->setVersion(variant.toQString()); break;
default:
break;
}
@@ -224,8 +231,8 @@ namespace BlackMisc
case IndexDbKeyAsString: // fall thru
case IndexDbStringKey: return m_dbKey.compare(compareValue.getDbKey());
case IndexDatabaseIcon: return Compare::compare(this->hasValidDbKey(), compareValue.hasValidDbKey());
default:
break;
case IndexVersion: return this->getVersion().compare(compareValue.getVersion());
default: break;
}
Q_ASSERT_X(false, Q_FUNC_INFO, "Compare failed");
return 0;
@@ -236,7 +243,7 @@ namespace BlackMisc
if (index.isEmpty()) { return false; }
if (ITimestampBased::canHandleIndex(index)) { return true;}
const int i = index.frontCasted<int>();
return (i >= static_cast<int>(IndexDbStringKey)) && (i <= static_cast<int>(IndexDatabaseIcon));
return (i >= static_cast<int>(IndexDbStringKey)) && (i < static_cast<int>(IndexEndMarker));
}
}
} // namespace

View File

@@ -12,10 +12,11 @@
#ifndef BLACKMISC_DB_DATASTORE_H
#define BLACKMISC_DB_DATASTORE_H
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/timestampbased.h"
#include "blackmisc/variant.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/blackmiscexport.h"
#include "blackconfig/buildconfig.h"
#include <QJsonObject>
#include <QJsonValue>
@@ -42,7 +43,30 @@ namespace BlackMisc
/*!
* Class from which a derived class can inherit datastore-related functions.
*/
class BLACKMISC_EXPORT IDatastoreObjectWithIntegerKey : public ITimestampBased
class BLACKMISC_EXPORT IDatastoreObject : public ITimestampBased
{
// since we use different keys all the compares, set, get are in the derived class
// in general we can say, it was a bad decisio to use different key types
// IndexDbIntegerKey = CPropertyIndex::GlobalIndexIDatastore for future usage
public:
//! Version info
const QString &getVersion() const { return m_version; }
//! Version info
void setVersion(const QString &version) { m_version = version; }
protected:
//! Set versionn and timestamp values
void setTimestampAndVersionFromDatabaseJson(const QJsonObject &json, const QString &prefix = QString());
QString m_version; //!< version info
};
/*!
* Class from which a derived class can inherit datastore-related functions.
*/
class BLACKMISC_EXPORT IDatastoreObjectWithIntegerKey : public IDatastoreObject
{
public:
//! Property index
@@ -51,7 +75,9 @@ namespace BlackMisc
IndexDbIntegerKey = CPropertyIndex::GlobalIndexIDatastoreInteger,
IndexDbKeyAsString,
IndexIsLoadedFromDb,
IndexDatabaseIcon
IndexDatabaseIcon,
IndexVersion,
IndexEndMarker //!< keep as last element
};
//! Get DB key.
@@ -113,7 +139,7 @@ namespace BlackMisc
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant);
//! Compare by index
//! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex
int comparePropertyByIndex(const CPropertyIndex &index, const IDatastoreObjectWithIntegerKey &compareValue) const;
//! Can given index be handled?
@@ -125,7 +151,7 @@ namespace BlackMisc
/*!
* Class from which a derived class can inherit datastore-related functions.
*/
class BLACKMISC_EXPORT IDatastoreObjectWithStringKey : public ITimestampBased
class BLACKMISC_EXPORT IDatastoreObjectWithStringKey : public IDatastoreObject
{
public:
//! Property index
@@ -134,7 +160,9 @@ namespace BlackMisc
IndexDbStringKey = CPropertyIndex::GlobalIndexIDatastoreString,
IndexDbKeyAsString,
IndexIsLoadedFromDb,
IndexDatabaseIcon
IndexDatabaseIcon,
IndexVersion,
IndexEndMarker //!< keep as last element
};
//! Get DB key.
@@ -192,7 +220,7 @@ namespace BlackMisc
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant);
//! Compare by index
//! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex
int comparePropertyByIndex(const BlackMisc::CPropertyIndex &index, const IDatastoreObjectWithStringKey &compareValue) const;
//! Can given index be handled

View File

@@ -148,9 +148,10 @@ namespace BlackMisc
GlobalIndexCSettingKeyboardHotkey = 11000,
GlobalIndexCKeyboardKey = 11100,
GlobalIndexCJoystickButton = 11200,
GlobalIndexIDatastoreInteger = 12000,
GlobalIndexIDatastoreString = 12100,
GlobalIndexCDbInfo = 12200,
GlobalIndexIDatastore = 12000,
GlobalIndexIDatastoreInteger = 12100,
GlobalIndexIDatastoreString = 12200,
GlobalIndexCDbInfo = 12300,
GlobalIndexCGlobalSetup = 13000,
GlobalIndexCArtifact = 13100,
GlobalIndexCDistribution = 13200,

View File

@@ -152,7 +152,7 @@ namespace BlackMisc
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant);
//! Compare for index
//! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex
int comparePropertyByIndex(const CPropertyIndex &index, const ITimestampBased &compareValue) const;
//! Update missing parts