SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
BaseSqlInterface.h
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 #pragma once
12 #include <iostream>
13 #include <string>
14 #include <unordered_map>
15 
16 #include "SQLamarr/db_functions.h"
17 
18 namespace SQLamarr
19 {
35  {
36  public:
39  virtual ~BaseSqlInterface();
40 
41  void sync_database(const std::string& db_uri)
43 
47  void invalidate_cache(void);
48 
49  protected: // members
51 
52 
53  private: //members
54  std::unordered_map<std::string, sqlite3_stmt*> m_queries;
55  sqlite3* m_cached_raw_ptr;
56 
57  protected: // methods
59  sqlite3_stmt* get_statement (
60  const std::string& name,
62  const std::string& query
64  );
65 
68  void begin_transaction () { sqlite3_exec(m_database.get(), "BEGIN", 0, 0, 0); }
69 
71  void end_transaction () { sqlite3_exec(m_database.get(), "COMMIT", 0, 0, 0); }
72 
74  int last_insert_row () { return sqlite3_last_insert_rowid(m_database.get()); }
75 
82  void using_sql_function (
83  const std::string& name,
84  int argc,
85  void (*xFunc)(sqlite3_context*, int, sqlite3_value**)
87  );
88 
90  bool exec_stmt (sqlite3_stmt*);
91  };
92 }
Abstract interface with helper functions to access an SQLite DB.
sqlite3_stmt * get_statement(const std::string &name, const std::string &query)
Creates or retrieve from cache a statement.
BaseSqlInterface(SQLite3DB &db)
Constructor, acquiring the database without ownership.
void begin_transaction()
Begin an SQL transaction stopping update to disk util end_transaction() is issued
void invalidate_cache(void)
Invalidate the cache of the queries.
void end_transaction()
End an SQL transaction re-enabling disk updates.
int last_insert_row()
Return the index of the last rows inserted in any table.
void sync_database(const std::string &db_uri)
bool exec_stmt(sqlite3_stmt *)
Execute a statement, possibly throwing an exception on failure.
void using_sql_function(const std::string &name, int argc, void(*xFunc)(sqlite3_context *, int, sqlite3_value **))
Register a static function in DB, enabling usage from SQL.
SQLite3DB & m_database
Reference to the SQLite database (not owned).
A database connection handler easying sharing the DB between C++ and Python.
Definition: db_functions.py:24
void update_db_connection(SQLite3DB &old_db, const std::string &db_uri, int flags=SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI)
Force synchronization to disk by closing and opening the connection.