SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
Plugin.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
14 
15 from SQLamarr.db_functions import SQLite3DB
16 
17 clib.new_Plugin.argtypes = (
18  ctypes.c_void_p, # void *db,
19  ctypes.c_char_p, # const char* library_path,
20  ctypes.c_char_p, # const char* function_name,
21  ctypes.c_char_p, # const char* query,
22  ctypes.c_char_p, # const char* output_table,
23  ctypes.c_char_p, # const char* comma_separated_outputs,
24  ctypes.c_char_p, # const char* comma_separated_references
25  )
26 
27 clib.new_Plugin.restype = c_TransformerPtr
28 
29 class Plugin:
30  """
31  Wrap an external function as defined in a compiled shared library.
32 
33  Python bindings for SQLamarr::Plugin.
34 
35  The C function `function_name` is selected from the shared object
36  `library_path` and evaluated for each row obtained executing the
37  `query` on the database `db`. Results are stored in the temporary
38  table `output_table` whose columns are named after the list of `outputs`.
39  The columns returned by the `query`
40  are all interpreted as inputs to the external functions, unless
41  they are listed as `references`, in that case they are copied to the output
42  table, easying JOIN operations with other tables in the database.
43 
44  """
45  def __init__ (
46  self,
47  db: SQLite3DB,
48  library_path: str,
49  function_name: str,
50  query: str,
51  output_table: str,
52  outputs: List[str],
53  references: List[str]
54  ):
55  """
56  Acquire the db and configure the interface with the compiled function.
57 
58 
59  @param db: An open database connection;
60  @param library_path: path-like string defining the library defining the
61  function to execute;
62  @param function_name: string defining the name of the function to execute;
63  @param query: SQL query defining the input and reference columns;
64  @param output_table: name of the table where reference and output are
65  stored;
66  @param outputs: list of the output column names for further reference;
67  @param references: list of columns selected by `query` to be used as
68  reference indices instead of passing as inputs to the external function.
69 
70 
71  """
72  self._self_self = clib.new_Plugin(
73  db.get(),
74  library_path.encode('ascii'),
75  function_name.encode('ascii'),
76  query.encode('ascii'),
77  output_table.encode('ascii'),
78  ";".join(outputs).encode('ascii'),
79  ";".join(references).encode('ascii'),
80  )
81 
82  self._function_name_function_name = function_name
83 
84  def __del__(self):
85  """@private: Release the bound class instance"""
86  clib.del_Transformer(self._self_self)
87 
88  @property
89  def raw_pointer(self):
90  """@private: Return the raw pointer to the algorithm."""
91  return self._self_self
92 
93  def __str__(self):
94  """@private: Indicateds the linked symbol for error displaying."""
95  return f"<SQLamarr.Plugin '{self._function_name}'>"
96 
97 
Wrap an external function as defined in a compiled shared library.
Definition: Plugin.py:29
def __init__(self, SQLite3DB db, str library_path, str function_name, str query, str output_table, List[str] outputs, List[str] references)
Acquire the db and configure the interface with the compiled function.
Definition: Plugin.py:54