SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
BaseSqlInterface.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 #include "SQLamarr/BaseSqlInterface.h"
12 #include "SQLamarr/custom_sql_functions.h"
13 #include "SQLamarr/SQLiteError.h"
14 #include "sqlite3.h"
15 #include <iostream>
16 
17 namespace SQLamarr
18 {
19  //==========================================================================
20  // Constructor
21  //==========================================================================
23  : m_database (db)
24  , m_queries ()
25  , m_cached_raw_ptr (nullptr)
26  {
27  sqlamarr_create_sql_functions(db.get());
28  }
29 
30  //==========================================================================
31  // Destructor
32  //==========================================================================
34  {
36  }
37 
38  //==========================================================================
39  // invalidate_cache
40  //==========================================================================
42  {
43  for (auto q = m_queries.begin(); q != m_queries.end(); ++q)
44  sqlite3_finalize(q->second);
45 
46  m_queries.clear(); // Cache invalidation
47  }
48 
49  //==========================================================================
50  // get_statement
51  //==========================================================================
53  const std::string& name,
54  const std::string& query
55  )
56  {
57  if (m_database.get() != m_cached_raw_ptr)
58  {
59  m_cached_raw_ptr = m_database.get();
61  }
62 
63  if (m_queries.find(name) == m_queries.end())
64  m_queries[name] = prepare_statement(m_database, query);
65 
66  sqlite3_reset(m_queries[name]);
67  return m_queries[name];
68  }
69 
70  //==========================================================================
71  // create_sql_function
72  //==========================================================================
74  const std::string& name,
75  int argc,
76  void (*xFunc)(sqlite3_context*, int, sqlite3_value**)
77  )
78  {
79  sqlite3_create_function(
80  m_database.get(),
81  name.c_str(), argc,
82  SQLITE_UTF8, nullptr, xFunc, nullptr, nullptr
83  );
84  }
85 
86  //==========================================================================
87  // exec_stmt
88  //==========================================================================
89  bool BaseSqlInterface::exec_stmt (sqlite3_stmt* stmt)
90  {
91  switch (sqlite3_step(stmt))
92  {
93  case SQLITE_DONE: return false;
94  case SQLITE_ROW: return true;
95  default:
96  std::cerr << sqlite3_errmsg(m_database.get()) << std::endl;
97  throw SQLiteError("SQL Error");
98  }
99 
100  return false;
101  }
102 
103 }
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 invalidate_cache(void)
Invalidate the cache of the queries.
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
sqlite3_stmt * prepare_statement(SQLite3DB &db, const std::string &query)
Prpare a statement.