From 4ee1ab448e7717806652ff347feb715c4e2202b4 Mon Sep 17 00:00:00 2001 From: silverqx Date: Thu, 4 Aug 2022 11:53:44 +0200 Subject: [PATCH] revisited BasePivot class --- include/orm/tiny/relations/basepivot.hpp | 43 ++++++++++++++---------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/include/orm/tiny/relations/basepivot.hpp b/include/orm/tiny/relations/basepivot.hpp index 9531eab53..633b63721 100644 --- a/include/orm/tiny/relations/basepivot.hpp +++ b/include/orm/tiny/relations/basepivot.hpp @@ -18,16 +18,17 @@ namespace Orm::Tiny::Relations /*! Base class for Pivot models. */ template - class BasePivot : public Model, public IsPivotModel + class BasePivot : public Model, + public IsPivotModel { + friend Model; + /*! Alias for the string utils. */ using StringUtils = Orm::Tiny::Utils::String; /*! Alias for the type utils. */ using TypeUtils = Orm::Utils::Type; public: - friend Model; - /*! Inherit constructors. */ using Model::Model; @@ -70,9 +71,9 @@ namespace Orm::Tiny::Relations /*! Get the table associated with the model. */ QString getTable() const; /*! Get the foreign key column name. */ - QString getForeignKey() const; + const QString &getForeignKey() const noexcept; /*! Get the "related key" column name. */ - QString getRelatedKey() const; + const QString &getRelatedKey() const noexcept; // TODO fuckup, timestamps in pivot, I will solve it when I will have to use timestamps in the code, anyway may be I will not need it, because I can pass to the method right away what I will need silverqx // TODO also don't forget unsetRelations() if pivotParent will be implemented silverqx @@ -225,16 +226,24 @@ namespace Orm::Tiny::Relations TinyBuilder & BasePivot::setKeysForSelectQuery(TinyBuilder &query) { - // TODO now isset also check for NULL, so I have to check for QVariant::isNull/isValid too silverqx - if (this->m_attributesHash.contains(this->getKeyName())) - return Model::setKeysForSelectQuery(query); - - query.whereEq(m_foreignKey, - this->getOriginal(m_foreignKey, this->getAttribute(m_foreignKey))); - - return query.whereEq(m_relatedKey, - this->getOriginal(m_relatedKey, - this->getAttribute(m_relatedKey))); + /* If the pivot table contains a primary key then use this primary key + in the where clause. */ + if (const auto &primaryKeyName = this->getKeyName(); + this->m_attributesHash.contains(primaryKeyName) + ) + // Also check if this primary key is valid + if (const auto id = this->getKeyForSelectQuery(); + id.isValid() && !id.isNull() + ) + return Model::setKeysForSelectQuery(query); + + // NOTE api different, we are using parenthesis around the following keys, I think it's a little safer silverqx + return query.where({ + {m_foreignKey, this->getOriginal(m_foreignKey, + this->getAttribute(m_foreignKey))}, + {m_relatedKey, this->getOriginal(m_relatedKey, + this->getAttribute(m_relatedKey))}, + }); } template @@ -251,13 +260,13 @@ namespace Orm::Tiny::Relations } template - QString BasePivot::getForeignKey() const + const QString &BasePivot::getForeignKey() const noexcept { return m_foreignKey; } template - QString BasePivot::getRelatedKey() const + const QString &BasePivot::getRelatedKey() const noexcept { return m_relatedKey; }