18 #include "catalog/pg_type.h" 19 #include "executor/spi.h" 21 #include "access/xact.h" 28 typedef bool (
Fetch_fn)(HeapTuple, TupleDesc,
void *);
48 int result = SPI_connect();
49 if (result == SPI_ERROR_CONNECT) {
64 int spi_result = SPI_finish();
96 if (saved_plan && *saved_plan) {
101 if (!(plan = SPI_prepare(qry, nargs, argtypes))) {
103 (errcode(ERRCODE_INTERNAL_ERROR),
104 errmsg(
"prepare_query fails"),
105 errdetail(
"SPI_prepare('%s') returns NULL " 112 *saved_plan = SPI_saveplan(plan);
116 exec_result = SPI_execute_plan(plan, args, NULL, read_only, 0);
117 if (exec_result < 0) {
119 (errcode(ERRCODE_INTERNAL_ERROR),
120 errmsg(
"prepare_query fails"),
121 errdetail(
"SPI_execute_plan('%s') returns error %d",
164 SPITupleTable *tuptab;
166 prepare_query(qry, nargs, argtypes, args, read_only, saved_plan);
167 fetched = SPI_processed;
168 tuptab = SPI_tuptable;
169 for(row = 0; row < fetched; row++) {
172 cntinue = process_row(tuptab->vals[row],
196 static bool ignore_this =
false;
197 bool col = DatumGetBool(SPI_getbinval(tuple, tupdesc, 1, &ignore_this));
198 *((
bool *) p_result) = col;
216 char *col = SPI_getvalue(tuple, tupdesc, 1);
217 char **p_str = (
char **) p_result;
238 rows =
query(qry, 0, argtypes, args,
false, NULL,
258 Oid argtypes[1] = {OIDOID};
261 args[0] = ObjectIdGetDatum(param);
262 rows =
query(qry, 1, argtypes, args,
false, NULL,
277 char dbname[NAMEDATALEN + 1];
296 char *col = SPI_getvalue(tuple, tupdesc, 1);
297 char *qry = palloc(strlen(col) + 15);
302 (void) sprintf(qry,
"select %s(%s)", col,
303 *((
bool *) p_param)?
"true":
"false");
306 if (ok != SPI_OK_CONNECT) {
308 (errcode(ERRCODE_INTERNAL_ERROR),
309 errmsg(
"failed to execute exec_init_fn() (1)"),
310 errdetail(
"SPI_connect() failed, returning %d.", ok)));
317 if (ok != SPI_OK_FINISH) {
319 (errcode(ERRCODE_INTERNAL_ERROR),
320 errmsg(
"failed to execute exec_init_fn() (2)"),
321 errdetail(
"SPI_finish() failed, returning %d.", ok)));
340 char *qry =
"select fn_name from veil.veil_init_fns order by priority";
346 if (ok != SPI_OK_CONNECT) {
348 (errcode(ERRCODE_INTERNAL_ERROR),
349 errmsg(
"failed to execute vl_call_init_fns() (1)"),
350 errdetail(
"SPI_connect() failed, returning %d.", ok)));
353 rows =
query(qry, 0, argtypes, args,
false, NULL,
357 if (ok != SPI_OK_FINISH) {
359 (errcode(ERRCODE_INTERNAL_ERROR),
360 errmsg(
"failed to execute vl_call_init_fns() (2)"),
361 errdetail(
"SPI_finish() failed, returning %d.", ok)));
static bool fetch_one_bool(HeapTuple tuple, TupleDesc tupdesc, void *p_result)
Fetch_fn function for processing a single row of a single integer for query.
bool() Fetch_fn(HeapTuple, TupleDesc, void *)
A Fetch_fn is a function that processes records, one at a time, returned from a query.
static bool fetch_one_str(HeapTuple tuple, TupleDesc tupdesc, void *p_result)
Fetch_fn function for processing a single row of a single integer for query.
int vl_spi_finish(bool pushed)
Reciprocal function for vl_spi_connect()
static int query(const char *qry, int nargs, Oid *argtypes, Datum *args, bool read_only, void **saved_plan, Fetch_fn process_row, void *fn_param)
Prepare and execute a query.
static void prepare_query(const char *qry, int nargs, Oid *argtypes, Datum *args, bool read_only, void **saved_plan)
Prepare a query for query().
Provide definitions for all non-local C-callable Veil functions.
Provides version information for veil.
static bool str_from_oid_query(const char *qry, const Oid param, char *result)
Executes a query by oid, that returns a single string value.
static bool exec_init_fn(HeapTuple tuple, TupleDesc tupdesc, void *p_param)
Fetch_fn function for executing registered veil_init() functions for query.
bool vl_db_exists(Oid db_id)
Determine whether the given oid represents an existing database or not.
int vl_call_init_fns(bool param)
Identify any registered init_functions and execute them.
bool vl_bool_from_query(const char *qry, bool *result)
Executes a query that returns a single bool value.
int vl_spi_connect(bool *p_pushed)
If already connected in this session, push the current connection, and get a new one.