diff --git a/src/blackmisc/db/dbinfo.cpp b/src/blackmisc/db/dbinfo.cpp new file mode 100644 index 000000000..0a128ca8e --- /dev/null +++ b/src/blackmisc/db/dbinfo.cpp @@ -0,0 +1,145 @@ +/* Copyright (C) 2016 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "dbinfo.h" +#include "blackmisc/comparefunctions.h" +#include + +using namespace BlackMisc::Network; + +namespace BlackMisc +{ + namespace Db + { + CDbInfo::CDbInfo(int key, const QString &tableName, int entries) : + IDatastoreObjectWithIntegerKey(key), + m_tableName(tableName.trimmed().toLower()), m_entries(entries) + { + this->setEntity(this->getEntity()); + Q_ASSERT_X(tableName.isEmpty() || this->m_entity != CEntityFlags::NoEntity, Q_FUNC_INFO, "Wrong entity"); + } + + bool CDbInfo::isValid() const + { + return this->m_entity != CEntityFlags::NoEntity && !this->m_tableName.isEmpty(); + } + + CEntityFlags::Entity CDbInfo::getEntity() const + { + if (this->m_entity != CEntityFlags::NoEntity) { return this->m_entity; } + if (this->m_tableName.isEmpty()) { return CEntityFlags::NoEntity; } + if (this->m_tableName.contains("airlineicao", Qt::CaseInsensitive)) { return CEntityFlags::AirlineIcaoEntity; } + if (this->m_tableName.contains("aircrafticao", Qt::CaseInsensitive)) { return CEntityFlags::AircraftIcaoEntity; } + if (this->m_tableName.contains("livery", Qt::CaseInsensitive)) { return CEntityFlags::LiveryEntity; } + if (this->m_tableName.contains("aircraftmodel", Qt::CaseInsensitive)) { return CEntityFlags::ModelEntity; } + if (this->m_tableName.contains("country", Qt::CaseInsensitive)) { return CEntityFlags::CountryEntity; } + return CEntityFlags::NoEntity; + } + + void CDbInfo::setEntity(CEntityFlags::Entity entity) + { + this->m_entity = entity; + } + + bool CDbInfo::matchesEntity(CEntityFlags::Entity entity) const + { + return entity.testFlag(CEntityFlags::entityToEntityFlag(this->getEntity())); + } + + void CDbInfo::setTableName(const QString &tableName) + { + m_tableName = tableName.trimmed().toLower(); + this->m_entity = this->getEntity(); + } + + QString CDbInfo::convertToQString(bool i18n) const + { + Q_UNUSED(i18n); + QString s("Table %1 with entries %1"); + return s.arg(this->m_tableName).arg(this->m_entries); + } + + CVariant CDbInfo::propertyByIndex(const BlackMisc::CPropertyIndex &index) const + { + if (index.isMyself()) { return CVariant::from(*this); } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexTableName: + return CVariant::fromValue(m_tableName); + case IndexEntries: + return CVariant::fromValue(m_entries); + case IndexEntity: + return CVariant::fromValue(m_entity); + default: + return (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) ? + IDatastoreObjectWithIntegerKey::propertyByIndex(index) : + CValueObject::propertyByIndex(index); + } + } + + void CDbInfo::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) + { + if (index.isMyself()) { (*this) = variant.to(); return; } + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexTableName: + this->setTableName(variant.toQString()); + break; + case IndexEntries: + this->setTableName(variant.toQString()); + break; + case IndexEntity: + this->setEntity(static_cast(variant.toInt())); + break; + default: + return (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) ? + IDatastoreObjectWithIntegerKey::setPropertyByIndex(index, variant) : + CValueObject::setPropertyByIndex(index, variant); + break; + } + } + + int CDbInfo::comparePropertyByIndex(const CPropertyIndex &index, const CDbInfo &compareValue) const + { + if (index.isMyself()) { return getTableName().compare(compareValue.getTableName(), Qt::CaseInsensitive); } + if (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) { return IDatastoreObjectWithIntegerKey::comparePropertyByIndex(index, compareValue);} + ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexTableName: + return getTableName().compare(compareValue.getTableName(), Qt::CaseInsensitive); + case IndexEntries: + return Compare::compare(this->getEntries(), compareValue.getEntries()); + case IndexEntity: + return Compare::compare(this->getEntity(), compareValue.getEntity()); + default: + Q_ASSERT_X(false, Q_FUNC_INFO, "No comparison possible"); + } + return 0; + } + + CDbInfo CDbInfo::fromDatabaseJson(const QJsonObject &json, const QString &prefix) + { + if (!existsKey(json, prefix)) + { + // when using relationship, this can be null + return CDbInfo(); + } + const int id(json.value(prefix + "id").toInt()); + const int entries(json.value(prefix + "entries").toInt()); + const QString tableName(json.value(prefix + "tablename").toString()); + CDbInfo dbInfo(id, tableName, entries); + dbInfo.setKeyAndTimestampFromDatabaseJson(json, prefix); + return dbInfo; + } + } // namespace +} // namespace + diff --git a/src/blackmisc/db/dbinfo.h b/src/blackmisc/db/dbinfo.h new file mode 100644 index 000000000..7bc8027fd --- /dev/null +++ b/src/blackmisc/db/dbinfo.h @@ -0,0 +1,104 @@ +/* Copyright (C) 2016 + * Swift Project Community / Contributors + * + * This file is part of Swift Project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_DB_DBINFO_H +#define BLACKMISC_DB_DBINFO_H + +#include "blackmisc/db/datastore.h" +#include "blackmisc/network/entityflags.h" +#include "blackmisc/valueobject.h" +#include "blackmisc/blackmiscexport.h" +#include + +namespace BlackMisc +{ + namespace Db + { + /*! + * Info about the latest models + */ + class BLACKMISC_EXPORT CDbInfo : + public CValueObject, + public IDatastoreObjectWithIntegerKey + { + public: + //! Properties by index + enum ColumnIndex + { + IndexTableName = BlackMisc::CPropertyIndex::GlobalIndexCDbInfo, + IndexEntries, + IndexEntity + }; + + //! Constructor + CDbInfo() = default; + + //! Constructor + CDbInfo(int key, const QString &tableName, int entries); + + //! Valid? + bool isValid() const; + + //! Table name + const QString &getTableName() const { return m_tableName; } + + //! Set table name + void setTableName(const QString &tableName); + + //! Get entity (based on table name + Network::CEntityFlags::Entity getEntity() const; + + //! Set entity, should be in sync with a corresponding table name + void setEntity(Network::CEntityFlags::Entity entity); + + //! Entry count + int getEntries() const { return m_entries; } + + //! Matches given entity + bool matchesEntity(Network::CEntityFlags::Entity entity) const; + + //! Set entries + void setEntries(int entries) { m_entries = entries; } + + //! \copydoc BlackMisc::Mixin::String::toQString + QString convertToQString(bool i18n = false) const; + + //! \copydoc BlackMisc::Mixin::Index::propertyByIndex + CVariant propertyByIndex(const CPropertyIndex &index) const; + + //! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex + void setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant); + + //! Compare by index + int comparePropertyByIndex(const CPropertyIndex &index, const CDbInfo &compareValue) const; + + //! From our database JSON format + static CDbInfo fromDatabaseJson(const QJsonObject &json, const QString &prefix = QString()); + + private: + QString m_tableName; //!< table name + int m_entries; //!< number of entries + BlackMisc::Network::CEntityFlags::Entity m_entity = BlackMisc::Network::CEntityFlags::NoEntity; //!< lazy initialized entity + + BLACK_METACLASS( + CDbInfo, + BLACK_METAMEMBER(dbKey), + BLACK_METAMEMBER(timestampMSecsSinceEpoch), + BLACK_METAMEMBER(tableName), + BLACK_METAMEMBER(entries) + ); + }; + } // namespace +} // namespace + +Q_DECLARE_METATYPE(BlackMisc::Db::CDbInfo) + +#endif // guard diff --git a/src/blackmisc/db/dbinfolist.cpp b/src/blackmisc/db/dbinfolist.cpp new file mode 100644 index 000000000..589024b8f --- /dev/null +++ b/src/blackmisc/db/dbinfolist.cpp @@ -0,0 +1,42 @@ +/* Copyright (C) 2016 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "dbinfolist.h" + +using namespace BlackMisc::Network; + +namespace BlackMisc +{ + namespace Db + { + CDbInfoList::CDbInfoList(const CSequence &other) : + CSequence(other) + { } + + CDbInfo CDbInfoList::findFirstByEntityOrDefault(CEntityFlags::Entity entity) const + { + for (const CDbInfo &info : *this) + { + if (info.matchesEntity(entity)) { return info; } + } + return CDbInfo(); + } + + CDbInfoList CDbInfoList::fromDatabaseJson(const QJsonArray &array) + { + CDbInfoList infoObjects; + for (const QJsonValue &value : array) + { + const CDbInfo info(CDbInfo::fromDatabaseJson(value.toObject())); + infoObjects.push_back(info); + } + return infoObjects; + } + } // namespace +} // namespace diff --git a/src/blackmisc/db/dbinfolist.h b/src/blackmisc/db/dbinfolist.h new file mode 100644 index 000000000..5cf618ed9 --- /dev/null +++ b/src/blackmisc/db/dbinfolist.h @@ -0,0 +1,56 @@ +/* Copyright (C) 2016 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_DB_DBINFOYLIST_H +#define BLACKMISC_DB_DBINFOYLIST_H + +#include "dbinfo.h" +#include "blackmisc/db/datastoreobjectlist.h" +#include "blackmisc/network/entityflags.h" +#include "blackmisc/collection.h" +#include "blackmisc/sequence.h" +#include "blackmisc/blackmiscexport.h" + +#include + +namespace BlackMisc +{ + namespace Db + { + //! Value object encapsulating a list of info objects. + class BLACKMISC_EXPORT CDbInfoList : + public CSequence, + public BlackMisc::Db::IDatastoreObjectList, + public BlackMisc::Mixin::MetaType + { + public: + BLACKMISC_DECLARE_USING_MIXIN_METATYPE(CDbInfoList) + + //! Default constructor. + CDbInfoList() = default; + + //! Construct from a base class object. + CDbInfoList(const CSequence &other); + + //! Find by entity + CDbInfo findFirstByEntityOrDefault(BlackMisc::Network::CEntityFlags::Entity entity) const; + + //! From our database JSON format + static CDbInfoList fromDatabaseJson(const QJsonArray &array); + }; + } //namespace +} //namespace + +Q_DECLARE_METATYPE(BlackMisc::Db::CDbInfoList) +Q_DECLARE_METATYPE(BlackMisc::CCollection) +Q_DECLARE_METATYPE(BlackMisc::CSequence) + +#endif //guard