added pluck overload

- added unit tests
 - updated docs
This commit is contained in:
silverqx
2023-05-28 08:25:30 +02:00
parent f9b5d84a9a
commit ef71215afd
4 changed files with 86 additions and 0 deletions

View File

@@ -510,6 +510,10 @@ The `pluck` method retrieves all of the values for a given column, the following
// {Desk, Chair}
The second overload allows returning the custom type `QVector<T>`:
auto plucked = products.pluck<QString>("name");
You may also specify how you wish the resulting collection to be keyed, this overload returns the `std::map<T, QVariant>`:
auto plucked = products.pluck<quint64>("name", "id");

View File

@@ -222,6 +222,9 @@ namespace Types
/*! Get a vector with the values in the given column. */
QVector<QVariant> pluck(const QString &column);
/*! Get a vector with the values in the given column. */
template<typename T>
QVector<T> pluck(const QString &column);
/*! Get a map with values in the given column and keyed by values in the key
column (attribute). */
template<typename T>
@@ -964,6 +967,29 @@ namespace Types
return result;
}
template<DerivedCollectionModel Model>
template<typename T>
QVector<T>
ModelsCollection<Model>::pluck(const QString &column)
{
QVector<T> result;
result.reserve(this->size());
for (ModelLoopType model : *this) {
ModelRawType *const modelPointer = toPointer(model);
// Don't handle the nullptr
if (const auto &attributesHash = modelPointer->getAttributesHash();
attributesHash.contains(column)
)
// Don't handle the null and not valid
result << modelPointer->getAttributes().at(attributesHash.at(column))
.value.template value<T>();
}
return result;
}
template<DerivedCollectionModel Model>
template<typename T>
std::map<T, QVariant>

View File

@@ -91,6 +91,7 @@ private Q_SLOTS:
void except_Empty() const;
void pluck() const;
void pluck_CustomType() const;
void pluck_KeyedById() const;
void pluck_KeyedById_LastDuplicate() const;
@@ -921,6 +922,30 @@ void tst_Collection_Models::pluck() const
QCOMPARE(result, expected);
}
void tst_Collection_Models::pluck_CustomType() const
{
auto images = AlbumImage::whereEq(Common::album_id, 2)->get();
QCOMPARE(images.size(), 5);
QCOMPARE(typeid (ModelsCollection<AlbumImage>), typeid (images));
QVERIFY(Common::verifyIds(images, {2, 3, 4, 5, 6}));
// Get result
const auto result = images.pluck<QString>(NAME);
// Verify
QCOMPARE(result.size(), 5);
QCOMPARE(typeid (result), typeid (QVector<QString>));
QVector<QString> expected {
QString("album2_image1"),
QString("album2_image2"),
QString("album2_image3"),
QString("album2_image4"),
QString("album2_image5"),
};
QCOMPARE(result, expected);
}
void tst_Collection_Models::pluck_KeyedById() const
{
auto images = AlbumImage::whereEq(Common::album_id, 2)->get();

View File

@@ -91,6 +91,7 @@ private Q_SLOTS:
void except_Empty() const;
void pluck() const;
void pluck_CustomType() const;
void pluck_KeyedById() const;
void pluck_KeyedById_LastDuplicate() const;
@@ -1180,6 +1181,36 @@ void tst_Collection_Relations::pluck() const
QCOMPARE(result, expected);
}
void tst_Collection_Relations::pluck_CustomType() const
{
auto album = Album::find(2);
QVERIFY(album);
QVERIFY(album->exists);
QCOMPARE(album->getKey(), QVariant(2));
QVERIFY(album->relationLoaded(Common::albumImages));
auto images = album->getRelation<AlbumImage>(Common::albumImages);
QCOMPARE(images.size(), 5);
QCOMPARE(typeid (ModelsCollection<AlbumImage *>), typeid (images));
QVERIFY(Common::verifyIds(images, {2, 3, 4, 5, 6}));
// Get result
const auto result = images.pluck<QString>(NAME);
// Verify
QCOMPARE(result.size(), 5);
QCOMPARE(typeid (result), typeid (QVector<QString>));
QVector<QString> expected {
QString("album2_image1"),
QString("album2_image2"),
QString("album2_image3"),
QString("album2_image4"),
QString("album2_image5"),
};
QCOMPARE(result, expected);
}
void tst_Collection_Relations::pluck_KeyedById() const
{
auto album = Album::find(2);