Xmipp  v3.23.11-Nereus
metadata_sql.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 /* SQL helpers for MetaDataDb */
27 
28 #ifndef CORE_METADATASQL_H
29 #define CORE_METADATASQL_H
30 
31 #include "xmipp_strings.h"
32 #include "metadata_label.h"
33 #include "xmipp_error.h"
35 #include "metadata_static.h"
36 #include "metadata_query.h"
37 
38 class MDSqlStaticInit;
39 class MDQuery;
40 class MetaDataDb;
41 class MDCache;
42 class FileName;
43 class sqlite3_stmt;
44 class sqlite3_context;
45 class sqlite3_value;
46 class sqlite3;
47 
52 //#include "metadata.h"
53 
54 /* return number of tables from a metadata file saved as sqlite */
55 int getBlocksInMetaDataFileDB(const FileName &inFile, StringVector& blockList);
56 
57 /*support for the REGEXP operator in sqlite*/
58 void sqlite_regexp(sqlite3_context* context, int argc, sqlite3_value** values);
59 
63 class MDSql
64 {
65 public:
66  static void dumpToFile(const FileName &fileName);
67  static void sqlTimeOut(int miliSeconds);
68 
85  static bool activateMathExtensions(void);
86 
87  /* activate regular expressions in sql */
88  static bool activateRegExtensions(void);
89 
100  bool deactivateThreadMuting(void);
103  bool activateThreadMuting(void);
104 
105 private:
109  void copyTableToFileDB(const FileName blockname, const FileName &fileName);
110 
114  void copyTableFromFileDB(const FileName blockname,
115  const FileName filename,
116  const std::vector<MDLabel> *desiredLabels,
117  const size_t maxRows=0
118  );
122  bool createMd();
123 
127  bool clearMd();
128 
129  size_t getObjId();
130 
133  size_t addRow();
134 
137  bool addColumn(MDLabel column);
138 
142  bool renameColumn(const std::vector<MDLabel> &oldLabel, const std::vector<MDLabel> &newlabel);
143 
146  // T is either "const MDObject*" or "MDObject*"
147  template <typename T>
148  bool setObjectValues(int id, const std::vector<T> &columnValues, const std::vector<MDLabel> *desiredLabels=NULL);
149 
152  bool setObjectValue(const int objId, const MDObject &value);
153 
156  bool setObjectValue(const MDObject &value);
157 
160  bool getObjectsValues(const std::vector<MDLabel> &labels, std::vector<MDObject> &values);
161 
164  bool getObjectValue(const int objId, MDObject &value);
165 
172  void selectObjects(std::vector<size_t> &objectsOut, const MDQuery *queryPtr = NULL);
173 
177  size_t size(void);
181  size_t deleteObjects(const MDQuery *queryPtr = NULL);
182 
186  size_t copyObjects(MDSql * sqlOut,
187  const MDQuery *queryPtr = NULL) const;
188  size_t copyObjects(MetaDataDb * mdPtrOut,
189  const MDQuery *queryPtr = NULL) const;
190 
193  void aggregateMd(MetaDataDb *mdPtrOut,
194  const std::vector<AggregateOperation> &operations,
195  const std::vector<MDLabel> &operateLabel);
196 
199  void aggregateMdGroupBy(MetaDataDb *mdPtrOut,
200  const AggregateOperation operation,
201  const std::vector<MDLabel> &groupByLabels ,
202  const MDLabel operateLabel,
203  const MDLabel resultLabel);
204 
209  double aggregateSingleDouble(const AggregateOperation operation,
210  MDLabel operateLabel);
211 
212 
217  size_t aggregateSingleSizeT(const AggregateOperation operation,
218  MDLabel operateLabel);
219 
223  void indexModify(const std::vector<MDLabel> &columns, bool create=true);
224 
227  size_t firstRow();
228  size_t lastRow();
229  size_t nextRow(size_t currentRow);
230  size_t previousRow(size_t currentRow);
231 
232  int columnMaxLength(MDLabel column);
233 
235  void setOperate(MetaDataDb *mdPtrOut, const std::vector<MDLabel> &columns, SetOperation operation);
236  void setOperate(const MetaDataDb *mdInLeft, const MetaDataDb *mdInRight, const std::vector<MDLabel> &columnsLeft,
237  const std::vector<MDLabel> &columnsRight, SetOperation operation);
239  bool operate(const String &expression);
240 
242  // FIXME this should not be necessary, as sqlite is build with multi-thread support. However, multiple access
243  // to database from multiple threads causes application crash
244  bool beThreadSafe;
245 
246 
251  MDSql(MetaDataDb *md);
252  ~MDSql();
253 
254  static int table_counter;
255  static sqlite3 *db;
256 
257  static MDSqlStaticInit initialization; //Just for initialization
258 
260  static bool sqlBegin();
261  static void sqlEnd();
262  static bool sqlBeginTrans();
263  static bool sqlCommitTrans();
268  int getUniqueId();
269 
270  bool dropTable();
271  bool createTable(const std::vector<MDLabel> * labelsVector = NULL, bool withObjID=true);
272  bool insertValues(double a, double b);
273  bool initializeSelect( bool addWhereObjId, const std::vector<MDLabel> &labels);
274  bool initializeInsert(const std::vector<MDLabel> *labels, const std::vector<MDObject*> &values);
275  void finalizePreparedStmt(void);
276  void prepareStmt(const std::stringstream &ss, sqlite3_stmt *stmt);
277  bool execSingleStmt(const std::stringstream &ss);
278  bool execSingleStmt(sqlite3_stmt *&stmt, const std::stringstream *ss = NULL);
279  size_t execSingleIntStmt(const std::stringstream &ss);
280  double execSingleDoubleStmt(const std::stringstream &ss);
281 
282  String tableName(const int tableId) const;
283 
284  bool bindStatement( size_t id);
285  int bindValue(sqlite3_stmt *stmt, const int position, const MDObject &valueIn);
286  void extractValue(sqlite3_stmt *stmt, const int position, MDObject &valueOut);
287 
288  static char *errmsg;
289  static const char *zLeftover;
290  static int rc;
291  static sqlite3_stmt *stmt;
292 
293  static std::stringstream preparedStream; // Stream.
294  static sqlite3_stmt * preparedStmt; // SQL statement.
295 
297  int tableId;
298  MetaDataDb *myMd;
299  MDCache *myCache;
300 
301  friend class MDSqlStaticInit;
302  friend class MetaDataDb;
303  friend class MDIterator;
305  bool equals(const MDSql &op);
306 
307 }
308 ;//close class MDSql
309 
310 
313 class MDCache
314 {
315 public:
316  sqlite3_stmt *iterStmt;
317  std::map<MDLabel, sqlite3_stmt*> getValueCache;
318  std::map<MDLabel, sqlite3_stmt*> setValueCache;
319  sqlite3_stmt *addRowStmt;
320 
321  MDCache();
322  ~MDCache();
323  void clear();
324 };
325 
329 {
330 private:
332  {
333  MDSql::sqlBegin();
334  }//close constructor
335 
336  ~MDSqlStaticInit()
337  {
338  MDSql::sqlEnd();
339  }//close destructor
340 
341  friend class MDSql;
342 }
343 ;//close class MDSqlStaticInit
344 
345 #endif
std::map< MDLabel, sqlite3_stmt * > getValueCache
Definition: metadata_sql.h:317
friend class MDIterator
Definition: metadata_sql.h:303
static bool activateMathExtensions(void)
void sqlite_regexp(sqlite3_context *context, int argc, sqlite3_value **values)
sqlite3_stmt * addRowStmt
Definition: metadata_sql.h:319
bool deactivateThreadMuting(void)
sqlite3_stmt * iterStmt
Definition: metadata_sql.h:316
int getBlocksInMetaDataFileDB(const FileName &inFile, StringVector &blockList)
std::vector< String > StringVector
Definition: xmipp_strings.h:35
static bool activateRegExtensions(void)
doublereal * b
static void sqlTimeOut(int miliSeconds)
static void dumpToFile(const FileName &fileName)
friend class MDSqlStaticInit
Definition: metadata_sql.h:301
bool activateThreadMuting(void)
std::string String
Definition: xmipp_strings.h:34
MDLabel
doublereal * a
std::map< MDLabel, sqlite3_stmt * > setValueCache
Definition: metadata_sql.h:318