TkN 2.4
Toolkit for Nuclei
Loading...
Searching...
No Matches
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 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#ifndef database_H
15#define database_H
16
17#include "tkn_config.h"
18
19#ifdef HAS_ROOT
20#include "TClass.h"
21#endif
22
23#include <vector>
24#include <map>
25#include <mutex>
26
27#include <sqlite3.h>
28#include <tklog.h>
29#include <tkstring.h>
30#include <tkdb_table.h>
31
32namespace tkn {
33
35
36 static std::recursive_mutex &get_mutex()
37 {
38 static std::recursive_mutex mutex;
39 return mutex;
40 }
41
42 static tkdatabase *g_database;
43
44public:
45 static tkdatabase *the_database();
46
47protected:
48 sqlite3 *fDataBase = nullptr;
49 std::map<tkstring, tkdb_table> fTables;
50 int fMode{};
51 bool fReading = false;
52 std::vector<tkstring> fViews;
53 std::map<tkstring, sqlite3_stmt *> fSQLStatement; // sql statements to select rows and loop over them (begin() next() end())
54 tkstring fName{};
55 tkstring fFullName{};
56
57public:
58 tkdatabase(bool opening_db = true);
59 virtual ~tkdatabase();
60
61 void set_sql_db(sqlite3 *_sql_db) { fDataBase = _sql_db; }
62 sqlite3 *get_sql_db() { return fDataBase; }
63
64 sqlite3 *open(tkstring _db, tkstring _option = "OPEN");
65 void close();
66 tkstring get_name() { return fName; }
67 tkstring get_full_name() { return fFullName; }
68
69 void load_tables();
70 void load_views();
71 void add_table(tkdb_table &_table);
72 bool has_table(const tkstring &_table_name);
73 bool has_view(const tkstring &_table_name);
74
75 std::vector<tkstring> &get_views() { return fViews; }
76
77 tkdb_table &new_table(tkstring _table_name);
78 void remove_table(tkstring _table_name);
79
80 tkdb_table *get_table(tkstring _table_name);
81 static int getdir(tkstring _dir, std::vector<tkstring> &_files);
82 static void list_files(tkstring _dir, tkstring _pattern);
83
84 void print(const tkstring &_properties = "", const tkstring &_opt = "");
85
86 tkdb_table &operator[](const char *_table)
87 {
88 return fTables[_table];
89 }
90
91 void begin(tkstring _selection, tkstring _from, tkstring _condition = "", tkstring _loop_name = "default", tkstring _extra_cmd = "");
92 bool next(const tkstring &_loop_name = "default");
93 void end(const tkstring &_loop_name = "default");
94
95 int count(const tkstring &_from, const tkstring &_condition = "");
96 tkstring get_value(tkstring _selection, tkstring _from, tkstring _condition = "");
97
98 // private:
99 int exec_sql(const char *_cmd);
100
101#define gdatabase (tkn::tkdatabase::the_database())
102
103#ifdef HAS_ROOT
105 ClassDef(tkdatabase, 0);
106#endif
107};
108} // namespace tkn
109
110#endif
Interface to the sqlite database.
Definition tkdatabase.h:34
int count(const tkstring &_from, const tkstring &_condition="")
resets column values and ends the select statement
virtual ~tkdatabase()
tkstring get_full_name()
Definition tkdatabase.h:67
void end(const tkstring &_loop_name="default")
reads the next entry coresponding to the select statement and fills columns
void remove_table(tkstring _table_name)
static void list_files(tkstring _dir, tkstring _pattern)
void add_table(tkdb_table &_table)
void begin(tkstring _selection, tkstring _from, tkstring _condition="", tkstring _loop_name="default", tkstring _extra_cmd="")
tkstring get_name()
Definition tkdatabase.h:66
tkstring get_value(tkstring _selection, tkstring _from, tkstring _condition="")
tkdb_table & new_table(tkstring _table_name)
void set_sql_db(sqlite3 *_sql_db)
Definition tkdatabase.h:61
sqlite3 * open(tkstring _db, tkstring _option="OPEN")
bool has_view(const tkstring &_table_name)
tkdatabase(bool opening_db=true)
void print(const tkstring &_properties="", const tkstring &_opt="")
int exec_sql(const char *_cmd)
returns the first value for selection
static int getdir(tkstring _dir, std::vector< tkstring > &_files)
bool has_table(const tkstring &_table_name)
tkdb_table * get_table(tkstring _table_name)
sqlite3 * get_sql_db()
Definition tkdatabase.h:62
bool next(const tkstring &_loop_name="default")
executes the select statement
std::vector< tkstring > & get_views()
Definition tkdatabase.h:75
tkdb_table & operator[](const char *_table)
Definition tkdatabase.h:86
static tkdatabase * the_database()
Representaiton of a sqlite data table.
Definition tkdb_table.h:29
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:31
Definition tklog.cpp:16