TkN 2.1
Toolkit for Nuclei
tkdatabase.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 database_H
38#define database_H
39
40#include "tkn_config.h"
41
42#ifdef HAS_ROOT
43#include "TClass.h"
44#endif
45
46#include <vector>
47#include <map>
48#include <mutex>
49
50#include <sqlite3.h>
51#include <tklog.h>
52#include <tkstring.h>
53#include <tkdb_table.h>
54
55namespace tkn {
56
58
59 static std::recursive_mutex& get_mutex() {
60 static std::recursive_mutex mutex;
61 return mutex;
62 }
63
64 static tkdatabase *g_database;
65
66public:
67 static tkdatabase *the_database();
68
69protected:
70 sqlite3 * fDataBase = nullptr;
71 std::map<tkstring,tkdb_table> fTables;
72 int fMode{};
73 bool fReading = false;
74 std::vector<tkstring> fViews;
75 std::map<tkstring, sqlite3_stmt*> fSQLStatement; // sql statements to select rows and loop over them (begin() next() end())
76 tkstring fName{};
77 tkstring fFullName{};
78
79public:
80 tkdatabase(bool opening_db=true);
81 virtual ~tkdatabase() = default;
82
83 void set_sql_db(sqlite3 * _sql_db){fDataBase = _sql_db;}
84 sqlite3 * get_sql_db(){return fDataBase;}
85
86 sqlite3* open(tkstring _db, tkstring _option="OPEN");
87 void close();
88 tkstring get_name(){return fName;}
89 tkstring get_full_name(){return fFullName;}
90
91 void load_tables();
92 void load_views();
93 void add_table(tkdb_table &_table);
94 bool has_table(const tkstring &_table_name);
95 bool has_view(const tkstring &_table_name);
96
97 std::vector<tkstring>& get_views(){return fViews;}
98
99 tkdb_table &new_table(tkstring _table_name);
100 void remove_table(tkstring _table_name);
101
102 tkdb_table* get_table(tkstring _table_name);
103 static int getdir (tkstring _dir, std::vector<tkstring> &_files);
104 static void list_files(tkstring _dir, tkstring _pattern);
105
106 void print(const tkstring &_properties="", const tkstring &_opt="");
107
108 tkdb_table &operator[](const char* _table) {
109 return fTables[_table];
110 }
111
112 void begin(tkstring _selection, tkstring _from, tkstring _condition="", tkstring _loop_name="default", tkstring _extra_cmd="");
113 bool next(const tkstring &_loop_name="default");
114 void end(const tkstring &_loop_name="default");
115
116 int count(const tkstring &_from, const tkstring &_condition="");
117 tkstring get_value(tkstring _selection, tkstring _from, tkstring _condition="");
118
119//private:
120 int exec_sql(const char *_cmd);
121
122#define gdatabase (tkn::tkdatabase::the_database())
123
124#ifdef HAS_ROOT
126 ClassDef(tkdatabase,0);
127#endif
128};
129}
130
131#endif
Interface to the sqlite database.
Definition: tkdatabase.h:57
int count(const tkstring &_from, const tkstring &_condition="")
resets column values and ends the select statement
Definition: tkdatabase.cpp:296
tkstring get_full_name()
Definition: tkdatabase.h:89
void end(const tkstring &_loop_name="default")
reads the next entry coresponding to the select statement and fills columns
Definition: tkdatabase.cpp:280
void remove_table(tkstring _table_name)
Definition: tkdatabase.cpp:193
static void list_files(tkstring _dir, tkstring _pattern)
void add_table(tkdb_table &_table)
Definition: tkdatabase.cpp:172
void begin(tkstring _selection, tkstring _from, tkstring _condition="", tkstring _loop_name="default", tkstring _extra_cmd="")
Definition: tkdatabase.cpp:219
tkstring get_name()
Definition: tkdatabase.h:88
tkstring get_value(tkstring _selection, tkstring _from, tkstring _condition="")
Definition: tkdatabase.cpp:304
virtual ~tkdatabase()=default
tkdb_table & new_table(tkstring _table_name)
Definition: tkdatabase.cpp:187
void set_sql_db(sqlite3 *_sql_db)
Definition: tkdatabase.h:83
sqlite3 * open(tkstring _db, tkstring _option="OPEN")
Definition: tkdatabase.cpp:80
bool has_view(const tkstring &_table_name)
Definition: tkdatabase.cpp:182
tkdatabase(bool opening_db=true)
Definition: tkdatabase.cpp:72
void print(const tkstring &_properties="", const tkstring &_opt="")
Definition: tkdatabase.cpp:205
int exec_sql(const char *_cmd)
returns the first value for selection
Definition: tkdatabase.cpp:327
static int getdir(tkstring _dir, std::vector< tkstring > &_files)
bool has_table(const tkstring &_table_name)
Definition: tkdatabase.cpp:177
tkdb_table * get_table(tkstring _table_name)
Definition: tkdatabase.cpp:200
sqlite3 * get_sql_db()
Definition: tkdatabase.h:84
bool next(const tkstring &_loop_name="default")
executes the select statement
Definition: tkdatabase.cpp:245
std::vector< tkstring > & get_views()
Definition: tkdatabase.h:97
tkdb_table & operator[](const char *_table)
Definition: tkdatabase.h:108
static tkdatabase * the_database()
Definition: tkdatabase.cpp:59
Representaiton of a sqlite data table.
Definition: tkdb_table.h:52
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition: tkstring.h:54
Definition: tklog.cpp:39