TkN 2.7
Toolkit for Nuclei
Loading...
Searching...
No Matches
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 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#ifndef tkdb_table_H
15#define tkdb_table_H
16
17#include <map>
18#include <iostream>
19#include <array>
20#include <vector>
21
22#include "sqlite3.h"
23#include "tkstring.h"
24#include "tkdb_column.h"
25
26namespace tkn {
27
28typedef std::map<tkstring,tkdb_column> row;
29
31{
32
33 /* data types in sqlite3
34#define SQLITE_INTEGER 1
35#define SQLITE_FLOAT 2
36#define SQLITE_BLOB 4
37#define SQLITE_NULL 5
38#ifdef SQLITE_TEXT
39# undef SQLITE_TEXT
40#else
41# define SQLITE_TEXT 3
42#endif
43#define SQLITE3_TEXT 3
44 */
45
46public:
47
52 // never ever ever change the order of the fields
53 double value = 0.;
54 double err = 0.;
55 double err_low = 0.;
56 double err_high = 0.;
59 bool filled = false;
60
61 void clear() {
62 value = 0.;
63 err = 0.;
64 err_low = 0.;
65 err_high = 0.;
66 unit = "";
67 info = "";
68 filled = false;
69 }
70
71 void print() {
72 std::cout<<"value : "<<value<<std::endl;
73 std::cout<<"unit : "<<unit<<std::endl;
74 std::cout<<"err : "<<err<<std::endl;
75 std::cout<<"err_low : "<<err_low<<std::endl;
76 std::cout<<"err_high: "<<err_high<<std::endl;
77 std::cout<<"info : "<<info<<std::endl;
78 }
79 };
80
81 using measure_columns = std::array<tkdb_column *, 6>;
82
83protected:
84 tkstring fName;
85 sqlite3* fDataBase{}; // pointer to the sql database owning the table
86 row fColumns; // map of db_columns ('name', column)
87 std::vector<tkstring> fOrderedColumnNames; // column name in insertion order
88 sqlite3_stmt* fSQL_stmt{}; // sql statement to select rows and loop over them (begin() next() end())
89 sqlite3_stmt* fInsertStatement{}; // reusable prepared INSERT statement
90 bool fReading = false;
91 tkstring fConstraint;
92
93 public:
94 tkdb_table(const char* _name, sqlite3 *_db);
96 tkdb_table(const tkdb_table &_other);
97 tkdb_table &operator=(const tkdb_table &_other);
98 tkdb_table(tkdb_table &&_other) noexcept;
99 tkdb_table &operator=(tkdb_table &&_other) noexcept;
100 virtual ~tkdb_table();
101
102 tkstring get_name(){return fName;}
103
104 tkdb_column &operator[](const char* _column) {
105 return fColumns[_column];
106 }
107
108 void print(const tkstring &_opt="");
109 void print_columns(const tkstring &_opt="");
110 void print_rows(const char* _selection, const char *_properties="*");
111
112 void begin(tkstring _condition, tkstring _selection="*");
113 bool next();
114 void end();
115
116 void load();
117 void add_column(const char *_colname, const char *_coltype);
118 void add_constraint(const char *_constraint);
119 bool has_column(const tkstring &_col_name);
120 tkdb_column *get_column(const tkstring &_col_name);
121 void reset_column_values();
122 void push_row();
123 void update_row(const tkstring &_where);
124 void write_to_database();
125 void push_dummy_row();
126 row & get_columns(){return fColumns;}
127
128 void read_measure(measure_data_struct &_struct, const tkstring &_col_base_name);
129 measure_columns get_measure_columns(const tkstring &_col_base_name);
130 void read_measure(measure_data_struct &_struct, const measure_columns &_columns);
131
132private:
133 void finalize_statements();
134 bool prepare_insert_statement();
135 int exec_sql(const char *_cmd);
136
137#ifdef HAS_ROOT
139 ClassDef(tkdb_table,0);
140#endif
141
142};
143}
144#endif
Simple structure to store a table column.
Definition tkdb_column.h:26
Representaiton of a sqlite data table.
Definition tkdb_table.h:31
std::array< tkdb_column *, 6 > measure_columns
Definition tkdb_table.h:81
void read_measure(measure_data_struct &_struct, const tkstring &_col_base_name)
tkdb_column * get_column(const tkstring &_col_name)
measure_columns get_measure_columns(const tkstring &_col_base_name)
tkdb_column & operator[](const char *_column)
Definition tkdb_table.h:104
void begin(tkstring _condition, tkstring _selection="*")
bool has_column(const tkstring &_col_name)
row & get_columns()
Definition tkdb_table.h:126
void update_row(const tkstring &_where)
tkdb_table & operator=(const tkdb_table &_other)
void load()
resets column values and ends the select statement
bool next()
executes the select statement
void print_columns(const tkstring &_opt="")
tkstring get_name()
Definition tkdb_table.h:102
void add_constraint(const char *_constraint)
void write_to_database()
void print_rows(const char *_selection, const char *_properties="*")
void end()
reads the next entry coresponding to the select statement and fills columns
void print(const tkstring &_opt="")
void add_column(const char *_colname, const char *_coltype)
void reset_column_values()
virtual ~tkdb_table()
tkdb_table(const char *_name, sqlite3 *_db)
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:33
Definition tklog.cpp:16
std::map< tkstring, tkdb_column > row
Definition tkdb_table.h:28
data structure used to fill a tkmeasure object from the sqlite database
Definition tkdb_table.h:51