Xmipp  v3.23.11-Nereus
metadata_query.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: J.M. De la Rosa Trevin (jmdelarosa@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef CORE_METADATAQUERY_H
27 #define CORE_METADATAQUERY_H
28 
29 #include <sstream>
30 
31 #include "metadata_label.h"
32 #include "xmipp_error.h"
33 #include "metadata_static.h"
34 
35 
41 class MDQuery
42 {
43 public:
44  int limit;
45  int offset;
47  bool asc;
48 
50  MDQuery(int limit = -1, int offset = 0, MDLabel orderLabel = MDL_OBJID,bool asc=true)
51  {
52  this->limit = limit;
53  this->offset = offset;
54  this->orderLabel = orderLabel;
55  this->asc=asc;
56  }
57 
59  virtual ~MDQuery() {}
60 
63  {
64  return (String)" ORDER BY " + MDL::label2Str(orderLabel) + (asc ? " ASC" : " DESC");
65  }
66 
68  String limitString() const;
69 
72  {
73  String queryString = this->queryStringFunc();
74  return (queryString == " ") ? " " : " WHERE " + queryString + " ";
75  }
76 
78  virtual String queryStringFunc() const
79  {
80  return " ";
81  }
82 }
83 ;//End of class MDQuery
84 
89 {
90  EQ,
91  NE,
92  GT,
93  LT,
94  GE,
95  LE
96 };
97 
104 {
105 public:
108 
109  template <class T>
111  {
112  this->op = op;
113  this->value = new MDObject(label, value);
114  }
115 
117  {
118  this->op = op;
119  this->value = new MDObject(value);
120  }
121 
123  {
124  delete this->value;
125  }
126 
127  String opString() const
128  {
129  switch (op)
130  {
131  case EQ:
132  return "=";
133  case NE:
134  return "!=";
135  case GT:
136  return ">";
137  case LT:
138  return "<";
139  case GE:
140  return ">=";
141  case LE:
142  return "<=";
143  default:
144  REPORT_ERROR(ERR_ARG_INCORRECT,"Unknown binary operator");
145  }
146  }
147 
148  virtual String queryStringFunc() const
149  {
150  return (value == NULL) ? " " : MDL::label2Str(value->label) + opString() + value->toString(false, true);
151  }
152 
153  template <class T>
154  void setValue(T &value)
155  {
156  this->value->setValue(value);
157  }
158 }
159 ;//end of class MDValueRelational
160 
171 {
172 public:
173  template <class T>
174  MDValueEQ(MDLabel label, const T &value, int limit = -1, int offset = 0, MDLabel orderLabel = MDL_OBJID)
175  :MDValueRelational(label, value, EQ, limit, offset, orderLabel)
176  {}
177 }
178 ;//end of class MDValueEQ
179 
184 {
185 public:
186  template <class T>
187  MDValueNE(MDLabel label, const T &value, int limit = -1, int offset = 0, MDLabel orderLabel = MDL_OBJID)
188  :MDValueRelational(label, value, NE, limit, offset, orderLabel)
189  {}
190 }
191 ;//end of class MDValueNE
192 
197 {
198 public:
199  template <class T>
200  MDValueGE(MDLabel label, const T &valueMin, int limit = -1,int offset = 0, MDLabel orderLabel = MDL_OBJID)
201  :MDValueRelational(label, valueMin, GE, limit, offset, orderLabel)
202  {}
203 }
204 ;//end of class MDValueGE
205 
210 {
211 public:
212  template <class T>
213  MDValueGT(MDLabel label, const T &valueMin, int limit = -1,int offset = 0, MDLabel orderLabel = MDL_OBJID)
214  :MDValueRelational(label, valueMin, GT, limit, offset, orderLabel)
215  {}
216 }
217 ;//end of class MDValueGT
218 
223 {
224 public:
225  template <class T>
226  MDValueLE(MDLabel label, const T &valueMax, int limit = -1,int offset = 0, MDLabel orderLabel = MDL_OBJID)
227  :MDValueRelational(label, valueMax, LE, limit, offset, orderLabel)
228  {}
229 }
230 ;//end of class MDValueLE
231 
236 {
237 public:
238  template <class T>
239  MDValueLT(MDLabel label, const T &valueMax, int limit = -1,int offset = 0, MDLabel orderLabel = MDL_OBJID)
240  :MDValueRelational(label, valueMax, LT, limit, offset, orderLabel)
241  {}
242 }
243 ;//end of class MDValueLT
244 
252 class MDValueRange: public MDQuery
253 {
254  MDValueRelational *query1, *query2;
255 public:
257  {
258  query1=query2=NULL;
259  }
260  template <class T>
261  MDValueRange(MDLabel label, const T &valueMin, const T &valueMax,
263  {
264  query1 = new MDValueRelational(label, valueMin, GE);
265  query2 = new MDValueRelational(label, valueMax, LE);
266  }
267 
268  MDValueRange(const MDObject &o1, const MDObject &o2,
270  {
271  if (o1.label != o2.label)
272  REPORT_ERROR(ERR_VALUE_INCORRECT, "Labels should be the same");
273  query1 = new MDValueRelational(o1, GE);
274  query2 = new MDValueRelational(o2, LE);
275 
276  }
277  virtual String queryStringFunc() const;
278 
280  {
281  delete query1;
282  delete query2;
283  }
284 }
285 ;//end of class MDValueRange
286 
294 class MDExpression: public MDQuery
295 {
296  String sExpression;
297 public:
299  {
300  sExpression = " 1=1 ";
301  }
302  MDExpression(String _sExpression,
303  int limit = -1,
304  int offset = 0,
306  {
307  sExpression=_sExpression;
308  }
309 
310  virtual String queryStringFunc() const
311  {
312  return sExpression;
313  }
314 
315 }
316 ;//end of class MDExpression
317 
334 class MDMultiQuery: public MDQuery
335 {
336 public:
337  std::vector<const MDQuery*> queries;
338  std::vector<String> operations;
339 
341  {
342  clear();
343  }
344  void addAndQuery(MDQuery &query)
345  {
346  queries.emplace_back(&query);
347  operations.emplace_back("AND");
348  }
349  void addOrQuery(MDQuery &query)
350  {
351  queries.emplace_back(&query);
352  operations.emplace_back("OR");
353  }
354 
355  void clear()
356  {
357  queries.clear();
358  operations.clear();
359  }
360 
361  virtual String queryStringFunc() const;
362 
363 }
364 ;//end of class MDMultiQuery
365 
366 #endif
object id (int), NOTE: This label is special and shouldn&#39;t be used
MDValueLT(MDLabel label, const T &valueMax, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
std::vector< String > operations
String whereString() const
MDValueLE(MDLabel label, const T &valueMax, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
Not equal.
Less than.
virtual String queryStringFunc() const
void addAndQuery(MDQuery &query)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
int limit
If distint of -1 the results will be limited to this value.
MDLabel orderLabel
Label to which apply sort of the results.
MDValueRelational(const MDObject &value, RelationalOp op, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
MDValueRange(MDLabel label, const T &valueMin, const T &valueMax, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
int offset
If distint of 0, offset elements will be discarded.
MDValueRelational(MDLabel label, const T &value, RelationalOp op, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
MDValueNE(MDLabel label, const T &value, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
String opString() const
MDLabel label
void setValue(T &value)
String orderByString() const
MDExpression(String _sExpression, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
Equal.
Incorrect argument received.
Definition: xmipp_error.h:113
virtual ~MDQuery()
Greater equal.
Greater than.
String limitString() const
String toString(bool withFormat=false, bool isSql=false) const
MDMultiQuery(int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
virtual String queryStringFunc() const
MDValueRange(const MDObject &o1, const MDObject &o2, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
virtual String queryStringFunc() const
std::string String
Definition: xmipp_strings.h:34
RelationalOp
MDQuery(int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID, bool asc=true)
MDValueGT(MDLabel label, const T &valueMin, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
MDValueGE(MDLabel label, const T &valueMin, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
static String label2Str(const MDLabel &label)
Less equal.
Incorrect value received.
Definition: xmipp_error.h:195
void addOrQuery(MDQuery &query)
MDLabel
MDValueEQ(MDLabel label, const T &value, int limit=-1, int offset=0, MDLabel orderLabel=MDL_OBJID)
std::vector< const MDQuery * > queries