1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PoolQuery.h
12 #ifndef ZYPP_POOLQUERY_H
13 #define ZYPP_POOLQUERY_H
15 #include "base/Regex.h"
17 #include "zypp/Resolvable.h"
18 #include "zypp/sat/SolvAttr.h"
19 #include "zypp/sat/SolvIterMixin.h"
20 #include "zypp/sat/LookupAttr.h"
22 #include "zypp/base/PtrTypes.h"
23 #include "zypp/base/Function.h"
30 ///////////////////////////////////////////////////////////////////
32 { /////////////////////////////////////////////////////////////////
36 class PoolQueryIterator;
39 ///////////////////////////////////////////////////////////////////
41 // CLASS NAME : PoolQuery
44 * Meta-data query API. Returns solvables of specified kinds from specified
45 * repositories with attributes matching the specified search strings.
47 * TODO: details, examples.
49 class PoolQuery : public sat::SolvIterMixin<PoolQuery, detail::PoolQueryIterator>
52 typedef std::set<std::string> StrContainer;
53 typedef std::set<Resolvable::Kind> Kinds;
54 typedef std::map<sat::SolvAttr, StrContainer> AttrRawStrMap;
55 typedef std::map<sat::SolvAttr, std::string> AttrCompiledStrMap;
56 typedef std::map<sat::SolvAttr, str::regex> AttrRegexMap;
58 typedef detail::PoolQueryIterator const_iterator;
59 typedef unsigned int size_type;
62 typedef function<bool( const sat::Solvable & )> ProcessResolvable;
67 /** Query result accessers. */
69 class PoolQueryIterator;
72 const_iterator begin() const;
74 const_iterator end() const;
82 * executes the query with the current settings
83 * results are yielded on the callback passed on
86 void execute(ProcessResolvable fnc);
89 * Filter by selectable kind.
91 * By default, all kinds will be returned. If addKind() is used,
92 * only the specified kinds will be returned (multiple kinds will be ORed).
94 * Pass ResTraits<T>::kind to this method, where T is one of the
95 * \ref Resolvable child classes (e.g. ResTraits<Pattern>::kind).
97 void addKind(const Resolvable::Kind &kind);
102 * By default, all repos will be returned. If addRepo() is used,
103 * only the specified repo will be returned (multiple repos will be ORed).
105 void addRepo(const std::string &repoalias);
107 /** Installed status filter setters. */
111 * Filter by status (installed uninstalled)
114 ALL = 0, // both install filter and uninstall filter bits are 0
118 void setInstalledOnly();
119 void setUninstalledOnly();
120 void setStatusFilterFlags( StatusFilter flags );
127 void addString(const std::string & value);
130 * Filter by the \a value of any available solvable attribute.
132 * \note Solvables of a kind not supporting the specified attribute will
133 * <b>not</b> be returned.
134 * \todo check the above
136 * \param attr Attribute identfier. Use sat::Solvattr::* constants
137 * \param value What to search for.
139 void addAttribute(const sat::SolvAttr & attr, const std::string & value = "");
142 * Filter by Selectable status.
144 * This should cover also plain 'is installed' and 'not installed' statuses.
146 * \param status Selectable status (zypp::ui::Status enum)
148 //void addStatus(const Status status);
151 * Add dependency filter.
153 * \param dtype depenedcy type
154 * \param name depenency name
155 * \param edition edition for a versioned dependency
156 * \param rel operand for a versioned dependency
158 * \todo maybe a isRegexp bool as in addName() for the name parameter would
159 * be handy here as well.
160 * \todo add more addDependecy() variants
162 void addDependency(const Dep & dtype,
163 const std::string & name,
164 const Edition & edition = Edition(),
165 const Rel & rel = Rel::EQ);
169 /** \name Text Matching Options */
172 * Turn case sentitivity on or off (unsets or sets \ref SEARCH_NOCASE flag).
173 * PoolQuery defaults to case insensitive search unless this method
176 * \param value Whether to turn the case sensitivity on (default) or off.
178 void setCaseSensitive(const bool value = true);
180 /** Set to match exact string instead of substring.*/
181 void setMatchExact();
182 /** Set to substring (the default). */
183 void setMatchSubstring();
184 /** Set to match globs. */
186 /** Set to use the query strings as regexes */
187 void setMatchRegex();
188 /** Set to match words (uses regex) */
190 //void setLocale(const Locale & locale);
195 * Require that all of the values set by addString, addAttribute, addDep
196 * match the values of respective attributes.
198 void setRequireAll(const bool require_all = true);
203 /** Search strings added via addString() */
204 const StrContainer & strings() const;
206 * Map (map<SolvAttr, StrContainer>) of attribute values added via
207 * addAttribute(), addDep in string form */
208 const AttrRawStrMap & attributes() const;
210 const Kinds & kinds() const;
212 const StrContainer & repos() const;
214 bool caseSensitive() const;
216 bool matchExact() const;
217 bool matchSubstring() const;
218 bool matchGlob() const;
219 bool matchRegex() const;
221 * Returns currently used string matching type.
222 * \see satsolver/repo.h
224 int matchType() const;
226 bool matchWord() const;
228 bool requireAll() const;
230 StatusFilter statusFilterFlags() const;
234 * Reads from stream query. Attributes is sepated by delim. Query is
235 * separated by two delim.
237 * \param str input stream which contains query
238 * \param delim delimeter for attributes
239 * \return true if non-empty query is recovered
241 * \see readPoolQueriesFromFile
243 bool recover( std::istream &str, char delim = '\n' );
246 * Writes a machine-readable string representation of the query to stream.
247 * Use \a delim as attribute delimiter.
249 * \param str output stream to write to
250 * \param delim delimiter for attributes
252 * \see writePoolQueriesToFile
254 void serialize( std::ostream &str, char delim = '\n' ) const;
256 /** Return a human-readable description of the query */
257 std::string asString() const;
259 bool operator==(const PoolQuery& b) const;
260 bool operator!=(const PoolQuery& b) const { return !(*this == b ); }
265 * Free function to set the satsolver repo search
268 * \see SEARCH_STRINGMASK
270 * \see SEARCH_SUBSTRING
274 * \see SEARCH_NO_STORAGE_SOLVABLE
276 void setFlags(int flags);
280 /** Pointer to implementation */
281 RW_pointer<Impl> _pimpl;
283 ///////////////////////////////////////////////////////////////////
285 /** \relates PoolQuery Stream output. */
286 std::ostream & operator<<( std::ostream & str, const PoolQuery & obj );
290 ///////////////////////////////////////////////////////////////////
292 { /////////////////////////////////////////////////////////////////
295 ///////////////////////////////////////////////////////////////////
297 // CLASS NAME : PoolQuery::PoolQueryIterator
302 class PoolQueryIterator : public boost::iterator_adaptor<
303 PoolQueryIterator // Derived
304 , sat::LookupAttr::iterator // Base
305 , const sat::Solvable // Value
306 , boost::forward_traversal_tag // CategoryOrTraversal
307 , const sat::Solvable // Reference
313 PoolQueryIterator(const PoolQueryIterator &);
316 PoolQueryIterator( const sat::LookupAttr::iterator & val_r )
317 { this->base_reference() = val_r; }
319 ~PoolQueryIterator();
321 PoolQueryIterator & operator=( const PoolQueryIterator & rhs );
324 friend class boost::iterator_core_access;
325 friend class PoolQuery::Impl;
328 scoped_ptr< ::_Dataiterator> & dip_r,
329 const PoolQuery::Impl * pqimpl);
331 const sat::Solvable dereference() const
333 return _sid ? sat::Solvable(_sid) : sat::Solvable::noSolvable;
338 bool matchSolvable();
341 /** current matching solvable id */
343 /** whether there is a next solvable to check */
345 /** whether to do text matching on our own (true) or the Dataiterator already did it */
349 * Depending on whether regexes are used in the search either \ref _str or
350 * \ref _regex (or either _attrs_str or _attrs_regex respectively) are used.
354 /** string matching option flags */
356 /** global query string compiled */
358 /** global query compiled regex */
360 /** Attribute to string map holding per-attribute query strings (compiled) */
361 PoolQuery::AttrCompiledStrMap _attrs_str;
362 /** Attribute to regex map holding per-attribute compiled regex */
363 PoolQuery::AttrRegexMap _attrs_regex;
364 /** Set of repository names include in the search. */
365 PoolQuery::StrContainer _repos;
366 /** Set of solvable kinds to include in the search. */
367 PoolQuery::Kinds _kinds;
368 /** Installed status filter flags. \see PoolQuery::StatusFilter */
372 /** used to copy current iterator in order to forward check for next attributes */
373 sat::LookupAttr::iterator _tmpit;
375 ///////////////////////////////////////////////////////////////////
377 ///////////////////////////////////////////////////////////////////
379 ///////////////////////////////////////////////////////////////////
381 /////////////////////////////////////////////////////////////////
383 ///////////////////////////////////////////////////////////////////
385 #endif // ZYPP_POOLQUERY_H