3. Changes in creating queries

To get a list of packages based on given criteria, they can be queried using dnf.query.Query class in DNF4 and libdnf5::rpm::PackageQuery class in DNF5.

In DNF4, the filter method doesn’t actually modify the query, but rather return a new query limiting it according to the given arguments. In DNF5, the original query is modified by the filters.

DNF4 Python:

1# Create a package query.
2packages = base.sack.query()
3
4# Filter the packages, the filters can be stacked one after another.
5packages = packages.filter(name="one")
6
7# Iterate over the filtered packages in the query.
8for pkg in packages:
9    print(pkg.name)

DNF5 Python:

1# Create a package query.
2packages = libdnf5.rpm.PackageQuery(base)
3
4# Filter the packages, the filters can be stacked one after another.
5packages.filter_name(["one"])
6
7# Iterate over the filtered packages in the query.
8for pkg in packages:
9    print(pkg.get_name())

DNF5 C++:

 1#include <libdnf5/rpm/package_query.hpp>
 2
 3// Create a package query.
 4libdnf5::rpm::PackageQuery packages(base);
 5
 6// Filter the packages, the filters can be stacked one after another.
 7packages.filter_name("one");
 8
 9// Iterate over the filtered packages in the query.
10for (const auto & pkg : packages) {
11    std::cout << pkg.get_name() << std::endl;
12}

The groups and environments are in DNF4 stored in the dnf.comps.Comps class, which is different from a query object, though it also provides some filtering. In DNF5, the groups and environments are queried using the libdnf5::comps::GroupQuery and libdnf5::comps::EnvironmentQuery classes respectively, similarly to the libdnf5::rpm::PackageQuery class. The example shows querying groups, but the environments are analogous.

DNF4 Python:

1# Get all groups.
2groups = base.comps.groups
3# Or get groups with a given pattern in id or name.
4groups = groups_by_pattern("group-id")
5
6# Iterate over the groups.
7for group in groups:
8    print(group.ui_name)

DNF5 Python:

1# Create a group query.
2groups = libdnf5.comps.GroupQuery(base)
3
4# Filter the groups, the filters can be stacked one after another.
5groups.filter_groupid("group-id")
6
7# Iterate over the filtered groups in the query.
8for group in groups:
9    print(group.get_name())

DNF5 C++:

 1#include <libdnf5/comps/group_query.hpp>
 2
 3// Create a group query.
 4libdnf5::comps::GroupQuery groups(base);
 5
 6// Filter the groups, the filters can be stacked one after another.
 7groups.filter_groupid("group-id");
 8
 9// Iterate over the filtered groups in the query.
10for (const auto & group : groups) {
11    std::cout << group.get_name() << std::endl;
12}