From 18ca6c4e848cf6e7ab0d26f009da2c51312cce2d Mon Sep 17 00:00:00 2001 From: silverqx Date: Sat, 27 May 2023 09:36:16 +0200 Subject: [PATCH] used column instead of key in ModelsCollection --- include/orm/tiny/types/modelscollection.hpp | 126 ++++++++++---------- 1 file changed, 65 insertions(+), 61 deletions(-) diff --git a/include/orm/tiny/types/modelscollection.hpp b/include/orm/tiny/types/modelscollection.hpp index b820c2d52..5e2491af9 100644 --- a/include/orm/tiny/types/modelscollection.hpp +++ b/include/orm/tiny/types/modelscollection.hpp @@ -267,51 +267,54 @@ namespace Types ModelsCollection reject(const std::function &callback); - /*! Filter models by the given key value pair. */ + /*! Filter models by the given column value pair. */ template ModelsCollection - where(const QString &key, const QString &comparison, V value); - /*! Filter models by the given key value pair. */ + where(const QString &column, const QString &comparison, V value); + /*! Filter models by the given column value pair. */ template - ModelsCollection whereEq(const QString &key, V value); + ModelsCollection whereEq(const QString &column, V value); - /*! Filter models where the value for the given key is the null QVariant. */ - ModelsCollection whereNull(const QString &key); - /*! Filter models where the value for the given key is not the null QVariant. */ - ModelsCollection whereNotNull(const QString &key); + /*! Filter models where the value for the given column is the null QVariant. */ + ModelsCollection whereNull(const QString &column); + /*! Filter models where the value for the given column is not the null + QVariant. */ + ModelsCollection whereNotNull(const QString &column); - /*! Filter models by the given key values pair. */ + /*! Filter models by the given column values pair. */ template ModelsCollection - whereIn(const QString &key, const std::unordered_set &values); - /*! Filter models by the given key values pair. */ + whereIn(const QString &column, const std::unordered_set &values); + /*! Filter models by the given column values pair. */ template ModelsCollection - whereNotIn(const QString &key, const std::unordered_set &values); + whereNotIn(const QString &column, const std::unordered_set &values); - /*! Filter models such that the value of the given key is between the given + /*! Filter models such that the value of the given column is between the given values. */ template ModelsCollection - whereBetween(const QString &key, const WhereBetweenCollectionItem &values); - /*! Filter models such that the value of the given key is not between the given - values. */ + whereBetween(const QString &column, const WhereBetweenCollectionItem &values); + /*! Filter models such that the value of the given column is not between + the given values. */ template ModelsCollection - whereNotBetween(const QString &key, const WhereBetweenCollectionItem &values); + whereNotBetween(const QString &column, + const WhereBetweenCollectionItem &values); - /*! Get the first model by the given key value pair. */ + /*! Get the first model by the given column value pair. */ template - ModelRawType *firstWhere(const QString &key, const QString &comparison, V value); - /*! Get the first model by the given key value pair. */ + ModelRawType * + firstWhere(const QString &column, const QString &comparison, V value); + /*! Get the first model by the given column value pair. */ template - ModelRawType *firstWhereEq(const QString &key, V value); + ModelRawType *firstWhereEq(const QString &column, V value); - /*! Get a single key's value from the first matching model in the collection. */ - QVariant value(const QString &key, const QVariant &defaultValue = {}) const; - /*! Get a single key's value from the first matching model in the collection. */ + /*! Get a single column value from the first matching model in the collection. */ + QVariant value(const QString &column, const QVariant &defaultValue = {}) const; + /*! Get a single column value from the first matching model in the collection. */ template - T value(const QString &key, const T &defaultValue = {}) const; + T value(const QString &column, const T &defaultValue = {}) const; /*! Execute a callback over each model. */ ModelsCollection & @@ -377,7 +380,7 @@ namespace Types /*! Get an operator checker callback. */ template std::function - operatorForWhere(const QString &key, const QString &comparison, V value) const; + operatorForWhere(const QString &column, const QString &comparison, V value) const; /*! Convert the Collection to the Collection. */ ModelsCollection @@ -1152,69 +1155,69 @@ namespace Types template template ModelsCollection::ModelRawType *> - ModelsCollection::where(const QString &key, const QString &comparison, + ModelsCollection::where(const QString &column, const QString &comparison, V value) { - return filter(operatorForWhere(key, comparison, std::move(value))); + return filter(operatorForWhere(column, comparison, std::move(value))); } template template ModelsCollection::ModelRawType *> - ModelsCollection::whereEq(const QString &key, V value) + ModelsCollection::whereEq(const QString &column, V value) { - return filter(operatorForWhere(key, EQ, std::move(value))); + return filter(operatorForWhere(column, EQ, std::move(value))); } template ModelsCollection::ModelRawType *> - ModelsCollection::whereNull(const QString &key) + ModelsCollection::whereNull(const QString &column) { - return filter([&key](ModelRawType *const model) + return filter([&column](ModelRawType *const model) { - return model->getAttribute(key).isNull(); + return model->getAttribute(column).isNull(); }); } template ModelsCollection::ModelRawType *> - ModelsCollection::whereNotNull(const QString &key) + ModelsCollection::whereNotNull(const QString &column) { - return filter([&key](ModelRawType *const model) + return filter([&column](ModelRawType *const model) { - return !model->getAttribute(key).isNull(); + return !model->getAttribute(column).isNull(); }); } template template ModelsCollection::ModelRawType *> - ModelsCollection::whereIn(const QString &key, + ModelsCollection::whereIn(const QString &column, const std::unordered_set &values) { // Nothing to do, no values passed if (values.empty()) return {}; - return filter([&key, &values](ModelRawType *const model) + return filter([&column, &values](ModelRawType *const model) { - return values.contains(model->template getAttribute(key)); + return values.contains(model->template getAttribute(column)); }); } template template ModelsCollection::ModelRawType *> - ModelsCollection::whereNotIn(const QString &key, + ModelsCollection::whereNotIn(const QString &column, const std::unordered_set &values) { // Nothing to do, no values passed, return a copy if (values.empty()) return toPointersCollection(); - return reject([&key, &values](ModelRawType *const model) + return reject([&column, &values](ModelRawType *const model) { - return values.contains(model->template getAttribute(key)); + return values.contains(model->template getAttribute(column)); }); } @@ -1222,11 +1225,11 @@ namespace Types template ModelsCollection::ModelRawType *> ModelsCollection::whereBetween( - const QString &key, const WhereBetweenCollectionItem &values) + const QString &column, const WhereBetweenCollectionItem &values) { - return filter([&key, &values](ModelRawType *const model) + return filter([&column, &values](ModelRawType *const model) { - const auto attribute = model->getAttribute(key); + const auto attribute = model->getAttribute(column); // Null or invalid attributes can't be handled in any other way anyway if (attribute.isNull() || !attribute.isValid()) @@ -1242,11 +1245,11 @@ namespace Types template ModelsCollection::ModelRawType *> ModelsCollection::whereNotBetween( - const QString &key, const WhereBetweenCollectionItem &values) + const QString &column, const WhereBetweenCollectionItem &values) { - return filter([&key, &values](ModelRawType *const model) + return filter([&column, &values](ModelRawType *const model) { - const auto attribute = model->getAttribute(key); + const auto attribute = model->getAttribute(column); // Null or invalid attributes can't be handled in any other way anyway if (attribute.isNull() || !attribute.isValid()) @@ -1261,23 +1264,23 @@ namespace Types template template typename ModelsCollection::ModelRawType * - ModelsCollection::firstWhere(const QString &key, const QString &comparison, + ModelsCollection::firstWhere(const QString &column, const QString &comparison, V value) { - return first(operatorForWhere(key, comparison, std::move(value))); + return first(operatorForWhere(column, comparison, std::move(value))); } template template typename ModelsCollection::ModelRawType * - ModelsCollection::firstWhereEq(const QString &key, V value) + ModelsCollection::firstWhereEq(const QString &column, V value) { - return first(operatorForWhere(key, EQ, std::move(value))); + return first(operatorForWhere(column, EQ, std::move(value))); } template QVariant - ModelsCollection::value(const QString &key, + ModelsCollection::value(const QString &column, const QVariant &defaultValue) const { // Nothing to do @@ -1287,15 +1290,15 @@ namespace Types const ModelRawType *const model = toPointer(StorageType::constFirst()); // Only one place where the nullptr is checked, it's needed - if (model == nullptr || !model->getAttributesHash().contains(key)) + if (model == nullptr || !model->getAttributesHash().contains(column)) return defaultValue; - return model->getAttribute(key); + return model->getAttribute(column); } template template - T ModelsCollection::value(const QString &key, const T &defaultValue) const + T ModelsCollection::value(const QString &column, const T &defaultValue) const { // Nothing to do if (this->isEmpty()) @@ -1304,10 +1307,10 @@ namespace Types const ModelRawType *const model = toPointer(StorageType::constFirst()); // Only one place where the nullptr is checked, it's needed - if (model == nullptr || !model->getAttributesHash().contains(key)) + if (model == nullptr || !model->getAttributesHash().contains(column)) return defaultValue; - return model->template getAttribute(key); + return model->template getAttribute(column); } template @@ -1500,13 +1503,14 @@ namespace Types template std::function::ModelRawType *)> ModelsCollection::operatorForWhere( - const QString &key, const QString &comparison, V value) const + const QString &column, const QString &comparison, V value) const { throwIfInvalidWhereOperator(comparison); - return [&key, &comparison, value = std::move(value)](ModelRawType *const model) + return [&column, &comparison, value = std::move(value)] + (ModelRawType *const model) { - const auto attribute = model->getAttribute(key); + const auto attribute = model->getAttribute(column); const auto retrieved = attribute.template value(); static_assert (std::is_convertible_v,