ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
tool_available.cpp
Go to the documentation of this file.
1/**
2 * @file tool_available.cpp
3 * @brief SQL function @c provsql.tool_available() – report whether an
4 * external tool is on the backend's resolved PATH.
5 *
6 * The check uses the same @c find_external_tool() helper that the
7 * compilers / WMC counters / GraphViz wrappers themselves consult, so
8 * the result reflects exactly what a subsequent
9 * @c probability_evaluate('compilation', '<tool>') call would see,
10 * including the @c provsql.tool_search_path GUC prepended to @c $PATH.
11 *
12 * Used by Studio (and any other client) to filter the list of usable
13 * knowledge compilers / model counters before offering them in the UI
14 * or running the probability benchmark.
15 */
16extern "C" {
17#include "postgres.h"
18#include "fmgr.h"
19#if PG_VERSION_NUM >= 160000
20#include "varatt.h"
21#endif
22
23PG_FUNCTION_INFO_V1(tool_available);
24}
25
26#include "external_tool.h"
27#include "provsql_utils_cpp.h"
28
29#include <string>
30
31/**
32 * @brief PostgreSQL-callable entry point.
33 *
34 * Argument: @c name (text), a bare executable name (e.g. @c "d4") or
35 * an absolute path. Names with a slash bypass the PATH walk and are
36 * tested directly with @c access(X_OK).
37 *
38 * Returns: @c true iff @c find_external_tool returns a non-empty
39 * resolved path. NULL input returns NULL (STRICT in the SQL
40 * declaration). A trailing whitespace-only input returns false.
41 */
42Datum tool_available(PG_FUNCTION_ARGS)
43{
44 try {
45 text *name_text = PG_GETARG_TEXT_PP(0);
46 std::string name(VARDATA_ANY(name_text), VARSIZE_ANY_EXHDR(name_text));
47 if (name.empty())
48 PG_RETURN_BOOL(false);
49 PG_RETURN_BOOL(!find_external_tool(name).empty());
50 } catch (const std::exception &e) {
51 provsql_error("%s", e.what());
52 } catch (...) {
53 provsql_error("Unknown exception");
54 }
55 PG_RETURN_BOOL(false);
56}
std::string find_external_tool(const std::string &name)
Locate an external tool by name.
Helpers for invoking external command-line tools.
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
C++ utility functions for UUID manipulation.
Datum tool_available(PG_FUNCTION_ARGS)
PostgreSQL-callable entry point.