mirror of
https://github.com/silverqx/TinyORM.git
synced 2025-12-30 07:19:34 -06:00
added pluck overload
- added unit tests - updated docs
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user