From dfb6b05e9da976b5fe2ae7d50c788346f44c1216 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 17 Jul 2017 00:17:25 +0200 Subject: [PATCH] Ref T109, datastore object and list * unified properties in int/string DB objects * fixed typo dbKeysAsString --- src/blackcore/db/airportdatareader.cpp | 2 +- src/blackcore/db/icaodatareader.cpp | 4 ++-- src/blackmisc/db/datastore.cpp | 22 +++++++++++++++---- src/blackmisc/db/datastore.h | 11 +++++++++- src/blackmisc/db/datastoreobjectlist.cpp | 28 +++++++++++++++++++----- src/blackmisc/db/datastoreobjectlist.h | 7 ++++-- 6 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/blackcore/db/airportdatareader.cpp b/src/blackcore/db/airportdatareader.cpp index 85c563112..e80652415 100644 --- a/src/blackcore/db/airportdatareader.cpp +++ b/src/blackcore/db/airportdatareader.cpp @@ -175,7 +175,7 @@ namespace BlackCore if (!inconsistent.isEmpty()) { logInconsistentData( - CStatusMessage(this, CStatusMessage::SeverityInfo, "Inconsistent airports: " + inconsistent.dbKeysAsStrings(", ")), + CStatusMessage(this, CStatusMessage::SeverityInfo, "Inconsistent airports: " + inconsistent.dbKeysAsString(", ")), Q_FUNC_INFO); } diff --git a/src/blackcore/db/icaodatareader.cpp b/src/blackcore/db/icaodatareader.cpp index c8d7524e1..23900fb85 100644 --- a/src/blackcore/db/icaodatareader.cpp +++ b/src/blackcore/db/icaodatareader.cpp @@ -257,7 +257,7 @@ namespace BlackCore if (!inconsistent.isEmpty()) { logInconsistentData( - CStatusMessage(this, CStatusMessage::SeverityInfo, "Inconsistent aircraft codes: " + inconsistent.dbKeysAsStrings(", ")), + CStatusMessage(this, CStatusMessage::SeverityInfo, "Inconsistent aircraft codes: " + inconsistent.dbKeysAsString(", ")), Q_FUNC_INFO); } @@ -307,7 +307,7 @@ namespace BlackCore if (!inconsistent.isEmpty()) { logInconsistentData( - CStatusMessage(this, CStatusMessage::SeverityInfo, "Inconsistent airline codes: " + inconsistent.dbKeysAsStrings(", ")), + CStatusMessage(this, CStatusMessage::SeverityInfo, "Inconsistent airline codes: " + inconsistent.dbKeysAsString(", ")), Q_FUNC_INFO); } diff --git a/src/blackmisc/db/datastore.cpp b/src/blackmisc/db/datastore.cpp index 6ace72931..9bcefd5bf 100644 --- a/src/blackmisc/db/datastore.cpp +++ b/src/blackmisc/db/datastore.cpp @@ -93,10 +93,12 @@ namespace BlackMisc CVariant IDatastoreObjectWithIntegerKey::propertyByIndex(const CPropertyIndex &index) const { if (ITimestampBased::canHandleIndex(index)) { return ITimestampBased::propertyByIndex(index); } - ColumnIndex i = index.frontCasted(); + const ColumnIndex i = index.frontCasted(); switch (i) { case IndexDbIntegerKey: return CVariant::from(this->m_dbKey); + case IndexDbKeyAsString: return CVariant::from(this->getDbKeyAsString()); + case IndexIsLoadedFromDb: return CVariant::from(this->hasValidDbKey()); case IndexDatabaseIcon: return CVariant::from(this->toDatabaseIcon()); default: break; } @@ -106,12 +108,14 @@ namespace BlackMisc void IDatastoreObjectWithIntegerKey::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) { if (ITimestampBased::canHandleIndex(index)) { ITimestampBased::setPropertyByIndex(index, variant); return; } - ColumnIndex i = index.frontCasted(); + const ColumnIndex i = index.frontCasted(); switch (i) { case IndexDbIntegerKey: this->m_dbKey = variant.toInt(); break; + case IndexDbKeyAsString: + this->m_dbKey = stringToDbKey(variant.toQString()); default: break; } @@ -123,6 +127,7 @@ namespace BlackMisc const ColumnIndex i = index.frontCasted(); switch (i) { + case IndexDbKeyAsString: // fall thru case IndexDbIntegerKey: return Compare::compare(this->m_dbKey, compareValue.getDbKey()); case IndexDatabaseIcon: return Compare::compare(this->hasValidDbKey(), compareValue.hasValidDbKey()); default: break; @@ -134,7 +139,7 @@ namespace BlackMisc bool IDatastoreObjectWithIntegerKey::canHandleIndex(const BlackMisc::CPropertyIndex &index) { if (ITimestampBased::canHandleIndex(index)) { return true;} - int i = index.frontCasted(); + const int i = index.frontCasted(); return (i >= static_cast(IndexDbIntegerKey)) && (i <= static_cast(IndexDatabaseIcon)); } @@ -145,6 +150,12 @@ namespace BlackMisc return null; } + QString IDatastoreObjectWithStringKey::getDbKeyAsStringInParentheses(const QString &prefix) const + { + if (this->m_dbKey.isEmpty()) { return ""; } + return prefix + "(" + m_dbKey + ")"; + } + bool IDatastoreObjectWithStringKey::matchesDbKeyState(Db::DbKeyStateFilter filter) const { if (filter == All) { return true; } @@ -178,6 +189,7 @@ namespace BlackMisc const ColumnIndex i = index.frontCasted(); switch (i) { + case IndexDbKeyAsString: // fall thru case IndexDbStringKey: return CVariant::from(this->m_dbKey); case IndexDatabaseIcon: return CVariant::from(this->toDatabaseIcon()); case IndexIsLoadedFromDb: return CVariant::from(this->m_loadedFromDb); @@ -194,6 +206,7 @@ namespace BlackMisc switch (i) { case IndexDbStringKey: + case IndexDbKeyAsString: this->m_dbKey = variant.value(); break; case IndexIsLoadedFromDb: @@ -210,6 +223,7 @@ namespace BlackMisc const ColumnIndex i = index.frontCasted(); switch (i) { + case IndexDbKeyAsString: // fall thru case IndexDbStringKey: return this->m_dbKey.compare(compareValue.getDbKey()); case IndexDatabaseIcon: return Compare::compare(this->hasValidDbKey(), compareValue.hasValidDbKey()); default: @@ -223,7 +237,7 @@ namespace BlackMisc { if (index.isEmpty()) { return false; } if (ITimestampBased::canHandleIndex(index)) { return true;} - int i = index.frontCasted(); + const int i = index.frontCasted(); return (i >= static_cast(IndexDbStringKey)) && (i <= static_cast(IndexDatabaseIcon)); } } diff --git a/src/blackmisc/db/datastore.h b/src/blackmisc/db/datastore.h index e85a4c865..1fa962fa7 100644 --- a/src/blackmisc/db/datastore.h +++ b/src/blackmisc/db/datastore.h @@ -49,6 +49,8 @@ namespace BlackMisc enum ColumnIndex { IndexDbIntegerKey = CPropertyIndex::GlobalIndexIDatastoreInteger, + IndexDbKeyAsString, + IndexIsLoadedFromDb, IndexDatabaseIcon }; @@ -61,7 +63,7 @@ namespace BlackMisc //! Key as JSON value, or null QJsonValue getDbKeyAsJsonValue() const; - //! Db ley in parentheses, e.g. "(3)" + //! Db key in parentheses, e.g. "(3)" QString getDbKeyAsStringInParentheses(const QString &prefix = {}) const; //! Set the DB key @@ -130,6 +132,7 @@ namespace BlackMisc enum ColumnIndex { IndexDbStringKey = CPropertyIndex::GlobalIndexIDatastoreString, + IndexDbKeyAsString, IndexIsLoadedFromDb, IndexDatabaseIcon }; @@ -137,9 +140,15 @@ namespace BlackMisc //! Get DB key. const QString &getDbKey() const { return m_dbKey; } + //! DB key as string + QString getDbKeyAsString() const { return getDbKey(); } + //! Key as JSON value, or null QJsonValue getDbKeyAsJsonValue() const; + //! Db key in parentheses, e.g. "(3)" + QString getDbKeyAsStringInParentheses(const QString &prefix = {}) const; + //! Set the DB key void setDbKey(const QString &key) { m_dbKey = key.trimmed().toUpper(); } diff --git a/src/blackmisc/db/datastoreobjectlist.cpp b/src/blackmisc/db/datastoreobjectlist.cpp index 93e6b4641..30180aa59 100644 --- a/src/blackmisc/db/datastoreobjectlist.cpp +++ b/src/blackmisc/db/datastoreobjectlist.cpp @@ -80,15 +80,33 @@ namespace BlackMisc } template - QString IDatastoreObjectList::dbKeysAsStrings(const QString &separator) const + QSet IDatastoreObjectList::toDbKeyStringSet() const + { + QSet keys; + for (const OBJ &obj : ITimestampObjectList::container()) + { + if (!obj.hasValidDbKey()) { continue; } + keys.insert(obj.getDbKeyAsString()); + } + return keys; + } + + template + QString IDatastoreObjectList::dbKeysAsString(const QString &separator) const { if (ITimestampObjectList::container().isEmpty()) { return ""; } - const QSet keys = IDatastoreObjectList::toDbKeySet(); + const QSet keys = IDatastoreObjectList::toDbKeyStringSet(); QString s; - for (const KEYTYPE &k : keys) + for (const QString &k : keys) { - if (!s.isEmpty()) { s += separator; } - s = s.append(k); // append works with string and int + if (s.isEmpty()) + { + s += k; + } + else + { + s += separator + k; + } } return s; } diff --git a/src/blackmisc/db/datastoreobjectlist.h b/src/blackmisc/db/datastoreobjectlist.h index bf5327970..bc22f8566 100644 --- a/src/blackmisc/db/datastoreobjectlist.h +++ b/src/blackmisc/db/datastoreobjectlist.h @@ -35,11 +35,14 @@ namespace BlackMisc //! Sort by timestamp void sortByKey(); - //! All keys as list + //! All keys as set QSet toDbKeySet() const; + //! All keys as string set (also int keys will be converted to string) + QSet toDbKeyStringSet() const; + //! The DB keys as string - QString dbKeysAsStrings(const QString &separator) const; + QString dbKeysAsString(const QString &separator) const; //! Max.key value (making sense with integer key) KEYTYPE getMaxKey(bool *ok = nullptr) const;