digiKam
coredbsearchxml.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2008-01-09
7  * Description : Core database searches XML queries manager
8  *
9  * Copyright (C) 2008-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
10  * Copyright (C) 2010-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  *
12  * This program is free software; you can redistribute it
13  * and/or modify it under the terms of the GNU General
14  * Public License as published by the Free Software Foundation;
15  * either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * ============================================================ */
24 
25 #ifndef DIGIKAM_CORE_DB_SEARCH_XML_H
26 #define DIGIKAM_CORE_DB_SEARCH_XML_H
27 
28 // Qt includes
29 
30 #include <QString>
31 #include <QDateTime>
32 #include <QXmlStreamReader>
33 #include <QXmlStreamWriter>
34 #include <QStringList>
35 #include <QVariant>
36 
37 // Local includes
38 
39 #include "digikam_export.h"
40 
41 namespace Digikam
42 {
43 
44 namespace SearchXml
45 {
46 
48 {
49  And,
50  Or,
52  OrNot
53 };
54 
55 enum Element
56 {
62  End
63 };
64 
66 {
75  Interval, // [a,b]
76  IntervalOpen, // (a,b)
82  Inside
83 };
84 
85 template <typename T>
86 bool testRelation(T v1, T v2, Relation rel)
87 {
88  if (rel == Equal)
89  {
90  return v1 == v2;
91  }
92  else if (rel == Unequal)
93  {
94  return v1 != v2;
95  }
96  else if (rel == LessThan)
97  {
98  return v1 < v2;
99  }
100  else if (rel == LessThanOrEqual)
101  {
102  return v1 <= v2;
103  }
104  else if (rel == GreaterThan)
105  {
106  return v1 > v2;
107  }
108  else if (rel == GreaterThanOrEqual)
109  {
110  return v1 >= v2;
111  }
112 
113  return false;
114 }
115 
120 {
121  return SearchXml::Or;
122 }
123 
125 {
126  return SearchXml::And;
127 }
128 
130 {
131  return SearchXml::Equal;
132 }
133 
134 } // namespace SearchXml
135 
136 // ---------------------------------------------------------------------------------
137 
138 class DIGIKAM_DATABASE_EXPORT SearchXmlReader : public QXmlStreamReader
139 {
140 public:
141 
142  explicit SearchXmlReader(const QString& xml);
143 
146  SearchXml::Element readNext();
147 
150  bool isGroupElement() const;
151 
154  bool isFieldElement() const;
155 
158  SearchXml::Operator groupOperator() const;
159 
162  QString groupCaption() const;
163 
166  SearchXml::Operator defaultFieldOperator() const;
167 
171  SearchXml::Operator fieldOperator() const;
172  QString fieldName() const;
173  SearchXml::Relation fieldRelation() const;
174 
179  QString value();
180  int valueToInt();
181  qlonglong valueToLongLong();
182  double valueToDouble();
183  QDateTime valueToDateTime();
184  QList<int> valueToIntList();
185  QList<qlonglong> valueToLongLongList();
186  QList<double> valueToDoubleList();
187  QStringList valueToStringList();
188  QList<QDateTime> valueToDateTimeList();
189 
190  QList<int> valueToIntOrIntList();
191  QList<double> valueToDoubleOrDoubleList();
192  QList<QString> valueToStringOrStringList();
193 
199  bool readToStartOfElement(const QString& name);
200 
204  void readToEndOfElement();
205 
209  void readToFirstField();
210 
211 protected:
212 
213  SearchXml::Operator readOperator(const QString&, SearchXml::Operator) const;
214  SearchXml::Relation readRelation(const QString&, SearchXml::Relation) const;
215 
216 protected:
217 
219 };
220 
221 // ---------------------------------------------------------------------------------
222 
223 class DIGIKAM_DATABASE_EXPORT SearchXmlWriter : public QXmlStreamWriter
224 {
225 public:
226 
232  explicit SearchXmlWriter();
233 
236  void writeGroup();
237 
241  void setGroupOperator(SearchXml::Operator op);
242 
245  void setGroupCaption(const QString& caption);
246 
250  void setDefaultFieldOperator(SearchXml::Operator op);
251 
257  void writeField(const QString& name, SearchXml::Relation relation);
258 
261  void setFieldOperator(SearchXml::Operator op);
262 
265  void writeValue(const QString& value);
266  void writeValue(int value);
267  void writeValue(qlonglong value);
268  void writeValue(float value, int precision = 6);
269  void writeValue(double value, int precision = 8);
270  void writeValue(const QDateTime& dateTime);
271  void writeValue(const QList<int>& valueList);
272  void writeValue(const QList<qlonglong>& valueList);
273  void writeValue(const QList<float>& valueList, int precision = 6);
274  void writeValue(const QList<double>& valueList, int precision = 8);
275  void writeValue(const QStringList& valueList);
276  void writeValue(const QList<QDateTime>& valueList);
277 
281  void finishField();
282 
289  void finishGroup();
290 
295  void finish();
296 
299  QString xml() const;
300 
304  static QString keywordSearch(const QString& keyword);
305 
306 protected:
307 
308  void writeOperator(const QString&, SearchXml::Operator);
309  void writeRelation(const QString&, SearchXml::Relation);
310 
311 protected:
312 
313  QString m_xml;
314 };
315 
316 // ---------------------------------------------------------------------------------
317 
318 namespace KeywordSearch
319 {
320 
325 DIGIKAM_DATABASE_EXPORT QStringList split(const QString& string);
326 
330 DIGIKAM_DATABASE_EXPORT QString merge(const QStringList& keywordList);
331 
337 DIGIKAM_DATABASE_EXPORT QString merge(const QString& previousContent, const QString& newEntry);
338 
339 } // namespace KeywordSearch
340 
341 // ---------------------------------------------------------------------------------
342 
343 class DIGIKAM_DATABASE_EXPORT KeywordSearchReader : public SearchXmlReader
344 {
345 public:
346 
347  explicit KeywordSearchReader(const QString& xml);
348 
350  QStringList keywords();
351 
353  bool isSimpleKeywordSearch();
354 
355 private:
356 
357  void readGroup(QStringList& list);
358  bool isSimpleKeywordSearchGroup();
359  QString readField();
360 };
361 
362 // ---------------------------------------------------------------------------------
363 
364 class DIGIKAM_DATABASE_EXPORT KeywordSearchWriter : public SearchXmlWriter
365 {
366 public:
367 
368  explicit KeywordSearchWriter();
369 
370  QString xml(const QStringList& keywordList);
371 };
372 
373 // ---------------------------------------------------------------------------------
374 
375 class DIGIKAM_DATABASE_EXPORT SearchXmlCachingReader : public SearchXmlReader
376 {
377 public:
378 
386  explicit SearchXmlCachingReader(const QString& xml);
387 
388  SearchXml::Element readNext();
389 
390  SearchXml::Operator groupOperator() const;
391  QString groupCaption() const;
392 
393  SearchXml::Operator fieldOperator() const;
394  QString fieldName() const;
395  SearchXml::Relation fieldRelation() const;
396  QString value();
397  int valueToInt();
398  qlonglong valueToLongLong();
399  double valueToDouble();
400  QDateTime valueToDateTime();
401  QList<int> valueToIntList();
402  QList<qlonglong> valueToLongLongList();
403  QList<double> valueToDoubleList();
404  QStringList valueToStringList();
405  QList<QDateTime> valueToDateTimeList();
406  QList<int> valueToIntOrIntList();
407  QList<double> valueToDoubleOrDoubleList();
408  QList<QString> valueToStringOrStringList();
409 
410 protected:
411 
413  QString m_groupCaption;
415  QString m_fieldName;
417  QVariant m_value;
419 };
420 
421 } // namespace Digikam
422 
423 #endif // DIGIKAM_CORE_DB_SEARCH_XML_H
Definition: coredbsearchxml.h:344
Definition: coredbsearchxml.h:365
Definition: coredbsearchxml.h:376
QVariant m_value
Definition: coredbsearchxml.h:417
SearchXml::Relation m_fieldRelation
Definition: coredbsearchxml.h:416
SearchXml::Operator m_groupOperator
Definition: coredbsearchxml.h:412
QString m_fieldName
Definition: coredbsearchxml.h:415
SearchXml::Operator m_fieldOperator
Definition: coredbsearchxml.h:414
QString m_groupCaption
Definition: coredbsearchxml.h:413
bool m_readValue
Definition: coredbsearchxml.h:418
Definition: coredbsearchxml.h:139
SearchXml::Operator m_defaultFieldOperator
Definition: coredbsearchxml.h:218
Definition: coredbsearchxml.h:224
QString m_xml
Definition: coredbsearchxml.h:313
qulonglong value
Definition: itemviewutilities.cpp:592
#define T
@ KeywordSearch
Definition: coredbconstants.h:62
DIGIKAM_DATABASE_EXPORT QStringList split(const QString &string)
Definition: coredbsearchxml.cpp:836
DIGIKAM_DATABASE_EXPORT QString merge(const QStringList &keywordList)
Definition: coredbsearchxml.cpp:867
SearchXml::Relation standardFieldRelation()
Definition: coredbsearchxml.h:129
Relation
Definition: coredbsearchxml.h:66
@ NotLike
Definition: coredbsearchxml.h:70
@ AllOf
Definition: coredbsearchxml.h:78
@ OneOf
Definition: coredbsearchxml.h:77
@ Like
Definition: coredbsearchxml.h:69
@ LessThanOrEqual
Definition: coredbsearchxml.h:73
@ Near
Definition: coredbsearchxml.h:81
@ InTree
Definition: coredbsearchxml.h:79
@ Equal
Definition: coredbsearchxml.h:67
@ IntervalOpen
Definition: coredbsearchxml.h:76
@ Inside
Definition: coredbsearchxml.h:82
@ LessThan
Definition: coredbsearchxml.h:71
@ Unequal
Definition: coredbsearchxml.h:68
@ Interval
Definition: coredbsearchxml.h:75
@ GreaterThanOrEqual
Definition: coredbsearchxml.h:74
@ NotInTree
Definition: coredbsearchxml.h:80
@ GreaterThan
Definition: coredbsearchxml.h:72
Element
Definition: coredbsearchxml.h:56
@ Search
Definition: coredbsearchxml.h:57
@ Field
Definition: coredbsearchxml.h:60
@ End
Definition: coredbsearchxml.h:62
@ Group
Definition: coredbsearchxml.h:58
@ GroupEnd
Definition: coredbsearchxml.h:59
@ FieldEnd
Definition: coredbsearchxml.h:61
bool testRelation(T v1, T v2, Relation rel)
Definition: coredbsearchxml.h:86
SearchXml::Operator standardGroupOperator()
Definition: coredbsearchxml.h:119
Operator
Definition: coredbsearchxml.h:48
@ Or
Definition: coredbsearchxml.h:50
@ OrNot
Definition: coredbsearchxml.h:52
@ AndNot
Definition: coredbsearchxml.h:51
@ And
Definition: coredbsearchxml.h:49
SearchXml::Operator standardFieldOperator()
Definition: coredbsearchxml.h:124
Definition: datefolderview.cpp:43