KaliVeda
Toolkit for HIC analysis
Loading...
Searching...
No Matches
SQLiteDB.h
1
3
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#include <unordered_map>
14#include <list>
15#include <map>
16#include <KVNameValueList.h>
17#include <KVNumberList.h>
18#include "TGraph.h"
19
20namespace KVSQLite {
21
91 namespace column_type {
98 }
99 namespace insert_mode {
106 }
107
110
111 class table;
112
113 class column {
120 friend class table;
121
122 std::pair<TString, KVSQLite_column_type> fNameType; //name & type of column
123 TString fConstraint;//column constraint
124 int fIndex;//index of column
125 static std::map<KVSQLite::column_type::types, TString> inv_type_map;
126 mutable KVNamedParameter fData; // data item in column
127 mutable void* fBlob;
128 mutable Long_t fBlobSize;// size of blob
131 TString fFKtable;//table for foreign key
132 TString fFKcolumn;//column for foreign key
133 mutable bool fIsNull;//for inserting NULL values
134 TString fTable;// name of parent table
135
136 void init_type_map();
137 const char* _type();
138
140 : fNameType(name, type), fConstraint(""), fIndex(idx), fData(name),
141 fBlob(nullptr), fBlobSize(0),
142 fPrimaryKey(false), fForeignKey(false),
143 fFKtable(""), fFKcolumn(""), fIsNull(false)
144 {
145 if (!inv_type_map.size()) init_type_map();
146 }
147 public:
148 virtual ~column()
149 {
151 if (fBlob) delete[](unsigned char*)fBlob;
152 }
153
154 const char* name() const
155 {
156 return fNameType.first;
157 }
159 {
160 return fNameType.second;
161 }
162 const char* type_name() const
163 {
164 return const_cast<column*>(this)->_type();
165 }
166 const char* get_declaration() const;
167 const char* get_table() const
168 {
170 return fTable;
171 }
172 void set_table(const TString& name)
173 {
175 fTable = name;
176 }
177 int index() const
178 {
179 return fIndex;
180 }
181
182 void print() const
183 {
184 std::cout << fIndex << "\t" << name() << "\t" << type_name() << "\n";
185 }
186 template<typename T>
187 void set_data(const T& x)
188 {
189 fData.Set(x);
190 fIsNull = false;
191 }
192 template <typename T>
193 const column& operator=(const T& x)
194 {
196 set_data(x);
197 return *this;
198 }
199
200 void set_null()
201 {
202 fIsNull = true;
203 }
204 bool is_null() const
205 {
206 return fIsNull;
207 }
208
209 template<typename T>
211 {
212 fBlob = (void*)&x;
213 fBlobSize = sizeof(x);
214 fIsNull = false;
215 }
216 template<typename T>
218 {
219 fBlob = (void*)x;
220 fBlobSize = sizeof(*x);
221 fIsNull = false;
222 }
223
224 void set_data_in_statement(TSQLStatement*, int idx = -1) const;
225 void set_data_from_statement(TSQLStatement* s, int idx = -1) const;
226 void set_constraint(const TString& c)
227 {
236 fConstraint = c;
237 if (c == "PRIMARY KEY") fPrimaryKey = true;
238 }
239 void set_foreign_key(const TString& _table, const TString& _column);
240 void set_foreign_key(const table& _table, const column& _column);
241 bool primary_key() const
242 {
243 return fPrimaryKey;
244 }
245 bool foreign_key() const
246 {
247 return fForeignKey;
248 }
249 const KVNamedParameter& data() const
250 {
251 return fData;
252 }
253 template <typename T>
254 T get_data() const
255 {
256 return fData.Get<T>();
257 }
258 template<typename T>
259 T* binary_data() const
260 {
261 return static_cast<T*>(fBlob);
262 }
263
264 ClassDef(column, 0) //Column in an SQLite database table
265 };
266 template<> void column::set_data(const KVNamedParameter&);
267
268 class table {
275 TString fName;//name of table
277 mutable std::vector<KVSQLite::column> fColumns;//list of columns
278 mutable std::unordered_map<std::string, int> fColMap; //map name of column to index
279 static std::map<TString, KVSQLite::column_type::types> type_map;
280 bool fTemp;//temporary table?
281
282 void init_type_map();
283
284 public:
285 table(const TString& Name = "")
286 : fName(Name), fInsert(KVSQLite::insert_mode::DEFAULT), fColumns(), fColMap(), fTemp(false)
287 {
288 if (!type_map.size()) init_type_map();
289 }
290 table(const TString& Name, const std::vector<KVSQLite::column>& cols)
291 : fName(Name), fInsert(KVSQLite::insert_mode::DEFAULT), fColumns(cols), fColMap(), fTemp(false)
292 {
293 if (!type_map.size()) init_type_map();
294 for (auto& c : fColumns) c.set_table(Name);
295 }
296 virtual ~table() {}
297
298 const char* name() const
299 {
300 return fName;
301 }
302 void set_name(const TString& name)
303 {
304 fName = name;
305 }
306
307 void show_columns() const;
309 {
324 fInsert = i;
325 }
326 const char* get_insert_command() const;
327 void set_temporary(bool temp = true)
328 {
330 fTemp = temp;
331 }
332 bool is_temporary() const
333 {
334 return fTemp;
335 }
336
343 column& add_column(const TString& name, const TString& type);
344 const column& add_primary_key(const TString& name);
345 const column& add_foreign_key(const TString& other_table, const TString& other_column);
346 const column& add_foreign_key(const table& other_table, const column& other_column);
347 const KVSQLite::column& operator[](int i) const
348 {
349 return fColumns[i];
350 }
352 {
353 return fColumns[i];
354 }
356 {
357 return fColumns[i];
358 }
359 bool has_column(const TString& name) const
360 {
362 return fColMap.count(name.Data());
363 }
364
366 {
367 return get_column(n);
368 }
369 const KVSQLite::column& operator[](const TString& n) const
370 {
371 return const_cast<table*>(this)->get_column(n);
372 }
374 {
375 if (!has_column(n)) {
376 std::cout << "Error in <KVSQLite::table::get_column(const TString&)> : "
377 << n << " is not a column of table " << name() << std::endl;
378 return fColumns[0];
379 }
380 return fColumns[fColMap[n.Data()]];
381 }
382
383 void print() const
384 {
385 std::cout << name() << "\n";
386 for (std::vector<KVSQLite::column>::const_iterator it = fColumns.begin(); it != fColumns.end(); ++it) it->print();
387 }
389 {
390 return fColumns.size();
391 }
392 int check_columns(const KVNameValueList&);
393 void prepare_data(const KVNameValueList&, const KVNamedParameter* = nullptr);
395
396 TString get_column_names(const TString& exclude = "", const TString& delim = ",") const;
397
398 ClassDef(table, 0) //Table in an SQLite database
399 };
400
401 class database {
402 std::unique_ptr<TSQLiteServer> fDBserv; //connection to database
403 mutable std::unordered_map<std::string, KVSQLite::table> fTables; //map of tables in database
404 mutable std::unique_ptr<TSQLStatement> fSQLstmt; //used for bulk operations
405 mutable std::list<const column*> fSQLstmtCols; // columns used in SQL statement
406 mutable table* fBulkTable; //pointer to table currently used with fSQLstmt
407 mutable bool fInserting;
408 mutable bool fSelecting;
409 mutable bool fEmptyResultSet;
412
413 void PrintResults(TSQLResult* tabent, int column_width = 20) const;
414 std::unique_ptr<TSQLResult> SelectRowsFromTable(
415 const TString& table,
416 const TString& columns = "*",
417 const TString& condition = "") const;
418
419 void read_table_infos();
420
421 public:
422 database() : fDBserv(nullptr), fTables(), fSQLstmt(nullptr), fBulkTable(nullptr), fInserting(false), fSelecting(false), fIsValid(false) {}
423 database(const TString& dbfile) : fDBserv(nullptr), fTables(), fSQLstmt(nullptr), fBulkTable(nullptr), fInserting(false), fSelecting(false), fIsValid(false)
424 {
425 open(dbfile);
426 }
427 database(const database& db) : fDBserv(nullptr), fTables(), fSQLstmt(nullptr), fBulkTable(nullptr), fInserting(false), fSelecting(false), fIsValid(false)
428 {
434 if (db.good()) open(db.fDBserv->GetDB());
435 }
437 {
438 if (&db != this && db.good()) open(db.fDBserv->GetDB());
439 return *this;
440 }
441 void show_tables() const;
443 {
444 return fTables.size();
445 }
446 virtual ~database()
447 {
448 close();
449 }
450 bool is_inserting() const
451 {
455 return fInserting;
456 }
457
458 void open(const TString& dbfile);
459 void close()
460 {
461 fDBserv->Close();
462 fTables.clear();
463 }
464 bool good() const
465 {
466 return fIsValid;
467 }
468
469 bool is_open() const
470 {
471 return fDBserv->IsConnected();
472 }
473
474 void Dump() const;
475
476 void add_table(const table&);
478 {
480 return fTables.count(table.Data());
481 }
482
484 {
485 return get_table(name);
486 }
487 const KVSQLite::table& operator[](const TString& name) const
488 {
489 return const_cast<database*>(this)->get_table(name);
490 }
492 {
493 if (!fTables.count(name.Data())) {
494 std::cout << "Error in <KVSQLite::database::get_table(const TString&)> : "
495 << name << " is not a table of database" << std::endl;
496 return fTables.begin()->second;
497 }
498 return fTables[name.Data()];
499 }
500
501 bool prepare_data_insertion(const TString&);
502 void insert_data_row();
503 void end_data_insertion();
504
505 bool select_data(const TString& tables, const TString& columns = "*", const TString& selection = "",
506 bool distinct = false, const TString& anything_else = "") const;
507 bool get_next_result() const;
509 const TString& selection = "", const TString& anything_else = "");
511 const TString& selection = "", const TString& anything_else = "");
512 KVNameValueList get_name_value_list(const TString& table, const TString& name_column, const TString& value_column,
513 const TString& selection = "", const TString& anything_else = "");
514 TGraph* create_graph(const TString& tablename, const TString& Xcolumn, const TString& Ycolumn, const TString& selection = "");
515
516 void clear_table(const TString& name);
517
518 int count(const TString& table, const TString& column = "*", const TString& selection = "", bool distinct = false) const;
519 bool update(const TString& table, const TString& columns, const TString& selection = "");
520 void delete_data(const TString& table, const TString& selection = "");
521
522 column& add_column(const TString& table, const TString& name, const TString& type);
523 void add_missing_columns(const TString& table, const KVNameValueList& l);
524
525 void copy_table_data(const TString& source, const TString& destination, const TString& columns = "*", const TString& selection = "");
526 void print_selection(const TString& table, const TString& columns, const TString& condition, int column_width = 20) const;
527 void print_selected_data(const TString& tables, const TString& columns = "*", const TString& selection = "", bool distinct = false, const TString& anything_else = "");
528
529 ClassDef(database, 0) //Interface to ROOT SQLite database backend
530 };
531}
532
533#endif
long Long_t
#define c(i)
#define ClassDef(name, id)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
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.
Long_t fBlobSize
binary data
Definition SQLiteDB.h:128
bool primary_key() const
Definition SQLiteDB.h:241
bool is_null() const
Definition SQLiteDB.h:204
void set_binary_data(T &x)
Definition SQLiteDB.h:210
void set_table(const TString &name)
Definition SQLiteDB.h:172
void set_data_in_statement(TSQLStatement *, int idx=-1) const
Definition SQLiteDB.cpp:967
std::pair< TString, KVSQLite_column_type > fNameType
Definition SQLiteDB.h:122
const char * _type()
Definition SQLiteDB.cpp:944
void set_foreign_key(const TString &_table, const TString &_column)
TString fFKcolumn
Definition SQLiteDB.h:132
KVNamedParameter fData
Definition SQLiteDB.h:126
const KVNamedParameter & data() const
Definition SQLiteDB.h:249
T get_data() const
Definition SQLiteDB.h:254
KVSQLite_column_type type() const
Definition SQLiteDB.h:158
int index() const
Definition SQLiteDB.h:177
const char * name() const
Definition SQLiteDB.h:154
TString fFKtable
Definition SQLiteDB.h:131
T * binary_data() const
Definition SQLiteDB.h:259
void set_binary_data(T *x)
Definition SQLiteDB.h:217
static std::map< KVSQLite::column_type::types, TString > inv_type_map
Definition SQLiteDB.h:125
TString fConstraint
Definition SQLiteDB.h:123
void set_data(const T &x)
Definition SQLiteDB.h:187
const char * get_declaration() const
return declaration for column, including type & constraint
Definition SQLiteDB.cpp:360
bool foreign_key() const
Definition SQLiteDB.h:245
void set_data_from_statement(TSQLStatement *s, int idx=-1) const
void print() const
Definition SQLiteDB.h:182
virtual ~column()
Definition SQLiteDB.h:148
column(int idx, const TString &name, KVSQLite_column_type type)
Definition SQLiteDB.h:139
const column & operator=(const T &x)
Definition SQLiteDB.h:193
void set_constraint(const TString &c)
Definition SQLiteDB.h:226
const char * get_table() const
Definition SQLiteDB.h:167
const char * type_name() const
Definition SQLiteDB.h:162
Interface to ROOT SQLite database backend.
Definition SQLiteDB.h:401
int get_number_of_tables() const
Definition SQLiteDB.h:442
void copy_table_data(const TString &source, const TString &destination, const TString &columns="*", const TString &selection="")
Definition SQLiteDB.cpp:903
std::list< const column * > fSQLstmtCols
Definition SQLiteDB.h:405
int count(const TString &table, const TString &column="*", const TString &selection="", bool distinct=false) const
Definition SQLiteDB.cpp:739
const KVSQLite::table & operator[](const TString &name) const
Definition SQLiteDB.h:487
database & operator=(const database &db)
Definition SQLiteDB.h:436
database(const TString &dbfile)
Definition SQLiteDB.h:423
TGraph * create_graph(const TString &tablename, const TString &Xcolumn, const TString &Ycolumn, const TString &selection="")
Definition SQLiteDB.cpp:699
std::unique_ptr< TSQLResult > SelectRowsFromTable(const TString &table, const TString &columns="*", const TString &condition="") const
Definition SQLiteDB.cpp:34
std::unique_ptr< TSQLStatement > fSQLstmt
Definition SQLiteDB.h:404
bool update(const TString &table, const TString &columns, const TString &selection="")
Definition SQLiteDB.cpp:776
void delete_data(const TString &table, const TString &selection="")
Definition SQLiteDB.cpp:834
void show_tables() const
print list of tables
Definition SQLiteDB.cpp:84
KVSQLite::table & get_table(const TString &name)
Definition SQLiteDB.h:491
column & add_column(const TString &table, const TString &name, const TString &type)
Definition SQLiteDB.cpp:855
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:192
KVSQLite::table & operator[](const TString &name)
Definition SQLiteDB.h:483
bool good() const
Definition SQLiteDB.h:464
std::unique_ptr< TSQLiteServer > fDBserv
Definition SQLiteDB.h:402
bool is_inserting() const
Definition SQLiteDB.h:450
void open(const TString &dbfile)
Definition SQLiteDB.cpp:110
bool has_table(const TString &table)
Definition SQLiteDB.h:477
void end_data_insertion()
Definition SQLiteDB.cpp:433
bool select_data(const TString &tables, const TString &columns="*", const TString &selection="", bool distinct=false, const TString &anything_else="") const
Definition SQLiteDB.cpp:493
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:675
virtual ~database()
Definition SQLiteDB.h:446
bool get_next_result() const
Definition SQLiteDB.cpp:595
TString get_string_list(const TString &table, const TString &column, const TString &selection="", const TString &anything_else="")
Definition SQLiteDB.cpp:651
void Dump() const
Print on stdout contents of database.
Definition SQLiteDB.cpp:171
std::unordered_map< std::string, KVSQLite::table > fTables
Definition SQLiteDB.h:403
database(const database &db)
Definition SQLiteDB.h:427
void add_table(const table &)
Definition SQLiteDB.cpp:219
void PrintResults(TSQLResult *tabent, int column_width=20) const
Definition SQLiteDB.cpp:143
bool prepare_data_insertion(const TString &)
Definition SQLiteDB.cpp:269
TString fSelectedColumns
Definition SQLiteDB.h:411
void clear_table(const TString &name)
Delete all data from table.
Definition SQLiteDB.cpp:723
void print_selected_data(const TString &tables, const TString &columns="*", const TString &selection="", bool distinct=false, const TString &anything_else="")
Print out results of a call to select_data().
Definition SQLiteDB.cpp:458
void read_table_infos()
initialise map of database tables from existing database
Definition SQLiteDB.cpp:52
bool is_open() const
Definition SQLiteDB.h:469
void add_missing_columns(const TString &table, const KVNameValueList &l)
Definition SQLiteDB.cpp:870
KVNumberList get_integer_list(const TString &table, const TString &column, const TString &selection="", const TString &anything_else="")
Definition SQLiteDB.cpp:630
bool is_temporary() const
Definition SQLiteDB.h:332
const KVSQLite::column & operator[](int i) const
Definition SQLiteDB.h:347
void print() const
Definition SQLiteDB.h:383
const column & add_foreign_key(const TString &other_table, const TString &other_column)
std::vector< KVSQLite::column > fColumns
Definition SQLiteDB.h:277
void set_temporary(bool temp=true)
Definition SQLiteDB.h:327
KVSQLite::column & operator[](int i)
Definition SQLiteDB.h:351
int check_columns(const KVNameValueList &)
const column & add_primary_key(const TString &name)
const KVSQLite::column & operator[](const TString &n) const
Definition SQLiteDB.h:369
table(const TString &Name, const std::vector< KVSQLite::column > &cols)
Definition SQLiteDB.h:290
const char * get_insert_command() const
Definition SQLiteDB.cpp:336
table(const TString &Name="")
Definition SQLiteDB.h:285
static std::map< TString, KVSQLite::column_type::types > type_map
Definition SQLiteDB.h:279
void set_name(const TString &name)
Definition SQLiteDB.h:302
const char * name() const
Definition SQLiteDB.h:298
column & add_column(const KVSQLite::column &c)
TString fName
Definition SQLiteDB.h:275
TString get_column_names(const TString &exclude="", const TString &delim=",") const
virtual ~table()
Definition SQLiteDB.h:296
column & add_column(const TString &name, KVSQLite_column_type type)
Definition SQLiteDB.h:338
void set_insert_mode(KVSQLite_insert_mode i)
Definition SQLiteDB.h:308
int number_of_columns() const
Definition SQLiteDB.h:388
KVSQLite::column & get_column(int i)
Definition SQLiteDB.h:355
bool has_column(const TString &name) const
Definition SQLiteDB.h:359
void prepare_data(const KVNameValueList &, const KVNamedParameter *=nullptr)
KVSQLite::column & operator[](const TString &n)
Definition SQLiteDB.h:365
void show_columns() const
print list of columns
KVSQLite_insert_mode fInsert
Definition SQLiteDB.h:276
void set_all_columns_null()
set the value of all columns in the table to NULL
KVSQLite::column & get_column(const TString &n)
Definition SQLiteDB.h:373
std::unordered_map< std::string, int > fColMap
Definition SQLiteDB.h:278
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
double T(double x)
const char * Name