- enable glob matching + test case
[platform/upstream/libzypp.git] / zypp / PoolQuery.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PoolQuery.h
10  *
11 */
12 #ifndef ZYPP_POOLQUERY_H
13 #define ZYPP_POOLQUERY_H
14
15 #include "zypp/ui/Selectable.h"
16 #include "zypp/sat/SolvAttr.h"
17
18 #include "zypp/base/Function.h"
19
20 extern "C"
21 {
22 struct _Dataiterator;
23 }
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28   ///////////////////////////////////////////////////////////////////
29
30   /**
31    * Meta-data query API. Returns solvables of specified kinds from specified
32    * repositories with attributes matching the specified search strings.
33    *
34    * TODO: details, examples.
35    */
36   class PoolQuery
37   {
38   public:
39     typedef std::map<sat::SolvAttr, std::vector<std::string> > AttrMap;
40     typedef std::map<sat::SolvAttr, std::string > CompiledAttrMap;
41     typedef unsigned int size_type;
42
43     class Impl;
44
45     class ResultIterator : public boost::iterator_adaptor<
46       ResultIterator                     // Derived
47       , ::_Dataiterator *                // Base
48       , sat::Solvable                    // Value
49       , boost::forward_traversal_tag     // CategoryOrTraversal
50       , sat::Solvable                    // Reference
51     >
52     {
53     public:
54       ResultIterator()
55       : ResultIterator::iterator_adaptor_(0), _has_next(true),
56         _attrs(CompiledAttrMap()), _do_matching(false), _pool((sat::Pool::instance()))
57       { _rdit = 0; _sid = 0; }
58
59     private:
60       friend class boost::iterator_core_access;
61       friend class PoolQuery;
62
63       ResultIterator(Impl * pqimpl);
64
65       sat::Solvable dereference() const
66       {
67         return _sid ? sat::Solvable(_sid) : sat::Solvable::noSolvable;
68       }
69
70       void increment();
71
72       bool matchSolvable();
73
74       template <class OtherDerived, class OtherIterator, class V, class C, class R, class D>
75         bool equal( const boost::iterator_adaptor<OtherDerived, OtherIterator, V, C, R, D> & rhs ) const
76       {
77         if (!rhs.base() && !base())
78           return true;
79         if (!rhs.base() || !base())
80           return false;
81         /*if (rhs.base()->solvid == base()->solvid)
82           return true;*/
83         return true;
84       }
85
86     private:
87       ::_Dataiterator * _rdit;
88       Impl * _pqimpl;
89       /*SolvableId*/ int _sid;
90       bool _has_next;
91       const CompiledAttrMap & _attrs;
92       bool _do_matching;
93       sat::Pool _pool;
94     };
95
96   public:
97     typedef function<bool( const sat::Solvable & )> ProcessResolvable;
98
99     PoolQuery();
100     ~PoolQuery();
101
102     /** Query result accessers. */
103     //@{
104
105     /** */
106     ResultIterator begin();
107     /** */
108     ResultIterator end();
109     /** */
110     bool empty();
111     /** */
112     size_type size();
113     //@}
114
115     /**
116      * executes the query with the current settings
117      * results are yielded on the callback passed on
118      * construction
119      */
120     void execute(ProcessResolvable fnc);
121
122     /**
123      * Filter by selectable kind.
124      *
125      * By default, all kinds will be returned. If addKind() is used,
126      * only the specified kinds will be returned (multiple kinds will be ORed).
127      *
128      * Pass ResTraits<T>::kind to this method, where T is one of the
129      * \ref Resolvable child classes (e.g. ResTraits<Pattern>::kind).
130      */
131     void addKind(const Resolvable::Kind &kind);
132
133     /**
134      * Filter by repo.
135      *
136      * By default, all repos will be returned. If addRepo() is used,
137      * only the specified repo will be returned (multiple repos will be ORed).
138      */
139     void addRepo(const std::string &repoalias);
140
141     /** Installed status filter setters. */
142     //@{
143
144     /**
145      * Filter by status (installed uninstalled)
146      */
147     enum StatusFilter {
148       ALL = 0, // both install filter and uninstall filter bits are 0
149       INSTALLED_ONLY = 1,
150       UNINSTALLED_ONLY = 2
151     };
152     void setInstalledOnly();
153     void setUninstalledOnly();
154     void setStatusFilterFlags( int flags );
155
156     //@}
157
158     /**
159      * 
160      */
161     void addString(const std::string & value);
162
163     /**
164      * Filter by the \a value of any available solvable attribute.
165      *
166      * \note Solvables of a kind not supporting the specified attribute will
167      * <b>not</b> be returned.
168      * \todo check the above
169      *
170      * \param attr Attribute identfier. Use sat::Solvattr::* constants
171      * \param value What to search for.
172      */
173     void addAttribute(const sat::SolvAttr & attr, const std::string & value = "");
174
175     /**
176      * Filter by Selectable status.
177      *
178      * This should cover also plain 'is installed' and 'not installed' statuses.
179      *
180      * \param status Selectable status (zypp::ui::Status enum)
181      */
182     //void addStatus(const Status status);
183
184     /**
185      * Add dependency filter.
186      *
187      * \param dtype   depenedcy type
188      * \param name    depenency name
189      * \param edition edition for a versioned dependency
190      * \param rel     operand for a versioned dependency
191      *
192      * \todo maybe a isRegexp bool as in addName() for the name parameter would
193      *       be handy here as well.
194      * \todo add more addDependecy() variants
195      *//*
196     void addDependency(const Dep & dtype,
197                        const std::string & name,
198                        const Edition & edition = Edition(),
199                        const Rel & rel = Rel::EQ);
200 */
201
202
203     /** \name Text Matching Options */
204     //@{
205     /**
206      * Turn case sentitivity on or off (unsets or sets \ref SEARCH_NOCASE flag).
207      * PoolQuery defaults to case insensitive search unless this method
208      * is used.
209      *
210      * \param value Whether to turn the case sensitivity on (default) or off.
211      */
212     void setCaseSensitive(const bool value = true);
213
214     /** Set to match exact string instead of substring.*/
215     void setMatchExact();
216     /** Set to substring (the default). */
217     void setMatchSubstring();
218     /** Set to match globs. */
219     void setMatchGlob();
220     /** Set to use the query strings as regexes */
221     void setMatchRegex();
222     /** Set to match words (uses regex) */
223     void setMatchWord();
224     //void setLocale(const Locale & locale);
225     //@}
226
227
228     /**
229      * Require that all of the query conditions set by
230      * addAttribute, addString, addDep are satisfied.
231      */
232     void requireAll(const bool require_all = true);
233
234     /**
235      * Reads from stream query. Attributes is sepated by delim. Query is
236      * separated by two delim.
237      *
238      * \param str input stream which contains query
239      * \param delim delimeter for attributes
240      * \return true if non-empty query is recovered
241      *
242      * \see readPoolQueriesFromFile
243      */
244     bool recover( std::istream &str, char delim = '\n' );
245
246     /**
247      * Writes a machine-readable string representation of the query to stream.
248      * Use \a delim as attribute delimiter.
249      *
250      * \param str output stream to write to
251      * \param delim delimiter for attributes
252      *
253      * \see writePoolQueriesToFile
254      */
255     void serialize( std::ostream &str, char delim = '\n' ) const;
256
257     /** Return a human-readable description of the query */
258     std::string asString() const;
259
260
261     // low level API
262
263     /**
264      * Free function to set the satsolver repo search
265      * flags.
266      *
267      * \see SEARCH_STRINGMASK
268      * \see SEARCH_STRING
269      * \see SEARCH_SUBSTRING
270      * \see SEARCH_GLOB
271      * \see SEARCH_REGEX
272      * \see SEARCH_NOCASE
273      * \see SEARCH_NO_STORAGE_SOLVABLE
274      */
275     void setFlags(int flags);
276
277   private:
278     /** Pointer to implementation */
279     RW_pointer<Impl> _pimpl;
280   };
281
282
283   /** \relates PoolQuery Stream output. */
284   std::ostream & operator<<( std::ostream & str, const PoolQuery & obj );
285
286
287   /////////////////////////////////////////////////////////////////
288 } // namespace zypp
289 ///////////////////////////////////////////////////////////////////
290
291 #endif // ZYPP_POOLQUERY_H