42 query.
Form(
"SELECT %s FROM '%s'", columns.
Data(),
table.Data());
43 if (condition !=
"") query +=
Form(
" WHERE %s", condition.
Data());
44 return std::unique_ptr<TSQLResult>(
fDBserv->Query(query));
55 std::unique_ptr<TList> tl(
fDBserv->GetTablesList());
57 TIter it_tab(tl.get());
58 while ((o = it_tab())) {
62 std::unique_ptr<TSQLResult> columnRes(
fDBserv->GetColumns(
"", o->
GetName()));
63 if (!columnRes.get()) {
64 Error(
"read_table_infos",
"Cannot get information on columns for table %s", o->
GetName());
67 std::unique_ptr<TSQLRow> columnRow(columnRes->Next());
68 while ((columnRow.get()) !=
nullptr) {
70 TString primary_key(columnRow->GetField(5));
72 columnRow.reset(columnRes->Next());
74 fTables.insert(std::pair<std::string, KVSQLite::table>(o->
GetName(), t));
87 std::cout <<
"Tables in database:" << std::endl;
91 for (std::map<std::string, KVSQLite::table>::const_iterator it =
fTables.begin();
94 std::cout <<
"\t" << it->first << std::endl;
123 Error(
"open",
"problem with SQLite database filename: %s", dbfile.
Data());
127 TString uri =
"sqlite://" + exp_path;
130 if (!
fDBserv->Exec(
"pragma schema_version")) {
146 std::unique_ptr<TSQLRow> row(
nullptr);
147 for (
int r = -1;
true; ++
r) {
149 row.reset(tabent->
Next());
150 if (row.get() ==
nullptr)
break;
151 std::cout << std::setw(6) <<
r;
153 for (
int f = 0;
f < nfields; ++
f) {
155 if (
f == 0) std::cout << std::setw(6) <<
"#";
156 std::cout <<
"|" << std::setw(column_width) << tabent->
GetFieldName(
f) ;
159 std::cout <<
"|" << std::setw(column_width) << row->GetField(
f) ;
175 std::cout <<
"Database : " <<
fDBserv->GetDB() <<
" [" <<
fDBserv->GetDBMS() <<
"]\n";
176 std::unique_ptr<TList> tl(
fDBserv->GetTablesList());
178 TIter it_tab(tl.get());
179 while ((o = it_tab())) {
181 std::cout <<
"Table : " << o->
GetName() <<
"\n";
185 std::cout << std::endl;
198 std::cout << std::endl;
238 command +=
"TABLE IF NOT EXISTS \"";
243 if (i) command +=
", ";
244 command += t[i].get_declaration();
249 fTables.insert(std::pair<std::string, KVSQLite::table>(t.
name(), t));
287 Error(
"database::prepare_data_insertion",
288 "bulk insertion in progress for table %s; call database::commit() to terminate transaction",
293 Error(
"database::prepare_data_insertion",
294 "bulk insertion in progress; call database::commit() to terminate transaction");
299 Error(
"database::prepare_data_insertion",
300 "data retrieval in progress; call get_next_result() until it returns false");
310 for (
int i = 0; i < ncol; ++i) {
319 for (
int i = 0; i < ncol; ++i) {
340 return Form(
"INSERT OR FAIL INTO \"%s\"(",
name());
343 return Form(
"INSERT OR IGNORE INTO \"%s\"(",
name());
346 return Form(
"INSERT OR REPLACE INTO \"%s\"(",
name());
352 return Form(
"INSERT INTO \"%s\"(",
name());
367 decl +=
", FOREIGN KEY";
369 decl +=
" REFERENCES ";
407 Error(
"database::insert_data_row",
408 "no transaction initialized; call prepare_data_insertion(name_table) before this method");
412 Error(
"database::insert_data_row",
413 "data retrieval in progress; call get_next_result() until it returns false, then call prepare_data_insertion(name_table) before this method");
419 for (
int i = 0; i < ncol; ++i) {
421 (*fBulkTable)[i].set_data_in_statement(
fSQLstmt.get(), idx);
439 Error(
"database::end_data_insertion",
440 "no transaction initialized; call prepare_data_insertion(name_table) first");
444 Error(
"database::insert_data_row",
445 "data retrieval in progress; call get_next_result() until it returns false, then call prepare_data_insertion(name_table) before this method");
462 if (
select_data(tables, columns, selection, distinct, anything_else)) {
464 std::cout << col->get_table() <<
"::" << col->name() <<
"\t\t\t";
466 std::cout << std::endl;
469 std::cout << col->data().GetString() <<
"\t\t\t";
471 std::cout << std::endl;
510 Error(
"database::select_data",
511 "data insertion in progress; call end_data_insertion() before retrieving data");
515 Error(
"database::select_data",
516 "data retrieval already in progress; call get_next_result() until it returns false before making new selection");
520 std::list<const table*> table_list;
521 KVString table_selection(
""), _tables(tables);
525 while (!_tables.
End()) {
527 table_list.push_back(&
operator[](tabnam));
528 if (i) table_selection +=
" NATURAL JOIN ";
529 table_selection +=
Form(
"\"%s\"", tabnam.
Data());
533 KVString column_selection(
""), _columns(columns);
534 if (columns ==
"*") {
535 column_selection =
"*";
539 for (
auto tabs : table_list) {
540 for (
int i = 0; i < tabs->number_of_columns(); ++i) {
546 if (distinct) column_selection =
"DISTINCT ";
550 while (!_columns.
End()) {
552 if (i) column_selection +=
", ";
553 column_selection +=
Form(
"\"%s\"", colnam.
Data());
556 for (
auto tabs : table_list)
if (tabs->has_column(colnam))
fSQLstmtCols.push_back(&(*tabs)[colnam]);
560 TString cond =
Form(
"SELECT %s FROM %s", column_selection.Data(), table_selection.Data());
561 if (selection !=
"") cond +=
Form(
" WHERE %s", selection.
Data());
562 if (anything_else !=
"") cond +=
Form(
" %s", anything_else.
Data());
565 Error(
"database::select_data",
"problem processing : %s", cond.
Data());
601 Error(
"database::get_next_result",
602 "data insertion in progress; call end_data_insertion() then select_data() before this method");
606 Error(
"database::get_next_result",
607 "no data retrieval in progress; select_data() must be called and return true before calling this method");
614 col->set_data_from_statement(
fSQLstmt.get(), idx);
661 if (result !=
"") result +=
",";
682 if (
select_data(tablename,
Form(
"%s,%s", name_column.
Data(), value_column.
Data()), selection,
false, anything_else)) {
683 table& tb = (*this)[tablename];
684 column& nom = tb[name_column];
685 column& val = tb[value_column];
707 table& tb = (*this)[tablename];
708 column& Xcol = tb[Xcolumn];
709 column& Ycol = tb[Ycolumn];
749 if (distinct) qry +=
"DISTINCT ";
754 if (selection !=
"") {
759 std::unique_ptr<TSQLResult> result(
fDBserv->Query(qry));
760 std::unique_ptr<TSQLRow> row(result->Next());
761 TString number = row->GetField(0);
762 return number.
Atoi();
787 Error(
"database::update",
788 "data insertion in progress; call end_data_insertion() before doing anything else");
792 Error(
"database::update",
793 "data retrieval in progress; call get_next_result() until it returns false before doing anything else");
801 for (
int i = 0; i < ncol; ++i) {
803 if (idx) query +=
",";
809 if (selection !=
"") query +=
Form(
" WHERE %s", selection.
Data());
814 for (
int i = 0; i < ncol; ++i) {
816 (*fBulkTable)[i].set_data_in_statement(
fSQLstmt.get(), idx);
845 if (selection !=
"") query +=
Form(
" WHERE %s", selection.
Data());
861 return (*
this)[
table].add_column(name,
type);
876 Error(
"database::add_missing_columns",
877 "data insertion in progress; call end_data_insertion() before doing anything else");
881 Error(
"database::add_missing_columns",
882 "data retrieval in progress; call get_next_result() until it returns false before doing anything else");
885 int ipar =
l.GetNpar();
886 table& tab = (*this)[_table_];
887 for (
int i = 0; i < ipar; ++i) {
913 if (COLUMNS !=
"*") {
918 while (!_columns.
End()) {
919 if (COLUMNS !=
"") COLUMNS +=
", ";
923 TString query =
Form(
"INSERT INTO \"%s\" SELECT %s FROM %s", destination.
Data(), COLUMNS.
Data(), source.
Data());
924 if (selection !=
"") query +=
Form(
" WHERE %s", selection.
Data());
974 if (idx < 0) idx =
index();
1022 if (idx < 0) idx =
index();
1039 if (!
fBlob)
fBlob = (
void*)
new unsigned char[256];
1098 std::cout <<
"Columns in table:" << std::endl;
1102 for (std::vector<KVSQLite::column>::const_iterator it =
fColumns.begin(); it !=
fColumns.end(); ++it) {
1104 std::cout <<
"\t" << it->name() <<
" [" << it->type_name() <<
"]" << std::endl;
1163 c.set_constraint(
"PRIMARY KEY");
1187 c.set_foreign_key(other_table, other_column);
1211 c.set_foreign_key(other_table, other_column);
1227 int ipar =
l.GetNpar();
1228 for (
int i = 0; i < ipar; ++i) {
1255 (*
this)[i].set_data(*p);
1257 (*
this)[i].set_null();
1270 (*this)[i].set_null();
1294 if (added) namelist += delim;
Defines macros for standard error messages.
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
void SetValue(const Char_t *name, value_type value)
A generic named parameter storing values of different types.
Bool_t HasSameValueAs(const KVNamedParameter &) const
const Char_t * GetString() const
void Set(const char *, const char *)
const Char_t * GetSQLType() const
Double_t GetDouble() const
Strings used to represent a set of ranges of values.
void Add(Int_t)
Add value 'n' to the list.
Long_t fBlobSize
binary data
void set_data_from_statement(TSQLStatement *s, int idx=-1) const
const char * type_name() const
std::pair< TString, KVSQLite_column_type > fNameType
static std::map< KVSQLite::column_type::types, TString > inv_type_map
void set_data_in_statement(TSQLStatement *, int idx=-1) const
const char * name() const
void set_foreign_key(const TString &_table, const TString &_column)
const char * get_declaration() const
return declaration for column, including type & constraint
KVSQLite_column_type type() const
const KVNamedParameter & data() const
void set_data(const T &x)
void set_constraint(const TString &c)
Interface to ROOT SQLite database backend ,.
void copy_table_data(const TString &source, const TString &destination, const TString &columns="*", const TString &selection="")
std::list< const column * > fSQLstmtCols
int count(const TString &table, const TString &column="*", const TString &selection="", bool distinct=false) const
TGraph * create_graph(const TString &tablename, const TString &Xcolumn, const TString &Ycolumn, const TString &selection="")
std::unique_ptr< TSQLResult > SelectRowsFromTable(const TString &table, const TString &columns="*", const TString &condition="") const
std::unique_ptr< TSQLStatement > fSQLstmt
bool update(const TString &table, const TString &columns, const TString &selection="")
void delete_data(const TString &table, const TString &selection="")
void show_tables() const
print list of tables
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.
std::unique_ptr< TSQLiteServer > fDBserv
void open(const TString &dbfile)
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
TString get_string_list(const TString &table, const TString &column, const TString &selection="", const TString &anything_else="")
void Dump() const
Print on stdout contents of database.
std::unordered_map< std::string, KVSQLite::table > fTables
void add_table(const table &)
void PrintResults(TSQLResult *tabent, int column_width=20) const
bool prepare_data_insertion(const TString &)
void clear_table(const TString &name)
Delete all data from 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().
void read_table_infos()
initialise map of database tables from existing database
void add_missing_columns(const TString &table, const KVNameValueList &l)
KVNumberList get_integer_list(const TString &table, const TString &column, const TString &selection="", const TString &anything_else="")
static std::map< TString, KVSQLite::column_type::types > type_map
const char * name() const
bool is_temporary() const
const column & add_primary_key(const TString &name)
void set_all_columns_null()
set the value of all columns in the table to NULL
std::vector< KVSQLite::column > fColumns
void show_columns() const
print list of columns
TString get_column_names(const TString &exclude="", const TString &delim=",") const
column & add_column(const KVSQLite::column &c)
int check_columns(const KVNameValueList &)
int number_of_columns() const
bool has_column(const TString &name) const
const column & add_foreign_key(const TString &other_table, const TString &other_column)
void prepare_data(const KVNameValueList &, const KVNamedParameter *=nullptr)
const char * get_insert_command() const
KVSQLite_insert_mode fInsert
std::unordered_map< std::string, int > fColMap
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
void Begin(TString delim) const
KVString Next(Bool_t strip_whitespace=kFALSE) const
virtual const char * GetName() const
virtual const char * GetName() const
virtual Int_t GetFieldCount()=0
virtual const char * GetFieldName(Int_t field)=0
virtual TSQLRow * Next()=0
static TSQLServer * Connect(const char *db, const char *uid, const char *pw)
const char * Data() const
void Form(const char *fmt,...)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
virtual char * ExpandPathName(const char *path)
const long double g
masses