KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
SQLiteDB.h
Go to the documentation of this file.
1 
4 #ifndef __SQLITEDB_H
5 #define __SQLITEDB_H
6 
7 #include "TSQLiteServer.h"
8 #include "TSQLStatement.h"
9 
10 #include "KVConfig.h"
11 #include <utility>
12 #include <iostream>
13 #ifdef WITH_CPP11
14 #include <unordered_map>
15 #endif
16 #include <map>
17 #include <KVNameValueList.h>
18 #include <KVNumberList.h>
19 #include "TGraph.h"
20 
21 namespace KVSQLite {
22 
91  namespace column_type {
92  enum types {
96  BLOB
97  };
98  }
99  namespace insert_mode {
100  enum types {
104  REPLACE
105  };
106  }
107 
110 
111  class table;
112 
113  class column {
114 
115  friend class table;
116 
117  std::pair<TString, KVSQLite_column_type> fNameType; //name & type of column
118  TString fConstraint;//column constraint
119  int fIndex;//index of column
120  static std::map<KVSQLite::column_type::types, TString> inv_type_map;
121  KVNamedParameter fData; // data item in column
122  void* fBlob;
123  Long_t fBlobSize;// size of blob
126  TString fFKtable;//table for foreign key
127  TString fFKcolumn;//column for foreign key
128  mutable bool fIsNull;//for inserting NULL values
129 
130  void init_type_map();
131  const char* _type();
132 
134  : fNameType(name, type), fConstraint(""), fIndex(idx), fData(name),
135  fBlob(nullptr), fBlobSize(0),
136  fPrimaryKey(false), fForeignKey(false),
137  fFKtable(""), fFKcolumn(""), fIsNull(false)
138  {
139  if (!inv_type_map.size()) init_type_map();
140  }
141  public:
142  virtual ~column()
143  {
145  if (fBlob) delete[](unsigned char*)fBlob;
146  }
147 
148  const char* name() const
149  {
150  return fNameType.first;
151  }
153  {
154  return fNameType.second;
155  }
156  const char* type_name() const
157  {
158  return const_cast<column*>(this)->_type();
159  }
160  const char* get_declaration() const;
161 
162  int index() const
163  {
164  return fIndex;
165  }
166 
167  void print() const
168  {
169  std::cout << fIndex << "\t" << name() << "\t" << type_name() << "\n";
170  }
171  template<typename T>
172  void set_data(const T& x)
173  {
174  fData.Set(x);
175  fIsNull = false;
176  }
177  template <typename T>
178  const column& operator=(const T& x)
179  {
181  set_data(x);
182  return *this;
183  }
184 
185  void set_null()
186  {
187  fIsNull = true;
188  }
189  bool is_null() const
190  {
191  return fIsNull;
192  }
193 
194  template<typename T>
195  void set_binary_data(T& x)
196  {
197  fBlob = (void*)&x;
198  fBlobSize = sizeof(x);
199  fIsNull = false;
200  }
201  template<typename T>
202  void set_binary_data(T* x)
203  {
204  fBlob = (void*)x;
205  fBlobSize = sizeof(*x);
206  fIsNull = false;
207  }
208 
209  void set_data_in_statement(TSQLStatement*, int idx = -1) const;
210  void set_data_from_statement(TSQLStatement* s, int idx = -1);
211  void set_constraint(const TString& c)
212  {
221  fConstraint = c;
222  if (c == "PRIMARY KEY") fPrimaryKey = true;
223  }
224  void set_foreign_key(const TString& _table, const TString& _column);
225  void set_foreign_key(const table& _table, const column& _column);
226  bool primary_key() const
227  {
228  return fPrimaryKey;
229  }
230  bool foreign_key() const
231  {
232  return fForeignKey;
233  }
234  const KVNamedParameter& data() const
235  {
236  return fData;
237  }
238  template <typename T>
239  T get_data() const
240  {
241  return fData.Get<T>();
242  }
243  template<typename T>
244  T* binary_data() const
245  {
246  return static_cast<T*>(fBlob);
247  }
248 
249  ClassDef(column, 0) //Column in an SQLite database table
250  };
251  template<> void column::set_data(const KVNamedParameter&);
252 
253  class table {
254  TString fName;//name of table
256  mutable std::vector<KVSQLite::column> fColumns;//list of columns
257 #ifdef WITH_CPP11
258  mutable std::unordered_map<std::string, int> fColMap; //map name of column to index
259 #else
260  mutable std::map<std::string, int> fColMap; //map name of column to index
261 #endif
262  static std::map<TString, KVSQLite::column_type::types> type_map;
263  bool fTemp;//temporary table?
264 
265  void init_type_map();
266 
267  public:
268  table(const TString& Name = "")
269  : fName(Name), fInsert(KVSQLite::insert_mode::DEFAULT), fColumns(), fColMap(), fTemp(false)
270  {
271  if (!type_map.size()) init_type_map();
272  }
273  table(const TString& Name, const std::vector<KVSQLite::column>& cols)
274  : fName(Name), fInsert(KVSQLite::insert_mode::DEFAULT), fColumns(cols), fColMap(), fTemp(false)
275  {
276  if (!type_map.size()) init_type_map();
277  }
278  virtual ~table() {}
279 
280  const char* name() const
281  {
282  return fName;
283  }
284  void set_name(const TString& name)
285  {
286  fName = name;
287  }
288 
289  void show_columns() const;
291  {
306  fInsert = i;
307  }
308  const char* get_insert_command() const;
309  void set_temporary(bool temp = true)
310  {
312  fTemp = temp;
313  }
314  bool is_temporary() const
315  {
316  return fTemp;
317  }
318 
321  {
323  return add_column(KVSQLite::column(fColumns.size(), name, type));
324  }
325  column& add_column(const TString& name, const TString& type);
326  const column& add_primary_key(const TString& name);
327  const column& add_foreign_key(const TString& name, const TString& other_table, const TString& other_column);
328  const column& add_foreign_key(const TString& name, const table& other_table, const column& other_column);
330  {
331  return fColumns[i];
332  }
333  bool has_column(const TString& name) const
334  {
336  return fColMap.count(name.Data());
337  }
338 
340  {
341  if (!has_column(n)) {
342  std::cout << "Error in <KVSQLite::table::operator[const TString&]> : "
343  << n << " is not a column of table " << name() << std::endl;
344  return fColumns[0];
345  }
346  return fColumns[fColMap[n.Data()]];
347  }
348  const KVSQLite::column& operator[](const TString& n) const
349  {
350  if (!has_column(n)) {
351  std::cout << "Error in <KVSQLite::table::operator[const TString&]> : "
352  << n << " is not a column of table " << name() << std::endl;
353  return fColumns[0];
354  }
355  return fColumns[fColMap[n.Data()]];
356  }
357 
358  void print() const
359  {
360  std::cout << name() << "\n";
361  for (std::vector<KVSQLite::column>::const_iterator it = fColumns.begin(); it != fColumns.end(); ++it) it->print();
362  }
363  int number_of_columns() const
364  {
365  return fColumns.size();
366  }
367  int check_columns(const KVNameValueList&);
368  void prepare_data(const KVNameValueList&, const KVNamedParameter* = nullptr);
369  void set_all_columns_null();
370 
371  ClassDef(table, 0) //Table in an SQLite database
372  };
373 
374  class database {
375  unique_ptr<TSQLiteServer> fDBserv; //connection to database
376 #ifdef WITH_CPP11
377  mutable std::unordered_map<std::string, KVSQLite::table> fTables; //map of tables in database
378 #else
379  mutable std::map<std::string, KVSQLite::table> fTables; //map of tables in database
380 #endif
381  mutable unique_ptr<TSQLStatement> fSQLstmt; //used for bulk operations
382  mutable KVSQLite::table* fBulkTable; //pointer to table currently used with fSQLstmt
383  mutable bool fInserting;
384  mutable bool fSelecting;
385  mutable bool fEmptyResultSet;
386  bool fIsValid;
388 
389  void PrintResults(TSQLResult* tabent, int column_width = 20) const;
390  unique_ptr<TSQLResult> SelectRowsFromTable(
391  const TString& table,
392  const TString& columns = "*",
393  const TString& condition = "") const;
394 
395  void read_table_infos();
396 
397  public:
398  database() : fDBserv(nullptr), fTables(), fSQLstmt(nullptr), fBulkTable(nullptr), fInserting(false), fSelecting(false), fIsValid(false) {}
399  database(const TString& dbfile) : fDBserv(nullptr), fTables(), fSQLstmt(nullptr), fBulkTable(nullptr), fInserting(false), fSelecting(false), fIsValid(false)
400  {
401  open(dbfile);
402  }
403  database(const database& db) : fDBserv(nullptr), fTables(), fSQLstmt(nullptr), fBulkTable(nullptr), fInserting(false), fSelecting(false), fIsValid(false)
404  {
410  if (db.good()) open(db.fDBserv->GetDB());
411  }
413  {
414  if (&db != this && db.good()) open(db.fDBserv->GetDB());
415  return *this;
416  }
417  void show_tables() const;
419  {
420  return fTables.size();
421  }
422  virtual ~database() {}
423  bool is_inserting() const
424  {
428  return fInserting;
429  }
430 
431  void open(const TString& dbfile);
432  void close()
433  {
434  fDBserv->Close();
435  fTables.clear();
436  }
437  bool good() const
438  {
439  return fIsValid;
440  }
441 
442  bool is_open() const
443  {
444  return fDBserv->IsConnected();
445  }
446 
447  void Dump() const;
448 
449  void add_table(KVSQLite::table&);
450  bool has_table(const TString& table)
451  {
453  return fTables.count(table.Data());
454  }
455 
457  {
458  if (!fTables.count(name.Data())) {
459  std::cout << "Error in <KVSQLite::database::operator[const TString&]> : "
460  << name << " is not a table of database" << std::endl;
461  return fTables.begin()->second;
462  }
463  return fTables[name.Data()];
464  }
465  const KVSQLite::table& operator[](const TString& name) const
466  {
467  if (!fTables.count(name.Data())) {
468  std::cout << "Error in <KVSQLite::database::operator[const TString&]> : "
469  << name << " is not a table of database" << std::endl;
470  return fTables.begin()->second;
471  }
472  return fTables[name.Data()];
473  }
474 
475  bool prepare_data_insertion(const TString&);
476  void insert_data_row();
477  void end_data_insertion();
478 
479  bool select_data(const TString& table, const TString& columns = "*", const TString& selection = "",
480  bool distinct = false, const TString& anything_else = "") const;
481  bool get_next_result() const;
483  const TString& selection = "", const TString& anything_else = "");
485  const TString& selection = "", const TString& anything_else = "");
486  KVNameValueList get_name_value_list(const TString& table, const TString& name_column, const TString& value_column,
487  const TString& selection = "", const TString& anything_else = "");
488  TGraph* create_graph(const TString& tablename, const TString& Xcolumn, const TString& Ycolumn, const TString& selection = "");
489 
490  void clear_table(const TString& name);
491 
492  int count(const TString& table, const TString& column = "*", const TString& selection = "", bool distinct = false) const;
493  bool update(const TString& table, const TString& columns, const TString& selection = "");
494  void delete_data(const TString& table, const TString& selection = "");
495 
496  column& add_column(const TString& table, const TString& name, const TString& type);
497  void add_missing_columns(const TString& table, const KVNameValueList& l);
498 
499  void copy_table_data(const TString& source, const TString& destination, const TString& columns = "*", const TString& selection = "");
500  void print_selection(const TString& table, const TString& columns, const TString& condition, int column_width = 20) const;
501 
502  ClassDef(database, 0) //Interface to ROOT SQLite database backend
503  };
504 }
505 
506 #endif
long Long_t
#define c(i)
#define ClassDef(name, id)
int type
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
A generic named parameter storing values of different types.
void Set(const char *, const char *)
Strings used to represent a set of ranges of values.
Definition: KVNumberList.h:83
Long_t fBlobSize
binary data
Definition: SQLiteDB.h:123
void init_type_map()
Definition: SQLiteDB.cpp:873
bool primary_key() const
Definition: SQLiteDB.h:226
bool is_null() const
Definition: SQLiteDB.h:189
void set_null()
Definition: SQLiteDB.h:185
void set_binary_data(T &x)
Definition: SQLiteDB.h:195
const char * _type()
Definition: SQLiteDB.cpp:885
const char * type_name() const
Definition: SQLiteDB.h:156
std::pair< TString, KVSQLite_column_type > fNameType
Definition: SQLiteDB.h:117
const column & operator=(const T &x)
Definition: SQLiteDB.h:178
static std::map< KVSQLite::column_type::types, TString > inv_type_map
Definition: SQLiteDB.h:120
void set_data_in_statement(TSQLStatement *, int idx=-1) const
Definition: SQLiteDB.cpp:908
const char * name() const
Definition: SQLiteDB.h:148
void set_foreign_key(const TString &_table, const TString &_column)
Definition: SQLiteDB.cpp:995
TString fFKcolumn
Definition: SQLiteDB.h:127
KVNamedParameter fData
Definition: SQLiteDB.h:121
const char * get_declaration() const
return declaration for column, including type & constraint
Definition: SQLiteDB.cpp:334
T get_data() const
Definition: SQLiteDB.h:239
KVSQLite_column_type type() const
Definition: SQLiteDB.h:152
int index() const
Definition: SQLiteDB.h:162
TString fFKtable
Definition: SQLiteDB.h:126
void set_binary_data(T *x)
Definition: SQLiteDB.h:202
T * binary_data() const
Definition: SQLiteDB.h:244
TString fConstraint
Definition: SQLiteDB.h:118
const KVNamedParameter & data() const
Definition: SQLiteDB.h:234
void set_data(const T &x)
Definition: SQLiteDB.h:172
bool foreign_key() const
Definition: SQLiteDB.h:230
void set_data_from_statement(TSQLStatement *s, int idx=-1)
Definition: SQLiteDB.cpp:951
void print() const
Definition: SQLiteDB.h:167
virtual ~column()
Definition: SQLiteDB.h:142
column(int idx, const TString &name, KVSQLite_column_type type)
Definition: SQLiteDB.h:133
void set_constraint(const TString &c)
Definition: SQLiteDB.h:211
Interface to ROOT SQLite database backend ,.
Definition: SQLiteDB.h:374
int get_number_of_tables() const
Definition: SQLiteDB.h:418
void copy_table_data(const TString &source, const TString &destination, const TString &columns="*", const TString &selection="")
Definition: SQLiteDB.cpp:844
void add_table(KVSQLite::table &)
Definition: SQLiteDB.cpp:193
int count(const TString &table, const TString &column="*", const TString &selection="", bool distinct=false) const
Definition: SQLiteDB.cpp:680
database(const TString &dbfile)
Definition: SQLiteDB.h:399
KVSQLite::table * fBulkTable
Definition: SQLiteDB.h:382
unique_ptr< TSQLiteServer > fDBserv
Definition: SQLiteDB.h:375
TGraph * create_graph(const TString &tablename, const TString &Xcolumn, const TString &Ycolumn, const TString &selection="")
Definition: SQLiteDB.cpp:640
unique_ptr< TSQLStatement > fSQLstmt
Definition: SQLiteDB.h:381
bool update(const TString &table, const TString &columns, const TString &selection="")
Definition: SQLiteDB.cpp:717
void delete_data(const TString &table, const TString &selection="")
Definition: SQLiteDB.cpp:775
void show_tables() const
print list of tables
Definition: SQLiteDB.cpp:77
const KVSQLite::table & operator[](const TString &name) const
Definition: SQLiteDB.h:465
column & add_column(const TString &table, const TString &name, const TString &type)
Definition: SQLiteDB.cpp:796
bool select_data(const TString &table, const TString &columns="*", const TString &selection="", bool distinct=false, const TString &anything_else="") const
Definition: SQLiteDB.cpp:441
void print_selection(const TString &table, const TString &columns, const TString &condition, int column_width=20) const
Print on stdout contents of database.
Definition: SQLiteDB.cpp:166
bool good() const
Definition: SQLiteDB.h:437
unique_ptr< TSQLResult > SelectRowsFromTable(const TString &table, const TString &columns="*", const TString &condition="") const
Definition: SQLiteDB.cpp:33
database & operator=(const database &db)
Definition: SQLiteDB.h:412
KVSQLite::table & operator[](const TString &name)
Definition: SQLiteDB.h:456
bool is_inserting() const
Definition: SQLiteDB.h:423
void insert_data_row()
Definition: SQLiteDB.cpp:365
void open(const TString &dbfile)
Definition: SQLiteDB.cpp:97
bool has_table(const TString &table)
Definition: SQLiteDB.h:450
void end_data_insertion()
Definition: SQLiteDB.cpp:405
KVNameValueList get_name_value_list(const TString &table, const TString &name_column, const TString &value_column, const TString &selection="", const TString &anything_else="")
Definition: SQLiteDB.cpp:616
virtual ~database()
Definition: SQLiteDB.h:422
bool get_next_result() const
Definition: SQLiteDB.cpp:526
TString get_string_list(const TString &table, const TString &column, const TString &selection="", const TString &anything_else="")
Definition: SQLiteDB.cpp:592
void Dump() const
Print on stdout contents of database.
Definition: SQLiteDB.cpp:145
std::unordered_map< std::string, KVSQLite::table > fTables
Definition: SQLiteDB.h:377
database(const database &db)
Definition: SQLiteDB.h:403
void PrintResults(TSQLResult *tabent, int column_width=20) const
Definition: SQLiteDB.cpp:117
bool prepare_data_insertion(const TString &)
Definition: SQLiteDB.cpp:243
TString fSelectedColumns
Definition: SQLiteDB.h:387
void clear_table(const TString &name)
Delete all data from table.
Definition: SQLiteDB.cpp:664
void read_table_infos()
initialise map of database tables from existing database
Definition: SQLiteDB.cpp:51
bool is_open() const
Definition: SQLiteDB.h:442
void add_missing_columns(const TString &table, const KVNameValueList &l)
Definition: SQLiteDB.cpp:811
KVNumberList get_integer_list(const TString &table, const TString &column, const TString &selection="", const TString &anything_else="")
Definition: SQLiteDB.cpp:571
static std::map< TString, KVSQLite::column_type::types > type_map
Definition: SQLiteDB.h:262
const char * name() const
Definition: SQLiteDB.h:280
bool is_temporary() const
Definition: SQLiteDB.h:314
KVSQLite::column & operator[](int i)
Definition: SQLiteDB.h:329
void print() const
Definition: SQLiteDB.h:358
const column & add_primary_key(const TString &name)
Definition: SQLiteDB.cpp:1092
void set_all_columns_null()
set the value of all columns in the table to NULL
Definition: SQLiteDB.cpp:1194
std::vector< KVSQLite::column > fColumns
Definition: SQLiteDB.h:256
void set_temporary(bool temp=true)
Definition: SQLiteDB.h:309
void show_columns() const
print list of columns
Definition: SQLiteDB.cpp:1036
void init_type_map()
Definition: SQLiteDB.cpp:1023
table(const TString &Name, const std::vector< KVSQLite::column > &cols)
Definition: SQLiteDB.h:273
table(const TString &Name="")
Definition: SQLiteDB.h:268
column & add_column(const KVSQLite::column &c)
Definition: SQLiteDB.cpp:1056
void set_name(const TString &name)
Definition: SQLiteDB.h:284
TString fName
Definition: SQLiteDB.h:254
KVSQLite::column & operator[](const TString &n)
Definition: SQLiteDB.h:339
virtual ~table()
Definition: SQLiteDB.h:278
int check_columns(const KVNameValueList &)
Definition: SQLiteDB.cpp:1149
void set_insert_mode(KVSQLite_insert_mode i)
Definition: SQLiteDB.h:290
int number_of_columns() const
Definition: SQLiteDB.h:363
const KVSQLite::column & operator[](const TString &n) const
Definition: SQLiteDB.h:348
bool has_column(const TString &name) const
Definition: SQLiteDB.h:333
column & add_column(const TString &name, KVSQLite_column_type type)
Definition: SQLiteDB.h:320
void prepare_data(const KVNameValueList &, const KVNamedParameter *=nullptr)
Definition: SQLiteDB.cpp:1172
const char * get_insert_command() const
Definition: SQLiteDB.cpp:310
KVSQLite_insert_mode fInsert
Definition: SQLiteDB.h:255
const column & add_foreign_key(const TString &name, const TString &other_table, const TString &other_column)
Definition: SQLiteDB.cpp:1114
std::unordered_map< std::string, int > fColMap
Definition: SQLiteDB.h:258
const char * Data() const
RooCmdArg Name(const char *name)
Double_t x[n]
const Int_t n
KVSQLite::column_type::types KVSQLite_column_type
Definition: SQLiteDB.h:109
KVSQLite::insert_mode::types KVSQLite_insert_mode
Definition: SQLiteDB.h:108
const long double s
Definition: KVUnits.h:94
double T(double x)