7 #include "KVSQLiteServer.h"
13 #include <unordered_map>
16 #include <KVNameValueList.h>
17 #include <KVNumberList.h>
31 namespace column_type {
40 namespace insert_mode {
64 std::pair<TString, KVSQLite_column_type> fNameType;
67 static std::map<KVSQLite::column_type::types, TString> inv_type_map;
81 has_data(false), fBlob(nullptr), fBlobSize(0),
82 fIsROWIDAlias(false), fIsNull(false)
84 if (!inv_type_map.size()) init_type_map();
95 if (fBlob)
delete[](
unsigned char*)fBlob;
100 return fNameType.first;
104 return fNameType.first;
108 return fNameType.second;
112 return const_cast<column*
>(
this)->_type();
141 template <
typename T>
162 fBlobSize =
sizeof(
x);
169 fBlobSize =
sizeof(*x);
189 if (fConstraint.
IsNull()) fConstraint =
"UNIQUE";
190 else fConstraint +=
" UNIQUE";
196 if (fConstraint.
IsNull()) fConstraint =
"NOT NULL";
197 else fConstraint +=
" NOT NULL";
206 defs +=
"CURRENT_DATE";
209 defs +=
"CURRENT_TIME";
212 defs +=
"CURRENT_TIMESTAMP";
214 if (fConstraint.
IsNull()) fConstraint = defs;
215 else fConstraint +=
" " + defs;
222 if (fConstraint.
IsNull()) fConstraint = defs;
223 else fConstraint +=
" " + defs;
230 if (fConstraint.
IsNull()) fConstraint = defs;
231 else fConstraint +=
" " + defs;
238 if (fConstraint.
IsNull()) fConstraint = defs;
239 else fConstraint +=
" " + defs;
246 template <
typename T>
249 return fData.
Get<
T>();
254 return static_cast<T*
>(fBlob);
258 return fIsROWIDAlias;
262 fIsROWIDAlias =
true;
276 TString child_key, parent_table, parent_key;
277 bool back_reference =
false;
283 : child_key(chk), parent_table(
pt), parent_key(pk), back_reference(back_ref)
287 return back_reference;
303 mutable std::vector<KVSQLite::column> fColumns;
304 mutable std::unordered_map<std::string, int> fColMap;
305 static std::map<TString, KVSQLite::column_type::types> type_map;
307 std::vector<foreign_key_constraint> fForeignKeys;
310 void init_type_map();
311 void set_foreign_key_back_references();
315 : fName(
Name), fInsert(
KVSQLite::insert_mode::DEFAULT), fColumns(cols), fColMap(), fTemp(false)
317 if (!type_map.size()) init_type_map();
318 for (
auto&
c : fColumns)
c.set_table(
Name);
324 : fDB(other.fDB), fName(other.fName), fInsert(other.fInsert), fColumns(other.fColumns), fColMap(other.fColMap),
325 fPrimaryKey(other.fPrimaryKey), fForeignKeys(other.fForeignKeys), fTemp(other.fTemp)
392 return fColMap.count(
name.Data());
406 TString what =
"Error in <KVSQLite::table::get_column(const TString&)> : "
407 +
n +
" is not a column of table " +
name();
408 throw std::runtime_error(what.
Data());
410 return fColumns[fColMap[
n.Data()]];
415 std::cout <<
name() <<
"\n";
416 for (std::vector<KVSQLite::column>::const_iterator it = fColumns.begin(); it != fColumns.end(); ++it) it->print();
420 return fColumns.size();
450 if (
get_column(fPrimaryKey).
type() == KVSQLite_column_type::INTEGER)
456 return !fPrimaryKey.
IsNull();
460 if (!ignore_back_references)
return !fForeignKeys.empty();
461 for (
auto&
f : fForeignKeys)
462 if (!
f.back_reference)
474 std::unique_ptr<KVSQLiteServer> fDBserv;
475 mutable std::unordered_map<std::string, KVSQLite::table> fTables;
476 mutable std::unique_ptr<TSQLStatement> fSQLstmt;
477 mutable std::list<const column*> fSQLstmtCols;
478 mutable table* fBulkTable;
479 mutable bool fInserting;
480 mutable bool fSelecting;
481 mutable bool fEmptyResultSet;
483 mutable TString fSelectedColumns;
485 void PrintResults(
TSQLResult* tabent,
int column_width = 20)
const;
486 std::unique_ptr<TSQLResult> SelectRowsFromTable(
489 const TString& condition =
"")
const;
491 void read_table_infos();
492 std::list<const table*> generate_table_selection_string(
const TString& tables,
KVString& table_selection)
const;
498 database() : fDBserv(nullptr), fTables(), fSQLstmt(nullptr), fBulkTable(nullptr), fInserting(false), fSelecting(false), fIsValid(false) {}
499 database(
const TString& dbfile) : fDBserv(nullptr), fTables(), fSQLstmt(nullptr), fBulkTable(nullptr), fInserting(false), fSelecting(false), fIsValid(false)
503 database(
const database& db) : fDBserv(nullptr), fTables(), fSQLstmt(nullptr), fBulkTable(nullptr), fInserting(false), fSelecting(false), fIsValid(false)
510 if (db.
good())
open(db.fDBserv->GetDB());
514 if (&db !=
this && db.
good())
open(db.fDBserv->GetDB());
520 return fTables.size();
547 return fDBserv->IsConnected();
556 return fTables.count(
table.Data());
569 if (!fTables.count(
name.Data())) {
570 std::cout <<
"Error in <KVSQLite::database::get_table(const TString&)> : "
571 <<
name <<
" is not a table of database" << std::endl;
572 return fTables.begin()->second;
574 return fTables[
name.Data()];
579 auto ret = fDBserv->Exec(sql_statement);
581 std::cerr <<
"database::execute : Exec(" << sql_statement <<
582 ") failed with code=" << fDBserv->GetErrorCode()
583 <<
" : " << fDBserv->GetErrorMsg() << std::endl;
589 auto stat = fDBserv->Statement(
query);
590 if (!stat &&
debug) {
591 std::cerr <<
"database::prepare_statement : Statement(" <<
query <<
592 ") failed with code=" << fDBserv->GetErrorCode()
593 <<
" : " << fDBserv->GetErrorMsg() << std::endl;
594 fSQLstmt.reset(
nullptr);
598 fSQLstmt.reset(stat);
603 auto result = std::unique_ptr<TSQLResult>(fDBserv->Query(
query));
605 std::cerr <<
"database::query : Query(" <<
query <<
606 ") failed with code=" << fDBserv->GetErrorCode()
607 <<
" : " << fDBserv->GetErrorMsg() << std::endl;
617 const TString& selection =
"",
const TString& anything_else =
"");
619 const TString& selection =
"",
const TString& anything_else =
"");
621 const TString& selection =
"",
const TString& anything_else =
"");
623 const TString& selection =
"",
const TString& anything_else =
"");
629 bool distinct =
false,
const TString& anything_else =
"")
const;
631 bool distinct =
false,
const TString& anything_else =
"")
const;
651 return count_column.
get_data<
int>();
#define ClassDef(name, id)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char * Form(const char *fmt,...)
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
A generic named parameter storing values of different types.
void Set(const char *, const char *)
Strings used to represent a set of ranges of values.
void set_binary_data(T &x)
void set_data_from_statement(TSQLStatement *s, int idx=-1) const
void set_table(const TString &name)
const column & operator=(const T &x)
TString get_declaration() const
void set_data_in_statement(TSQLStatement *, int idx=-1) const
const char * name() const
column & DEFAULT(default_time t)
KVSQLite_column_type type() const
column & DEFAULT(const TString &s)
TString get_data_string_for_insert()
void set_binary_data(T *x)
const KVNamedParameter & data() const
void set_data(const T &x)
TString get_table() const
void set_constraint(const TString &c)
column & DEFAULT(double d)
Interface to ROOT SQLite database backend.
int get_number_of_tables() const
void copy_table_data(const TString &source, const TString &destination, const TString &columns="*", const TString &selection="")
database(const TString &dbfile)
table & add_table(const table &)
TGraph * create_graph(const TString &tablename, const TString &Xcolumn, const TString &Ycolumn, const TString &selection="")
int get_count_column_value() const
bool update(const TString &table, const TString &columns, const TString &selection="")
void show_tables() const
print list of tables
const KVSQLite::table & operator[](const TString &name) const
void insert_data_row_selected_columns()
void delete_table(const TString &table)
column & add_column(const TString &table, const TString &name, const TString &type)
void print_selection(const TString &table, const TString &columns, const TString &condition, int column_width=20) const
Print on stdout contents of database.
void delete_rows_in_table(const TString &table, const TString &selection="")
bool execute(const TString &sql_statement)
database & operator=(const database &db)
KVNumberList get_integer_list(const TString &tables, const TString &column, const TString &selection="", const TString &anything_else="")
KVSQLite::table & operator[](const TString &name)
bool select_data_in_list(const TString &tables, const KVNameValueList &selection, const TString &columns="*", bool distinct=false, const TString &anything_else="") const
bool is_inserting() const
void open(const TString &dbfile)
bool has_table(const TString &table)
void get_name_value_list_for_each_row(const TString &table, std::function< bool(const KVNameValueList &)> callback, const TString &selection="", const TString &anything_else="")
void end_data_insertion()
bool select_data(const TString &tables, const TString &columns="*", const TString &selection="", bool distinct=false, const TString &anything_else="") const
KVNameValueList get_name_value_list(const TString &table, const TString &name_column, const TString &value_column, const TString &selection="", const TString &anything_else="")
bool get_next_result() const
bool prepare_data_insertion_selected_columns(const TString &table)
void Dump() const
Print on stdout contents of database.
database(const database &db)
TString get_string_list(const TString &tables, const TString &column, const TString &selection="", const TString &anything_else="")
KVSQLite::table & get_table(const TString &name)
bool prepare_data_insertion(const TString &)
void clear_table(const TString &name)
Delete all rows in table, but does not remove table.
void print_selected_data(const TString &tables, const TString &columns="*", const TString &selection="", bool distinct=false, const TString &anything_else="")
Print out results of a call to select_data().
int count(const TString &tables, const TString &column="*", const TString &selection="", bool distinct=false, const TString &anything_else="") const
void add_missing_columns(const TString &table, const KVNameValueList &l)
std::unique_ptr< TSQLResult > query(const TString &query) const
bool prepare_statement(const TString &query) const
foreign_key_constraint(const foreign_key_constraint &)=default
bool is_back_reference() const
foreign_key_constraint()=default
foreign_key_constraint(const TString &chk, const TString &pt, const TString &pk="", bool back_ref=false)
foreign_key_constraint(foreign_key_constraint &&)=default
const char * name() const
bool has_foreign_keys(bool ignore_back_references=true) const
bool is_temporary() const
void primary_key(const TString &cols)
KVSQLite::column & operator[](int i)
void set_all_columns_null()
set the value of all columns in the table to NULL
void set_temporary(bool temp=true)
table(const table &other)
void show_columns() const
print list of columns
bool has_primary_key() const
void foreign_key(const TString &child_key, const TString &parent_table, const TString &parent_key)
table(const TString &Name, const std::vector< KVSQLite::column > &cols)
TString get_column_names(const TString &exclude="", const TString &delim=",") const
table(const TString &Name="")
column & add_column(const KVSQLite::column &c)
void set_name(const TString &name)
KVSQLite::column & get_column(const TString &n)
KVSQLite::column & operator[](const TString &n)
int check_columns(const KVNameValueList &)
void set_insert_mode(KVSQLite_insert_mode i)
int number_of_columns() const
const KVSQLite::column & operator[](const TString &n) const
TString get_table_creation_command() const
bool has_column(const TString &name) const
KVSQLite::column & get_column(int i)
void prepare_insert_single_row()
column & add_column(const TString &name, KVSQLite_column_type type)
void prepare_data(const KVNameValueList &, const KVNamedParameter *=nullptr)
const char * get_insert_command() const
void insert_single_row()
use to add single row data to table. see prepare_insert_single_row() for usage.
const KVSQLite::column & operator[](int i) const
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
TSubString Strip(EStripType s=kTrailing, char c=' ') const
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
RooCmdArg Name(const char *name)
KVSQLite::column_type::types KVSQLite_column_type
KVSQLite::insert_mode::types KVSQLite_insert_mode