TkN 2.7.1
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
26#include "tkn_sqlite3.h"
27#include <tklog.h>
28#include <tkstring.h>
29#include <tkdb_table.h>
30
31namespace tkn {
32
40public:
41 static tkdatabase *the_database();
42
43protected:
44 sqlite3 *fDataBase = nullptr;
45 std::map<tkstring, tkdb_table> fTables;
46 int fMode{};
47 bool fReading = false;
48 std::vector<tkstring> fViews;
49 std::map<tkstring, sqlite3_stmt *> fSQLStatement; // sql statements to select rows and loop over them (begin() next() end())
50 std::map<tkstring, std::vector<std::vector<tkdb_column *>>> fSQLColumnBindings;
51 tkstring fName{};
52 tkstring fFullName{};
53
54public:
55 tkdatabase(bool opening_db = true);
56 virtual ~tkdatabase();
57
58 void set_sql_db(sqlite3 *_sql_db) { fDataBase = _sql_db; }
59 sqlite3 *get_sql_db() { return fDataBase; }
60
61 sqlite3 *open(tkstring _db, tkstring _option = "OPEN");
62 void close();
63 tkstring get_name() { return fName; }
64 tkstring get_full_name() { return fFullName; }
65
66 void load_tables();
67 void load_views();
68 void add_table(tkdb_table &_table);
69 bool has_table(const tkstring &_table_name);
70 bool has_view(const tkstring &_table_name);
71
72 std::vector<tkstring> &get_views() { return fViews; }
73
74 tkdb_table &new_table(tkstring _table_name);
75 void remove_table(tkstring _table_name);
76
77 tkdb_table *get_table(tkstring _table_name);
78 static int getdir(tkstring _dir, std::vector<tkstring> &_files);
79 static void list_files(tkstring _dir, tkstring _pattern);
80
81 void print(const tkstring &_properties = "", const tkstring &_opt = "");
82
83 tkdb_table &operator[](const char *_table)
84 {
85 return fTables[_table];
86 }
87
88 void begin(tkstring _selection, tkstring _from, tkstring _condition = "", tkstring _loop_name = "default", tkstring _extra_cmd = "");
89 bool next(const tkstring &_loop_name = "default");
90 void end(const tkstring &_loop_name = "default");
91
92 int count(const tkstring &_from, const tkstring &_condition = "");
93 tkstring get_value(tkstring _selection, tkstring _from, tkstring _condition = "");
94
95 // private:
96 int exec_sql(const char *_cmd);
97
98#define gdatabase (tkn::tkdatabase::the_database())
99
100#ifdef HAS_ROOT
102 ClassDef(tkdatabase, 0);
103#endif
104};
105} // namespace tkn
106
107#endif
Interface to the sqlite database.
Definition tkdatabase.h:39
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:64
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:63
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:58
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:59
bool next(const tkstring &_loop_name="default")
executes the select statement
std::vector< tkstring > & get_views()
Definition tkdatabase.h:72
tkdb_table & operator[](const char *_table)
Definition tkdatabase.h:83
static tkdatabase * the_database()
Representaiton of a sqlite data table.
Definition tkdb_table.h:31
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:33
Definition tklog.cpp:16