SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
CleanEventStore.cpp
1 // (c) Copyright 2022 CERN for the benefit of the LHCb Collaboration.
2 //
3 // This software is distributed under the terms of the GNU General Public
4 // Licence version 3 (GPL Version 3), copied verbatim in the file "LICENCE".
5 //
6 // In applying this licence, CERN does not waive the privileges and immunities
7 // granted to it by virtue of its status as an Intergovernmental Organization
8 // or submit itself to any jurisdiction.
9 
10 
11 // STL
12 #include <string>
13 #include <vector>
14 #include <iostream>
15 #include <algorithm>
16 
17 // SQLite3
18 #include "sqlite3.h"
19 
20 // SQLamarr
21 #include "SQLamarr/CleanEventStore.h"
22 
23 namespace SQLamarr
24 {
25  //============================================================================
26  // execute
27  //============================================================================
29  {
30  sqlite3_stmt* list_tables = get_statement("list_tables",
31  "SELECT name FROM sqlite_master WHERE type='table'");
32  sqlite3_stmt* list_temp_tables = get_statement("list_temp_tables",
33  "SELECT name FROM sqlite_temp_master WHERE type='table'");
34 
35  std::vector<std::string> tables;
36  while (exec_stmt(list_tables))
37  tables.push_back(
38  reinterpret_cast<const char *>(sqlite3_column_text(list_tables, 0))
39  );
40 
41  while (exec_stmt(list_temp_tables))
42  tables.push_back(
43  reinterpret_cast<const char *>(sqlite3_column_text(list_temp_tables, 0))
44  );
45 
46  char buffer[1024];
47  for (auto& table: tables)
48  {
49  sprintf(buffer, "DELETE FROM %s", table.c_str());
50  exec_stmt(get_statement(buffer, buffer));
51  }
52  }
53 }
sqlite3_stmt * get_statement(const std::string &name, const std::string &query)
Creates or retrieve from cache a statement.
bool exec_stmt(sqlite3_stmt *)
Execute a statement, possibly throwing an exception on failure.
void execute() override
Execute the algorithm, cleaning the database.