TkN 2.1
Toolkit for Nuclei
tkdb_table.h
1/********************************************************************************
2 * Copyright (c) : Université de Lyon 1, CNRS/IN2P3, UMR5822, *
3 * IP2I, F-69622 Villeurbanne Cedex, France *
4 * Normandie Université, ENSICAEN, UNICAEN, CNRS/IN2P3, *
5 * LPC Caen, F-14000 Caen, France *
6 * Contibutor(s) : *
7 * Jérémie Dudouet jeremie.dudouet@cnrs.fr [2020] *
8 * Diego Gruyer diego.gruyer@cnrs.fr [2020] *
9 * *
10 * This software is governed by the CeCILL-B license under French law and *
11 * abiding by the rules of distribution of free software. You can use, *
12 * modify and/ or redistribute the software under the terms of the *
13 * CeCILL-B license as circulated by CEA, CNRS and INRIA at the following *
14 * URL \"http://www.cecill.info\". *
15 * *
16 * As a counterpart to the access to the source code and rights to copy, *
17 * modify and redistribute granted by the license, users are provided *
18 * only with a limited warranty and the software's author, the holder of *
19 * the economic rights, and the successive licensors have only limited *
20 * liability. *
21 * *
22 * In this respect, the user's attention is drawn to the risks associated *
23 * with loading, using, modifying and/or developing or reproducing the *
24 * software by the user in light of its specific status of free software, *
25 * that may mean that it is complicated to manipulate, and that also *
26 * therefore means that it is reserved for developers and experienced *
27 * professionals having in-depth computer knowledge. Users are therefore *
28 * encouraged to load and test the software's suitability as regards *
29 * their requirements in conditions enabling the security of their *
30 * systems and/or data to be ensured and, more generally, to use and *
31 * operate it in the same conditions as regards security. *
32 * *
33 * The fact that you are presently reading this means that you have had *
34 * knowledge of the CeCILL-B license and that you accept its terms. *
35 ********************************************************************************/
36
37#ifndef tkdb_table_H
38#define tkdb_table_H
39
40#include <map>
41#include <iostream>
42
43#include "sqlite3.h"
44#include "tkstring.h"
45#include "tkdb_column.h"
46
47namespace tkn {
48
49typedef std::map<tkstring,tkdb_column> row;
50
52{
53
54 /* data types in sqlite3
55#define SQLITE_INTEGER 1
56#define SQLITE_FLOAT 2
57#define SQLITE_BLOB 4
58#define SQLITE_NULL 5
59#ifdef SQLITE_TEXT
60# undef SQLITE_TEXT
61#else
62# define SQLITE_TEXT 3
63#endif
64#define SQLITE3_TEXT 3
65 */
66
67public:
68
73 // never ever ever change the order of the fields
74 double value = 0.;
75 double err = 0.;
76 double err_low = 0.;
77 double err_high = 0.;
80 bool filled = false;
81
82 void clear() {
83 value = 0.;
84 err = 0.;
85 err_low = 0.;
86 err_high = 0.;
87 unit = "";
88 info = "";
89 filled = false;
90 }
91
92 void print() {
93 std::cout<<"value : "<<value<<std::endl;
94 std::cout<<"unit : "<<unit<<std::endl;
95 std::cout<<"err : "<<err<<std::endl;
96 std::cout<<"err_low : "<<err_low<<std::endl;
97 std::cout<<"err_high: "<<err_high<<std::endl;
98 std::cout<<"info : "<<info<<std::endl;
99 }
100 };
101
102protected:
103 tkstring fName;
104 sqlite3* fDataBase{}; // pointer to the sql database owning the table
105 row fColumns; // map of db_columns ('name', column)
106 std::vector<tkstring> fOrderedColumnNames; // column name in insertion order
107 sqlite3_stmt* fSQL_stmt{}; // sql statement to select rows and loop over them (begin() next() end())
108 bool fReading = false;
109 tkstring fConstraint;
110
111 public:
112 tkdb_table(const char* _name, sqlite3 *_db);
114 virtual ~tkdb_table();
115
116 tkstring get_name(){return fName;}
117
118 tkdb_column &operator[](const char* _column) {
119 return fColumns[_column];
120 }
121
122 void print(const tkstring &_opt="");
123 void print_columns(const tkstring &_opt="");
124 void print_rows(const char* _selection, const char *_properties="*");
125
126 void begin(tkstring _condition, tkstring _selection="*");
127 bool next();
128 void end();
129
130 void load();
131 void add_column(const char *_colname, const char *_coltype);
132 void add_constraint(const char *_constraint);
133 bool has_column(const tkstring &_col_name);
134 void reset_column_values();
135 void push_row();
136 void update_row(const tkstring &_where);
137 void write_to_database();
138 void push_dummy_row();
139 row & get_columns(){return fColumns;}
140
141 void read_measure(measure_data_struct &_struct, const tkstring &_col_base_name);
142
143private:
144 int exec_sql(const char *_cmd);
145
146#ifdef HAS_ROOT
148 ClassDef(tkdb_table,0);
149#endif
150
151};
152}
153#endif
Simple structure to store a table column.
Definition: tkdb_column.h:49
Representaiton of a sqlite data table.
Definition: tkdb_table.h:52
void read_measure(measure_data_struct &_struct, const tkstring &_col_base_name)
Definition: tkdb_table.cpp:304
tkdb_column & operator[](const char *_column)
Definition: tkdb_table.h:118
void begin(tkstring _condition, tkstring _selection="*")
Definition: tkdb_table.cpp:121
bool has_column(const tkstring &_col_name)
Definition: tkdb_table.cpp:209
row & get_columns()
Definition: tkdb_table.h:139
void update_row(const tkstring &_where)
Definition: tkdb_table.cpp:242
void load()
resets column values and ends the select statement
Definition: tkdb_table.cpp:62
bool next()
executes the select statement
Definition: tkdb_table.cpp:140
void print_columns(const tkstring &_opt="")
Definition: tkdb_table.cpp:88
void push_dummy_row()
Definition: tkdb_table.cpp:284
tkstring get_name()
Definition: tkdb_table.h:116
void add_constraint(const char *_constraint)
Definition: tkdb_table.cpp:203
void write_to_database()
Definition: tkdb_table.cpp:265
void print_rows(const char *_selection, const char *_properties="*")
Definition: tkdb_table.cpp:100
void end()
reads the next entry coresponding to the select statement and fills columns
Definition: tkdb_table.cpp:163
void print(const tkstring &_opt="")
Definition: tkdb_table.cpp:82
void add_column(const char *_colname, const char *_coltype)
Definition: tkdb_table.cpp:195
void reset_column_values()
Definition: tkdb_table.cpp:214
virtual ~tkdb_table()
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition: tkstring.h:54
Definition: tklog.cpp:39
std::map< tkstring, tkdb_column > row
Definition: tkdb_table.h:49
data structure used to fill a tkmeasure object from the sqlite database
Definition: tkdb_table.h:72