SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
TemporaryTable.py
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 import ctypes
11 from ctypes import POINTER
12 from SQLamarr import clib, c_TransformerPtr
13 from typing import List, Union
14 
15 from SQLamarr.db_functions import SQLite3DB
16 
17 clib.new_TemporaryTable.argtypes = (
18  ctypes.c_void_p, # void *db,
19  ctypes.c_char_p, # const char* output_table,
20  ctypes.c_char_p, # const char* comma_separated_outputs,
21  ctypes.c_char_p, # const char* query,
22  ctypes.c_bool, # bool make_persistent
23  )
24 
25 clib.new_TemporaryTable.restype = c_TransformerPtr
26 
28  """Creates a temporary table from an SQL query. Persitency can be enabled.
29 
30  Python binding of `SQLamarr::TemporaryTable`.
31  """
32 
33  def __init__ (
34  self,
35  db: SQLite3DB,
36  output_table: str,
37  outputs: List[str],
38  query: Union[str, List[str]],
39  make_persistent: bool = False,
40  ):
41  """
42  Acquires the reference to an open connection to the DB and configure the
43  Transformer.
44 
45  @param db: An open database connection;
46  @param output_table: name of the table where the query output is stored;
47  @param outputs: list of the output column names for further reference;
48  @param query: SQL query (or queries) defining the output columns;
49  @param make_persistent: mark the TABLE as persistent.
50  """
51  if isinstance(query, str):
52  query = [query]
53 
54  self._self_self = clib.new_TemporaryTable(
55  db.get(),
56  output_table.encode('ascii'),
57  ";".join(outputs).encode('ascii'),
58  ";".join([q.replace(";", " ") for q in query]).encode('ascii'),
59  make_persistent,
60  )
61 
62  def __del__(self):
63  """@private: Release the bound class instance"""
64  clib.del_Transformer(self._self_self)
65 
66  @property
67  def raw_pointer(self):
68  """@private: Return the raw pointer to the algorithm."""
69  return self._self_self
70 
71 
72 
Creates a temporary table from an SQL query.
def __init__(self, SQLite3DB db, str output_table, List[str] outputs, Union[str, List[str]] query, bool make_persistent=False)
Acquires the reference to an open connection to the DB and configure the Transformer.