22 #include "SQLamarr/BasePlugin.h"
31 const std::string& library,
32 const std::string& function_name,
33 const std::string& select_query,
34 const std::string& output_table,
35 const std::vector<std::string> outputs,
36 const std::vector<std::string> reference_keys
40 , m_function_name (function_name)
41 , m_select_query (select_query)
42 , m_output_table (output_table)
44 , m_refkeys (reference_keys)
45 , m_handle (dlopen(library.c_str(), RTLD_LAZY))
49 std::cerr <<
"Failure while loading " << m_library << std::endl;
50 throw std::runtime_error(
"Failed loading library");
62 std::vector<std::string> BasePlugin::get_column_names()
const
64 std::vector<std::string> ret;
65 ret.insert(ret.end(), m_refkeys.begin(), m_refkeys.end());
66 ret.insert(ret.end(), m_outputs.begin(), m_outputs.end());
74 std::string BasePlugin::compose_delete_query()
77 s <<
"DELETE FROM " << m_output_table <<
";";
84 std::string BasePlugin::compose_create_query()
87 s <<
"CREATE TEMPORARY TABLE IF NOT EXISTS "
88 << m_output_table <<
"(";
90 for (
auto c: m_refkeys)
91 s << c <<
" INTEGER" <<
", ";
93 for (
auto c: m_outputs)
94 s << c <<
" REAL" << (c != m_outputs.back() ?
", ":
"");
104 std::string BasePlugin::compose_insert_query()
107 s <<
"INSERT INTO " << m_output_table <<
" (";
109 std::vector<std::string> col_names = get_column_names();
110 for (
auto c: col_names)
111 s << c << (c != col_names.back() ?
", ":
"");
114 for (
auto c: col_names)
115 s <<
"?" << (c != col_names.back() ?
", ":
"");
131 "create_output_table", compose_create_query().c_str()
137 "delete_output_table", compose_delete_query().c_str()
143 "insert_in_output_table", compose_insert_query().c_str()
149 m_select_query.c_str()
156 sqlite3_reset(insert_in_output_table);
158 std::vector<float> input;
159 std::vector<float> output(m_outputs.size());
162 const int nCols = sqlite3_column_count(select_input);
163 for (
int iCol=0; iCol < nCols; ++iCol)
166 const std::string column(sqlite3_column_name(select_input, iCol));
167 std::vector<std::string> col_names = get_column_names();
168 auto col_iterator = std::find(m_refkeys.begin(), m_refkeys.end(), column);
171 if (col_iterator != m_refkeys.end())
175 insert_in_output_table,
176 1 + col_iterator - m_refkeys.begin(),
177 sqlite3_column_int(select_input, iCol)
189 for (
size_t iOutput = 0; iOutput < m_outputs.size(); ++iOutput)
191 insert_in_output_table,
192 m_refkeys.size() + iOutput + 1,
void execute() override
Execute the external function and copies the output in a new table.
BasePlugin(SQLite3DB &db, const std::string &library, const std::string &function_name, const std::string &select_query, const std::string &output_table, const std::vector< std::string > outputs, const std::vector< std::string > reference_keys={"ref_id"})
Constructor.
virtual void eval_parametrization(float *output, const float *input)=0
Evaluate the external parametrization.
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.
void begin_transaction()
Begin an SQL transaction stopping update to disk util end_transaction() is issued
void end_transaction()
End an SQL transaction re-enabling disk updates.
bool exec_stmt(sqlite3_stmt *)
Execute a statement, possibly throwing an exception on failure.
A database connection handler easying sharing the DB between C++ and Python.
void validate_token(const std::string &token)
Ensure a token is alphanumeric.
float read_as_float(sqlite3_stmt *, int)
Read a column field from a sqlite3 statement and convert it to float.