Veil2
Postgres extension for VPD implementations
query.c File Reference

Functions to simplify SPI-based queries. More...

#include <stdio.h>
#include "postgres.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "access/xact.h"
#include "veil2.h"
Include dependency graph for query.c:

Go to the source code of this file.

Functions

void veil2_spi_connect (bool *p_pushed, const char *msg)
 
void veil2_spi_finish (bool pushed, const char *msg)
 
static void prepare_query (const char *qry, int nargs, Oid *argtypes, Datum *args, const char *nulls, bool read_only, void **saved_plan)
 
int veil2_query_wn (const char *qry, int nargs, Oid *argtypes, Datum *args, const char *nulls, bool read_only, void **saved_plan, Fetch_fn process_row, void *fn_param)
 
int veil2_query (const char *qry, int nargs, Oid *argtypes, Datum *args, bool read_only, void **saved_plan, Fetch_fn process_row, void *fn_param)
 
static bool fetch_one_bool (HeapTuple tuple, TupleDesc tupdesc, void *p_result)
 
bool veil2_bool_from_query (const char *qry, int nargs, Oid *argtypes, Datum *args, void **saved_plan, bool *result)
 

Detailed Description

Function Documentation

◆ fetch_one_bool()

static bool fetch_one_bool ( HeapTuple  tuple,
TupleDesc  tupdesc,
void *  p_result 
)
static

Fetch_fn function for processing a single row of a single integer for ::query.

Parameters
tupleThe row to be processed
tupdescDescriptor for the types of the fields in the tuple.
p_resultPointer to an int4 variable into which the value returned from the query will be placed.
Returns
false. This causes ::query to terminate after processing a single row.

Definition at line 242 of file query.c.

◆ prepare_query()

static void prepare_query ( const char *  qry,
int  nargs,
Oid *  argtypes,
Datum *  args,
const char *  nulls,
bool  read_only,
void **  saved_plan 
)
static

Prepare a query for veil2_query(). This creates and executes a plan. The caller must have established an SPI connection. It is assumed that no parameters to the query will be null.

Parameters
qryThe text of the SQL query to be performed.
nargsThe number of input parameters ($1, $2, etc) to the query
argtypesPointer to an array containing the OIDs of the data
argsActual parameters types of the parameters
nullsString identifying which args are null. Null args contain 'n' in the appropriate character position, otherwise there will be a space. If no args may be null, a NULL value can be used instead of a string.
read_onlyWhether the query should be read-only or not
saved_planAdress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used. This may be NULL, in which case the query plan will not be saved.

Definition at line 101 of file query.c.

◆ veil2_bool_from_query()

bool veil2_bool_from_query ( const char *  qry,
int  nargs,
Oid *  argtypes,
Datum *  args,
void **  saved_plan,
bool *  result 
)

Executes a query that returns a single bool value.

Parameters
qryThe text of the query to be performed.
nargsThe number of input parameters ($1, $2, etc) to the query
argtypesPointer to an array containing the OIDs of the data
argsActual parameters
saved_planAdress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used. This may be NULL, in which case the query plan will not be saved.
resultVariable into which the result of the query will be placed.
Returns
true if the query returned a record, false otherwise.

Definition at line 267 of file query.c.

Here is the call graph for this function:

◆ veil2_query()

int veil2_query ( const char *  qry,
int  nargs,
Oid *  argtypes,
Datum *  args,
bool  read_only,
void **  saved_plan,
Fetch_fn  process_row,
void *  fn_param 
)

Execute a query (with all args being non-null) and process the results.

Parameters
qryThe text of the SQL query to be performed.
nargsThe number of input parameters ($1, $2, etc) to the query
argtypesPointer to an array containing the OIDs of the data
argsActual parameters types of the parameters
read_onlyWhether the query should be read-only or not.
saved_planAdress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used. This may be NULL, in which case the query plan will not be saved.
process_rowA Fetch_fn() to process each tuple retruned by the query.
fn_paramA parameter to pass to process_row.
Returns
The number of rows processed.

Definition at line 218 of file query.c.

Here is the call graph for this function:

◆ veil2_query_wn()

int veil2_query_wn ( const char *  qry,
int  nargs,
Oid *  argtypes,
Datum *  args,
const char *  nulls,
bool  read_only,
void **  saved_plan,
Fetch_fn  process_row,
void *  fn_param 
)

Execute a query with nulls (ie allowing null arguments) and process the results.

Parameters
qryThe text of the SQL query to be performed.
nargsThe number of input parameters ($1, $2, etc) to the query
argtypesPointer to an array containing the OIDs of the data
argsActual parameters types of the parameters
nullsString identifying which args are null. Null args contain 'n' in the appropriate character position, otherwise there will be a space. If no args may be null, a NULL value can be used instead of a string.
read_onlyWhether the query should be read-only or not.
saved_planAdress of void pointer into which the query plan will be saved. Passing the same void pointer on a subsequent call will cause the saved query plan to be re-used. This may be NULL, in which case the query plan will not be saved.
process_rowA Fetch_fn() to process each tuple retruned by the query.
fn_paramA parameter to pass to process_row.
Returns
The number of rows processed.

Definition at line 165 of file query.c.

Here is the call graph for this function:

◆ veil2_spi_connect()

void veil2_spi_connect ( bool *  p_pushed,
const char *  msg 
)

If already connected in this session, push the current connection, and get a new one. We are already connected, if:

  • are within a query
  • and the current transaction id matches the saved transaction id
    Parameters
    p_pushedPointer to a boolean into which we will record whether we have saved a presiously active SPI connection. This allows recursive queries, which is probably overkill for our needs, but since the overhead is low...
    msgAn error message string to be issued in the event of a failure.
    Returns
    integer giving an SPI error code or success.

Definition at line 38 of file query.c.

◆ veil2_spi_finish()

void veil2_spi_finish ( bool  pushed,
const char *  msg 
)

Reciprocal function for veil2_spi_connect()

Parameters
pushedBoolean as set up by veil2_spi_connect(). This tells us whether to revert to a previously active SPI connection.
msgAn error message string to be issued in the event of a failure.
Returns
integer giving an SPI error code or success.

Definition at line 66 of file query.c.